@@ -71,52 +71,55 @@ public void execute() {
7171 List <SyncTask > allSyncTasks = ugandaEMRSyncService .getAllSyncTask ();
7272 List <SyncTask > syncTasks = allSyncTasks .stream ().filter (p -> order .getAccessionNumber ().equals (p .getSyncTask ()) && syncTaskType .getId ().equals (p .getSyncTaskType ().getId ())).collect (Collectors .toList ());
7373
74- if (syncTasks .size ()< 1 ) {
74+ if (syncTasks .size () < 1 ) {
7575 Map <String , String > dataOutput = generateVLFHIROrderTestRequestBody ((TestOrder ) order , VL_SEND_SAMPLE_FHIR_JSON_STRING );
7676 String json = dataOutput .get ("json" );
7777
7878 try {
7979 Map map = ugandaEMRHttpURLConnection .sendPostBy (syncTaskType .getUrl (), syncTaskType .getUrlUserName (), syncTaskType .getUrlPassword (), "" , json , false );
8080 if (map != null ) {
81+ Map responseType = handleReturnedResponses (order , map );
82+ Integer response = (Integer ) map .get ("responseCode" );
83+
84+ if (map .get ("responseCode" ).toString ().equals ("400" ) && responseType .get ("responseType" ).toString ().equals ("Duplicate" )) {
85+ response = 200 ;
86+ }
87+
8188 SyncTask newSyncTask = new SyncTask ();
8289 newSyncTask .setDateSent (new Date ());
8390 newSyncTask .setCreator (Context .getUserService ().getUser (1 ));
8491 newSyncTask .setSentToUrl (syncTaskType .getUrl ());
8592 newSyncTask .setRequireAction (true );
8693 newSyncTask .setActionCompleted (false );
8794 newSyncTask .setSyncTask (order .getAccessionNumber ());
88- newSyncTask .setStatusCode ((Integer ) map . get ( "responseCode" ) );
89- newSyncTask .setStatus ((String )map .get ("responseMessage" ));
95+ newSyncTask .setStatusCode ((Integer ) response );
96+ newSyncTask .setStatus ((String ) map .get ("responseMessage" ));
9097 newSyncTask .setSyncTaskType (ugandaEMRSyncService .getSyncTaskTypeByUUID (VIRAL_LOAD_SYNC_TYPE_UUID ));
9198 ugandaEMRSyncService .saveSyncTask (newSyncTask );
92- try {
93- handleReturnedResponses (order ,map );
94- }catch (Exception exception ){
95- log .error (exception );
96- }
9799 }
98100 } catch (Exception e ) {
99- log .error ("Failed to create sync task" ,e );
101+ log .error ("Failed to create sync task" , e );
100102 }
101103 }
102104 }
103105 }
104106
105- private void handleReturnedResponses (Order order ,Map response ) {
106- OrderService orderService =Context .getOrderService ();
107- if (response .get ("responseCode" ).equals (400 ) && response .get ("responseMessage" ).toString ().contains ("The specimen ID:" ) && response .get ("responseMessage" ).toString ().contains ("is not HIE compliant" )){
108- try {
109- orderService .discontinueOrder (order , response .get ("responseMessage" ).toString (), new Date (), order .getOrderer (), order .getEncounter ());
110- } catch (Exception e ) {
111- log .error (e );
112- }
113- }else if (response .get ("responseCode" ).equals (400 ) && response .get ("responseMessage" ).toString ().toLowerCase ().contains ("duplicate" )){
114- try {
107+ private Map handleReturnedResponses (Order order , Map response ) {
108+ Map responseType = new HashMap <>();
109+ OrderService orderService = Context .getOrderService ();
110+ try {
111+ if (response .get ("responseCode" ).equals (400 ) && response .get ("responseMessage" ).toString ().contains ("The specimen ID:" ) && response .get ("responseMessage" ).toString ().contains ("is not HIE compliant" )) {
115112 orderService .discontinueOrder (order , response .get ("responseMessage" ).toString (), new Date (), order .getOrderer (), order .getEncounter ());
116- } catch (Exception e ) {
117- log .error (e );
113+ responseType .put ("responseType" , "Not HIE compliant" );
114+ } else if (response .get ("responseCode" ).equals (400 ) && response .get ("responseMessage" ).toString ().toLowerCase ().contains ("duplicate" )) {
115+ //TODO need to update openmrs version in sync in order to support updating fulfiller status
116+ responseType .put ("responseType" , "Duplicate" );
118117 }
118+ } catch (Exception e ) {
119+ log .error (e );
119120 }
121+
122+ return responseType ;
120123 }
121124
122125
@@ -144,7 +147,7 @@ public Map<String, String> generateVLFHIRTestRequestBody(Encounter encounter, St
144147 String healthCenterCode = ugandaEMRSyncService .getHealthCenterCode ();
145148 String requestType = encounter .getEncounterType ().getName ();
146149 String sourceSystem = "UgandaEMR" ;
147- String patientARTNO = ugandaEMRSyncService .getPatientIdentifier (encounter .getPatient (),PATIENT_IDENTIFIER_TYPE );
150+ String patientARTNO = ugandaEMRSyncService .getPatientIdentifier (encounter .getPatient (), PATIENT_IDENTIFIER_TYPE );
148151 String sampleID = encounter .getEncounterId ().toString ();
149152 String sampleCollectionDate = encounter .getEncounterDatetime ().toString ();
150153 String clinicianNames = getProviderByEncounterRole (encounter , "clinician" );
@@ -180,12 +183,11 @@ public Map<String, String> generateVLFHIROrderTestRequestBody(TestOrder testOrde
180183 if (testOrder != null ) {
181184
182185
183-
184186 String healthCenterName = ugandaEMRSyncService .getHealthCenterName ();
185187 String healthCenterCode = ugandaEMRSyncService .getHealthCenterCode ();
186188 String requestType = proccessMappings (testOrder .getConcept ());
187189 String sourceSystem = "UgandaEMR" ;
188- String patientARTNO = ugandaEMRSyncService .getPatientIdentifier (testOrder .getPatient (),PATIENT_IDENTIFIER_TYPE );
190+ String patientARTNO = ugandaEMRSyncService .getPatientIdentifier (testOrder .getPatient (), PATIENT_IDENTIFIER_TYPE );
189191 String sampleID = testOrder .getAccessionNumber ();
190192 String sampleCollectionDate = testOrder .getEncounter ().getEncounterDatetime ().toString ();
191193 String clinicianNames = testOrder .getOrderer ().getName ();
@@ -197,16 +199,16 @@ public Map<String, String> generateVLFHIROrderTestRequestBody(TestOrder testOrde
197199 if (getProviderAttributeValue (Objects .requireNonNull (getProviderAppributesFromPerson (testOrder .getCreator ().getPerson ()))) != null ) {
198200 labTechContact = getProviderAttributeValue (Objects .requireNonNull (getProviderAppributesFromPerson (testOrder .getCreator ().getPerson ())));
199201 }
200- }catch (Exception e ){
201- log .error ("Could not add Lab technician telephone number" ,e );
202+ } catch (Exception e ) {
203+ log .error ("Could not add Lab technician telephone number" , e );
202204 }
203205
204206 String obsSampleType = testOrder .getSpecimenSource ().getName ().getName ();
205207 if (getProviderAttributeValue (testOrder .getOrderer ().getActiveAttributes ()) != null ) {
206208 ordererContact = getProviderAttributeValue (testOrder .getOrderer ().getActiveAttributes ());
207209 }
208210
209- filledJsonFile = String .format (jsonFHIRMap , healthCenterCode , healthCenterName , requestType , sourceSystem , patientARTNO , sampleID , obsSampleType , sampleCollectionDate , labTechNames , labTechContact ,sampleCollectionDate , clinicianNames , ordererContact , "CPHL" );
211+ filledJsonFile = String .format (jsonFHIRMap , healthCenterCode , healthCenterName , requestType , sourceSystem , patientARTNO , sampleID , obsSampleType , sampleCollectionDate , labTechNames , labTechContact , sampleCollectionDate , clinicianNames , ordererContact , "CPHL" );
210212 }
211213 jsonMap .put ("json" , filledJsonFile );
212214 return jsonMap ;
0 commit comments