/
SyncPullServiceImpl.java
153 lines (127 loc) · 6.9 KB
/
SyncPullServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.openmrs.module.sync2.api.service.impl;
import org.openmrs.module.sync2.SyncConstants;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.filter.impl.PullFilterService;
import org.openmrs.module.sync2.api.model.SyncCategory;
import org.openmrs.module.sync2.api.model.SyncObject;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.model.enums.SyncOperation;
import org.openmrs.module.sync2.api.service.ParentObjectHashcodeService;
import org.openmrs.module.sync2.api.service.SyncAuditService;
import org.openmrs.module.sync2.api.service.SyncPullService;
import org.openmrs.module.sync2.api.service.UnifyService;
import org.openmrs.module.sync2.api.utils.ContextUtils;
import org.openmrs.module.sync2.api.utils.SyncHashcodeUtils;
import org.openmrs.module.sync2.api.utils.SyncUtils;
import org.openmrs.module.sync2.client.reader.ParentFeedReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
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.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.SimpleObjectSerializationUtils.serialize;
@Component(value = SyncConstants.SYNC_PULL_SERVICE_BEAN)
public class SyncPullServiceImpl extends AbstractSynchronizationService implements SyncPullService {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncPullServiceImpl.class);
@Autowired
private PullFilterService pullFilterService;
@Autowired
private SyncAuditService syncAuditService;
@Autowired
private ParentObjectHashcodeService parentObjectHashcodeService;
@Autowired
private UnifyService unifyService;
private static final String FAILED_SYNC_MESSAGE = "Problem with pulling from parent";
@Override
public AuditMessage pullAndSaveObjectFromParent(SyncCategory category, Map<String, String> resourceLinks,
String action, String clientName, String uuid) {
AuditMessage auditMessage = initSynchronization(category, resourceLinks, action, clientName);
boolean shouldSynchronize = true;
try {
String parentPull = getBaseResourceUrl(resourceLinks, clientName);
String localPull = getPullUrl(resourceLinks, clientName, CHILD);
String localPush = getPushUrl(resourceLinks, clientName, CHILD);
SyncObject pulledObject = new SyncObject(getPulledObject(category, action, clientName, uuid, parentPull));
pulledObject.setSimpleObject(SyncUtils.isDeleteAction(action) ? null :
unifyService.unifyObject(pulledObject.getBaseObject(), category, clientName));
SyncObject localPulledObject = new SyncObject(syncClient.pullData(category, clientName, localPull, CHILD));
localPulledObject.setSimpleObject(unifyService.unifyObject(localPulledObject.getBaseObject(), category, clientName));
shouldSynchronize = pullFilterService.shouldBeSynced(category, pulledObject.getBaseObject(), action)
&& pulledObject.getBaseObject() != null
&& shouldSynchronize(pulledObject.getSimpleObject(), localPulledObject.getSimpleObject(), action);
if (shouldSynchronize) {
String hashCode = null;
if (!SyncUtils.isDeleteAction(action)) {
pulledObject.setBaseObject(detectAndResolveConflict(
pulledObject, localPulledObject, auditMessage).getBaseObject());
hashCode = SyncHashcodeUtils.getHashcode(
unifyService.unifyObject(pulledObject.getBaseObject(), category, clientName));
}
syncClient.pushData(category, pulledObject.getBaseObject(), clientName, localPush, action, CHILD);
parentObjectHashcodeService.save(uuid, hashCode);
}
auditMessage = successfulMessage(auditMessage, serialize(pulledObject.getSimpleObject()));
} catch (Error | Exception e) {
if (SyncUtils.isAuditMessageCategory(category) && SyncUtils.isUnauthorizedException(e)) {
shouldSynchronize = false;
} else {
auditMessage = failedMessage(auditMessage, e);
}
} finally {
if (shouldSynchronize) {
auditMessage = syncAuditService.saveAuditMessageDuringSync(auditMessage);
}
}
return auditMessage;
}
@Override
public List<AuditMessage> pullAndSaveObjectFromParent(SyncCategory category, String uuid) {
return synchronizeObject(category, uuid);
}
@Override
protected List<String> determineActionsBasingOnSyncType(Object localObj, Object parentObj) {
return determineActions(localObj, parentObj);
}
@Override
protected AuditMessage synchronizeObject(SyncCategory category, Map<String, String> resourceLinks, String action,
String clientName, String uuid) {
return pullAndSaveObjectFromParent(category, resourceLinks, action, clientName, uuid);
}
@Override
protected SyncOperation getOperation() {
return SyncOperation.PULL;
}
@Override
protected String getBaseResourceUrl(Map<String, String> resourceLinks, String clientName) {
return getPullUrl(resourceLinks, clientName, PARENT);
}
@Override
protected Logger getLogger() {
return LOGGER;
}
@Override
protected String getFailedSynchronizationMessage() {
return FAILED_SYNC_MESSAGE;
}
@Override
public void pullAndSaveObjectsFromParent(SyncCategory category) throws SyncException {
ParentFeedReader parentFeedReader = ContextUtils.getParentFeedReader();
parentFeedReader.pullAndProcessFeeds(category);
}
@Override
public AuditMessage pullAndSaveObjectFromParent(SyncCategory category, Map<String, String> resourceLinks,
String action) {
String clientName = SyncUtils.selectAppropriateClientName(resourceLinks, category.getCategory(), getOperation());
String uuid = extractUUIDFromResourceLinks(resourceLinks, category.getCategory(), clientName);
return pullAndSaveObjectFromParent(category, resourceLinks, action, clientName, uuid);
}
private Object getPulledObject(SyncCategory category, String action, String clientName, String uuid, String parentPull) {
return pullData(category, action, clientName, uuid, parentPull, PARENT);
}
}