· 7 years ago · Oct 27, 2018, 02:40 AM
1package com.ict3103team35.sit.doctotrust.Doctor;
2
3import android.content.Intent;
4import android.content.SharedPreferences;
5import android.support.v7.app.AppCompatActivity;
6import android.os.Bundle;
7import android.util.Base64;
8import android.util.Log;
9import android.view.View;
10import android.widget.Button;
11import android.widget.EditText;
12import android.widget.TextView;
13import android.widget.Toast;
14
15import com.ict3103team35.sit.doctotrust.CryptoClass.CryptoActivity;
16import com.ict3103team35.sit.doctotrust.Models.DefaultCreateResponse;
17import com.ict3103team35.sit.doctotrust.Models.DefaultGetResponse;
18import com.ict3103team35.sit.doctotrust.Models.MedicalHistory;
19import com.ict3103team35.sit.doctotrust.Models.MedicalRecord;
20import com.ict3103team35.sit.doctotrust.Patients.Patient_MedicalHistoryViewerActivity;
21import com.ict3103team35.sit.doctotrust.Patients.Patient_TodoActivity;
22import com.ict3103team35.sit.doctotrust.R;
23import com.ict3103team35.sit.doctotrust.Services.RetrofitClient;
24
25import java.util.Map;
26
27import javax.crypto.SecretKey;
28import javax.crypto.spec.IvParameterSpec;
29
30import retrofit2.Call;
31import retrofit2.Callback;
32import retrofit2.Response;
33
34public class Doctor_MedicalHistoryUpdate extends AppCompatActivity {
35
36 private String USER_NRIC_FLAG = "";//user flag to make activity unique to each user NRIC
37 private String USER_ROLE_FLAG = "";//user flag to make activity unique to each user role
38 private String USER_NRIC_INTENT = "";//user bundle to get user nric from precedent activity
39
40 private String TAG = "Class-Doctor_MedicalHistoryUpdate";
41
42 private Button btnUpload;
43 private EditText etAllergies, etMedications, etDiagnosis, etSurgeries;
44 private TextView tvOutputValue;
45 private CryptoActivity encAnddec = new CryptoActivity();
46 private String Encrypted_PATIENT_AES_KEY;
47 private static final String AES_ALIAS = "PASSWORD_KEY";
48 private SharedPreferences.Editor editor;
49 private SharedPreferences preferences;
50 private static final String mypreference = "StoringofIV";
51 private String MyMedIVValue = "MyMedIVValue";
52 private String MyMedicalkey = "Patient";
53 private String MyPassIVValue = "MyPassIVValue";
54 private IvParameterSpec SharepreferenceIV;
55 private IvParameterSpec OneTimeIV;
56 SecretKey OneTimeKey;
57
58 MedicalHistory medicalHistory = new MedicalHistory();
59
60 @Override
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 setContentView(R.layout.activity_doctor_medicalhistoryupdate);
64
65 etAllergies = (EditText) findViewById(R.id.medicalHistory_etAllergies);
66 etMedications = (EditText) findViewById(R.id.medicalHistory_etMedications);
67 etDiagnosis = (EditText) findViewById(R.id.medicalHistory_etDiagnosis);
68 etSurgeries = (EditText) findViewById(R.id.medicalHistory_etSurgeries);
69 btnUpload = (Button) findViewById(R.id.doctor_btnMedicalHistoryUpload);
70 tvOutputValue = (TextView) findViewById(R.id.medicalHistory_tvSubmitOutputValue);
71
72 USER_NRIC_FLAG = "S9222228Z";//get user nric flag session
73 Log.d(TAG, "USER_NRIC_FLAG : " + USER_NRIC_FLAG );
74
75 USER_NRIC_INTENT = getIntent().getStringExtra("user_nric_intent");//get user nric from intent
76 Log.d(TAG, "user_nric_intent : " + USER_NRIC_INTENT);
77
78 preferences = getSharedPreferences(mypreference, MODE_PRIVATE);
79
80 if (preferences.contains(MyPassIVValue)) {
81 byte[] iv = Base64.decode(preferences.getString(MyPassIVValue, ""), Base64.DEFAULT);//IV to be sent to database
82 SharepreferenceIV = new IvParameterSpec(iv);
83 }
84 OneTimeKey= encAnddec.generateAESKey(256);
85 String PATIENT_AES_KEY = Base64.encodeToString(OneTimeKey.getEncoded(), Base64.DEFAULT);
86 try {
87 Encrypted_PATIENT_AES_KEY = encAnddec.encryptPassword(PATIENT_AES_KEY, encAnddec.getSecretKey(AES_ALIAS),SharepreferenceIV);//in order to use medical history key, encrypt first using patient secret key
88 } catch (Exception e) {
89 e.printStackTrace();
90 }
91
92 OneTimeIV = encAnddec.MedIVs(16);
93 String MedicalIV = Base64.encodeToString(OneTimeIV.getIV(), Base64.DEFAULT); //sent to database
94 //store iv and secretkey into sharedpreferences
95 preferences = getSharedPreferences(mypreference, MODE_PRIVATE);
96 editor = preferences.edit();
97 editor.putString(MyMedIVValue, MedicalIV);
98 editor.putString(MyMedicalkey,Encrypted_PATIENT_AES_KEY);
99 editor.apply();
100
101
102 getMedicalHistory(USER_NRIC_INTENT);//get medical history
103
104
105 btnUpload.setOnClickListener(new View.OnClickListener() {
106 @Override
107 public void onClick(View v) {
108 //Store to medical history model
109 try {
110 String allergic = encAnddec.encryptMedicalHistory(etAllergies.getText().toString(), OneTimeKey, OneTimeIV);
111 String currenttaking = encAnddec.encryptMedicalHistory(etMedications.getText().toString(), OneTimeKey, OneTimeIV);
112 String medicalprob =encAnddec.encryptMedicalHistory(etDiagnosis.getText().toString(), OneTimeKey, OneTimeIV);
113 String surgeries = encAnddec.encryptMedicalHistory(etSurgeries.getText().toString(), OneTimeKey, OneTimeIV);
114
115 medicalHistory.setPatient_nric(USER_NRIC_INTENT);
116 medicalHistory.setAllergic(allergic);
117 medicalHistory.setCurrenttaking(currenttaking);
118 medicalHistory.setMedicalproblem(medicalprob);
119 medicalHistory.setSurgeries(surgeries);
120 Log.d("allergy",allergic);
121 //update medical history db
122 updateMedicalHistory(medicalHistory,USER_NRIC_FLAG,"accesslog?");
123 } catch (Exception e) {
124 e.printStackTrace();
125 }
126
127
128
129 }
130 });
131
132
133 }
134
135 /**
136 * Read medical history by nric
137 * @param nric
138 */
139 public void getMedicalHistory(String nric){
140 Log.d(TAG, "getMedicalHistory");
141
142 RetrofitClient.getInstance().getApi().getMedicalHistory(nric).enqueue(
143 new Callback<DefaultGetResponse>() {
144 @Override
145 public void onResponse(Call<DefaultGetResponse> call, Response<DefaultGetResponse> response) {
146 if(response!=null){
147 //Successfully insert
148 DefaultGetResponse dgr = response.body();
149 MedicalHistory medicalHistory = dgr.getMedicalHistory();
150 Log.d(TAG, medicalHistory.toString());
151
152 String allergic = medicalHistory.getAllergic();
153 String currenttaking = medicalHistory.getCurrenttaking();
154 String medicalprob =medicalHistory.getMedicalproblem();
155 String surgeries = medicalHistory.getSurgeries();
156
157 try {
158// encAnddec.decryptMedicalHistory(allergic, OneTimeKey, OneTimeIV);
159// encAnddec.decryptMedicalHistory(currenttaking, OneTimeKey, OneTimeIV);
160// encAnddec.decryptMedicalHistory( medicalprob, OneTimeKey, OneTimeIV);
161// encAnddec.decryptMedicalHistory( surgeries, OneTimeKey, OneTimeIV);
162 Log.d(TAG, allergic);
163 //set viewa
164 etAllergies.setText(encAnddec.decryptMedicalHistory(allergic, OneTimeKey, OneTimeIV));
165 etMedications.setText(encAnddec.decryptMedicalHistory(currenttaking, OneTimeKey, OneTimeIV));
166 etDiagnosis.setText(encAnddec.decryptMedicalHistory( medicalprob, OneTimeKey, OneTimeIV));
167 etSurgeries.setText(encAnddec.decryptMedicalHistory( surgeries, OneTimeKey, OneTimeIV));
168
169 } catch (Exception e) {
170 e.printStackTrace();
171 }
172
173
174
175
176 }
177 else{
178 //User Query Failure
179 String s = response.errorBody().toString();
180 Log.i(TAG,"Message:"+"getMedicalHistory Query Failure");
181 }
182 }
183 @Override
184 public void onFailure(Call<DefaultGetResponse> call, Throwable t) {
185 Log.i(TAG,"on Failure");
186 Log.i(TAG,t.getMessage());
187 }
188 });
189
190 }
191
192 /**
193 * Update medical history
194 * @param mh
195 * @param medicalhistory_modify_by_doctor_id
196 * @param accesslog
197 */
198 public void updateMedicalHistory(MedicalHistory mh,String medicalhistory_modify_by_doctor_id,String accesslog){
199
200
201 Log.i(TAG,"updateMedicalHistory:");
202 RetrofitClient.getInstance().getApi()
203 .updateMedicalHistory(mh.getAllergic(),mh.getCurrenttaking(),mh.getMedicalproblem(),
204 mh.getSurgeries(),mh.getPatient_nric(),
205 medicalhistory_modify_by_doctor_id,accesslog).enqueue(
206 new Callback<DefaultCreateResponse>() {
207 @Override
208 public void onResponse(Call<DefaultCreateResponse> call, Response<DefaultCreateResponse> response) {
209 if(response.code()==201){
210 //Successfully insert
211 DefaultCreateResponse dr =response.body();
212 Log.i(TAG,"success:"+dr.getMsg());
213
214 Toast.makeText(getApplicationContext(),"Medical record updated",Toast.LENGTH_SHORT).show();
215 Intent i = new Intent(getApplicationContext(),Patient_MedicalHistoryViewerActivity.class);
216 i.putExtra("user_nric_intent", USER_NRIC_INTENT);
217 startActivity(i);
218
219 }
220 else{
221 //User Query Failure
222 Log.i(TAG,response.code()+"");
223 Log.i(TAG,"Message:"+"Create MedicalHistory Query Failure");
224 }
225 }
226 @Override
227 public void onFailure(Call<DefaultCreateResponse> call, Throwable t) {
228 Log.i(TAG,"on Failure");
229 Log.i(TAG,t.getMessage());
230 }
231 });
232
233 }
234}