1111import org .json .JSONArray ;
1212import org .json .JSONException ;
1313import org .json .JSONObject ;
14+ import org .opensrp .api .domain .Location ;
1415import org .opensrp .api .util .LocationTree ;
1516import org .opensrp .common .util .HttpResponse ;
1617import 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}
0 commit comments