/
SyncUtils.java
221 lines (185 loc) · 7.65 KB
/
SyncUtils.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
209
210
211
212
213
214
215
216
217
218
219
220
221
package org.openmrs.module.sync2.api.utils;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.type.TypeReference;
import org.codehaus.jackson.util.DefaultPrettyPrinter;
import org.openmrs.api.context.Context;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.configuration.SyncConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import static org.openmrs.module.sync2.SyncConstants.RESOURCE_PREFERRED_CLIENT;
public class SyncUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncUtils.class);
private static final String FHIR = "fhir";
private static final String REST = "rest";
public static String readResourceFile(String file) throws SyncException {
try (InputStream in = SyncUtils.class.getClassLoader().getResourceAsStream(file)) {
if (in == null) {
throw new SyncException("Resource '" + file + "' doesn't exist");
}
return IOUtils.toString(in);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static SyncConfiguration parseJsonFileToSyncConfiguration(String resourcePath) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(readResourceFile(resourcePath), SyncConfiguration.class);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static SyncConfiguration parseJsonStringToSyncConfiguration(String value) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(value, SyncConfiguration.class);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static boolean isValidateJson(String json) throws SyncException {
try {
final JsonParser parser = new ObjectMapper().getJsonFactory().createJsonParser(json);
while (parser.nextToken() != null) {}
} catch (JsonParseException jpe) {
return false;
} catch (IOException e) {
throw new SyncException(e);
}
return true;
}
public static String writeSyncConfigurationToJsonString(SyncConfiguration syncConfiguration) throws SyncException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
return writer.writeValueAsString(syncConfiguration);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static void writeSyncConfigurationToJsonFile(SyncConfiguration syncConfigurations, String file)
throws SyncException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
File resultFile = new File(SyncUtils.class.getClassLoader().getResource("").getPath() + file);
writer.writeValue(resultFile, syncConfigurations);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static boolean resourceFileExists(String path) {
InputStream in = SyncUtils.class.getClassLoader().getResourceAsStream(path);
return in != null;
}
public static Map<String, String> getLinks(String json) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {};
try {
HashMap<String, String> result = mapper.readValue(json, typeRef);
return result;
} catch (IOException e) {
throw new SyncException(String.format("Invalid resource links JSON Object: %s ", json), e);
}
}
public static String getBaseUrl(String stringUri) {
try {
URI uri = new URI(stringUri);
return uri.getScheme() + "://" + uri.getAuthority() + "/";
} catch(URISyntaxException e) {
throw new SyncException(String.format("Bad Atomfeed URI: %s. ", stringUri), e);
}
}
public static String getCategoryAndActionByTag(String tag) {
Map<String, String> map = parseTag(tag);
return map.get("Category.term");
}
private static HashMap<String, String> parseTag(String tag) {
final HashMap<String, String> map = new HashMap<>();
for (String pair : tag.split("\n")) {
String[] keyValue = pair.split("=");
map.put(keyValue[0], keyValue[1]);
}
return map;
}
public static String getPreferredUrl(Map<String, String> resourceLinks) {
String preferredClient = getPreferredClient();
String result = resourceLinks.get(preferredClient);
if (result == null && resourceLinks.size() > 0) {
result = getFirstResourceLink(resourceLinks);
}
return result;
}
private static String getFirstResourceLink(Map<String, String> resourceLinks) {
if (resourceLinks.size() > 0) {
return resourceLinks.values().iterator().next();
}
return "";
}
public static String getPreferredClient() {
return Context.getAdministrationService().getGlobalProperty(RESOURCE_PREFERRED_CLIENT);
}
public static String extractUUIDFromResourceLinks(Map<String, String> resourceLinks) {
for (String client : resourceLinks.keySet()) {
switch (client) {
case REST:
return extractUUIDFromRestResource(resourceLinks.get(client));
case FHIR:
return extractUUIDFromFHIRResource(resourceLinks.get(client));
default:
}
}
LOGGER.error("Couldn't find any supported client to extract uuid from.");
return null;
}
private static String extractUUIDFromRestResource(String link) {
String[] tokens = link.split("/");
// todo throw custom sync2 exception if tokens.length != 6
return tokens[5].split("\\?")[0];
}
private static String extractUUIDFromFHIRResource(String link) {
String[] tokens = link.split("/");
// todo throw custom sync2 exception if tokens.length != 5
return tokens[4];
}
public static String getResourceUrl(String client, String url) {
switch (client) {
case FHIR:
return getFhirResourceUrl(url);
case REST:
return getRestResourceUrl(url);
default:
LOGGER.error("Couldn't find any supported client to extract resource url from.");
return null;
}
}
private static String getRestResourceUrl(String url) {
return url.substring(0, url.lastIndexOf("/"));
// todo throw custom sync2 exception if tokens.length != 6
}
private static String getFhirResourceUrl(String url) {
return url.substring(0, url.lastIndexOf("/"));
// todo throw custom sync2 exception if tokens.length != 5
}
public static <T> String serializeMap(Map<T, T> map) {
try {
return new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
} catch (IOException ex) {
throw new SyncException("Cannot serialize map", ex);
}
}
}