Skip to content

Commit 3f73e98

Browse files
committed
Rch Import event serialized
1 parent c540401 commit 3f73e98

File tree

1 file changed

+41
-140
lines changed

1 file changed

+41
-140
lines changed

rch/src/main/java/org/motechproject/nms/rch/service/impl/RchWsImportServiceImpl.java

Lines changed: 41 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,7 @@ public void importRchMothersData(MotechEvent motechEvent) {
311311

312312
if (keelDeelApiResponse != null) {
313313
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "mother", stateId.toString());
314-
executeScpCommand(tempFilePath,false);
315-
316-
Map<String, Object> params = new HashMap<>();
317-
params.put("state", stateId.toString());
318-
params.put("tempFilePath", tempFilePath);
319-
params.put("fromDate", startReferenceDate);
320-
params.put("endDate", endReferenceDate);
321-
params.put("stateName", stateName);
322-
params.put("stateCode", stateCode.toString());
323-
324-
MotechEvent thirdEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "mother", params);
325-
eventRelay.sendEventMessage(thirdEvent);
314+
processThirdApiEvent(stateId.toString(), startReferenceDate, endReferenceDate, tempFilePath, "mother", stateName, stateCode.toString());
326315

327316
success = true; // Mark as successful
328317
} else {
@@ -359,14 +348,19 @@ public void handleThirdApiEvent(MotechEvent event) {
359348
String entityType = event.getSubject().split("\\.")[2]; // Determines if it's mother, child, or asha
360349
String stateName = (String) event.getParameters().get("stateName");
361350
String stateCode = (String) event.getParameters().get("stateCode");
351+
352+
processThirdApiEvent(stateId, from, to, tempFilePath, entityType, stateName, stateCode);
353+
}
354+
355+
private void processThirdApiEvent(String stateId, LocalDate from, LocalDate to, String tempFilePath,
356+
String entityType, String stateName, String stateCode) {
362357
boolean status = false;
363358
String thirdApiResponse = null;
364359

365360
RchUserType userType = RchUserType.valueOf(entityType.toUpperCase());
366361

367362
try {
368-
369-
executeScpCommand(tempFilePath, true);
363+
// executeScpCommand(tempFilePath, true);
370364
String payload = rchWebServiceFacade.readPayloadFromTempFile(tempFilePath);
371365

372366
if (payload != null) {
@@ -377,10 +371,10 @@ public void handleThirdApiEvent(MotechEvent event) {
377371
File responseFile = rchWebServiceFacade.generateJsonResponseFile(thirdApiResponse, userType, Long.valueOf(stateId));
378372

379373
if (responseFile != null) {
380-
LOGGER.info("RCH {} third api response successfully written to file. Copying to remote directory.", userType);
374+
LOGGER.info("RCH {} third API response successfully written to file. Copying to remote directory.", userType);
381375
status = rchWebServiceFacade.retryScpAndAudit(responseFile.getName(), from, to, Long.valueOf(stateId), userType, 0);
382376
} else {
383-
LOGGER.error("Error writing {} third api response to file for state {}", userType, stateId);
377+
LOGGER.error("Error writing {} third API response to file for state {}", userType, stateId);
384378
}
385379
}
386380

@@ -390,8 +384,7 @@ public void handleThirdApiEvent(MotechEvent event) {
390384
handleError(userType, Long.valueOf(stateId), stateName, Long.valueOf(stateCode), from, to, "Error during SCP file transfer.");
391385
}
392386

393-
}
394-
catch (Exception e) {
387+
} catch (Exception e) {
395388
handleError(userType, Long.valueOf(stateId), stateName, Long.valueOf(stateCode), from, to, e.getMessage());
396389
}
397390
}
@@ -447,18 +440,7 @@ public void importRchDistrictData(MotechEvent motechEvent) {
447440
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
448441

449442
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "district", stateId.toString());
450-
451-
executeScpCommand(tempFilePath,false);
452-
453-
Map<String, Object> params = new HashMap<>();
454-
params.put("state", stateId.toString());
455-
params.put("tempFilePath", tempFilePath);
456-
params.put("fromDate", startDate);
457-
params.put("endDate", endDate);
458-
params.put("stateName", stateName);
459-
params.put("stateCode", stateCode.toString());
460-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "district", params);
461-
eventRelay.sendEventMessage(nextEvent);
443+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.DISTRICT);
462444

