/
SyncPushServiceImpl.java
208 lines (175 loc) · 8.98 KB
/
SyncPushServiceImpl.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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.PushFilterService;
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.OpenMRSSyncInstance;
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.SyncPushService;
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.LocalFeedReader;
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;
@Component(value = SyncConstants.SYNC_PUSH_SERVICE_BEAN)
public class SyncPushServiceImpl extends AbstractSynchronizationService implements SyncPushService {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncPushService.class);
@Autowired
private PushFilterService pushFilterService;
@Autowired
private SyncAuditService syncAuditService;
@Autowired
private ParentObjectHashcodeService parentObjectHashcodeService;
@Autowired
private UnifyService unifyService;
private static final String FAILED_SYNC_MESSAGE = "Problem with pushing to parent";
@Override
public AuditMessage readAndPushObjectToParent(SyncCategory category, Map<String, String> resourceLinks,
String action, String clientName, String uuid) {
AuditMessage auditMessage = initSynchronization(category, resourceLinks, action, clientName);
boolean shouldSynchronize = true;
try {
String parentPush = getBaseResourceUrl(resourceLinks, clientName);
String localPull = getPullUrl(resourceLinks, clientName, CHILD);
String parentPull = getPullUrl(resourceLinks, clientName, PARENT);
SyncObject localObj = new SyncObject(getLocalObject(category, action, clientName, uuid, localPull));
localObj.setSimpleObject(unifyService.unifyObject(localObj.getBaseObject(), category, clientName));
SyncObject parentObj = new SyncObject(syncClient.pullData(category, clientName, parentPull, PARENT));
parentObj.setSimpleObject(isDeleteAction(action) ? null :
unifyService.unifyObject(parentObj.getBaseObject(), category, clientName));
shouldSynchronize = pushFilterService.shouldBeSynced(category, localObj.getBaseObject(), action)
&& localObj.getBaseObject() != null
&& shouldSynchronize(localObj.getSimpleObject(), parentObj.getSimpleObject());
if (shouldSynchronize) {
String hashCode = null;
if (!isDeleteAction(action)) {
localObj.setBaseObject(detectAndResolveConflict(
localObj, parentObj, auditMessage).getBaseObject());
hashCode = SyncHashcodeUtils.getHashcode(
unifyService.unifyObject(localObj.getBaseObject(), category, clientName));
}
syncClient.pushData(category, localObj.getBaseObject(), clientName, parentPush, action, PARENT);
parentObjectHashcodeService.save(uuid, hashCode);
}
auditMessage = successfulMessage(auditMessage);
} catch (Error | Exception e) {
auditMessage = failedMessage(auditMessage, e);
} finally {
if (shouldSynchronize) {
auditMessage = syncAuditService.saveAuditMessageDuringSync(auditMessage);
}
}
return auditMessage;
}
@Override
public AuditMessage mergeForcePush(Object merged, SyncCategory category, Map<String, String> resourceLinks,
String action, String uuid) {
AuditMessage auditMessage = initSynchronization(category, resourceLinks, action, SyncConstants.REST_CLIENT);
AuditMessage parentAudit = forcePush(merged, category, resourceLinks, action, SyncConstants.REST_CLIENT, PARENT);
AuditMessage childAudit = forcePush(merged, category, resourceLinks, action, SyncConstants.REST_CLIENT, CHILD);
if (parentAudit.getSuccess()) {
try {
String hashCode = SyncHashcodeUtils.getHashcode(unifyService.unifyObject(merged, category,
SyncConstants.REST_CLIENT));
parentObjectHashcodeService.save(uuid, hashCode);
} catch (Error | Exception e) {
return syncAuditService.saveAuditMessage(failedMessage(auditMessage, e));
}
}
return syncAuditService.saveAuditMessage(combineForceAuditMessages(auditMessage, parentAudit, childAudit));
}
@Override
public List<AuditMessage> readAndPushObjectToParent(SyncCategory category, String uuid) {
return synchronizeObject(category, uuid);
}
@Override
public void readAndPushObjectsToParent(SyncCategory category) throws SyncException {
LocalFeedReader localFeedReader = ContextUtils.getLocalFeedReader();
localFeedReader.readAndPushAllFeeds(category);
}
@Override
protected List<String> determineActionsBasingOnSyncType(Object localObj, Object parentObj) {
return determineActions(parentObj, localObj);
}
@Override
protected AuditMessage synchronizeObject(SyncCategory category, Map<String, String> resourceLinks, String action,
String clientName, String uuid) {
return readAndPushObjectToParent(category, resourceLinks, action, clientName, uuid);
}
@Override
protected SyncOperation getOperation() {
return SyncOperation.PUSH;
}
@Override
protected String getBaseResourceUrl(Map<String, String> resourceLinks, String clientName) {
return getPushUrl(resourceLinks, clientName, PARENT);
}
@Override
protected Logger getLogger() {
return LOGGER;
}
@Override
protected String getFailedSynchronizationMessage() {
return FAILED_SYNC_MESSAGE;
}
@Override
public AuditMessage readAndPushObjectToParent(SyncCategory category, Map<String, String> resourceLinks,
String action) {
String clientName = SyncUtils.selectAppropriateClientName(resourceLinks, category.getCategory(), getOperation());
String uuid = extractUUIDFromResourceLinks(resourceLinks);
return readAndPushObjectToParent(category, resourceLinks, action, clientName, uuid);
}
private Object getLocalObject(SyncCategory category, String action, String clientName, String uuid, String localPull) {
return pullData(category, action, clientName, uuid, localPull, CHILD);
}
private AuditMessage forcePush(Object merged, SyncCategory category, Map<String, String> resourceLinks,
String action, String clientName, OpenMRSSyncInstance instance) {
AuditMessage message = initSynchronization(category, resourceLinks, action, SyncConstants.REST_CLIENT);
try {
syncClient.pushData(
category,
merged,
clientName,
getPushUrl(resourceLinks, clientName, instance),
action,
instance);
message = successfulMessage(message);
} catch (Error | Exception e) {
message = failedMessage(message, e);
}
return syncAuditService.saveAuditMessageDuringSync(message);
}
private AuditMessage combineForceAuditMessages(AuditMessage combined, AuditMessage msg, AuditMessage msg2) {
if (msg.getSuccess()) {
if (msg2.getSuccess()) {
combined = successfulMessage(combined);
} else {
combined.setDetails(msg2.getDetails());
combined.setSuccess(false);
}
} else {
StringBuilder sb = new StringBuilder(msg.getDetails());
if (!msg2.getSuccess()) {
sb.append(msg2.getDetails());
}
combined.setDetails(sb.toString());
combined.setSuccess(false);
}
return combined;
}
}