Skip to content

Commit

Permalink
SYNCT-98: Improving delete operation on the Patient Object.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkornowski committed Jan 24, 2018
1 parent 4c4cc0e commit 5ca507a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
@@ -1,36 +1,29 @@
package org.openmrs.module.sync2.api.impl;

import org.openmrs.OpenmrsObject;
import org.openmrs.module.fhir.api.util.FHIRPatientUtil;
import org.openmrs.module.sync2.api.SyncAuditService;
import org.openmrs.module.sync2.api.SyncPullService;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.sync.SyncClient;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.openmrs.module.sync2.api.utils.SyncUtils;
import org.openmrs.module.sync2.client.RestResourceCreationUtil;
import org.openmrs.module.sync2.client.rest.resource.RestResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;

import java.util.Map;

import static org.openmrs.module.sync2.SyncConstants.ACTION_VOIDED;
import static org.openmrs.module.sync2.SyncConstants.PULL_OPERATION;
import static org.openmrs.module.sync2.SyncConstants.PULL_SUCCESS_MESSAGE;
import static org.openmrs.module.sync2.SyncConstants.REST_CLIENT_KEY;
import static org.openmrs.module.sync2.SyncConstants.FHIR_CLIENT_KEY;
import static org.openmrs.module.sync2.SyncConstants.CATEGORY_PATIENT;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.CHILD;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.PARENT;
import static org.openmrs.module.sync2.api.utils.SyncAuditUtils.prepareBaseAuditMessage;
import static org.openmrs.module.sync2.api.utils.SyncUtils.extractUUIDFromResourceLinks;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPullUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPushUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.serializeMapToPrettyJson;
import static org.openmrs.module.sync2.api.utils.SyncUtils.compareLocalAndPulled;

