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

Commit

Permalink
code syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
manutarus committed Sep 20, 2017
1 parent ec5a551 commit ae8382a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public JSONObject getEncounterByUuid(String uuid, boolean noRepresentationTag) t
}

public JSONObject getObsByEncounterUuid(String encounterUuid) throws JSONException {
// The data format returned contains the obs uuid and concept uuids
return new JSONObject(HttpUtil.get(getURL() + "/" + ENCOUNTER_URL + "/" + encounterUuid,
"v=custom:(uuid,obs:(uuid,concept:(uuid)))", OPENMRS_USER, OPENMRS_PWD).body());
}
Expand All @@ -92,7 +91,6 @@ public JSONObject getEncounterType(String encounterType) throws JSONException {
// its hard to find whether it was network error or object not found or server error
JSONObject resEncounterType = new JSONObject(
HttpUtil.get(getURL() + "/" + ENCOUNTER__TYPE_URL, "v=full", OPENMRS_USER, OPENMRS_PWD).body());

if (resEncounterType.has("results") && resEncounterType.get("results") instanceof JSONArray) {
JSONArray res = resEncounterType.getJSONArray("results");
for (int i = 0; i < res.length(); i++) {
Expand Down Expand Up @@ -139,9 +137,8 @@ public JSONObject createEncounter(Event e) throws JSONException {
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
.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
Expand All @@ -150,7 +147,7 @@ public JSONObject createEncounter(Event e) throws JSONException {
}
if (StringUtils.isEmptyOrWhitespaceOnly(obs.getParentCode())) {
p.put(obs.getFieldCode(), convertObsToJson(obs));
}else {
} else {
//find parent obs if not found search and fill or create one
JSONArray parentObs = p.get(obs.getParentCode());
if (parentObs == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.opensrp.connector.openmrs.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.mysql.jdbc.StringUtils;
import org.apache.commons.lang.ArrayUtils;
import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -15,50 +12,52 @@
import org.opensrp.common.util.HttpUtil;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class OpenmrsLocationService extends OpenmrsService {

private static final String LOCATION_URL = "ws/rest/v1/location";

public OpenmrsLocationService() {
}

public OpenmrsLocationService(String openmrsUrl, String user, String password) {
super(openmrsUrl, user, password);
}

public Location getLocation(String locationIdOrName) throws JSONException {
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/"
+ (locationIdOrName.replaceAll(" ", "%20")), "v=full", OPENMRS_USER, OPENMRS_PWD);

HttpResponse op = HttpUtil
.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/" + (locationIdOrName
.replaceAll(" ", "%20")), "v=full", OPENMRS_USER, OPENMRS_PWD);
if (!StringUtils.isEmptyOrWhitespaceOnly(op.body())) {
return makeLocation(op.body());
}
return null;
}

public Location getParent(JSONObject locobj) throws JSONException {
JSONObject parentL = (locobj.has("parentLocation") && !locobj.isNull("parentLocation")) ? locobj
.getJSONObject("parentLocation") : null;

JSONObject parentL = (locobj.has("parentLocation") && !locobj.isNull("parentLocation")) ?
locobj.getJSONObject("parentLocation") :
null;
if (parentL != null) {
return new Location(parentL.getString("uuid"), parentL.getString("display"), null, getParent(parentL));
}
return null;
}

private Location makeLocation(String locationJson) throws JSONException {
JSONObject obj = new JSONObject(locationJson);
Location p = getParent(obj);
Location l = new Location(obj.getString("uuid"), obj.getString("name"), null, null, p, null, null);
JSONArray t = obj.getJSONArray("tags");

for (int i = 0; i < t.length(); i++) {
l.addTag(t.getJSONObject(i).getString("display"));
}

JSONArray a = obj.getJSONArray("attributes");

for (int i = 0; i < a.length(); i++) {
Expand All @@ -68,95 +67,92 @@ private Location makeLocation(String locationJson) throws JSONException {

return l;
}

private Location makeLocation(JSONObject location) throws JSONException {
return makeLocation(location.toString());
}

public LocationTree getLocationTree() throws JSONException {
LocationTree ltr = new LocationTree();
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL, "v=full",
OPENMRS_USER, OPENMRS_PWD);

HttpResponse op = HttpUtil
.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL, "v=full", OPENMRS_USER, OPENMRS_PWD);
JSONArray res = new JSONObject(op.body()).getJSONArray("results");
if (res.length() == 0) {
return ltr;
}

for (int i = 0; i < res.length(); i++) {
ltr.addLocation(makeLocation(res.getJSONObject(i)));
}
return ltr;
}

public LocationTree getLocationTreeOf(String locationIdOrName) throws JSONException {
LocationTree ltr = new LocationTree();

fillTreeWithHierarchy(ltr, locationIdOrName);
fillTreeWithUpperHierarchy(ltr, locationIdOrName);

return ltr;
}

public LocationTree getLocationTreeWithUpperHierachyOf(String locationIdOrName) throws JSONException {
LocationTree ltr = new LocationTree();
fillTreeWithUpperHierarchy(ltr, locationIdOrName);
return ltr;
}

public LocationTree getLocationTreeOf(String[] locationIdsOrNames) throws JSONException {
LocationTree ltr = new LocationTree();

for (String loc : locationIdsOrNames) {
String locTreeId = fillTreeWithHierarchy(ltr, loc);
Location lp = ltr.findLocation(locTreeId).getParentLocation();
if (lp != null) {
fillTreeWithUpperHierarchy(ltr, lp.getLocationId());
}
}

return ltr;
}

private String fillTreeWithHierarchy(LocationTree ltr, String locationIdOrName) throws JSONException {
HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/"
+ (locationIdOrName.replaceAll(" ", "%20")), "v=full", OPENMRS_USER, OPENMRS_PWD);

HttpResponse op = HttpUtil
.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/" + (locationIdOrName
.replaceAll(" ", "%20")), "v=full", OPENMRS_USER, OPENMRS_PWD);

JSONObject lo = new JSONObject(op.body());
Location l = makeLocation(op.body());
ltr.addLocation(l);

if (lo.has("childLocations")) {
JSONArray lch = lo.getJSONArray("childLocations");

for (int i = 0; i < lch.length(); i++) {

JSONObject cj = lch.getJSONObject(i);
fillTreeWithHierarchy(ltr, cj.getString("uuid"));
}
}
return l.getLocationId();
}

private void fillTreeWithUpperHierarchy(LocationTree ltr, String locationId) throws JSONException {
HttpResponse op = HttpUtil.get(
HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/" + (locationId.replaceAll(" ", "%20")),
"v=full", OPENMRS_USER, OPENMRS_PWD);

HttpResponse op = HttpUtil.get(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/" + (locationId
.replaceAll(" ", "%20")), "v=full", OPENMRS_USER, OPENMRS_PWD);
Location l = makeLocation(op.body());
ltr.addLocation(l);

if (l.getParentLocation() != null) {
fillTreeWithUpperHierarchy(ltr, l.getParentLocation().getLocationId());
}
}

