Skip to content

Commit

Permalink
SYNCT-71: Extended database
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Serkowski committed Dec 15, 2017
1 parent 8b809a5 commit ab99036
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 60 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/org/openmrs/module/sync2/SyncConstants.java
Expand Up @@ -19,8 +19,8 @@ public class SyncConstants {

public static final String STATUS_COLUMN_NAME = "success";

public static final String PULL_ACTION = "PULL";
public static final String PULL_OPERATION = "PULL";

public static final String PUSH_ACTION = "PUSH";
public static final String PUSH_OPERATION = "PUSH";

}
Expand Up @@ -51,31 +51,31 @@ public String getPaginatedMessages(Integer page, Integer pageSize, Boolean succe
}

@Override
public AuditMessage saveSuccessfulAudit(String resourceName, String resourceUrl, String action, String error) throws APIException {
public AuditMessage saveSuccessfulAudit(String resourceName, String resourceUrl, String action, String details) throws APIException {
if (configuration.getSyncConfiguration().getGeneral().isPersistSuccessAudit()) {
AuditMessage newItem = new AuditMessage();
newItem.setSuccess(true);
newItem.setTimestamp(new Timestamp(System.currentTimeMillis()));
newItem.setResourceName(resourceName);
newItem.setResourceUrl(resourceUrl);
newItem.setUsedResourceUrl(resourceUrl);
newItem.setAction(action);
newItem.setError(error);
newItem.setDetails(details);

return dao.saveItem(newItem);
}
return null;
}

@Override
public AuditMessage saveFailedAudit(String resourceName, String resourceUrl, String action, String error) throws APIException {
public AuditMessage saveFailedAudit(String resourceName, String resourceUrl, String action, String details) throws APIException {
if (configuration.getSyncConfiguration().getGeneral().isPersistFailureAudit()) {
AuditMessage newItem = new AuditMessage();
newItem.setSuccess(false);
newItem.setTimestamp(new Timestamp(System.currentTimeMillis()));
newItem.setResourceName(resourceName);
newItem.setResourceUrl(resourceUrl);
newItem.setUsedResourceUrl(resourceUrl);
newItem.setAction(action);
newItem.setError(error);
newItem.setDetails(details);

return dao.saveItem(newItem);
}
Expand All @@ -89,6 +89,9 @@ public AuditMessage saveAuditMessage(AuditMessage auditMessage) {
boolean persistFailureAudit = !auditMessage.getSuccess()
&& configuration.getSyncConfiguration().getGeneral().isPersistFailureAudit();
if (persistFailureAudit || persistSuccessAudit) {
if (auditMessage.getTimestamp() == null) {
auditMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
}
return dao.saveItem(auditMessage);
}
return null;
Expand All @@ -99,7 +102,6 @@ private String serializeResults(AuditMessageList results) {
gsonBuilder.registerTypeAdapter(AuditMessage.class, new AuditMessage.AuditMessageSerializer());
Gson gson = gsonBuilder.create();

gson.getAdapter(AuditMessage.AuditMessageSerializer.class);
return gson.toJson(results);
}
}
Expand Up @@ -14,7 +14,7 @@
import java.sql.Timestamp;
import java.util.Map;

import static org.openmrs.module.sync2.SyncConstants.PULL_ACTION;
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.api.utils.SyncUtils.getPreferredUrl;

Expand All @@ -34,7 +34,7 @@ public void pullDataFromParentAndSave(String category, Map<String, String> resou

AuditMessage auditMessage = prepareBaseAuditMessage();
auditMessage.setResourceName(category);
auditMessage.setResourceUrl(getPreferredUrl(resourceLinks));
auditMessage.setUsedResourceUrl(getPreferredUrl(resourceLinks));
// TODO: set action & operation
// TODO: set links

Expand All @@ -43,11 +43,11 @@ public void pullDataFromParentAndSave(String category, Map<String, String> resou
syncPersistence.persistRetrievedData(pulledObject, action);

auditMessage.setSuccess(true);
auditMessage.setError(PULL_SUCCESS_MESSAGE);
auditMessage.setDetails(PULL_SUCCESS_MESSAGE);
} catch (Exception e) {
LOGGER.error("Problem with pulling from parent", e);
auditMessage.setSuccess(false);
auditMessage.setError(ExceptionUtils.getFullStackTrace(e));
auditMessage.setDetails(ExceptionUtils.getFullStackTrace(e));
} finally {
auditService.saveAuditMessage(auditMessage);
}
Expand All @@ -56,7 +56,7 @@ public void pullDataFromParentAndSave(String category, Map<String, String> resou
private AuditMessage prepareBaseAuditMessage() {
AuditMessage auditMessage = new AuditMessage();
auditMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
auditMessage.setAction(PULL_ACTION); // TODO: rename to PULL_OPERATION
auditMessage.setAction(PULL_OPERATION); // TODO: rename to PULL_OPERATION
return auditMessage;
}
}
Expand Up @@ -17,7 +17,7 @@
import java.sql.Timestamp;
import java.util.Map;

import static org.openmrs.module.sync2.SyncConstants.PUSH_ACTION;
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.SyncUtils.getPreferredUrl;

Expand All @@ -44,7 +44,7 @@ public void readDataAndPushToParent(String category, Map<String, String> resourc

AuditMessage auditMessage = prepareBaseAuditMessage();
auditMessage.setResourceName(category);
auditMessage.setResourceUrl(getPreferredUrl(resourceLinks));
auditMessage.setUsedResourceUrl(getPreferredUrl(resourceLinks));
// TODO: set action & operation
// TODO: set links

Expand All @@ -54,11 +54,11 @@ public void readDataAndPushToParent(String category, Map<String, String> resourc
syncClient.pushDataToParent(data, resourceLinks, getParentUri());

auditMessage.setSuccess(true);
auditMessage.setError(PUSH_SUCCESS_MESSAGE);
auditMessage.setDetails(PUSH_SUCCESS_MESSAGE);
} catch (Exception e) {
LOGGER.error("Problem with pushing to parent", e);
auditMessage.setSuccess(false);
auditMessage.setError(ExceptionUtils.getFullStackTrace(e));
auditMessage.setDetails(ExceptionUtils.getFullStackTrace(e));
} finally {
auditService.saveAuditMessage(auditMessage);
}
Expand All @@ -71,7 +71,7 @@ private String getParentUri() {
private AuditMessage prepareBaseAuditMessage() {
AuditMessage auditMessage = new AuditMessage();
auditMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
auditMessage.setAction(PUSH_ACTION); // TODO: rename to PUSH_OPERATION
auditMessage.setAction(PUSH_OPERATION); // TODO: rename to PUSH_OPERATION
return auditMessage;
}
}
Expand Up @@ -48,27 +48,43 @@ public class AuditMessage extends BaseOpenmrsData {
private String resourceName;

@Basic
@Column(name = "resource_url")
private String resourceUrl;

@Column(name = "used_resource_url")
private String usedResourceUrl;

@Basic
@Column(name = "available_resource_urls")
private String availableResourceUrls;

@Basic
@Column(name = "parent_url")
private String parentUrl;

@Basic
@Column(name = "local_url")
private String localUrl;

@Basic
@Column(name = "action")
private String action;


@Basic
@Column(name = "operation")
private String operation;

@Basic
@Column(name = "error")
private String error;
@Column(name = "details")
private String details;


public AuditMessage() {
}

public AuditMessage(Integer id, Boolean success, Date timestamp, String resourceName, String resourceUrl) {
public AuditMessage(Integer id, Boolean success, Date timestamp, String resourceName, String usedResourceUrl) {
this.id = id;
this.success = success;
this.timestamp = new Date(timestamp.getTime());
this.resourceName = resourceName;
this.resourceUrl = resourceUrl;
this.usedResourceUrl = usedResourceUrl;
}

@Override
Expand Down Expand Up @@ -115,20 +131,20 @@ public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}

public String getResourceUrl() {
return resourceUrl;
public String getUsedResourceUrl() {
return usedResourceUrl;
}

public void setResourceUrl(String resourceUrl) {
this.resourceUrl = resourceUrl;
public void setUsedResourceUrl(String usedResourceUrl) {
this.usedResourceUrl = usedResourceUrl;
}

public String getError() {
return error;
public String getDetails() {
return details;
}

public void setError(String error) {
this.error = error;
public void setDetails(String details) {
this.details = details;
}

public String getAction() {
Expand All @@ -138,6 +154,38 @@ public String getAction() {
public void setAction(String action) {
this.action = action;
}

public String getAvailableResourceUrls() {
return availableResourceUrls;
}

public void setAvailableResourceUrls(String availableResourceUrls) {
this.availableResourceUrls = availableResourceUrls;
}

public String getParentUrl() {
return parentUrl;
}

public void setParentUrl(String parentUrl) {
this.parentUrl = parentUrl;
}

public String getLocalUrl() {
return localUrl;
}

public void setLocalUrl(String localUrl) {
this.localUrl = localUrl;
}

public String getOperation() {
return operation;
}

public void setOperation(String operation) {
this.operation = operation;
}

@Override
public boolean equals(Object o) {
Expand All @@ -149,19 +197,25 @@ public boolean equals(Object o) {
}

AuditMessage auditMessage = (AuditMessage) o;
return Objects.equals(this.success, auditMessage.success)
return Objects.equals(this.getUuid(), auditMessage.getUuid())
&& Objects.equals(this.success, auditMessage.success)
&& Objects.equals(this.timestamp, auditMessage.timestamp)
&& Objects.equals(this.resourceName, auditMessage.resourceName)
&& Objects.equals(this.resourceUrl, auditMessage.resourceUrl)
&& Objects.equals(this.error, auditMessage.error)
&& Objects.equals(this.action, auditMessage.action);
&& Objects.equals(this.usedResourceUrl, auditMessage.usedResourceUrl)
&& Objects.equals(this.availableResourceUrls, auditMessage.availableResourceUrls)
&& Objects.equals(this.parentUrl, auditMessage.parentUrl)
&& Objects.equals(this.localUrl, auditMessage.localUrl)
&& Objects.equals(this.action, auditMessage.action)
&& Objects.equals(this.operation, auditMessage.operation)
&& Objects.equals(this.details, auditMessage.details);
}

@Override
public int hashCode() {
return Objects.hash(success, timestamp, resourceName, resourceUrl, error, action);
return Objects.hash(success, timestamp, resourceName, usedResourceUrl, availableResourceUrls, parentUrl,
localUrl, action, details, action);
}

public static class AuditMessageSerializer implements JsonSerializer<AuditMessage> {

@Override
Expand All @@ -175,9 +229,13 @@ public JsonElement serialize(AuditMessage src, Type typeOfSrc, JsonSerialization
object.addProperty("success", src.success);
object.addProperty("timestamp", formatter.format(src.timestamp));
object.addProperty("resourceName", src.resourceName);
object.addProperty("resourceUrl", src.resourceUrl);
object.addProperty("error", src.error);
object.addProperty("usedResourceUrl", src.usedResourceUrl);
object.addProperty("availableResourceUrls", src.availableResourceUrls);
object.addProperty("parentUrl", src.parentUrl);
object.addProperty("localUrl", src.localUrl);
object.addProperty("action", src.action);
object.addProperty("operation", src.operation);
object.addProperty("details", src.details);

return object;
}
Expand Down
10 changes: 8 additions & 2 deletions api/src/main/resources/SyncAuditMessage.hbm.xml
Expand Up @@ -11,10 +11,16 @@
<property column="uuid" name="uuid"/>
<property column="success" name="success" type="boolean" />
<property column="timestamp" name="timestamp" type="java.util.Date"/>

<property column="resource_name" name="resourceName" type="java.lang.String" />
<property column="resource_url" name="resourceUrl" type="java.lang.String" />
<property column="used_resource_url" name="usedResourceUrl" type="java.lang.String" />
<property column="available_resource_urls" name="availableResourceUrls" type="java.lang.String" />
<property column="parent_url" name="parentUrl" type="java.lang.String" />
<property column="local_url" name="localUrl" type="java.lang.String" />

<property column="action" name="action" type="java.lang.String" />
<property column="error" name="error" type="java.lang.String" />
<property column="operation" name="operation" type="java.lang.String" />
<property column="details" name="details" type="java.lang.String" />

<many-to-one name="changedBy" class="org.openmrs.User" column="changed_by" />

Expand Down
10 changes: 8 additions & 2 deletions api/src/main/resources/liquibase.xml
Expand Up @@ -23,10 +23,16 @@
<column name="timestamp" type="datetime" >
<constraints nullable="false"/>
</column>

<column name="resource_name" type="varchar(255)" />
<column name="resource_url" type="varchar(255)" />
<column name="used_resource_url" type="varchar(255)" />
<column name="available_resource_urls" type="LONGTEXT" />
<column name="parent_url" type="varchar(255)" />
<column name="local_url" type="varchar(255)" />

<column name="action" type="varchar(255)" />
<column name="error" type="LONGTEXT"/>
<column name="operation" type="varchar(255)" />
<column name="details" type="LONGTEXT"/>

<column name="changed_by" type="varchar(255)" />
<column name="creator" type="int"/>
Expand Down

0 comments on commit ab99036

Please sign in to comment.