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

Commit

Permalink
Merge pull request #426 from OpenSRP/zeir_updates_to_omrs
Browse files Browse the repository at this point in the history
Zeir updates to omrs
  • Loading branch information
manutarus authored Apr 25, 2018
2 parents 8cb7cf8 + c74b620 commit 25e8cf2
Show file tree
Hide file tree
Showing 6 changed files with 512 additions and 483 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public DateTime logger(String message, String subject) {

}

public JSONObject pushClient(long start) throws JSONException {
public JSONObject pushClient(long start) {
try {
List<Client> cl = clientService.findByServerVersion(start);
logger.info("Clients list size " + cl.size());
Expand All @@ -123,7 +123,6 @@ public JSONObject pushClient(long start) throws JSONException {
if (cl != null && !cl.isEmpty()) {
patientService.processClients(cl, patientsJsonArray,
SchedulerConfig.openmrs_syncer_sync_client_by_date_updated, "OPENMRS FAILED CLIENT PUSH");

logger.info("RUNNING FOR RELATIONSHIPS");
patientService.createRelationShip(cl, "OPENMRS FAILED CLIENT RELATIONSHIP PUSH");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.opensrp.connector.openmrs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.StringUtils;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -17,14 +13,21 @@
import org.opensrp.domain.Obs;
import org.opensrp.domain.User;
import org.opensrp.service.ClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EncounterService extends OpenmrsService {

private static Logger logger = LoggerFactory.getLogger(EncounterService.class.toString());

public static final String OPENMRS_UUID_IDENTIFIER_TYPE = "OPENMRS_UUID";

private static final String ENCOUNTER_URL = "ws/rest/v1/encounter";//"ws/rest/emrapi/encounter";
Expand Down Expand Up @@ -116,85 +119,83 @@ public JSONObject convertEncounterToOpenmrsJson(String name, String description)
a.put("description", description);
return a;
}

public JSONObject createEncounter(Event e) throws JSONException {
String ptuuid = patientService.getPatientByIdentifierUUID(e.getBaseEntityId());
if (ptuuid == null) {
return null;
}
JSONObject enc = new JSONObject();

String pruuid = userService.getPersonUUIDByUser(e.getProviderId());

enc.put("encounterDatetime", OPENMRS_DATE.format(e.getEventDate().toDate()));
// patient must be existing in OpenMRS before it submits an encounter. if it doesnot it would throw NPE
enc.put("patient", ptuuid);
//TODO enc.put("patientUuid", pt.getString("uuid"));
enc.put("encounterType", e.getEventType());
//TODO enc.put("encounterTypeUuid", e.getEventType());
enc.put("location", e.getLocationId());
enc.put("provider", pruuid);

List<Obs> ol = e.getObs();
Map<String, JSONArray> p = new HashMap<>();
Map<String, JSONArray> pc = new HashMap<>();

if (ol != null)
for (Obs obs : ol) {
if (!StringUtils.isEmptyOrWhitespaceOnly(obs.getFieldCode())
&& (obs.getFieldType() == null || obs.getFieldType().equalsIgnoreCase("concept"))) {
// skipping empty obs and fields that don't have concepts if no parent simply make it root obs

if (obs.getFieldType().equals("concept") && obs.getFormSubmissionField().equals("Birth_Facility_Name")
&& obs.getValue() != null) {
Location location = openmrsLocationService.getLocation(obs.getValue().toString());
if (location != null && location.getName() != null) {
obs.setValue(location.getName());
} else {
if (e.getEventType().equals("Update Birth Registration")) {
patientService.updatePersonAddress(e);
} else if (e.getEventType().equals("Death")) {
patientService.updatePersonAsDeceased(e);
}else if (e.getEventType().equals("Move To Catchment")) {
patientService.moveToCatchment(e);
}
JSONObject enc = new JSONObject();

String pruuid = userService.getPersonUUIDByUser(e.getProviderId());

enc.put("encounterDatetime", OPENMRS_DATE.format(e.getEventDate().toDate()));
// patient must be existing in OpenMRS before it submits an encounter. if it doesnot it would throw NPE
enc.put("patient", ptuuid);
//TODO enc.put("patientUuid", pt.getString("uuid"));
enc.put("encounterType", e.getEventType());
//TODO enc.put("encounterTypeUuid", e.getEventType());
enc.put("location", e.getLocationId());
enc.put("provider", pruuid);

List<Obs> ol = e.getObs();
Map<String, JSONArray> p = new HashMap<>();
Map<String, JSONArray> pc = new HashMap<>();

if (ol != null)
for (Obs obs : ol) {
if (!StringUtils.isEmptyOrWhitespaceOnly(obs.getFieldCode())
&& (obs.getFieldType() == null || obs.getFieldType().equalsIgnoreCase("concept"))) {
// skipping empty obs and fields that don't have concepts if no parent simply make it root obs
if (obs.getFieldType().equals("concept")
&& obs.getFormSubmissionField().equals("Birth_Facility_Name") && obs.getValue() != null) {
Location location = openmrsLocationService.getLocation(obs.getValue().toString());
if (location != null && location.getName() != null) {
obs.setValue(location.getName());
}
}
generateObs(p, pc, obs, ol);
}
if (StringUtils.isEmptyOrWhitespaceOnly(obs.getParentCode())) {
p.put(obs.getFieldCode(), convertObsToJson(obs));
} else {
//find parent obs if not found search and fill or create one
JSONArray parentObs = p.get(obs.getParentCode());
if (parentObs == null) {
p.put(obs.getParentCode(), convertObsToJson(getOrCreateParent(ol, obs)));
}
// find if any other exists with same parent if so add to the list otherwise create new list
JSONArray obl = pc.get(obs.getParentCode());
if (obl == null) {
obl = new JSONArray();
}
JSONArray addobs = convertObsToJson(obs);
for (int i = 0; i < addobs.length(); i++) {
obl.put(addobs.getJSONObject(i));
}

JSONArray obar = new JSONArray();

for (String ok : p.keySet()) {
for (int i = 0; i < p.get(ok).length(); i++) {
JSONObject obo = p.get(ok).getJSONObject(i);
JSONArray cob = pc.get(ok);
if (cob != null && cob.length() > 0) {
//fix for vaccines wrong parent concept remove the if-condition once the right concepts are passed
if (e.getEventType().equals("Vaccination")) {
JSONObject vaccineParent = new JSONObject();
vaccineParent.put("concept", ok);
cob.put(vaccineParent);
obar = concatArray(obar, cob);
} else {
obo.put("groupMembers", cob);
obar.put(obo);
}
pc.put(obs.getParentCode(), obl);
} else {
obar.put(obo);
}
}
}

JSONArray obar = new JSONArray();
for (String ok : p.keySet()) {
for (int i = 0; i < p.get(ok).length(); i++) {
JSONObject obo = p.get(ok).getJSONObject(i);

JSONArray cob = pc.get(ok);
if (cob != null && cob.length() > 0) {
obo.put("groupMembers", cob);
}

obar.put(obo);
}
enc.put("obs", obar);
HttpResponse op = HttpUtil.post(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + ENCOUNTER_URL, "",
enc.toString(), OPENMRS_USER, OPENMRS_PWD);
return new JSONObject(op.body());
}
enc.put("obs", obar);

HttpResponse op = HttpUtil.post(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + ENCOUNTER_URL, "",
enc.toString(), OPENMRS_USER, OPENMRS_PWD);
return new JSONObject(op.body());
}

public JSONObject buildUpdateEncounter(Event e) throws JSONException {

String openmrsuuid = e.getIdentifier(OPENMRS_UUID_IDENTIFIER_TYPE);
JSONObject encounterObsUuids = getObsByEncounterUuid(openmrsuuid);
JSONArray obsUuids = encounterObsUuids.getJSONArray("obs");
Expand Down Expand Up @@ -228,25 +229,7 @@ public JSONObject buildUpdateEncounter(Event e) throws JSONException {
&& openmrsLocationService.getLocation(obs.getValue().toString()).getName() != null) {
obs.setValue(openmrsLocationService.getLocation(obs.getValue().toString()).getName());
}
if (StringUtils.isEmptyOrWhitespaceOnly(obs.getParentCode())) {
p.put(obs.getFieldCode(), convertObsToJson(obs));
} else {
//find parent obs if not found search and fill or create one
JSONArray parentObs = p.get(obs.getParentCode());
if (parentObs == null) {
p.put(obs.getParentCode(), convertObsToJson(getOrCreateParent(ol, obs)));
}
// find if any other exists with same parent if so add to the list otherwise create new list
JSONArray obl = pc.get(obs.getParentCode());
if (obl == null) {
obl = new JSONArray();
}
JSONArray addobs = convertObsToJson(obs);
for (int i = 0; i < addobs.length(); i++) {
obl.put(addobs.getJSONObject(i));
}
pc.put(obs.getParentCode(), obl);
}
generateObs(p, pc, obs, ol);
}
}

Expand Down Expand Up @@ -278,6 +261,35 @@ public JSONObject buildUpdateEncounter(Event e) throws JSONException {
return enc;
}

private void generateObs(Map<String, JSONArray> p, Map<String, JSONArray> pc, Obs obs, List<Obs> ol) {
try {
if (StringUtils.isEmptyOrWhitespaceOnly(obs.getParentCode())) {

p.put(obs.getFieldCode(), convertObsToJson(obs));

} else {
//find parent obs if not found search and fill or create one
JSONArray parentObs = p.get(obs.getParentCode());
if (parentObs == null) {
p.put(obs.getParentCode(), convertObsToJson(getOrCreateParent(ol, obs)));
}
// find if any other exists with same parent if so add to the list otherwise create new list
JSONArray obl = pc.get(obs.getParentCode());
if (obl == null) {
obl = new JSONArray();
}
JSONArray addobs = convertObsToJson(obs);
for (int i = 0; i < addobs.length(); i++) {
obl.put(addobs.getJSONObject(i));
}
pc.put(obs.getParentCode(), obl);
}
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
}

public JSONObject updateEncounter(Event e) throws JSONException {
if (StringUtils.isEmptyOrWhitespaceOnly(e.getIdentifier(OPENMRS_UUID_IDENTIFIER_TYPE))) {
throw new IllegalArgumentException("Encounter was never pushed to OpenMRS as " + OPENMRS_UUID_IDENTIFIER_TYPE
Expand All @@ -296,9 +308,6 @@ public JSONObject updateEncounter(Event e) throws JSONException {

private String getObsUuid(JSONObject obs, JSONArray obsUuids) throws JSONException {
String uuid = "";
// obs = {"concept":"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}
// obsUuids = [{"concept":{"uuid":"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},"uuid":"b267b2f5-94be-43e8-85c4-4e36f2eb8471"}, {}]

for (int i = 0; i < obsUuids.length(); i++) {
JSONObject obsUuid = obsUuids.getJSONObject(i);
JSONObject conceptObj = obsUuid.getJSONObject("concept");
Expand Down Expand Up @@ -393,13 +402,16 @@ public Event convertToEvent(JSONObject encounter) throws JSONException {
return e;
}

public synchronized Event processUpdateEvents(Event event) throws JSONException {
if (event.getEventType().equals("Death")) {
patientService.updatePersonAsDeceased(event);
} else if (event.getEventType().equals("Update Birth Registration")) {
patientService.updatePersonAddressAndName(event);
private JSONArray concatArray(JSONArray... arrs) throws JSONException {
JSONArray result = new JSONArray();
for (JSONArray arr : arrs) {
if (arr != null && arr.length() > 0) {
for (int i = 0; i < arr.length(); i++) {
result.put(arr.get(i));
}
}

}

return event;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
public abstract class OpenmrsService {
@Value("#{opensrp['openmrs.url']}")
protected String OPENMRS_BASE_URL;

@Value("#{opensrp['openmrs.username']}")
protected String OPENMRS_USER;

@Value("#{opensrp['openmrs.password']}")
protected String OPENMRS_PWD;

public static final SimpleDateFormat OPENMRS_DATE = new SimpleDateFormat("yyyy-MM-dd");
public static final String PROBABLE_CAUSE_OF_DEATH_CONCEPT= "5002AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
public static final String PROBABLE_CAUSE_OF_DEATH_TEXT= "160218AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
public static final String PROBABLE_CAUSE_PARENT_CONCEPT= "5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
public static final String OTHER_NON_CODED_CONCEPT= "5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
public static final String PARENT_CHILD_RELATIONSHIP= "8d91a210-c2cc-11de-8d13-0010c6dffd0f";
public static final String SIBLING_SIBLING_RELATIONSHIP= "8d91a01c-c2cc-11de-8d13-0010c6dffd0f";

public OpenmrsService() { }

public OpenmrsService(String openmrsUrl, String user, String password) {
OPENMRS_BASE_URL = openmrsUrl;
OPENMRS_USER = user;
Expand All @@ -37,13 +39,13 @@ public OpenmrsService(String openmrsUrl, String user, String password) {
public String getURL() {
return HttpUtil.removeEndingSlash(OPENMRS_BASE_URL);
}

void setURL(String url) {
OPENMRS_BASE_URL = url;
}

public static void main(String[] args) {
System.out.println(OPENMRS_DATE.format(new Date()));
}
}

}
Loading

0 comments on commit 25e8cf2

Please sign in to comment.