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

OpenMRS2x support #484

Open
wants to merge 1 commit into
base: tb-reach
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.opensrp.common.util;

public enum OpenMRSCrossVariables {

TEAM_MEMBER_URL {
public String makeVariable(String openMRSVersion) {
if (openMRSVersion.startsWith("1")) {
return "ws/rest/v1/teammodule/member";
} else {
return "ws/rest/v1/team/teammember";
}
}
},

LOCATIONS_JSON_KEY {
public String makeVariable(String openMRSVersion) {
if (openMRSVersion.startsWith("1")) {
return "location";
} else {
return "locations";
}
}
};

public abstract String makeVariable(String openMRSVersion);

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public JSONObject createEncounter(Event e) throws JSONException {
enc.put("encounterType", e.getEventType());
//TODO enc.put("encounterTypeUuid", e.getEventType());
enc.put("location", e.getLocationId());
enc.put("provider", pruuid);
makeProvider(enc, e.getProviderId());

List<Obs> ol = e.getObs();
Map<String, JSONArray> p = new HashMap<>();
Map<String, JSONArray> pc = new HashMap<>();
Expand Down Expand Up @@ -193,6 +193,24 @@ public JSONObject createEncounter(Event e) throws JSONException {
enc.toString(), OPENMRS_USER, OPENMRS_PWD);
return new JSONObject(op.body());
}

private void makeProvider(JSONObject jsonObject, String providerId) {
try {
if (OPENMRS_VERSION.startsWith("1")) {
jsonObject.put(OPENMRS_PROVIDER, userService.getPersonUUIDByUser(providerId));
} else {
JSONArray providerRoleArray = new JSONArray();
JSONObject providerObj = new JSONObject();
providerObj.put(OPENMRS_PROVIDER, userService.getProvider(null, userService.getUser(providerId).getBaseEntityId()).getString("uuid"));
providerObj.put("encounterRole", userService.getEncounterRoleUUID(OPENMRS_PROVIDER));
providerRoleArray.put(providerObj);
jsonObject.put("encounterProviders", providerRoleArray);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}

public JSONObject buildUpdateEncounter(Event e) throws JSONException {
String openmrsuuid = e.getIdentifier(OPENMRS_UUID_IDENTIFIER_TYPE);
Expand All @@ -212,7 +230,7 @@ public JSONObject buildUpdateEncounter(Event e) throws JSONException {
//TODO enc.put("patientUuid", pt.getString("uuid"));
enc.put("encounterType", e.getEventType());
enc.put("location", e.getLocationId());
enc.put("provider", pruuid == null ? "" : pruuid);
makeProvider(enc, e.getProviderId());

List<Obs> ol = e.getObs();
Map<String, JSONArray> p = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public abstract class OpenmrsService {

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


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

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 OPENMRS_PROVIDER= "provider";
public OpenmrsService() { }

public OpenmrsService(String openmrsUrl, String user, String password) {
Expand All @@ -46,4 +50,4 @@ public static void main(String[] args) {
System.out.println(OPENMRS_DATE.format(new Date()));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.opensrp.connector.openmrs.service;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.opensrp.api.domain.User;
import org.opensrp.common.util.HttpResponse;
import org.opensrp.common.util.HttpUtil;
import org.opensrp.common.util.OpenMRSCrossVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -20,9 +22,14 @@ public class OpenmrsUserService extends OpenmrsService {
private static final String PROVIDER_URL = "ws/rest/v1/provider";

private static final String TEAM_MEMBER_URL = "ws/rest/v1/teammodule/member";

private static final String ENCOUNTER_ROLE_URL = "ws/rest/v1/encounterrole";

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


public static final String CUSTOM_UUID_PARAM = "v=custom:(uuid)";


public OpenmrsUserService() {
}

Expand Down Expand Up @@ -113,23 +120,44 @@ public String getPersonUUIDByUser(String username) throws JSONException {

return null;
}

public JSONObject getTeamMember(String uuid) throws JSONException {
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + TEAM_MEMBER_URL + "/" + uuid,
"v=full", OPENMRS_USER, OPENMRS_PWD);
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + OpenMRSCrossVariables.TEAM_MEMBER_URL.makeVariable(OPENMRS_VERSION) + "/" + uuid, "v=full", OPENMRS_USER, OPENMRS_PWD);
return new JSONObject(op.body());
}

public JSONObject getProvider(String identifier) throws JSONException {
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + PROVIDER_URL,
"v=full&q=" + identifier, OPENMRS_USER, OPENMRS_PWD);

public JSONObject getProvider(String identifier, String user) throws JSONException {
String payload;
if (user != null && !StringUtils.isBlank(user)) {
payload = "user=" + user+"&"+CUSTOM_UUID_PARAM;
} else {
payload = "v=full&q=" + identifier;
}
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + PROVIDER_URL, payload, OPENMRS_USER, OPENMRS_PWD);
JSONArray res = new JSONObject(op.body()).getJSONArray("results");
if (res.length() == 0) {
return null;
}
JSONObject obj = res.getJSONObject(0);
return obj;
}

public String getEncounterRoleUUID(String encounterRole) throws JSONException {

if (encounterRole == null || StringUtils.isBlank(encounterRole)) {
return null;
}
JSONObject encounterRoles = new JSONObject(HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + ENCOUNTER_ROLE_URL, "v=custom:(uuid,display)", OPENMRS_USER, OPENMRS_PWD).body());
if (encounterRoles.has("results") && encounterRoles.get("results") instanceof JSONArray) {
JSONArray res = encounterRoles.getJSONArray("results");
for (int i = 0; i < res.length(); i++) {
if (res.getJSONObject(i).getString("display").equalsIgnoreCase(encounterRole)) {
return res.getJSONObject(i).getString("uuid");
}
}
}
return null;
}

public JSONObject createProvider(String existingUsername, String identifier) throws JSONException {
JSONObject p = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void testHHScheduleData() throws JSONException, ParseException, JsonIOExc
household.addHHMember((Client) dep.get(hhmid).get("client"), (Event) dep.get(hhmid).get("event"));
}
if (pushToOpenmrsForTest) {
JSONObject pr = us.getProvider(fs.anmId());
JSONObject pr = us.getProvider(fs.anmId(),null);
if (pr == null) {
us.createProvider(fs.anmId(), fs.anmId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.opensrp.api.domain.User;
import org.opensrp.api.util.LocationTree;
import org.opensrp.common.domain.UserDetail;
import org.opensrp.common.util.OpenMRSCrossVariables;
import org.opensrp.connector.openmrs.service.OpenmrsLocationService;
import org.opensrp.connector.openmrs.service.OpenmrsUserService;
import org.opensrp.web.security.DrishtiAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -45,6 +47,8 @@ public class UserController {
private DrishtiAuthenticationProvider opensrpAuthenticationProvider;
private OpenmrsLocationService openmrsLocationService;
private OpenmrsUserService openmrsUserService;
@Value("#{opensrp['openmrs.version']}")
protected String OPENMRS_VERSION;

@Autowired
public UserController(OpenmrsLocationService openmrsLocationService, OpenmrsUserService openmrsUserService,
Expand Down Expand Up @@ -101,7 +105,7 @@ public ResponseEntity<String> authenticate(HttpServletRequest request) throws JS
JSONObject tm = null;
try{
tm = openmrsUserService.getTeamMember(u.getAttribute("_PERSON_UUID").toString());
JSONArray locs = tm.getJSONArray("location");
JSONArray locs = tm.getJSONArray(OpenMRSCrossVariables.LOCATIONS_JSON_KEY.makeVariable(OPENMRS_VERSION));
for (int i = 0; i < locs.length(); i++) {
lid += locs.getJSONObject(i).getString("uuid")+";;";
}
Expand Down