From 9565ff567bdeee4dd88d781d462226eb848472fd Mon Sep 17 00:00:00 2001 From: Ephraim Muhia Date: Wed, 22 Nov 2017 12:41:23 +0300 Subject: [PATCH] Replace strings/locations not accepted in OMRS Signed-off-by: Ephraim Muhia --- .../schedule/OpenmrsSyncerListener.java | 2 +- .../schedule/OpenmrsValidateDataSync.java | 42 +++++++---- .../openmrs/service/PatientService.java | 75 +++++++++++++++---- .../resources/applicationContext-opensrp.xml | 8 +- 4 files changed, 93 insertions(+), 34 deletions(-) diff --git a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsSyncerListener.java b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsSyncerListener.java index 7181b0cdd2..d81f0187ca 100644 --- a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsSyncerListener.java +++ b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsSyncerListener.java @@ -125,7 +125,7 @@ public JSONObject pushClient(long start) throws JSONException { SchedulerConfig.openmrs_syncer_sync_client_by_date_updated, "OPENMRS FAILED CLIENT PUSH"); logger.info("RUNNING FOR RELATIONSHIPS"); - patientService.createRelationShip(cl); + patientService.createRelationShip(cl, "OPENMRS FAILED CLIENT RELATIONSHIP PUSH"); } returnJsonObject.put("patient", patientsJsonArray); // only for test code purpose returnJsonObject.put("relation", relationshipsArray);// only for test code purpose diff --git a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsValidateDataSync.java b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsValidateDataSync.java index b9bddd7f0d..3dddc63bc3 100644 --- a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsValidateDataSync.java +++ b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/schedule/OpenmrsValidateDataSync.java @@ -7,6 +7,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.joda.time.DateTime; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.opensrp.connector.openmrs.constants.OpenmrsConstants.SchedulerConfig; import org.opensrp.connector.openmrs.service.EncounterService; @@ -53,6 +54,7 @@ public OpenmrsValidateDataSync(ConfigService config, PatientService patientServi this.clientService = clientService; this.encounterService = encounterService; this.eventService = eventService; + this.errorTraceService = errorTraceService; this.config.registerAppStateToken(SchedulerConfig.openmrs_client_sync_validator_timestamp, 0, "OpenMRS data Sync validation to keep track of client records were not successfully pushed to OpenMRS", true); @@ -77,23 +79,20 @@ public void syncToOpenMRS() { AppStateToken lastValidated = config .getAppStateTokenByName(SchedulerConfig.openmrs_client_sync_validator_timestamp); Long start = lastValidated == null || lastValidated.getValue() == null ? 0 : lastValidated.longValue(); - List cl = clientService.notInOpenMRSByServerVersion(start, calendar); + List cl = clientService.notInOpenMRSByServerVersion(start, calendar); logger.info("Clients_list_size " + cl.size()); - JSONArray patientsJsonArray = new JSONArray(); - patientService.processClients(cl, patientsJsonArray, SchedulerConfig.openmrs_client_sync_validator_timestamp, - "OPENMRS FAILED CLIENT VALIDATION"); - - logger.info("RUNNING FOR RELATIONSHIPS"); - patientService.createRelationShip(cl); - + pushValidateClient(cl); + lastValidated = config.getAppStateTokenByName(SchedulerConfig.openmrs_event_sync_validator_timestamp); start = lastValidated == null || lastValidated.getValue() == null ? 0 : lastValidated.longValue(); + List el = eventService.notInOpenMRSByServerVersion(start, calendar); logger.info("Events list size " + el.size()); + pushValidateEvent(el); - + } catch (Exception ex) { logger.error("", ex); @@ -103,8 +102,27 @@ public void syncToOpenMRS() { } } + public void pushValidateClient(List cl) throws JSONException { + try { + + JSONArray patientsJsonArray = new JSONArray(); + + if (cl != null && !cl.isEmpty()) { + patientService.processClients(cl, patientsJsonArray, SchedulerConfig.openmrs_client_sync_validator_timestamp, + "OPENMRS FAILED CLIENT VALIDATION"); + + logger.info("RUNNING FOR RELATIONSHIPS"); + patientService.createRelationShip(cl, "OPENMRS FAILED CLIENT RELATIONSHIP VALIDATION"); + } + } + catch (Exception e) { + logger.error("", e); + errorTraceService.log("OPENMRS FAILED CLIENT VALIDATION", Client.class.getName(), "", + ExceptionUtils.getStackTrace(e), ""); + } + } + public JSONObject pushValidateEvent(List el) { - JSONObject eventJson = null; for (Event e : el) { try { @@ -112,8 +130,7 @@ public JSONObject pushValidateEvent(List el) { if (eventJson != null && eventJson.has("uuid")) { e.addIdentifier(EncounterService.OPENMRS_UUID_IDENTIFIER_TYPE, eventJson.getString("uuid")); eventService.updateEvent(e); - config.updateAppStateToken(SchedulerConfig.openmrs_event_sync_validator_timestamp, - e.getServerVersion()); + config.updateAppStateToken(SchedulerConfig.openmrs_event_sync_validator_timestamp, e.getServerVersion()); } } catch (Exception ex2) { @@ -123,7 +140,6 @@ public JSONObject pushValidateEvent(List el) { } } return eventJson; - } } diff --git a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java index 121a2218c4..b8db346710 100644 --- a/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java +++ b/opensrp-connector/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java @@ -11,6 +11,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.opensrp.api.domain.Location; import org.opensrp.api.util.LocationTree; import org.opensrp.common.util.HttpResponse; import org.opensrp.common.util.HttpUtil; @@ -202,7 +203,7 @@ public JSONObject convertRaletionsShipToOpenmrsJson(String personB, String perso return relation; } - public void createRelationShip(List cl) throws JSONException { + public void createRelationShip(List cl, String errorType) throws JSONException { if (cl != null && !cl.isEmpty()) for (Client c : cl) { try { @@ -237,6 +238,8 @@ && getPatientByIdentifier(client.getBaseEntityId()) != null) { } catch (Exception e) { e.printStackTrace(); + errorTraceService.log(errorType, Client.class.getName(), c.getBaseEntityId(), + ExceptionUtils.getStackTrace(e), ""); } } } @@ -345,17 +348,20 @@ public JSONObject convertBaseEntityToOpenmrsJson(Client be, boolean update) thro if (!fn.equals("-")) { fn = fn.replaceAll("[^A-Za-z0-9\\s]+", ""); } + fn = convertToOpenmrsString(fn); String mn = be.getMiddleName() == null ? "" : be.getMiddleName(); if (!mn.equals("-")) { mn = mn.replaceAll("[^A-Za-z0-9\\s]+", ""); } + mn = convertToOpenmrsString(mn); String ln = (be.getLastName() == null || be.getLastName().equals(".")) ? "-" : be.getLastName(); if (!ln.equals("-")) { ln = ln.replaceAll("[^A-Za-z0-9\\s]+", ""); } + ln = convertToOpenmrsString(ln); List registrationEvents = eventService.findByBaseEntityId(be.getBaseEntityId()); for (Event event : registrationEvents) { @@ -392,7 +398,7 @@ public JSONArray convertAttributesToOpenmrsJson(Map attributes) for (Entry at : attributes.entrySet()) { JSONObject a = new JSONObject(); a.put("attributeType", getPersonAttributeType(at.getKey()).getString("uuid")); - a.put("value", at.getValue()); + a.put("value", convertToOpenmrsString(at.getValue())); attrs.put(a); } @@ -408,15 +414,15 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti for (Address ad : adl) { JSONObject jao = new JSONObject(); if (ad.getAddressFields() != null) { - jao.put("address1", - ad.getAddressFieldMatchingRegex("(?i)(ADDRESS1|HOUSE_NUMBER|HOUSE|HOUSE_NO|UNIT|UNIT_NUMBER|UNIT_NO)")); - jao.put("address2", ad.getAddressFieldMatchingRegex("(?i)(ADDRESS2|STREET|STREET_NUMBER|STREET_NO|LANE)")); + jao.put("address1", convertToOpenmrsString( + ad.getAddressFieldMatchingRegex("(?i)(ADDRESS1|HOUSE_NUMBER|HOUSE|HOUSE_NO|UNIT|UNIT_NUMBER|UNIT_NO)"))); + jao.put("address2", convertToOpenmrsString( + ad.getAddressFieldMatchingRegex("(?i)(ADDRESS2|STREET|STREET_NUMBER|STREET_NO|LANE)"))); String address3 = ad.getAddressFieldMatchingRegex("(?i)(ADDRESS3|SECTOR|AREA)"); - if (!address3.equals("Other") && address3 != null && !StringUtils.isEmptyOrWhitespaceOnly(address3)) { - address3 = openmrsLocationService.getLocation(address3).getName(); - } - jao.put("address3", address3); - jao.put("address5", ad.getAddressFieldMatchingRegex("(?i)(ADDRESS5|OTHER_RESIDENTIAL_AREA)")); + address3 = fetchLocationByUUID(address3); + jao.put("address3", convertToOpenmrsString(address3)); + jao.put("address5", + convertToOpenmrsString(ad.getAddressFieldMatchingRegex("(?i)(ADDRESS5|OTHER_RESIDENTIAL_AREA)"))); List registrationEvents = eventService.findByBaseEntityId(client.getBaseEntityId()); for (Event event : registrationEvents) { @@ -433,9 +439,8 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti Map locationsHierarchyMap = openmrsLocationService .getLocationsHierarchy(locationTree); - String clientAddress4 = openmrsLocationService.getLocation(obs2.getValue().toString()) - .getName(); - jao.put("address4", clientAddress4); + String clientAddress4 = fetchLocationByUUID(obs2.getValue().toString()); + jao.put("address4", convertToOpenmrsString(clientAddress4)); jao.put("countyDistrict", locationsHierarchyMap.containsKey(AllowedLevels.DISTRICT.toString()) ? locationsHierarchyMap.get(AllowedLevels.DISTRICT.toString()) @@ -454,11 +459,11 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti } } - jao.put("cityVillage", ad.getCityVillage()); + jao.put("cityVillage", convertToOpenmrsString(ad.getCityVillage())); } - jao.put("address6", ad.getAddressType()); - jao.put("postalCode", ad.getPostalCode()); + jao.put("address6", convertToOpenmrsString(ad.getAddressType())); + jao.put("postalCode", convertToOpenmrsString(ad.getPostalCode())); jao.put("latitude", ad.getLatitude()); jao.put("longitude", ad.getLongitude()); if (ad.getStartDate() != null) { @@ -792,4 +797,42 @@ public void updateIdentifiers(String personUUID, Client c) throws JSONException } } } + + private String convertToOpenmrsString(String s) { + if (org.apache.commons.lang3.StringUtils.isEmpty(s)) { + return s; + } + + s = s.replaceAll("\t", ""); + s = org.apache.commons.lang3.StringUtils.stripAccents(s); + return s; + + } + + private Object convertToOpenmrsString(Object o) { + if (o != null && o instanceof String) { + return convertToOpenmrsString(o.toString()); + } + return o; + + } + + private String fetchLocationByUUID(String locationUUID) { + try { + if (locationUUID == null || StringUtils.isEmptyOrWhitespaceOnly(locationUUID) + || locationUUID.equalsIgnoreCase("Other")) { + return locationUUID; + } + Location location = openmrsLocationService.getLocation(locationUUID); + if (location == null) { + return "Unknown Location Id: " + locationUUID; + } else { + return location.getName(); + } + } + catch (Exception e) { + return "Unknown Location Id: " + locationUUID; + } + } + } diff --git a/opensrp-core/src/main/resources/applicationContext-opensrp.xml b/opensrp-core/src/main/resources/applicationContext-opensrp.xml index da35cd154b..1b1a5d8396 100644 --- a/opensrp-core/src/main/resources/applicationContext-opensrp.xml +++ b/opensrp-core/src/main/resources/applicationContext-opensrp.xml @@ -41,18 +41,18 @@ - + + + fixed-delay="600000" /> -