463445
} catch (Exception e) {
464446
String error = String.format("Error processing RCH district data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -505,25 +487,34 @@ public void handleApiEvent(MotechEvent event) {
505487
String tempFilePath = (String) event.getParameters().get("tempFilePath");
506488
String stateName = (String) event.getParameters().get("stateName");
507489
String stateCode = (String) event.getParameters().get("stateCode");
490+
491+
executeScpCommand(tempFilePath, true);
492+
493+
processLocationThirdApiEvent(stateId, from, to, tempFilePath, stateName, stateCode, userType);
494+
}
495+
496+
497+
private void processLocationThirdApiEvent(String stateId, LocalDate from, LocalDate to, String tempFilePath,
498+
String stateName, String stateCode, RchUserType userType) {
508499
boolean status = false;
509500
String thirdApiResponse = null;
510501

511502
try {
512-
executeScpCommand(tempFilePath,true);
513503
String payload = rchWebServiceFacade.readPayloadFromTempFile(tempFilePath);
514504

515-
if(payload!=null) {
516-
thirdApiResponse = rchWebServiceFacade.callThirdApi(payload);
505+
if (payload != null) {
506+
thirdApiResponse = rchWebServiceFacade.callThirdApi(payload);
507+
}
517508

509+
if (thirdApiResponse != null) {
510+
File responseFile = rchWebServiceFacade.generateJsonResponseFile(thirdApiResponse, userType, Long.valueOf(stateId));
511+
if (responseFile != null) {
512+
LOGGER.info("RCH {} response successfully written to file. Copying to remote directory.", userType);
513+
status = rchWebServiceFacade.retryScpAndAudit(responseFile.getName(), from, to, Long.valueOf(stateId), userType, 0);
514+
} else {
515+
LOGGER.error("Error writing {} response to file for state {}", userType, stateId);
516+
}
518517
}
519-
if(thirdApiResponse!=null){
520-
File responseFile = rchWebServiceFacade.generateJsonResponseFile(thirdApiResponse, userType, Long.valueOf(stateId));
521-
if (responseFile != null) {
522-
LOGGER.info("RCH {} response successfully written to file. Copying to remote directory.", userType);
523-
status = rchWebServiceFacade.retryScpAndAudit(responseFile.getName(), from, to, Long.valueOf(stateId), userType, 0);
524-
} else {
525-
LOGGER.error("Error writing {} response to file for state {}", userType, stateId);
526-
}}
527518

528519
if (status) {
529520
LOGGER.info("{} data processed successfully for state ID: {}", userType, stateId);
@@ -566,17 +557,7 @@ public void importRchTalukaData(MotechEvent motechEvent) {
566557
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
567558

568559
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "taluka", stateId.toString());
569-
executeScpCommand(tempFilePath,false);
570-
571-
Map<String, Object> params = new HashMap<>();
572-
params.put("state", stateId.toString());
573-
params.put("tempFilePath", tempFilePath);
574-
params.put("fromDate", startDate);
575-
params.put("endDate", endDate);
576-
params.put("stateName", stateName);
577-
params.put("stateCode", stateCode.toString());
578-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "taluka", params);
579-
eventRelay.sendEventMessage(nextEvent);
560+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.TALUKA);
580561

581562
} catch (Exception e) {
582563
String error = String.format("Error processing RCH taluka data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -615,17 +596,7 @@ public void importRchVillageData(MotechEvent motechEvent) {
615596
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
616597

617598
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "village", stateId.toString());
618-
executeScpCommand(tempFilePath,false);
619-
620-
Map<String, Object> params = new HashMap<>();
621-
params.put("state", stateId.toString());
622-
params.put("tempFilePath", tempFilePath);
623-
params.put("fromDate", startDate);
624-
params.put("endDate", endDate);
625-
params.put("stateName", stateName);
626-
params.put("stateCode", stateCode.toString());
627-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "village", params);
628-
eventRelay.sendEventMessage(nextEvent);
599+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.VILLAGE);
629600

630601
} catch (Exception e) {
631602
String error = String.format("Error processing RCH village data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -665,17 +636,7 @@ public void importRchHealthBlockData(MotechEvent motechEvent) {
665636
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
666637

667638
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "healthblock", stateId.toString());
668-
executeScpCommand(tempFilePath,false);
669-
670-
Map<String, Object> params = new HashMap<>();
671-
params.put("state", stateId.toString());
672-
params.put("tempFilePath", tempFilePath);
673-
params.put("fromDate", startDate);
674-
params.put("endDate", endDate);
675-
params.put("stateName", stateName);
676-
params.put("stateCode", stateCode.toString());
677-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "healthblock", params);
678-
eventRelay.sendEventMessage(nextEvent);
639+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.HEALTHBLOCK);
679640

680641
} catch (Exception e) {
681642
String error = String.format("Error processing RCH healthblock data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -713,17 +674,7 @@ public void importRchTalukaHealthBlockData(MotechEvent motechEvent) {
713674
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
714675

715676
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "talukahealthblock", stateId.toString());
716-
executeScpCommand(tempFilePath,false);
717-
718-
Map<String, Object> params = new HashMap<>();
719-
params.put("state", stateId.toString());
720-
params.put("tempFilePath", tempFilePath);
721-
params.put("fromDate", startDate);
722-
params.put("endDate", endDate);
723-
params.put("stateName", stateName);
724-
params.put("stateCode", stateCode.toString());
725-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "talukahealthblock", params);
726-
eventRelay.sendEventMessage(nextEvent);
677+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.TALUKAHEALTHBLOCK);
727678

