Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 95de286

Browse files
authored
Merge pull request #351 from OpenSRP/issue350
Replace strings/locations not accepted in OMRS
2 parents d9ea051 + 9565ff5 commit 95de286

4 files changed

Lines changed: 93 additions & 34 deletions

File tree

opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsSyncerListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public JSONObject pushClient(long start) throws JSONException {
125125
SchedulerConfig.openmrs_syncer_sync_client_by_date_updated, "OPENMRS FAILED CLIENT PUSH");
126126

127127
logger.info("RUNNING FOR RELATIONSHIPS");
128-
patientService.createRelationShip(cl);
128+
patientService.createRelationShip(cl, "OPENMRS FAILED CLIENT RELATIONSHIP PUSH");
129129
}
130130
returnJsonObject.put("patient", patientsJsonArray); // only for test code purpose
131131
returnJsonObject.put("relation", relationshipsArray);// only for test code purpose

opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsValidateDataSync.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.lang3.exception.ExceptionUtils;
88
import org.joda.time.DateTime;
99
import org.json.JSONArray;
10+
import org.json.JSONException;
1011
import org.json.JSONObject;
1112
import org.opensrp.connector.openmrs.constants.OpenmrsConstants.SchedulerConfig;
1213
import org.opensrp.connector.openmrs.service.EncounterService;
@@ -53,6 +54,7 @@ public OpenmrsValidateDataSync(ConfigService config, PatientService patientServi
5354
this.clientService = clientService;
5455
this.encounterService = encounterService;
5556
this.eventService = eventService;
57+
this.errorTraceService = errorTraceService;
5658

5759
this.config.registerAppStateToken(SchedulerConfig.openmrs_client_sync_validator_timestamp, 0,
5860
"OpenMRS data Sync validation to keep track of client records were not successfully pushed to OpenMRS", true);
@@ -77,23 +79,20 @@ public void syncToOpenMRS() {
7779
AppStateToken lastValidated = config
7880
.getAppStateTokenByName(SchedulerConfig.openmrs_client_sync_validator_timestamp);
7981
Long start = lastValidated == null || lastValidated.getValue() == null ? 0 : lastValidated.longValue();
80-
List<Client> cl = clientService.notInOpenMRSByServerVersion(start, calendar);
8182

83+
List<Client> cl = clientService.notInOpenMRSByServerVersion(start, calendar);
8284
logger.info("Clients_list_size " + cl.size());
83-
JSONArray patientsJsonArray = new JSONArray();
8485

85-
patientService.processClients(cl, patientsJsonArray, SchedulerConfig.openmrs_client_sync_validator_timestamp,
86-
"OPENMRS FAILED CLIENT VALIDATION");
87-
88-
logger.info("RUNNING FOR RELATIONSHIPS");
89-
patientService.createRelationShip(cl);
90-
86+
pushValidateClient(cl);
87+
9188
lastValidated = config.getAppStateTokenByName(SchedulerConfig.openmrs_event_sync_validator_timestamp);
9289
start = lastValidated == null || lastValidated.getValue() == null ? 0 : lastValidated.longValue();
90+
9391
List<Event> el = eventService.notInOpenMRSByServerVersion(start, calendar);
9492
logger.info("Events list size " + el.size());
93+
9594
pushValidateEvent(el);
96-
95+
9796
}
9897
catch (Exception ex) {
9998
logger.error("", ex);
@@ -103,17 +102,35 @@ public void syncToOpenMRS() {
103102
}
104103
}
105104

105+
public void pushValidateClient(List<Client> cl) throws JSONException {
106+
try {
107+
108+
JSONArray patientsJsonArray = new JSONArray();
109+
110+
if (cl != null && !cl.isEmpty()) {
111+
patientService.processClients(cl, patientsJsonArray, SchedulerConfig.openmrs_client_sync_validator_timestamp,
112+
"OPENMRS FAILED CLIENT VALIDATION");
113+
114+
logger.info("RUNNING FOR RELATIONSHIPS");
115+
patientService.createRelationShip(cl, "OPENMRS FAILED CLIENT RELATIONSHIP VALIDATION");
116+
}
117+
}
118+
catch (Exception e) {
119+
logger.error("", e);
120+
errorTraceService.log("OPENMRS FAILED CLIENT VALIDATION", Client.class.getName(), "",
121+
ExceptionUtils.getStackTrace(e), "");
122+
}
123+
}
124+
106125
public JSONObject pushValidateEvent(List<Event> el) {
107-
108126
JSONObject eventJson = null;
109127
for (Event e : el) {
110128
try {
111129
eventJson = encounterService.createEncounter(e);
112130
if (eventJson != null && eventJson.has("uuid")) {
113131
e.addIdentifier(EncounterService.OPENMRS_UUID_IDENTIFIER_TYPE, eventJson.getString("uuid"));
114132
eventService.updateEvent(e);
115-
config.updateAppStateToken(SchedulerConfig.openmrs_event_sync_validator_timestamp,
116-
e.getServerVersion());
133+
config.updateAppStateToken(SchedulerConfig.openmrs_event_sync_validator_timestamp, e.getServerVersion());
117134
}
118135
}
119136
catch (Exception ex2) {
@@ -123,7 +140,6 @@ public JSONObject pushValidateEvent(List<Event> el) {
123140
}
124141
}
125142
return eventJson;
126-
127143
}
128144

129145
}

opensrp-connector/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.json.JSONArray;
1212
import org.json.JSONException;
1313
import org.json.JSONObject;
14+
import org.opensrp.api.domain.Location;
1415
import org.opensrp.api.util.LocationTree;
1516
import org.opensrp.common.util.HttpResponse;
1617
import org.opensrp.common.util.HttpUtil;
@@ -202,7 +203,7 @@ public JSONObject convertRaletionsShipToOpenmrsJson(String personB, String perso
202203
return relation;
203204
}
204205

205-
public void createRelationShip(List<Client> cl) throws JSONException {
206+
public void createRelationShip(List<Client> cl, String errorType) throws JSONException {
206207
if (cl != null && !cl.isEmpty())
207208
for (Client c : cl) {
208209
try {
@@ -237,6 +238,8 @@ && getPatientByIdentifier(client.getBaseEntityId()) != null) {
237238
}
238239
catch (Exception e) {
239240
e.printStackTrace();
241+
errorTraceService.log(errorType, Client.class.getName(), c.getBaseEntityId(),
242+
ExceptionUtils.getStackTrace(e), "");
240243
}
241244
}
242245
}
@@ -345,17 +348,20 @@ public JSONObject convertBaseEntityToOpenmrsJson(Client be, boolean update) thro
345348
if (!fn.equals("-")) {
346349
fn = fn.replaceAll("[^A-Za-z0-9\\s]+", "");
347350
}
351+
fn = convertToOpenmrsString(fn);
348352

349353
String mn = be.getMiddleName() == null ? "" : be.getMiddleName();
350354

351355
if (!mn.equals("-")) {
352356
mn = mn.replaceAll("[^A-Za-z0-9\\s]+", "");
353357
}
358+
mn = convertToOpenmrsString(mn);
354359

355360
String ln = (be.getLastName() == null || be.getLastName().equals(".")) ? "-" : be.getLastName();
356361
if (!ln.equals("-")) {
357362
ln = ln.replaceAll("[^A-Za-z0-9\\s]+", "");
358363
}
364+
ln = convertToOpenmrsString(ln);
359365

360366
List<Event> registrationEvents = eventService.findByBaseEntityId(be.getBaseEntityId());
361367
for (Event event : registrationEvents) {
@@ -392,7 +398,7 @@ public JSONArray convertAttributesToOpenmrsJson(Map<String, Object> attributes)
392398
for (Entry<String, Object> at : attributes.entrySet()) {
393399
JSONObject a = new JSONObject();
394400
a.put("attributeType", getPersonAttributeType(at.getKey()).getString("uuid"));
395-
a.put("value", at.getValue());
401+
a.put("value", convertToOpenmrsString(at.getValue()));
396402
attrs.put(a);
397403
}
398404

@@ -408,15 +414,15 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti
408414
for (Address ad : adl) {
409415
JSONObject jao = new JSONObject();
410416
if (ad.getAddressFields() != null) {
411-
jao.put("address1",
412-
ad.getAddressFieldMatchingRegex("(?i)(ADDRESS1|HOUSE_NUMBER|HOUSE|HOUSE_NO|UNIT|UNIT_NUMBER|UNIT_NO)"));
413-
jao.put("address2", ad.getAddressFieldMatchingRegex("(?i)(ADDRESS2|STREET|STREET_NUMBER|STREET_NO|LANE)"));
417+
jao.put("address1", convertToOpenmrsString(
418+
ad.getAddressFieldMatchingRegex("(?i)(ADDRESS1|HOUSE_NUMBER|HOUSE|HOUSE_NO|UNIT|UNIT_NUMBER|UNIT_NO)")));
419+
jao.put("address2", convertToOpenmrsString(
420+
ad.getAddressFieldMatchingRegex("(?i)(ADDRESS2|STREET|STREET_NUMBER|STREET_NO|LANE)")));
414421
String address3 = ad.getAddressFieldMatchingRegex("(?i)(ADDRESS3|SECTOR|AREA)");
415-
if (!address3.equals("Other") && address3 != null && !StringUtils.isEmptyOrWhitespaceOnly(address3)) {
416-
address3 = openmrsLocationService.getLocation(address3).getName();
417-
}
418-
jao.put("address3", address3);
419-
jao.put("address5", ad.getAddressFieldMatchingRegex("(?i)(ADDRESS5|OTHER_RESIDENTIAL_AREA)"));
422+
address3 = fetchLocationByUUID(address3);
423+
jao.put("address3", convertToOpenmrsString(address3));
424+
jao.put("address5",
425+
convertToOpenmrsString(ad.getAddressFieldMatchingRegex("(?i)(ADDRESS5|OTHER_RESIDENTIAL_AREA)")));
420426

421427
List<Event> registrationEvents = eventService.findByBaseEntityId(client.getBaseEntityId());
422428
for (Event event : registrationEvents) {
@@ -433,9 +439,8 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti
433439
Map<String, String> locationsHierarchyMap = openmrsLocationService
434440
.getLocationsHierarchy(locationTree);
435441

436-
String clientAddress4 = openmrsLocationService.getLocation(obs2.getValue().toString())
437-
.getName();
438-
jao.put("address4", clientAddress4);
442+
String clientAddress4 = fetchLocationByUUID(obs2.getValue().toString());
443+
jao.put("address4", convertToOpenmrsString(clientAddress4));
439444
jao.put("countyDistrict",
440445
locationsHierarchyMap.containsKey(AllowedLevels.DISTRICT.toString())
441446
? locationsHierarchyMap.get(AllowedLevels.DISTRICT.toString())
@@ -454,11 +459,11 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti
454459

455460
}
456461
}
457-
jao.put("cityVillage", ad.getCityVillage());
462+
jao.put("cityVillage", convertToOpenmrsString(ad.getCityVillage()));
458463

459464
}
460-
jao.put("address6", ad.getAddressType());
461-
jao.put("postalCode", ad.getPostalCode());
465+
jao.put("address6", convertToOpenmrsString(ad.getAddressType()));
466+
jao.put("postalCode", convertToOpenmrsString(ad.getPostalCode()));
462467
jao.put("latitude", ad.getLatitude());
463468
jao.put("longitude", ad.getLongitude());
464469
if (ad.getStartDate() != null) {
@@ -792,4 +797,42 @@ public void updateIdentifiers(String personUUID, Client c) throws JSONException
792797
}
793798
}
794799
}
800+
801+
private String convertToOpenmrsString(String s) {
802+
if (org.apache.commons.lang3.StringUtils.isEmpty(s)) {
803+
return s;
804+
}
805+
806+
s = s.replaceAll("\t", "");
807+
s = org.apache.commons.lang3.StringUtils.stripAccents(s);
808+
return s;
809+
810+
}
811+
812+
private Object convertToOpenmrsString(Object o) {
813+
if (o != null && o instanceof String) {
814+
return convertToOpenmrsString(o.toString());
815+
}
816+
return o;
817+
818+
}
819+
820+
private String fetchLocationByUUID(String locationUUID) {
821+
try {
822+
if (locationUUID == null || StringUtils.isEmptyOrWhitespaceOnly(locationUUID)
823+
|| locationUUID.equalsIgnoreCase("Other")) {
824+
return locationUUID;
825+
}
826+
Location location = openmrsLocationService.getLocation(locationUUID);
827+
if (location == null) {
828+
return "Unknown Location Id: " + locationUUID;
829+
} else {
830+
return location.getName();
831+
}
832+
}
833+
catch (Exception e) {
834+
return "Unknown Location Id: " + locationUUID;
835+
}
836+
}
837+
795838
}

opensrp-core/src/main/resources/applicationContext-opensrp.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@
4141
<constructor-arg ref="couchDbInstance" />
4242
</bean>
4343

44-
<task:scheduler id="applicationScheduler" pool-size="10" />
44+
<task:scheduler id="applicationScheduler" pool-size="50" />
4545

4646
<task:scheduled-tasks scheduler="applicationScheduler">
4747
<task:scheduled ref="eventsListener" method="processEvent"
4848
fixed-delay="180000" />
4949
<task:scheduled ref="openmrsSyncerListener" method="pushToOpenMRS"
5050
fixed-delay="300000" />
51+
<task:scheduled ref="openmrsValidateDataSync" method="syncToOpenMRS"
52+
fixed-delay="420000" />
5153
<task:scheduled ref="DHIS2DatasetPush" method="pushToDHIS2"
52-
fixed-delay="560000" />
54+
fixed-delay="600000" />
5355
<task:scheduled ref="openmrsAtomfeedListener" method="syncAtomfeeds"
54-
fixed-delay="660000" />
55-
<task:scheduled ref="openmrsValidateDataSync" method="syncToOpenMRS"
5656
fixed-delay="900000" />
5757
</task:scheduled-tasks>
5858

0 commit comments

Comments
 (0)