-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONSyncrhonizer.java
More file actions
148 lines (133 loc) · 6.2 KB
/
Copy pathJSONSyncrhonizer.java
File metadata and controls
148 lines (133 loc) · 6.2 KB
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
package com.jsonsync;
import java.util.Date;
import com.jsonsync.utils.DateUtils;
import com.jsonsync.utils.json.JSONException;
import com.jsonsync.utils.json.JSONObject;
/**
*
* @author Arnaud CAVAILHEZ
*/
public class JSONSyncrhonizer {
private static JSONSyncPersistenceInterface persistenceInteface;
public static void startPersistence(JSONSyncPersistenceInterface persistenceIntefaceCandidate) {
persistenceInteface = persistenceIntefaceCandidate;
}
public static void startLocalPersistence() {
persistenceInteface = new JSONSyncHashMapPersistence();
}
public static JSONObject update(String syncId, String clientId, JSONObject newClientJSON) {
try {
//sanity null checks
if (syncId == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer update called with null sync Id ");
}
if (clientId == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer update called with null client Id ");
}
if (newClientJSON == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer update called with null client son, please call subscribe instead ");
}
JSONObject syncJSON = getSyncJSON(syncId);
if (syncJSON == null) {
throw new JSONSyncRuntimeException("No sync data found with sync_id:" + syncId + " -- cannot update, please call subscribe first");
}
if (!syncJSON.has(clientId)) {
throw new JSONSyncRuntimeException("No client JSON found with sync_id:" + syncId + " and client_id:" + clientId + " -- cannot update, please call subscribe if it's a new client_id");
}
JSONObject oldClientSyncJSON = syncJSON.getJSONObject(clientId);
String oldClientJSONString = oldClientSyncJSON.getString("data");
JSONObject oldClientJSON = new JSONObject(oldClientJSONString);
//extract master data
String masterJSONString = syncJSON.getString("master_data");
JSONObject masterJSON = new JSONObject(masterJSONString);
//synchronize
JSONSyncUtils.updateMaster(masterJSON, oldClientJSON, newClientJSON);
saveMasterJSON(syncId, clientId, masterJSON, syncJSON);
return masterJSON;
} catch (JSONException ex) {
throw new JSONSyncRuntimeException("Could not sync", ex);
}
}
public static JSONObject subscribe(String syncId, String clientId) {
//sanity null checks
if (syncId == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer subscribe called with null sync Id ");
}
if (clientId == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer subscribe called with null client Id ");
}
//create new sync entry (if first client) or subscribe client id
JSONObject syncJSON = getSyncJSON(syncId);
if (syncJSON == null) {
//new sync id, create master entry
JSONObject masterJSON = new JSONObject();
try {
saveMasterJSON(syncId, clientId, masterJSON, new JSONObject());
} catch (Exception ex) {
throw new JSONSyncRuntimeException("Could not create new sync entry with sync_id:" + syncId, ex);
}
return masterJSON;
} else {
try {
//extract master data
String masterJSONString = syncJSON.getString("master_data");
JSONObject masterJSON = new JSONObject(masterJSONString);
//save update
saveMasterJSON(syncId, clientId, masterJSON, syncJSON);
return masterJSON;
} catch (JSONException ex) {
throw new JSONSyncRuntimeException("Could not subscribe", ex);
}
}
}
public static JSONObject get(String syncId) {
try {
//sanity null checks
if (syncId == null) {
throw new JSONSyncRuntimeException("JSONSyncrhonizer subscribe called with null sync Id ");
}
JSONObject syncJSON = getSyncJSON(syncId);
if (syncJSON == null) {
throw new JSONSyncRuntimeException("No sync data found with sync_id:" + syncId + " -- cannot extract, please call subscribe first");
}
String masterJSONString = syncJSON.getString("master_data");
JSONObject masterJSON = new JSONObject(masterJSONString);
return masterJSON;
} catch (JSONException ex) {
throw new JSONSyncRuntimeException("Could not extract master data for sync_id:" + syncId, ex);
}
}
private static void saveMasterJSON(String syncId, String clientId, JSONObject masterJSON, JSONObject syncJSON) throws JSONException {
//store
String now = DateUtils.encode(new Date());
//store client sync data
JSONObject newClientSyncJSON = new JSONObject();
newClientSyncJSON.put("last_sync", now);
newClientSyncJSON.put("data", masterJSON.toString());
syncJSON.remove(clientId);
syncJSON.put(clientId, newClientSyncJSON);
//store master data
syncJSON.remove("master_data");
syncJSON.put("master_data", masterJSON.toString());
syncJSON.remove("last_sync");
syncJSON.put("last_sync", now);
//store new master JSON to client
try {
persistenceInteface.put(syncId, syncJSON.toString());
} catch (Exception ex) {
throw new JSONSyncRuntimeException("Cannot save sync data with sync_id:" + syncId + " and client_id:" + clientId + " -- cannot update or subscribe, no data lost", ex);
}
}
private static JSONObject getSyncJSON(String syncId) {
String syncData = persistenceInteface.get(syncId);
if (syncData == null) {
return null;
}
try {
JSONObject syncJSON = new JSONObject(syncData);
return syncJSON;
} catch (JSONException ex) {
throw new JSONSyncRuntimeException("Cannot read sync data with sync_id:" + syncId + " -- cannot update or subscribe, serious problem", ex);
}
}
}