@Component("sync2.syncPullService")
public class SyncPullServiceImpl implements SyncPullService {
Expand All @@ -51,6 +44,8 @@ public AuditMessage pullDataFromParentAndSave(String category, Map<String, Strin

boolean pulledObjectExist = false;
LOGGER.info(String.format("Pull category: %s, address: %s, action: %s", category, parentPull, action));
String uuid = extractUUIDFromResourceLinks(resourceLinks);


AuditMessage auditMessage = prepareBaseAuditMessage(PULL_OPERATION);
auditMessage.setResourceName(category);
Expand All @@ -60,10 +55,11 @@ public AuditMessage pullDataFromParentAndSave(String category, Map<String, Strin
auditMessage.setAction(action);

try {
Object pulledObject = syncClient.pullData(category, clientName, parentPull, PARENT);
Object localPulledObject = getLocalPulledObject(category, clientName, localPull);
Object pulledObject = action.equals(ACTION_VOIDED) ? uuid : syncClient.pullData(category, clientName, parentPull, PARENT);
Object localPulledObject = syncClient.pullData(category, clientName, localPull, CHILD);
pulledObjectExist = pulledObject != null ?
compareLocalAndPulled(clientName, category, pulledObject, localPulledObject) : true;

pulledObjectExist = compareLocalAndPulled(clientName, category, pulledObject, localPulledObject);
if (!pulledObjectExist) {
syncClient.pushData(pulledObject, clientName, localPush, action, CHILD);
}
Expand All @@ -76,8 +72,9 @@ public AuditMessage pullDataFromParentAndSave(String category, Map<String, Strin
auditMessage.setSuccess(false);
auditMessage.setDetails(ExceptionUtils.getFullStackTrace(e));
} finally {
if (!pulledObjectExist)
if (!pulledObjectExist) {
auditMessage = syncAuditService.saveAuditMessage(auditMessage);
}
}
return auditMessage;
}
Expand All @@ -89,43 +86,4 @@ public AuditMessage pullDataFromParentAndSave(String category, Map<String, Strin
return pullDataFromParentAndSave(category, resourceLinks, action, clientName);
}

private boolean compareLocalAndPulled(String clientName, String category, Object pulled, Object local) {
boolean result = false;

if (null != local) {
switch (clientName) {
case REST_CLIENT_KEY:
RestResource restLocal = RestResourceCreationUtil.createRestResourceFromOpenMRSData((OpenmrsObject) local);
RestResource restPulled = RestResourceCreationUtil.createRestResourceFromOpenMRSData((OpenmrsObject) pulled);

result = restLocal.equals(restPulled);
break;
case FHIR_CLIENT_KEY:
result = category.equals(CATEGORY_PATIENT) ?
FHIRPatientUtil.compareCurrentPatients(local, pulled) : local.equals(pulled);
break;
default:
result = local.equals(pulled);
}
}

return result;
}

private Object getLocalPulledObject(String category, String clientName, String localResourceURL) {
Object result;

try {
result = syncClient.pullData(category, clientName, localResourceURL, CHILD);
} catch(HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
result = null;
} else {
throw new SyncException("Error during reading local object: ", e);
}
}

return result;
}

}
Expand Up @@ -19,6 +19,7 @@
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.CHILD;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.PARENT;
import static org.openmrs.module.sync2.api.utils.SyncAuditUtils.prepareBaseAuditMessage;
import static org.openmrs.module.sync2.api.utils.SyncUtils.compareLocalAndPulled;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPullUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.extractUUIDFromResourceLinks;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPushUrl;
Expand All @@ -37,7 +38,9 @@ public AuditMessage readDataAndPushToParent(String category, Map<String, String>
String action, String clientName) {
String parentPush = getPushUrl(resourceLinks, clientName, PARENT);
String localPull = getPullUrl(resourceLinks, clientName, CHILD);
String parentPull = getPullUrl(resourceLinks, clientName, PARENT);

boolean pulledObjectExist = false;
LOGGER.info(String.format("SyncPushService category: %s, address: %s, action: %s", category, parentPush, action));
String uuid = extractUUIDFromResourceLinks(resourceLinks);

Expand All @@ -49,9 +52,14 @@ public AuditMessage readDataAndPushToParent(String category, Map<String, String>
auditMessage.setAction(action);

try {
Object data = action.equals(ACTION_VOIDED) ? uuid :
syncClient.pullData(category, clientName, localPull, CHILD);
syncClient.pushData(data, clientName, parentPush, action, PARENT);
Object localObj = action.equals(ACTION_VOIDED) ? uuid : syncClient.pullData(category, clientName, localPull, CHILD);
Object parentObj = syncClient.pullData(category, clientName, parentPull, PARENT);
pulledObjectExist = localObj != null ?
compareLocalAndPulled(clientName, category, localObj, parentObj) : true;

if (!pulledObjectExist) {
syncClient.pushData(localObj, clientName, parentPush, action, PARENT);
}

auditMessage.setSuccess(true);
auditMessage.setDetails(PUSH_SUCCESS_MESSAGE);
Expand All @@ -60,7 +68,9 @@ public AuditMessage readDataAndPushToParent(String category, Map<String, String>
auditMessage.setSuccess(false);
auditMessage.setDetails(ExceptionUtils.getFullStackTrace(e));
} finally {
auditMessage = syncAuditService.saveAuditMessage(auditMessage);
if (!pulledObjectExist) {
auditMessage = syncAuditService.saveAuditMessage(auditMessage);
}
}
return auditMessage;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.openmrs.module.sync2.client.ClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
Expand All @@ -19,6 +20,7 @@
import static org.openmrs.module.sync2.SyncConstants.LOCAL_USERNAME_PROPERTY;
import static org.openmrs.module.sync2.SyncConstants.PARENT_PASSWORD_PROPERTY;
import static org.openmrs.module.sync2.SyncConstants.PARENT_USERNAME_PROPERTY;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.CHILD;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.PARENT;

public class SyncClient {
Expand All @@ -29,12 +31,23 @@ public class SyncClient {
private String password;

public Object pullData(String category, String clientName, String resourceUrl, OpenMRSSyncInstance instance) {
Object result;
setUpCredentials(instance);

ClientFactory clientFactory = new ClientFactory();
Client client = clientFactory.createClient(clientName);

return client.retrieveObject(category, resourceUrl, username, password);
try {
result = client.retrieveObject(category, resourceUrl, username, password);
} catch(HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
result = null;
} else {
throw new SyncException("Error during reading local object: ", e);
}
}

return result;
}

public ResponseEntity<String> pushData(Object object, String clientName,
Expand Down
Expand Up @@ -2,19 +2,26 @@

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.openmrs.OpenmrsObject;
import org.openmrs.api.context.Context;
import org.openmrs.module.atomfeed.api.db.EventAction;
import org.openmrs.module.atomfeed.client.AtomFeedClient;
import org.openmrs.module.fhir.api.util.FHIRPatientUtil;
import org.openmrs.module.sync2.SyncConstants;
import org.openmrs.module.sync2.api.SyncConfigurationService;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.enums.AtomfeedTagContent;
import org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance;
import org.openmrs.module.sync2.api.sync.SyncClient;
import org.openmrs.module.sync2.client.RestResourceCreationUtil;
import org.openmrs.module.sync2.client.rest.resource.Location;
import org.openmrs.module.sync2.client.rest.resource.Patient;
import org.openmrs.module.sync2.client.rest.resource.Privilege;
import org.openmrs.module.sync2.client.rest.resource.RestResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -165,7 +172,28 @@ public static Class getClass(String client, String category) {
}
}

public static boolean compareLocalAndPulled(String clientName, String category, Object from, Object dest) {
boolean result = false;

if (null != dest && !(from instanceof String)) {
switch (clientName) {
case REST_CLIENT_KEY:
RestResource restLocal = RestResourceCreationUtil.createRestResourceFromOpenMRSData((OpenmrsObject) dest);
RestResource restPulled = RestResourceCreationUtil.createRestResourceFromOpenMRSData((OpenmrsObject) from);

result = restLocal.equals(restPulled);
break;
case FHIR_CLIENT_KEY:
result = category.equals(CATEGORY_PATIENT) ?
FHIRPatientUtil.compareCurrentPatients(dest, from) : dest.equals(from);
break;
default:
result = dest.equals(from);
}
}

return result;
}

private static String extractUUIDFromRestResource(String link) {
String[] tokens = link.split("/");
Expand Down

0 comments on commit 5ca507a

Please sign in to comment.