728679
} catch (Exception e) {
729680
String error = String.format("Error processing RCH talukahealthblock data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -761,17 +712,7 @@ public void importRchHealthFacilityData(MotechEvent motechEvent) {
761712
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
762713

763714
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "healthfacility", stateId.toString());
764-
executeScpCommand(tempFilePath,false);
765-
766-
Map<String, Object> params = new HashMap<>();
767-
params.put("state", stateId.toString());
768-
params.put("tempFilePath", tempFilePath);
769-
params.put("fromDate", startDate);
770-
params.put("endDate", endDate);
771-
params.put("stateName", stateName);
772-
params.put("stateCode", stateCode.toString());
773-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "healthfacility", params);
774-
eventRelay.sendEventMessage(nextEvent);
715+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.HEALTHFACILITY);
775716

776717
} catch (Exception e) {
777718
String error = String.format("Error processing RCH healthfacility data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -810,17 +751,7 @@ public void importRchHealthSubFacilityData(MotechEvent motechEvent) {
810751
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
811752

812753
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "healthsubfacility", stateId.toString());
813-
executeScpCommand(tempFilePath,false);
814-
815-
Map<String, Object> params = new HashMap<>();
816-
params.put("state", stateId.toString());
817-
params.put("tempFilePath", tempFilePath);
818-
params.put("fromDate", startDate);
819-
params.put("endDate", endDate);
820-
params.put("stateName", stateName);
821-
params.put("stateCode", stateCode.toString());
822-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "healthsubfacility", params);
823-
eventRelay.sendEventMessage(nextEvent);
754+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.HEALTHSUBFACILITY);
824755

825756
} catch (Exception e) {
826757
String error = String.format("Error processing RCH healthfacility data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -859,17 +790,7 @@ public void importRchVillageHealthSubFacilityData(MotechEvent motechEvent) {
859790
String keelDeelApiResponse = rchWebServiceFacade.callKeelDeelApiLocations(token, keel, deel);
860791

861792
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "villagehealthsubfacility", stateId.toString());
862-
executeScpCommand(tempFilePath,false);
863-
864-
Map<String, Object> params = new HashMap<>();
865-
params.put("state", stateId.toString());
866-
params.put("tempFilePath", tempFilePath);
867-
params.put("fromDate", startDate);
868-
params.put("endDate", endDate);
869-
params.put("stateName", stateName);
870-
params.put("stateCode", stateCode.toString());
871-
MotechEvent nextEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "villagehealthsubfacility", params);
872-
eventRelay.sendEventMessage(nextEvent);
793+
processLocationThirdApiEvent(stateId.toString(), startDate, endDate, tempFilePath, stateName, stateCode.toString(), RchUserType.VILLAGEHEALTHSUBFACILITY);
873794

874795
} catch (Exception e) {
875796
String error = String.format("Error processing RCH villagehealthsubfacility data for state %s (ID: %d): %s", stateName, stateId, e.getMessage());
@@ -933,17 +854,7 @@ public void importRchChildrenData(MotechEvent motechEvent) {
933854

934855
if (keelDeelApiResponse != null) {
935856
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "child", stateId.toString());
936-
executeScpCommand(tempFilePath,false);
937-
938-
Map<String, Object> params = new HashMap<>();
939-
params.put("state", stateId.toString());
940-
params.put("tempFilePath", tempFilePath);
941-
params.put("fromDate", startReferenceDate);
942-
params.put("endDate", endReferenceDate);
943-
params.put("stateName", stateName);
944-
params.put("stateCode", stateCode.toString());
945-
MotechEvent thirdEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "child", params);
946-
eventRelay.sendEventMessage(thirdEvent);
857+
processThirdApiEvent(stateId.toString(), startReferenceDate, endReferenceDate, tempFilePath, "child", stateName, stateCode.toString());
947858

948859
success = true;
949860
} else {
@@ -1023,17 +934,7 @@ public void importRchAshaData(MotechEvent motechEvent) {
1023934

1024935
if (keelDeelApiResponse != null) {
1025936
String tempFilePath = rchWebServiceFacade.saveToFile(keelDeelApiResponse, "asha", stateId.toString());
1026-
executeScpCommand(tempFilePath,false);
1027-
1028-
Map<String, Object> params = new HashMap<>();
1029-
params.put("state", stateId.toString());
1030-
params.put("tempFilePath", tempFilePath);
1031-
params.put("fromDate", startReferenceDate);
1032-
params.put("endDate", endReferenceDate);
1033-
params.put("stateName", stateName);
1034-
params.put("stateCode", stateCode.toString());
1035-
MotechEvent thirdEvent = new MotechEvent(Constants.SECOND_EVENT_PREFIX + "asha", params);
1036-
eventRelay.sendEventMessage(thirdEvent);
937+
processThirdApiEvent(stateId.toString(), startReferenceDate, endReferenceDate, tempFilePath, "asha", stateName, stateCode.toString());
1037938

1038939
success = true;
1039940
} else {

0 commit comments

Comments
 (0)