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

Commit

Permalink
MK: Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Maimoona Kausar committed Sep 8, 2016
1 parent 139a49c commit 78cc42d
Show file tree
Hide file tree
Showing 41 changed files with 452 additions and 176 deletions.
2 changes: 1 addition & 1 deletion assets/config/opensrp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mcts-report-delay-in-days=10
mcts.poll.time.interval.in.minutes=10

# OpenMRS configuration
openmrs.url=http://localhost:8181/openmrs/
openmrs.url=http://localhost:8383/openmrs/
openmrs.username=admin
openmrs.password=Admin123

Expand Down
10 changes: 5 additions & 5 deletions build/maven.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#database configuration that is not likely to change unless massive refactoring
couchdb.db.opensrp=opensrp
couchdb.db.form=opensrp-form
couchdb.db.opensrp=a-opensrp
couchdb.db.form=a-opensrp-form
couchdb.db.atomfeed=atomfeed
couchdb.db.mcts=opensrp-mcts
couchdb.db.motech-scheduletracking=motech-scheduletracking-api
couchdb.db.error=opensrp-errortrace
couchdb.db.mcts=a-opensrp-mcts
couchdb.db.motech-scheduletracking=a-motech-scheduletracking-api
couchdb.db.error=a-opensrp-errortrace


db.quartz=motechquartz
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.opensrp.connector;
package org.opensrp.common.util;

import java.io.IOException;
import java.net.Socket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

