Skip to content

Commit

Permalink
SYNCT-30: Added dependence between audit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
areklalo committed Dec 18, 2017
1 parent 09b6244 commit 33671fe
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
Expand Up @@ -7,6 +7,7 @@
import org.openmrs.module.sync2.SyncModuleConfig;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public interface SyncAuditService extends OpenmrsService {
Expand All @@ -26,4 +27,8 @@ public interface SyncAuditService extends OpenmrsService {
@Authorized(SyncModuleConfig.SYNC_AUDIT_PRIVILEGE)
@Transactional
AuditMessage saveAuditMessage(AuditMessage auditMessage) throws APIException;

@Authorized(SyncModuleConfig.SYNC_AUDIT_PRIVILEGE)
@Transactional
AuditMessage setNextAudit(AuditMessage current, AuditMessage next) throws APIException;
}
Expand Up @@ -4,7 +4,6 @@
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.openmrs.api.db.hibernate.DbSession;
Expand All @@ -15,7 +14,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;


Expand Down
Expand Up @@ -64,6 +64,15 @@ public AuditMessage saveAuditMessage(AuditMessage auditMessage) {
return null;
}

@Override
public AuditMessage setNextAudit(AuditMessage current, AuditMessage next) throws APIException {
if (current == null || next == null) {
return null;
}
current.setNextMessage(next.getId());
return dao.saveItem(current);
}

private String serializeResults(AuditMessageList results) {
GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls();
gsonBuilder.registerTypeAdapter(AuditMessage.class, new AuditMessage.AuditMessageSerializer());
Expand Down
@@ -1,11 +1,13 @@
package org.openmrs.module.sync2.api.impl;

import org.openmrs.api.APIException;
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.SyncPushService;
import org.openmrs.module.sync2.api.SyncRetryService;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.utils.SyncUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -25,6 +27,9 @@ public class SyncRetryServiceImpl implements SyncRetryService {
@Autowired
private SyncPushService syncPushService;

@Autowired
private SyncAuditService syncAuditService;

@Autowired
private SyncConfigurationService configuration;

Expand All @@ -49,9 +54,13 @@ private AuditMessage retryPush(AuditMessage message) {

private AuditMessage retryPull(AuditMessage message) {
String parentAddress = configuration.getSyncConfiguration().getGeneral().getParentFeedLocation();
parentAddress = SyncUtils.getBaseUrl(parentAddress);

Map<String, String> map = new HashMap<>();
map.put(message.getLinkType(), message.getUsedResourceUrl());
message = syncPullService.pullDataFromParentAndSave(message.getResourceName(), map, parentAddress, message.getAction());
return message;

AuditMessage newMesssage = syncPullService.pullDataFromParentAndSave(message.getResourceName(), map, parentAddress, message.getAction());
syncAuditService.setNextAudit(message, newMesssage);
return newMesssage;
}
}
Expand Up @@ -44,6 +44,8 @@ public class AuditMessage extends BaseOpenmrsData {

private String linkType;

private Integer nextMessage;

public AuditMessage() {
}

Expand Down Expand Up @@ -155,6 +157,14 @@ public void setLinkType(String linkType) {
this.linkType = linkType;
}

public Integer getNextMessage() {
return nextMessage;
}

public void setNextMessage(Integer nextMessage) {
this.nextMessage = nextMessage;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -178,15 +188,17 @@ public boolean equals(Object o) {
&& Objects.equals(this.details, auditMessage.details)
&& Objects.equals(this.action, auditMessage.action)
&& Objects.equals(this.operation, auditMessage.operation)
&& Objects.equals(this.linkType, auditMessage.linkType);
&& Objects.equals(this.linkType, auditMessage.linkType)
&& Objects.equals(this.nextMessage, auditMessage.nextMessage);
}



@Override
public int hashCode() {

return Objects.hash(success, timestamp, resourceName, usedResourceUrl, availableResourceUrls, parentUrl,
localUrl, action, details, action, linkType);
localUrl, action, details, action, linkType, nextMessage);
}

public static class AuditMessageSerializer implements JsonSerializer<AuditMessage> {
Expand All @@ -210,6 +222,7 @@ public JsonElement serialize(AuditMessage src, Type typeOfSrc, JsonSerialization
object.addProperty("operation", src.operation);
object.addProperty("details", src.details);
object.addProperty("linkType", src.linkType);
object.addProperty("nextMessage", src.nextMessage);

return object;
}
Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/SyncAuditMessage.hbm.xml
Expand Up @@ -22,6 +22,7 @@
<property column="details" name="details" type="java.lang.String" />
<property column="operation" name="operation" type="java.lang.String" />
<property column="link_type" name="linkType" type="java.lang.String" />
<property column="next_message" name="nextMessage" type="int" />

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

Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/liquibase.xml
Expand Up @@ -33,6 +33,7 @@
<column name="details" type="LONGTEXT"/>
<column name="operation" type="varchar(255)"/>
<column name="link_type" type="varchar(255)"/>
<column name="next_message" type="int" />

<column name="changed_by" type="varchar(255)" />
<column name="creator" type="int"/>
Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/messages.properties
Expand Up @@ -43,6 +43,7 @@ sync2.log.header.details.status.failure=Failure
sync2.log.header.details.status.success=Success

sync2.log.header.retry=Retry
sync2.log.header.nextMessage=Next message

sync2.log.status.all=All
sync2.log.status.success=Success
Expand Down
17 changes: 13 additions & 4 deletions omod/src/main/webapp/fragments/auditDetails.gsp
@@ -1,5 +1,6 @@
<%
def messagesPrefix = "sync2.log.header"
def detailViewProvider = "sync2"
ui.includeJavascript("sync2", "sync2.audit.retry.js")
%>

Expand Down Expand Up @@ -72,8 +73,16 @@

<% if (auditLog != null && !auditLog.success) { %>
<br />
<a class="button confirm right" onClick="retry();">
<i class="icon-retweet"></i>
${ ui.message(artifactId + '.retry') }
</a>
<% if (auditLog.nextMessage != null) { %>
<a class="button right" href="${ ui.pageLink(detailViewProvider, "details",
[messageId: auditLog.nextMessage, backPage: param.backPage[0], backPageIndex: param.backPageIndex]) }">
<i class="icon-chevron-right"></i>
${ ui.message(artifactId + '.nextMessage') }
</a>
<% } else { %>
<a class="button confirm right" onClick="retry();">
<i class="icon-retweet"></i>
${ ui.message(artifactId + '.retry') }
</a>
<% } %>
<% } %>

0 comments on commit 33671fe

Please sign in to comment.