Skip to content

Commit

Permalink
SYNCT-71: Added saving additional audit information
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Serkowski committed Dec 15, 2017
1 parent fd1f1c2 commit 1ffeace
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 12 deletions.
@@ -1,11 +1,13 @@
package org.openmrs.module.sync2.api.impl;

import org.openmrs.module.sync2.api.SyncAuditService;
import org.openmrs.module.sync2.api.SyncConfigurationService;
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;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +24,9 @@
public class SyncPullServiceImpl implements SyncPullService {

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

@Autowired
private SyncConfigurationService configurationService;

@Autowired
private SyncAuditService auditService;
Expand All @@ -35,8 +40,10 @@ public void pullDataFromParentAndSave(String category, Map<String, String> resou
AuditMessage auditMessage = prepareBaseAuditMessage();
auditMessage.setResourceName(category);
auditMessage.setUsedResourceUrl(getPreferredUrl(resourceLinks));
// TODO: set action & operation
// TODO: set links
auditMessage.setAvailableResourceUrls(SyncUtils.serializeMap(resourceLinks));
auditMessage.setParentUrl(getParentUri());
auditMessage.setLocalUrl(getLocalUri());
auditMessage.setAction(action);

try {
Object pulledObject = syncClient.pullDataFromParent(category, resourceLinks, address);
Expand All @@ -53,10 +60,18 @@ public void pullDataFromParentAndSave(String category, Map<String, String> resou
}
}

private String getParentUri() {
return configurationService.getSyncConfiguration().getGeneral().getParentFeedLocation();
}

private String getLocalUri() {
return configurationService.getSyncConfiguration().getGeneral().getLocalFeedLocation();
}

private AuditMessage prepareBaseAuditMessage() {
AuditMessage auditMessage = new AuditMessage();
auditMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
auditMessage.setAction(PULL_OPERATION); // TODO: rename to PULL_OPERATION
auditMessage.setOperation(PULL_OPERATION);
return auditMessage;
}
}
Expand Up @@ -39,18 +39,18 @@ public class SyncPushServiceImpl implements SyncPushService {
@Override
public void readDataAndPushToParent(String category, Map<String, String> resourceLinks, String address, String action) {
LOGGER.info(String.format("SyncPushService category: %s, address: %s, action: %s", category, address, action));

String preferredClient = Context.getAdministrationService().getGlobalProperty(RESOURCE_PREFERRED_CLIENT);

AuditMessage auditMessage = prepareBaseAuditMessage();
auditMessage.setResourceName(category);
auditMessage.setUsedResourceUrl(getPreferredUrl(resourceLinks));
// TODO: set action & operation
// TODO: set links
auditMessage.setAvailableResourceUrls(SyncUtils.serializeMap(resourceLinks));
auditMessage.setParentUrl(getParentUri());
auditMessage.setLocalUrl(getLocalUri());
auditMessage.setAction(action);

try {
String uuid = SyncUtils.extractUUIDFromResourceLinks(resourceLinks);
Object data = syncPersistence.retrieveData(preferredClient, category, uuid);
Object data = syncPersistence.retrieveData(getPreferredClient(), category, uuid);
syncClient.pushDataToParent(data, resourceLinks, getParentUri());

auditMessage.setSuccess(true);
Expand All @@ -64,14 +64,22 @@ public void readDataAndPushToParent(String category, Map<String, String> resourc
}
}

private String getPreferredClient() {
return Context.getAdministrationService().getGlobalProperty(RESOURCE_PREFERRED_CLIENT);
}

private String getParentUri() {
return configurationService.getSyncConfiguration().getGeneral().getParentFeedLocation();
}

private String getLocalUri() {
return configurationService.getSyncConfiguration().getGeneral().getLocalFeedLocation();
}

private AuditMessage prepareBaseAuditMessage() {
AuditMessage auditMessage = new AuditMessage();
auditMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
auditMessage.setAction(PUSH_OPERATION); // TODO: rename to PUSH_OPERATION
auditMessage.setOperation(PUSH_OPERATION);
return auditMessage;
}
}
Expand Up @@ -193,4 +193,14 @@ private static String getFhirResourceUrl(String url) {
return url.substring(0, url.lastIndexOf("/"));
// todo throw custom sync2 exception if tokens.length != 5
}

public static <T> String serializeMap(Map<T, T> map) {
try {
return new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
} catch (IOException ex) {
throw new SyncException("Cannot serialize map", ex);
}
}
}
2 changes: 1 addition & 1 deletion api/src/main/resources/messages.properties
Expand Up @@ -32,7 +32,7 @@ sync2.log.header.status=Status
sync2.log.header.type=Type
sync2.log.header.action=Action
sync2.log.header.operation=Operation
sync2.log.header.details.messageNotFound=Messages not found
sync2.log.header.details.messageNotFound=The message not found
sync2.log.header.details.status.failure=Failure
sync2.log.header.details.status.success=Success

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/resources/messages_es.properties
Expand Up @@ -9,7 +9,7 @@ sync2.log.header.status=Status
sync2.log.header.type=Type
sync2.log.header.action=Action
sync2.log.header.operation=Operation
sync2.log.header.details.messageNotFound=Messages not found
sync2.log.header.details.messageNotFound=The message not found
sync2.log.header.details.status.failure=Failure
sync2.log.header.details.status.success=Success

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/resources/messages_fr.properties
Expand Up @@ -10,7 +10,7 @@ sync2.log.header.status=Status
sync2.log.header.type=Type
sync2.log.header.action=Action
sync2.log.header.operation=Operation
sync2.log.header.details.messageNotFound=Messages not found
sync2.log.header.details.messageNotFound=The message not found
sync2.log.header.details.status.failure=Failure
sync2.log.header.details.status.success=Success

Expand Down
Expand Up @@ -10,12 +10,16 @@
import org.openmrs.module.sync2.api.model.configuration.SyncMethodConfiguration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SyncUtilsTest {

private static final String sampleSyncConfigurationPath = "sampleSyncConfiguration.json";
private static final String incorrectSyncConfigurationPath = "incorrectSyncConfiguration.json";
private static final String sampleSerializedMap = "sampleSerializedMap.json";

private static final String notExistingFilePath = "pathToNotExistingFile";
private static final SyncConfiguration expectedConfiguration = new SyncConfiguration();

Expand Down Expand Up @@ -115,4 +119,13 @@ public void resourceFileExists_exist() throws SyncException {
public void resourceFileExists_notExist() throws SyncException {
Assert.assertFalse(SyncUtils.resourceFileExists(notExistingFilePath));
}

@Test
public void serializeMap_shouldSerializeMapWithStrings() throws SyncException {
Map<String, String> map = new HashMap<>();
map.put("k1", "v1");
map.put("k2", "v2");

Assert.assertEquals(SyncUtils.readResourceFile(sampleSerializedMap), SyncUtils.serializeMap(map));
}
}
4 changes: 4 additions & 0 deletions api/src/test/resources/sampleSerializedMap.json
@@ -0,0 +1,4 @@
{
"k1" : "v1",
"k2" : "v2"
}

0 comments on commit 1ffeace

Please sign in to comment.