public class DateUtil {
private static DateUtility dateUtility = new RealDate();
static DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public static DateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
public static DateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static DateFormat yyyyMMddTHHmmssSSSZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Expand Down Expand Up @@ -46,16 +45,20 @@ private static DateTime toTime(LocalDate referenceDateForSchedule) {
* @return
* @throws ParseException
*/
public static Date parseDate(String date) throws ParseException{
public static DateTime parseDate(String date) throws ParseException{
try{
return yyyyMMdd.parse(date);
return new DateTime(yyyyMMdd.parse(date).getTime());
}
catch(ParseException e){}
try {
return yyyyMMddHHmmss.parse(date);
return new DateTime(yyyyMMddHHmmss.parse(date).getTime());
} catch (ParseException e) {}

return yyyyMMddTHHmmssSSSZ.parse(date);
try {
return new DateTime(yyyyMMddTHHmmssSSSZ.parse(date).getTime());
} catch (ParseException e) {}

return DateTime.parse(date);
}

public static LocalDate tryParse(String value, LocalDate defaultValue) {
Expand All @@ -71,7 +74,7 @@ public static Date getDateFromString(String dateString)
try {
if(dateString!=null && !dateString.equals("null") && dateString.length()>0)
{
parsed = sdf.parse(dateString.trim());
parsed = yyyyMMddTHHmmssSSSZ.parse(dateString.trim());
}
} catch (ParseException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
public class HttpResponse {
private final boolean isSuccess;
private final String body;
private Integer statusCode;

public HttpResponse(boolean isSuccess, String body) {
public HttpResponse(boolean isSuccess, String body) {
this.isSuccess = isSuccess;
this.body = body;
}

public HttpResponse(boolean isSuccess, int statusCode, String body) {
this.isSuccess = isSuccess;
this.body = body;
this.statusCode = statusCode;
}

public boolean isSuccess() {
return isSuccess;
Expand All @@ -16,4 +23,8 @@ public boolean isSuccess() {
public String body() {
return body;
}

public Integer statusCode() {
return statusCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.opensrp.connector;
package org.opensrp.common.util;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyStore;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
Expand All @@ -20,18 +21,17 @@
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.opensrp.common.util.HttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mysql.jdbc.StringUtils;

@Component
public class HttpUtil {

public enum AuthType{
BASIC, TOKEN, NONE
}

private final static DefaultHttpClient httpClient = init();

@SuppressWarnings("deprecation")
public static DefaultHttpClient init() {
try {
//TODO add option to ignore cetificate validation in opensrp.prop
Expand All @@ -57,42 +57,63 @@ public static DefaultHttpClient init() {
}

public static HttpResponse post(String url, String payload, String data, String username,String password) {
return post(url, payload, data, username, password, "application/json");
return post(url, payload, data, "application/json", AuthType.BASIC, username+":"+password);
}

public static HttpResponse post(String url, String payload, String data) {
return post(url, payload, data, "application/json", AuthType.NONE, "");
}

public static HttpResponse postWithToken(String url, String payload, String data, String token) {
return post(url, payload, data, "application/json", AuthType.TOKEN, token);
}

public static HttpResponse post(String url, String payload, String data, String username,String password, String contentType) {
public static HttpResponse post(String url, String payload, String data, String contentType, AuthType authType, String authString) {
try {
HttpPost request = (HttpPost) makeConnection(url, payload, RequestMethod.POST, true, username, password);
HttpPost request = (HttpPost) makeConnection(url, payload, RequestMethod.POST, authType, authString);
request.setHeader(HTTP.CONTENT_TYPE, contentType);
StringEntity entity = new StringEntity(data);
StringEntity entity = new StringEntity(data==null?"":data);
System.out.println(data);
entity.setContentEncoding(contentType);
request.setEntity(entity);
org.apache.http.HttpResponse response = httpClient.execute(request);
return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK, IOUtils.toString(response.getEntity().getContent()));
return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK, response.getStatusLine()
.getStatusCode(),IOUtils.toString(response.getEntity().getContent()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public static HttpResponse get(String url, String payload, String username, String password) {
return get(url, payload, AuthType.BASIC, username+":"+password);
}

public static HttpResponse get(String url, String payload) {
return get(url, payload, AuthType.NONE, "");
}

public static HttpResponse getWithToken(String url, String payload, String token) {
return get(url, payload, AuthType.BASIC, token);
}

public static HttpResponse get(String url, String payload, AuthType authType, String authString) {
try {
HttpGet request = (HttpGet) makeConnection(url, payload, RequestMethod.GET, true, username, password);
HttpGet request = (HttpGet) makeConnection(url, payload, RequestMethod.GET, authType, authString);
org.apache.http.HttpResponse response = httpClient.execute(request);
return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK, IOUtils.toString(response.getEntity().getContent()));
return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK, response.getStatusLine().getStatusCode(), IOUtils.toString(response.getEntity().getContent()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

static HttpRequestBase makeConnection(String url, String payload, RequestMethod method, boolean useBasicAuth, String username, String password) throws URISyntaxException {
static HttpRequestBase makeConnection(String url, String payload, RequestMethod method, AuthType authType, String authString) throws URISyntaxException {
String charset = "UTF-8";

if(url.endsWith("/")){
url = url.substring(0, url.lastIndexOf("/"));
}
url = (url+(StringUtils.isEmptyOrWhitespaceOnly(payload)?"":("?"+payload))).replaceAll(" ", "%20");
url = (url+(StringUtils.isBlank(payload)?"":("?"+payload))).replaceAll(" ", "%20");
URI urlo = new URI(url);

HttpRequestBase requestBase = null;
Expand All @@ -108,10 +129,14 @@ else if(method.equals(RequestMethod.PUT)){
requestBase.setURI(urlo);
requestBase.addHeader("Accept-Charset", charset);

if(useBasicAuth){
String encoded = new String(Base64.encodeBase64((username+":"+password).getBytes()));
if(authType.name().equalsIgnoreCase("basic")){
String encoded = authString.matches(".+:.+")?new String(Base64.encodeBase64(authString.getBytes())):authString;
requestBase.addHeader("Authorization", "Basic "+encoded);
}
else if(authType.name().equalsIgnoreCase("token")){
requestBase.addHeader("Authorization", "Token "+authString);
}

System.out.println(url);
return requestBase;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.opensrp.common.util;

public enum RequestMethod {
POST , GET, PUT
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.opensrp.connector;
package org.opensrp.common.util;

import java.io.IOException;
import java.net.InetAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ public <T> T executeWithTransaction(AFTransactionWork<T> action) throws RuntimeE
public void process(Event event) {
log.info("Processing item : "+event.getContent());
try {
String uuid = event.getContent().substring(event.getContent().lastIndexOf("/")+1);
JSONObject e = encounterService.getEncounterByUuid(uuid, true);
String content = event.getContent().substring(event.getContent().lastIndexOf("/")+1);
JSONObject e = encounterService.getEncounterByUuid(content, true);
if(e == null){
throw new RuntimeException("Encounter uuid ("+uuid+") specified in atomfeed content did not return any encounter.");
throw new RuntimeException("Encounter uuid specified in atomfeed content ("+content+") did not return any encounter.");
}

log.info(e.toString());

org.opensrp.domain.Event enc = encounterService.convertToEvent(e);
org.opensrp.domain.Event existing = eventService.find(uuid);
org.opensrp.domain.Event existing = eventService.find(e.getString("uuid"));
if(existing == null){
log.info("New Event");
eventService.addEvent(enc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class PatientAtomfeed extends OpenmrsService implements EventWorker, Atom

private AtomFeedProperties atomFeedProperties;
private AFTransactionManager transactionManager;
private WebClient webClient;
private AtomFeedClient client;

private PatientService patientService;
Expand All @@ -56,7 +55,7 @@ public <T> T executeWithTransaction(AFTransactionWork<T> action) throws RuntimeE
return action.execute();
}
};
this.webClient = new WebClient();
WebClient webClient = new WebClient();

URI uri = new URI(OPENMRS_BASE_URL+OpenmrsConstants.ATOMFEED_URL+CATEGORY_URL);
this.client = new AtomFeedClient(new AllFeeds(webClient), allMarkers, allFailedEvents, atomFeedProperties, transactionManager, uri, this);
Expand All @@ -69,10 +68,10 @@ public <T> T executeWithTransaction(AFTransactionWork<T> action) throws RuntimeE
public void process(Event event) {
log.info("Processing item : "+event.getContent());
try {
String uuid = event.getContent().substring(event.getContent().lastIndexOf("/")+1);
JSONObject p = patientService.getPatientByUuid(uuid, true);
String content = event.getContent().substring(event.getContent().lastIndexOf("/")+1);
JSONObject p = patientService.getPatientByUuid(content, true);
if(p == null){
throw new RuntimeException("Patient uuid ("+uuid+") specified in atomfeed content did not return any patient.");
throw new RuntimeException("Patient uuid specified in atomfeed content ("+content+") did not return any patient.");
}
Client c = patientService.convertToClient(p);
Client existing = clientService.findClient(c);
Expand All @@ -84,12 +83,12 @@ public void process(Event event) {
log.info("New Client -> Posted Thrive ID back to OpenMRS : "+newId);
}
else {
String srpIdInOpenmrs = c.getIdentifierMatchingRegex(PatientService.OPENSRP_IDENTIFIER_TYPE_MATCHER);
c = clientService.mergeClient(c);
String srpIdInOpenmrs = c.getBaseEntityId();
Client cmerged = clientService.mergeClient(c);
//TODO what if in any case thrive id is assigned to some other patient
if(StringUtils.isBlank(srpIdInOpenmrs) || !srpIdInOpenmrs.equalsIgnoreCase(c.getBaseEntityId())){
if(StringUtils.isBlank(srpIdInOpenmrs) || !srpIdInOpenmrs.equalsIgnoreCase(cmerged.getBaseEntityId())){
// if openmrs doesnot have openSRP UID or have a different UID then update
JSONObject newId = patientService.addThriveId(c.getBaseEntityId(), p);
JSONObject newId = patientService.addThriveId(cmerged.getBaseEntityId(), p);
log.info("Existing Client missing Valid SRP UID -> Posted Thrive ID back to OpenMRS : "+newId);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

package org.opensrp.connector.openmrs.constants;

import org.ict4h.atomfeed.client.AtomFeedProperties;

/**
* Mappings in OpenSRP for OpenMRS entities and properties
*/
Expand All @@ -11,16 +9,20 @@ public class OpenmrsConstants {
public static final String SCHEDULER_TRACKER_SYNCER_SUBJECT = "OpenMRS Scheduler Tracker Syncer";
public static final String SCHEDULER_OPENMRS_ATOMFEED_SYNCER_SUBJECT = "OpenMRS Atomfeed Syncer";
public static final String ENROLLMENT_TRACK_UUID = "openmrsTrackUuid";
public static final String SCHEDULER_OPENMRS_DATA_PUSH_SUBJECT = "OpenMRS Data Pusher";

public static final String ATOMFEED_URL = "ws/atomfeed";
public static final String ATOMFEED_DATABASE_CONNECTOR = "atomfeedDatabaseConnector";


public enum ScheduleTrackerConfig {
openmrs_syncer_sync_by_last_update_enrollment,
public enum SchedulerConfig {
openmrs_syncer_sync_schedule_tracker_by_last_update_enrollment,

openmrs_syncer_sync_client_by_date_updated,
openmrs_syncer_sync_client_by_date_voided,
openmrs_syncer_sync_event_by_date_updated,
openmrs_syncer_sync_event_by_date_voided,
openmrs_syncer_sync_status,
openmrs_syncer_sync_timestamp
}

public static AtomFeedProperties DEFUALT_ATOM_FEED_PROPERTIES = new AtomFeedProperties();
}
Loading

0 comments on commit 78cc42d

Please sign in to comment.