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

Commit

Permalink
Replace strings/locations not accepted in OMRS
Browse files Browse the repository at this point in the history
Signed-off-by: Ephraim Muhia <[email protected]>
  • Loading branch information
Ephraim Muhia authored and Ephraim Muhia committed Nov 22, 2017
1 parent d9ea051 commit 9565ff5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<Client> cl = clientService.notInOpenMRSByServerVersion(start, calendar);

List<Client> 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<Event> el = eventService.notInOpenMRSByServerVersion(start, calendar);
logger.info("Events list size " + el.size());

pushValidateEvent(el);

}
catch (Exception ex) {
logger.error("", ex);
Expand All @@ -103,17 +102,35 @@ public void syncToOpenMRS() {
}
}

public void pushValidateClient(List<Client> 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<Event> el) {

JSONObject eventJson = null;
for (Event e : el) {
try {
eventJson = encounterService.createEncounter(e);
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) {
Expand All @@ -123,7 +140,6 @@ public JSONObject pushValidateEvent(List<Event> el) {
}
}
return eventJson;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -202,7 +203,7 @@ public JSONObject convertRaletionsShipToOpenmrsJson(String personB, String perso
return relation;
}

public void createRelationShip(List<Client> cl) throws JSONException {
public void createRelationShip(List<Client> cl, String errorType) throws JSONException {
if (cl != null && !cl.isEmpty())
for (Client c : cl) {
try {
Expand Down Expand Up @@ -237,6 +238,8 @@ && getPatientByIdentifier(client.getBaseEntityId()) != null) {
}
catch (Exception e) {
e.printStackTrace();
errorTraceService.log(errorType, Client.class.getName(), c.getBaseEntityId(),
ExceptionUtils.getStackTrace(e), "");
}
}
}
Expand Down Expand Up @@ -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<Event> registrationEvents = eventService.findByBaseEntityId(be.getBaseEntityId());
for (Event event : registrationEvents) {
Expand Down Expand Up @@ -392,7 +398,7 @@ public JSONArray convertAttributesToOpenmrsJson(Map<String, Object> attributes)
for (Entry<String, Object> 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);
}

Expand All @@ -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<Event> registrationEvents = eventService.findByBaseEntityId(client.getBaseEntityId());
for (Event event : registrationEvents) {
Expand All @@ -433,9 +439,8 @@ public JSONArray convertAddressesToOpenmrsJson(Client client) throws JSONExcepti
Map<String, String> 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())
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@
<constructor-arg ref="couchDbInstance" />
</bean>

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

<task:scheduled-tasks scheduler="applicationScheduler">
<task:scheduled ref="eventsListener" method="processEvent"
fixed-delay="180000" />
<task:scheduled ref="openmrsSyncerListener" method="pushToOpenMRS"
fixed-delay="300000" />
<task:scheduled ref="openmrsValidateDataSync" method="syncToOpenMRS"
fixed-delay="420000" />
<task:scheduled ref="DHIS2DatasetPush" method="pushToDHIS2"
fixed-delay="560000" />
fixed-delay="600000" />
<task:scheduled ref="openmrsAtomfeedListener" method="syncAtomfeeds"
fixed-delay="660000" />
<task:scheduled ref="openmrsValidateDataSync" method="syncToOpenMRS"
fixed-delay="900000" />
</task:scheduled-tasks>

Expand Down

0 comments on commit 9565ff5

Please sign in to comment.