public Map<String, String> getLocationsHierarchy(LocationTree locationTree) throws JSONException {
Map<String, String> map = new HashMap<>();
if (locationTree == null) {
return map;
}

Map<String, TreeNode<String, Location>> locationsHierarchy = locationTree.getLocationsHierarchy();
if (locationsHierarchy != null && !locationsHierarchy.isEmpty()) {
for (TreeNode<String, Location> value : locationsHierarchy.values()) {
Expand All @@ -165,15 +161,15 @@ public Map<String, String> getLocationsHierarchy(LocationTree locationTree) thro
}
return map;
}

private void extractLocations(Map<String, String> map, TreeNode<String, Location> value) throws JSONException {
if (value == null || value.getNode() == null) {
return;
}

final String[] allowedLevels = { AllowedLevels.COUNTRY.toString(), AllowedLevels.PROVINCE.toString(),
AllowedLevels.DISTRICT.toString() };
AllowedLevels.DISTRICT.toString() };

Location node = value.getNode();
String name = node.getName();
Set<String> tags = node.getTags();
Expand All @@ -185,7 +181,7 @@ private void extractLocations(Map<String, String> map, TreeNode<String, Location
}
}
}

}
Map<String, TreeNode<String, Location>> children = value.getChildren();
if (children != null && !children.isEmpty()) {
Expand All @@ -194,20 +190,22 @@ private void extractLocations(Map<String, String> map, TreeNode<String, Location
}
}
}

public enum AllowedLevels {
COUNTRY("Country"), PROVINCE("Province"), DISTRICT("District");

private final String display;

private AllowedLevels(final String display) {
this.display = display;
}

@Override
public String toString() {
return display;
}

};

}

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

import java.text.SimpleDateFormat;
import java.util.Date;

import org.opensrp.common.util.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
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 OpenmrsService() { }


public static final String PROBABLE_CAUSE_PARENT_CONCEPT = "5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

public OpenmrsService() {
}

public OpenmrsService(String openmrsUrl, String user, String password) {
OPENMRS_BASE_URL = openmrsUrl;
OPENMRS_USER = user;
OPENMRS_PWD = password;
OPENMRS_BASE_URL = openmrsUrl;
OPENMRS_USER = user;
OPENMRS_PWD = password;
}

/**
* returns url after trimming ending slash
*
* @return
*/
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()));
public static void main(String[] args) {
System.out.println(OPENMRS_DATE.format(new Date()));
}

}

}

0 comments on commit ae8382a

Please sign in to comment.