Skip to content

Commit

Permalink
Changed using SyncPersistence to specific service API.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkornowski committed Jan 9, 2018
1 parent 563cade commit 5687adc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Expand Up @@ -5,7 +5,6 @@
import org.openmrs.module.sync2.api.SyncPullService;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.sync.SyncClient;
import org.openmrs.module.sync2.api.sync.SyncPersistence;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.openmrs.module.sync2.api.utils.SyncUtils;
import org.slf4j.Logger;
Expand All @@ -21,6 +20,8 @@
import static org.openmrs.module.sync2.api.utils.SyncUtils.getFullUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getParentBaseUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.serializeMapToPrettyJson;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getLocalBaseUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPushPath;

@Component("sync2.syncPullService")
public class SyncPullServiceImpl implements SyncPullService {
Expand All @@ -34,26 +35,26 @@ public class SyncPullServiceImpl implements SyncPullService {
private SyncConfigurationService configurationService;

private SyncClient syncClient = new SyncClient();
private SyncPersistence syncPersistence = new SyncPersistence();


@Override
public AuditMessage pullDataFromParentAndSave(String category, Map<String, String> resourceLinks,
String action, String clientName) {
String resourceURL = getFullUrl(getParentBaseUrl(configurationService), resourceLinks.get(clientName));
String parentResourceURL = getFullUrl(getParentBaseUrl(configurationService), resourceLinks.get(clientName));
String localResourceURL = getFullUrl(getLocalBaseUrl(configurationService), getPushPath(resourceLinks.get(clientName)));

LOGGER.info(String.format("Pull category: %s, address: %s, action: %s", category, resourceURL, action));
LOGGER.info(String.format("Pull category: %s, address: %s, action: %s", category, parentResourceURL, action));

AuditMessage auditMessage = prepareBaseAuditMessage(PULL_OPERATION, configurationService);
auditMessage.setResourceName(category);
auditMessage.setUsedResourceUrl(resourceURL);
auditMessage.setUsedResourceUrl(parentResourceURL);
auditMessage.setLinkType(clientName);
auditMessage.setAvailableResourceUrls(serializeMapToPrettyJson(resourceLinks));
auditMessage.setAction(action);

try {
Object pulledObject = syncClient.pullDataFromParent(category, clientName, resourceURL);
syncPersistence.persistRetrievedData(pulledObject, action);
Object pulledObject = syncClient.pullData(category, clientName, parentResourceURL);
syncClient.pushData(pulledObject, clientName, localResourceURL, action);

auditMessage.setSuccess(true);
auditMessage.setDetails(PULL_SUCCESS_MESSAGE);
} catch (Exception e) {
Expand Down
Expand Up @@ -6,7 +6,6 @@
import org.openmrs.module.sync2.api.SyncPushService;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.sync.SyncClient;
import org.openmrs.module.sync2.api.sync.SyncPersistence;
import org.openmrs.module.sync2.api.utils.SyncUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,12 +18,12 @@
import static org.openmrs.module.sync2.SyncConstants.PUSH_OPERATION;
import static org.openmrs.module.sync2.SyncConstants.PUSH_SUCCESS_MESSAGE;
import static org.openmrs.module.sync2.api.utils.SyncAuditUtils.prepareBaseAuditMessage;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getLocalBaseUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getFullUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getParentBaseUrl;
import static org.openmrs.module.sync2.api.utils.SyncUtils.getPushPath;
import static org.openmrs.module.sync2.api.utils.SyncUtils.extractUUIDFromResourceLinks;


@Component("sync2.syncPushService")
public class SyncPushServiceImpl implements SyncPushService {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncPushService.class);
Expand All @@ -36,27 +35,27 @@ public class SyncPushServiceImpl implements SyncPushService {
private SyncConfigurationService configurationService;

private SyncClient syncClient = new SyncClient();
private SyncPersistence syncPersistence = new SyncPersistence();

@Override
public AuditMessage readDataAndPushToParent(String category, Map<String, String> resourceLinks,
String action, String clientName) {
String resourceURL = getFullUrl(getParentBaseUrl(configurationService), getPushPath(resourceLinks.get(clientName)));
String parentResourceURL = getFullUrl(getParentBaseUrl(configurationService), getPushPath(resourceLinks.get(clientName)));
String localResourceURL = getFullUrl(getLocalBaseUrl(configurationService), resourceLinks.get(clientName));

LOGGER.info(String.format("SyncPushService category: %s, address: %s, action: %s", category, resourceURL, action));
LOGGER.info(String.format("SyncPushService category: %s, address: %s, action: %s", category, parentResourceURL, action));

String uuid = extractUUIDFromResourceLinks(resourceLinks);

AuditMessage auditMessage = prepareBaseAuditMessage(PUSH_OPERATION, configurationService);
auditMessage.setResourceName(category);
auditMessage.setUsedResourceUrl(resourceURL);
auditMessage.setUsedResourceUrl(parentResourceURL);
auditMessage.setLinkType(clientName);
auditMessage.setAvailableResourceUrls(SyncUtils.serializeMapToPrettyJson(resourceLinks));
auditMessage.setAction(action);
try {
Object data = action.equals(ACTION_VOIDED) ? uuid : syncPersistence.retrieveData(clientName, category, uuid);
syncClient.pushDataToParent(data, clientName, resourceURL, action);
Object data = action.equals(ACTION_VOIDED) ? uuid : syncClient.pullData(category, clientName, localResourceURL);
syncClient.pushData(data, clientName, parentResourceURL, action);

auditMessage.setSuccess(true);
auditMessage.setDetails(PUSH_SUCCESS_MESSAGE);
} catch (Exception e) {
Expand Down
Expand Up @@ -20,7 +20,7 @@ public class SyncClient {

private static final Logger LOGGER = LoggerFactory.getLogger(SyncClient.class);

public Object pullDataFromParent(String category, String clientName, String resourceUrl) {
public Object pullData(String category, String clientName, String resourceUrl) {
String username = Context.getAdministrationService().getGlobalProperty(PARENT_USERNAME_PROPERTY);
String password = Context.getAdministrationService().getGlobalProperty(PARENT_PASSWORD_PROPERTY);

Expand All @@ -30,7 +30,7 @@ public Object pullDataFromParent(String category, String clientName, String reso
return client.retrieveObject(category, resourceUrl, username, password);
}

public ResponseEntity<String> pushDataToParent(Object object, String clientName, String resourceUrl, String action) {
public ResponseEntity<String> pushData(Object object, String clientName, String resourceUrl, String action) {
String username = Context.getAdministrationService().getGlobalProperty(PARENT_USERNAME_PROPERTY);
String password = Context.getAdministrationService().getGlobalProperty(PARENT_PASSWORD_PROPERTY);
Client client = new ClientFactory().createClient(clientName);
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void setUp() throws Exception {
public void pullDataFromParent_shouldCallRestClient() {
SyncClient resourceManager = new SyncClient();

Object pulledObject = resourceManager.pullDataFromParent(PATIENT_CATEGORY, REST_CLIENT_KEY,
Object pulledObject = resourceManager.pullData(PATIENT_CATEGORY, REST_CLIENT_KEY,
REST_FULL_RESOURCE_URL);

assertThat(pulledObject, is(expectedPatient));
Expand All @@ -100,7 +100,7 @@ public void pullDataFromParent_shouldCallRestClient() {
public void pullDataFromParent_shouldCallFHIRClient() {
SyncClient resourceManager = new SyncClient();

Object pulledObject = resourceManager.pullDataFromParent(PATIENT_CATEGORY, FHIR_CLIENT_KEY,
Object pulledObject = resourceManager.pullData(PATIENT_CATEGORY, FHIR_CLIENT_KEY,
FHIR_FULL_RESOURCE_URL);

assertThat(pulledObject, is(expectedPatient));
Expand Down

0 comments on commit 5687adc

Please sign in to comment.