-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCHOAppSyncServiceImpl.java
More file actions
741 lines (604 loc) · 35.5 KB
/
CHOAppSyncServiceImpl.java
File metadata and controls
741 lines (604 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.hwc.service.choApp;
import com.google.gson.*;
import com.iemr.hwc.data.benFlowStatus.BeneficiaryFlowStatus;
import com.iemr.hwc.data.choApp.OutreachActivity;
import com.iemr.hwc.data.choApp.UserActivityLogs;
import com.iemr.hwc.data.doctor.PrescriptionTemplates;
import com.iemr.hwc.data.nurse.BeneficiaryVisitDetail;
import com.iemr.hwc.data.quickConsultation.BenChiefComplaint;
import com.iemr.hwc.repo.benFlowStatus.BeneficiaryFlowStatusRepo;
import com.iemr.hwc.repo.choApp.OutreachActivityRepo;
import com.iemr.hwc.repo.choApp.UserActivityLogsRepo;
import com.iemr.hwc.repo.doctor.PrescriptionTemplatesRepo;
import com.iemr.hwc.repo.nurse.BenAnthropometryRepo;
import com.iemr.hwc.repo.nurse.BenPhysicalVitalRepo;
import com.iemr.hwc.repo.nurse.BenVisitDetailRepo;
import com.iemr.hwc.repo.quickConsultation.BenChiefComplaintRepo;
import com.iemr.hwc.service.benFlowStatus.CommonBenStatusFlowServiceImpl;
import com.iemr.hwc.service.common.transaction.CommonNurseServiceImpl;
import com.iemr.hwc.service.generalOPD.GeneralOPDServiceImpl;
import com.iemr.hwc.utils.RestTemplateUtil;
import com.iemr.hwc.utils.exception.IEMRException;
import com.iemr.hwc.utils.request.SyncSearchRequest;
import com.iemr.hwc.utils.response.OutputResponse;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.*;
import jakarta.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.*;
@Service
@PropertySource("classpath:application.properties")
public class CHOAppSyncServiceImpl implements CHOAppSyncService {
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Value("${registrationUrl}")
private String registrationUrl;
@Value("${syncSearchByLocation}")
private String syncSearchByLocation;
@Value("${getBenCountToSync}")
private String getBenCountToSync;
private CommonBenStatusFlowServiceImpl commonBenStatusFlowServiceImpl;
private BeneficiaryFlowStatusRepo beneficiaryFlowStatusRepo;
private UserActivityLogsRepo userActivityLogsRepo;
private CommonNurseServiceImpl commonNurseServiceImpl;
private BenAnthropometryRepo benAnthropometryRepo;
private BenPhysicalVitalRepo benPhysicalVitalRepo;
private BenChiefComplaintRepo benChiefComplaintRepo;
private GeneralOPDServiceImpl generalOPDServiceImpl;
private BenVisitDetailRepo benVisitDetailRepo;
private PrescriptionTemplatesRepo prescriptionTemplatesRepo;
private OutreachActivityRepo outreachActivityRepo;
@Autowired
public void setOutreachActivityRepo(OutreachActivityRepo outreachActivityRepo){
this.outreachActivityRepo = outreachActivityRepo;
}
@Autowired
public void setPrescriptionTemplatesRepo(PrescriptionTemplatesRepo prescriptionTemplatesRepo){
this.prescriptionTemplatesRepo = prescriptionTemplatesRepo;
}
@Autowired
public void setCommonBenStatusFlowServiceImpl(CommonBenStatusFlowServiceImpl commonBenStatusFlowServiceImpl) {
this.commonBenStatusFlowServiceImpl = commonBenStatusFlowServiceImpl;
}
@Autowired
public void setBeneficiaryFlowStatusRepo(BeneficiaryFlowStatusRepo beneficiaryFlowStatusRepo) {
this.beneficiaryFlowStatusRepo = beneficiaryFlowStatusRepo;
}
@Autowired
public void setUserActivityLogsRepo(UserActivityLogsRepo userActivityLogsRepo){
this.userActivityLogsRepo = userActivityLogsRepo;
}
@Autowired
public void setCommonNurseServiceImpl(CommonNurseServiceImpl commonNurseServiceImpl){
this.commonNurseServiceImpl = commonNurseServiceImpl;
}
@Autowired
public void setBenAnthropometryRepo(BenAnthropometryRepo benAnthropometryRepo){
this.benAnthropometryRepo = benAnthropometryRepo;
}
@Autowired
public void setBenPhysicalVitalRepo(BenPhysicalVitalRepo benPhysicalVitalRepo){
this.benPhysicalVitalRepo = benPhysicalVitalRepo;
}
@Autowired
public void setBenChiefComplaintRepo(BenChiefComplaintRepo benChiefComplaintRepo){
this.benChiefComplaintRepo = benChiefComplaintRepo;
}
@Autowired
public void setGeneralOPDServiceImpl(GeneralOPDServiceImpl generalOPDServiceImpl){
this.generalOPDServiceImpl = generalOPDServiceImpl;
}
@Autowired
public void setBenVisitDetailRepo(BenVisitDetailRepo benVisitDetailRepo){
this.benVisitDetailRepo = benVisitDetailRepo;
}
public ResponseEntity<String> registerCHOAPPBeneficiary(String comingRequest, String Authorization){
OutputResponse outputResponse = new OutputResponse();
JsonObject responseObj = new JsonObject();
HttpStatus status = HttpStatus.BAD_REQUEST;
Long beneficiaryRegID = null;
Long beneficiaryID = null;
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", MediaType.APPLICATION_JSON + ";charset=utf-8");
headers.add("AUTHORIZATION", Authorization);
HttpEntity<Object> registrationRequest = RestTemplateUtil.createRequestEntity(comingRequest, Authorization);
try {
ResponseEntity<String> registrationResponse = restTemplate.exchange(registrationUrl, HttpMethod.POST, registrationRequest,
String.class);
String registrationResponseStr = registrationResponse.getBody();
JSONObject registrationResponseObj = new JSONObject(registrationResponseStr);
if (registrationResponseObj.getInt("statusCode") == 200) {
beneficiaryRegID = registrationResponseObj.getJSONObject("data").getLong("beneficiaryRegID");
beneficiaryID = registrationResponseObj.getJSONObject("data").getLong("beneficiaryID");
int i = commonBenStatusFlowServiceImpl.createBenFlowRecord(comingRequest, beneficiaryRegID, beneficiaryID);
if (i > 0) {
if (i == 1) {
responseObj.addProperty("beneficiaryID", beneficiaryID);
responseObj.addProperty("beneficiaryRegID", beneficiaryRegID);
outputResponse.setResponse(responseObj.toString());
status = HttpStatus.OK;
}
} else {
logger.error("Couldn't create a new benFlowStatus record for the registered beneficiary");
outputResponse.setError(500, "Beneficiary creation successful but couldn't create new flow status for it.");
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
} else {
logger.error("Error encountered in Common-API service while registering beneficiary. "
+ registrationResponseObj.getString("status"));
outputResponse.setError(registrationResponseObj.getInt("statusCode"), "Error encountered in Common-API service while registering beneficiary. "
+ registrationResponseObj.getString("status"));
}
} catch(ResourceAccessException e){
logger.error("Error establishing connection with Common-API service. " + e);
outputResponse.setError(503, "Error establishing connection with Common-API service. ");
status = HttpStatus.SERVICE_UNAVAILABLE;
} catch(RestClientResponseException e){
logger.error("Error encountered in Common-API service while registering beneficiary. " + e);
outputResponse.setError(e.getRawStatusCode(), "Error encountered in Common-API service while registering beneficiary. " + e);
status = HttpStatus.valueOf(e.getRawStatusCode());
} catch (JSONException e){
logger.error("Encountered JSON exception " + e);
outputResponse.setError(502, "Error registering the beneficiary.Encountered JSON exception " + e);
status = HttpStatus.BAD_GATEWAY;
} catch (Exception e){
logger.error("Encountered exception " + e);
outputResponse.setError(500, "Error registering the beneficiary.Encountered exception " + e);
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
headers.remove("AUTHORIZATION");
return new ResponseEntity<> (outputResponse.toString(),headers,status);
}
public ResponseEntity<String> getBeneficiaryByVillageIDAndLastModifiedDate(SyncSearchRequest villageIDAndLastSyncDate, String Authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("AUTHORIZATION", Authorization);
RestTemplate restTemplate = new RestTemplate();
try {
if(villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());
villageIDAndLastSyncDate.setLastModifiedDate(dt.toDate().getTime());
String identityRequestString = new GsonBuilder().create().toJson(villageIDAndLastSyncDate);
HttpEntity<Object> request = RestTemplateUtil.createRequestEntity(identityRequestString, Authorization);
ResponseEntity<String> response = restTemplate.exchange(syncSearchByLocation, HttpMethod.POST, request,
String.class);
if (response.hasBody()) {
JSONObject responseJSON = new JSONObject(response.getBody());
JSONArray jsonArray = new JSONArray(responseJSON.getJSONObject("response").getString("data"));
outputResponse.setResponse(jsonArray.toString());
}
}else{
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
}
catch(ResourceAccessException e){
logger.error("Error establishing connection with Identity service. " + e);
outputResponse.setError(503, "Error establishing connection with Identity service. ");
statusCode = HttpStatus.SERVICE_UNAVAILABLE;
} catch(RestClientResponseException e){
logger.error("Error encountered in Identity service while searching beneficiary based on villageIDs and lastSyncDate " + e);
outputResponse.setError(e.getRawStatusCode(), "Error encountered in Identity service while searching beneficiary based on villageIDs and lastSyncDate " + e);
statusCode = HttpStatus.valueOf(e.getRawStatusCode());
} catch (JSONException e){
logger.error("Encountered JSON exception while parsing response from Identity service " + e);
outputResponse.setError(502, "Encountered JSON exception while parsing response from Identity service " + e);
statusCode = HttpStatus.BAD_GATEWAY;
} catch (Exception e){
logger.error("Encountered exception " + e);
outputResponse.setError(500, "Error searching beneficiaries to sync. Exception " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
headers.remove("AUTHORIZATION");
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(),headers,statusCode);
}
public ResponseEntity<String> countBeneficiaryByVillageIDAndLastModifiedDate(SyncSearchRequest villageIDAndLastSyncDate, String Authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("AUTHORIZATION", Authorization);
RestTemplate restTemplate = new RestTemplate();
try {
if(villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());
villageIDAndLastSyncDate.setLastModifiedDate(dt.toDate().getTime());
String identityRequestString = new GsonBuilder().create().toJson(villageIDAndLastSyncDate);
HttpEntity<Object> request = RestTemplateUtil.createRequestEntity(identityRequestString, Authorization);
ResponseEntity<String> response = restTemplate.exchange(getBenCountToSync, HttpMethod.POST, request,
String.class);
if (response.hasBody()) {
JSONObject responseJSON = new JSONObject(response.getBody());
String count = responseJSON.getJSONObject("response").getString("data");
outputResponse.setResponse(count);
}
}else{
logger.error("Unable to get count of beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
}
catch(ResourceAccessException e){
logger.error("Error establishing connection with Identity service. " + e);
outputResponse.setError(503, "Error establishing connection with Identity service. ");
statusCode = HttpStatus.SERVICE_UNAVAILABLE;
} catch(RestClientResponseException e){
logger.error("Error encountered in Identity service while getting count of beneficiary based on villageIDs and lastSyncDate " + e);
outputResponse.setError(e.getRawStatusCode(), "Error encountered in Identity service while getting count of beneficiary based on villageIDs and lastSyncDate " + e);
statusCode = HttpStatus.valueOf(e.getRawStatusCode());
} catch (JSONException e){
logger.error("Encountered JSON exception while parsing response from Identity service " + e);
outputResponse.setError(502, "Encountered JSON exception while parsing response from Identity service " + e);
statusCode = HttpStatus.BAD_GATEWAY;
} catch (Exception e){
logger.error("Encountered exception " + e);
outputResponse.setError(500, "Error getting count of beneficiaries to sync. Exception " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
headers.remove("AUTHORIZATION");
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(),headers,statusCode);
}
@Override
public ResponseEntity<String> countFlowRecordsByVillageIDAndLastModifiedDate(SyncSearchRequest villageIDAndLastSyncDate, String Authorization) {
HttpStatus statusCode = HttpStatus.OK;
OutputResponse outputResponse = new OutputResponse();
Long benFlowCount;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
if (villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());
benFlowCount = beneficiaryFlowStatusRepo.getFlowRecordsCount(villageIDAndLastSyncDate.getVillageID(),
new Timestamp(dt.toDate().getTime()));
outputResponse.setResponse(String.valueOf(benFlowCount));
}else{
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
} catch (IllegalArgumentException e){
logger.error("Encountered exception. " + e);
outputResponse.setError(400, "Encountered exception. Exception " + e);
statusCode = HttpStatus.BAD_REQUEST;
} catch (Exception e){
logger.error("Encountered exception while fetching ben flow status records to sync " + e);
outputResponse.setError(500, "Error fetching ben flow status records to sync . Exception " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(),headers,statusCode);
}
@Override
public ResponseEntity<String> getFlowRecordsByVillageIDAndLastModifiedDate(SyncSearchRequest villageIDAndLastSyncDate, String Authorization) {
HttpStatus statusCode = HttpStatus.OK;
OutputResponse outputResponse = new OutputResponse();
ArrayList<BeneficiaryFlowStatus> benFlowList;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
if (villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());
benFlowList = beneficiaryFlowStatusRepo.getFlowRecordsToSync(villageIDAndLastSyncDate.getVillageID(),
new Timestamp(dt.toDate().getTime()));
outputResponse.setResponse(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create().toJson(benFlowList));
}else{
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
} catch (IllegalArgumentException e){
logger.error("Encountered exception. " + e);
outputResponse.setError(400, "Encountered exception. Exception " + e);
statusCode = HttpStatus.BAD_REQUEST;
} catch (Exception e){
logger.error("Encountered exception while fetching ben flow status records to sync " + e);
outputResponse.setError(500, "Error fetching ben flow status records to sync . Exception " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(),headers,statusCode);
}
@Override
public ResponseEntity<String> saveUserActivityLogs(List<UserActivityLogs> logsList, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try{
for (UserActivityLogs log : logsList){
if(log.getUserImage() != null) {
byte[] imageByte = Base64.getDecoder().decode(log.getUserImage());
log.setImageData(imageByte);
}
}
List<UserActivityLogs> savedList = (List<UserActivityLogs>) userActivityLogsRepo.saveAll(logsList);
if (savedList.size() == logsList.size()){
outputResponse.setResponse("Data saved successfully");
}else {
throw new Exception();
}
} catch (IEMRException | DataIntegrityViolationException |
JsonSyntaxException | NumberFormatException e){
logger.error("Encountered exception EITHER due to incorrect payload syntax OR" +
" because of missing userId. " + e);
outputResponse.setError(400,"Encountered exception EITHER due to incorrect payload syntax OR " +
"because of missing userId. Please check the payload. " + e);
statusCode = HttpStatus.BAD_REQUEST;
} catch (Exception e){
logger.error("Encountered exception while saving UserActivityLogs. " + e);
outputResponse.setError(500,"Encountered exception while saving UserActivityLogs. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toString(),headers,statusCode);
}
@Override
public ResponseEntity<String> getBeneficiaryNurseFormDataGeneralOPD(String comingRequest, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try{
JSONObject obj = new JSONObject(comingRequest);
Long benRegID = obj.getLong("benRegID");
Long visitCode = obj.getLong("visitCode");
Map<String, Object> response = new HashMap<>();
Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithoutExposeAnnotation().create();
//Fetching visitDetails, chief complaints
response.put("GOPDNurseVisitDetail", commonNurseServiceImpl.getCSVisitDetails(benRegID, visitCode));
ArrayList<Object[]> resList = benChiefComplaintRepo.getBenChiefComplaints(benRegID, visitCode);
ArrayList<BenChiefComplaint> benChiefComplaints = BenChiefComplaint.getBenChiefComplaints(resList);
response.put("BenChiefComplaints", benChiefComplaints);
//Fetching vitals
response.put("benAnthropometryDetail", benAnthropometryRepo.getBenAnthropometryDetail(benRegID, visitCode));
response.put("benPhysicalVitalDetail", benPhysicalVitalRepo.getBenPhysicalVitalDetail(benRegID, visitCode));
outputResponse.setResponse(gson.toJson(response));
} catch (JSONException e) {
logger.error("Encountered exception EITHER due to incorrect payload syntax OR" +
" because of missing benRegID/visitCode " + e);
outputResponse.setError(400, "Encountered exception EITHER due to incorrect payload syntax OR " +
"because of missing benRegID/visitCode. Please check the payload. " + e);
statusCode = HttpStatus.BAD_REQUEST;
}catch (NullPointerException e){
logger.error("No ben visit detail record found. Exception encountered while setting setting variable to a null object " + e);
outputResponse.setError(404,"No ben visit detail record found. Please make sure the Ids in payload are correct. " + e);
statusCode = HttpStatus.NOT_FOUND;
} catch (Exception e){
logger.error("Encountered exception while fetching nurse data(visit details,vitals,chief complaints,history,examinations)" +
" for beneficiary. " + e);
outputResponse.setError(500,"Encountered exception while fetching nurse data" +
"(visit details,vitals,chief complaints,history,examinations) for beneficiary. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(),headers,statusCode);
}
@Override
public ResponseEntity<String> saveBeneficiaryNurseFormDataGeneralOPD(String requestObj, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
JsonObject jsnOBJ;
JsonParser jsnParser = new JsonParser();
JsonElement jsonElement = jsnParser.parse(requestObj);
jsnOBJ = jsonElement.getAsJsonObject();
if (jsnOBJ != null) {
String genOPDRes = generalOPDServiceImpl.saveNurseData(jsnOBJ, authorization);
JsonObject responseObject;
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(genOPDRes);
responseObject = element.getAsJsonObject();
Gson gson = new Gson();
if(responseObject.has("visitCode") && responseObject.get("visitCode")!=null){
BeneficiaryVisitDetail visitDetail= benVisitDetailRepo.findByVisitCode(responseObject.get("visitCode").getAsLong());
JsonElement ele = gson.toJsonTree(visitDetail.getBenVisitID());
responseObject.add("visitID",ele);
outputResponse.setResponse(gson.toJson(responseObject));
}
else {
outputResponse.setResponse(genOPDRes);
}
} else {
logger.error("Invalid request object " + requestObj);
outputResponse.setError(400," Bad Request. Invalid request payload");
statusCode = HttpStatus.BAD_REQUEST;
}
} catch (Exception e) {
logger.error("Error in nurse data saving :" + e);
outputResponse.setError(500, "Unable to save data " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toString(),headers,statusCode);
}
@Override
public ResponseEntity<String> savePrescriptionTemplatesToServer(List<PrescriptionTemplates> templateList, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try{
List<PrescriptionTemplates> savedList = (List<PrescriptionTemplates>) prescriptionTemplatesRepo.saveAll(templateList);
if (savedList.size() == templateList.size()){
outputResponse.setResponse("Data saved successfully");
}else {
throw new Exception();
}
} catch (IEMRException | DataIntegrityViolationException |
JsonSyntaxException | NumberFormatException e){
logger.error("Encountered exception EITHER due to incorrect payload syntax OR" +
" because of missing userId. " + e);
outputResponse.setError(400,"Encountered exception EITHER due to incorrect payload syntax OR " +
"because of missing userId. Please check the payload. " + e);
statusCode = HttpStatus.BAD_REQUEST;
} catch (Exception e){
logger.error("Encountered exception while saving Prescription templates. " + e);
outputResponse.setError(500,"Encountered exception while saving Prescription templates. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toString(),headers,statusCode);
}
@Override
public ResponseEntity<String> savePrescriptionTemplatesToApp(Integer userID, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
List<PrescriptionTemplates> templateList = prescriptionTemplatesRepo
.getPrescriptionTemplatesByUserID(userID);
outputResponse.setResponse(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls()
.create().toJson(templateList));
} catch (Exception e) {
logger.error("Error while fetching Prescription Templates userID : " + userID);
outputResponse.setError(500, "Unable to fetch Prescription Templates userID" + userID + "Exception - " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(), headers, statusCode);
}
@Override
public ResponseEntity<String> deletePrescriptionTemplates(Integer userID, Integer tempID) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
prescriptionTemplatesRepo.deletePrescriptionTemplatesByUserIDAndTempID(userID, tempID);
outputResponse.setResponse("Successfully deleted");
} catch (Exception e) {
logger.error("Error while deleting Prescription Templates userID : " + userID + " tempID : " + tempID);
outputResponse.setError(500, "Unable to delete Prescription Templates userID : " + userID + " tempID "
+ tempID + "Exception - " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toString(), headers, statusCode);
}
@Override
public ResponseEntity<String> createNewOutreachActivity(OutreachActivity activity, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try{
if(activity.getImg1() != null) {
byte[] imageByte1 = Base64.getDecoder().decode(activity.getImg1());
activity.setImg1Data(imageByte1);
}
if(activity.getImg2() != null) {
byte[] imageByte2 = Base64.getDecoder().decode(activity.getImg2());
activity.setImg2Data(imageByte2);
}
outreachActivityRepo.save(activity);
outputResponse.setResponse("Data saved successfully");
} catch (DataIntegrityViolationException |
JsonSyntaxException | NumberFormatException e){
logger.error("Encountered exception EITHER due to incorrect payload syntax OR" +
" because of missing userId. " + e);
outputResponse.setError(400,"Encountered exception EITHER due to incorrect payload syntax OR " +
"because of missing userId. Please check the payload. " + e);
statusCode = HttpStatus.BAD_REQUEST;
} catch (Exception e){
logger.error("Encountered exception while saving outreach activity. " + e);
outputResponse.setError(500,"Encountered exception while saving outreach activity. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toString(),headers,statusCode);
}
@Override
public ResponseEntity<String> getActivitiesByUser(Integer userId, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
ArrayList<Object[]> activitiesObj = outreachActivityRepo.getActivitiesByUserID(userId);
ArrayList<OutreachActivity> activities = OutreachActivity.getActivitiesForUser(activitiesObj);
outputResponse.setResponse(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls()
.create().toJson(activities));
} catch (Exception e) {
logger.error("Encountered exception while fetching activity userId : " + userId);
outputResponse.setError(500, "Encountered exception while fetching activity. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(), headers, statusCode);
}
@Override
public ResponseEntity<String> getActivityById(Integer activityId, String authorization) {
OutputResponse outputResponse = new OutputResponse();
HttpStatus statusCode = HttpStatus.OK;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
try {
Optional<OutreachActivity> activityOptional = outreachActivityRepo.findById(activityId);
if (activityOptional.isPresent()) {
OutreachActivity activity = activityOptional.get();
if (activity.getImg1Data() != null) {
String img1 = Base64.getEncoder().encodeToString(activity.getImg1Data());
activity.setImg1(img1);
}
if (activity.getImg2Data() != null) {
String img2 = Base64.getEncoder().encodeToString(activity.getImg2Data());
activity.setImg2(img2);
}
outputResponse.setResponse(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls()
.create().toJson(activity));
}else {
outputResponse.setError(404, "Activity not found with ID : "+activityId);
statusCode = HttpStatus.NOT_FOUND;
}
} catch (Exception e) {
logger.error("Encountered exception while fetching activity activityId " + activityId);
outputResponse.setError(500, "Encountered exception while fetching activity. " + e);
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(outputResponse.toStringWithSerializeNulls(), headers, statusCode);
}
}