1
|
package eu.dnetlib.enabling.datasources;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.text.ParseException;
|
5
|
import java.text.SimpleDateFormat;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Date;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
|
11
|
import org.antlr.stringtemplate.StringTemplate;
|
12
|
import org.apache.commons.io.IOUtils;
|
13
|
import org.apache.commons.lang.StringEscapeUtils;
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.dom4j.Document;
|
18
|
import org.dom4j.Node;
|
19
|
import org.springframework.core.io.ClassPathResource;
|
20
|
import org.springframework.core.io.Resource;
|
21
|
|
22
|
import com.google.common.base.Splitter;
|
23
|
import com.google.common.collect.Lists;
|
24
|
import com.google.common.collect.Maps;
|
25
|
|
26
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
|
27
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
|
28
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
|
29
|
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
|
30
|
|
31
|
public class DatasourceFunctions {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(DatasourceFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
|
34
|
|
35
|
private static Resource ifaceTemplate = new ClassPathResource("/eu/dnetlib/enabling/datasources/repo_interface.st");
|
36
|
|
37
|
public static String buildInterface(final String infopackage) {
|
38
|
final IfaceDesc iface = generateIfaceDesc(infopackage);
|
39
|
|
40
|
if (iface != null) {
|
41
|
final StringTemplate i = getTemplate(ifaceTemplate);
|
42
|
i.setAttribute("ifc", iface);
|
43
|
|
44
|
return i.toString();
|
45
|
} else {
|
46
|
return "";
|
47
|
}
|
48
|
}
|
49
|
|
50
|
public static IfaceDesc generateIfaceDesc(final String infopackage) {
|
51
|
if (infopackage == null || infopackage.trim().isEmpty()) { return null; }
|
52
|
|
53
|
final IfaceDesc iface = new IfaceDesc();
|
54
|
iface.setId(getValueBetween(infopackage, null, "<==1==>"));
|
55
|
iface.setTypology(getValueBetween(infopackage, "<==1==>", "<==2==>"));
|
56
|
iface.setCompliance(getValueBetween(infopackage, "<==2==>", "<==3==>"));
|
57
|
iface.setContentDescription(getValueBetween(infopackage, "<==3==>", "<==4==>"));
|
58
|
iface.setAccessProtocol(getValueBetween(infopackage, "<==4==>", "<==5==>"));
|
59
|
iface.setActive(Boolean.parseBoolean(getValueBetween(infopackage, "<==5==>", "<==6==>")));
|
60
|
iface.setRemovable(Boolean.parseBoolean(getValueBetween(infopackage, "<==6==>", "<==7==>")));
|
61
|
|
62
|
final Map<String, String> accessParams = Maps.newHashMap();
|
63
|
final Map<String, String> extraParams = Maps.newHashMap();
|
64
|
|
65
|
for (String param : parseParams(getValueBetween(infopackage, "<==7==>", null))) {
|
66
|
|
67
|
final String[] arr = param.split("###");
|
68
|
|
69
|
if (arr.length == 3) {
|
70
|
final boolean accessParam = Boolean.parseBoolean(arr[0].trim());
|
71
|
final String paramName = arr[1].trim();
|
72
|
final String paramValue = arr.length > 2 ? arr[2].trim() : "";
|
73
|
|
74
|
if (accessParam) {
|
75
|
if (paramName.equals(DatasourceParams.baseUrl.toString())) {
|
76
|
iface.setBaseUrl(paramValue);
|
77
|
} else {
|
78
|
accessParams.put(paramName, paramValue);
|
79
|
}
|
80
|
} else {
|
81
|
extraParams.put(paramName, paramValue);
|
82
|
}
|
83
|
} else {
|
84
|
log.debug("Invalid Datasource Parameter");
|
85
|
}
|
86
|
}
|
87
|
|
88
|
iface.setAccessParams(accessParams);
|
89
|
iface.setExtraFields(extraParams);
|
90
|
|
91
|
return iface;
|
92
|
}
|
93
|
|
94
|
private static String getValueBetween(final String s, final String pre, final String post) {
|
95
|
if (pre == null && post == null) {
|
96
|
return StringEscapeUtils.escapeXml(s);
|
97
|
} else if (pre == null) {
|
98
|
return StringEscapeUtils.escapeXml(StringUtils.substringBefore(s, post));
|
99
|
} else if (post == null) {
|
100
|
return StringEscapeUtils.escapeXml(StringUtils.substringAfter(s, pre));
|
101
|
} else {
|
102
|
return StringEscapeUtils.escapeXml(StringUtils.substringBetween(s, pre, post));
|
103
|
}
|
104
|
}
|
105
|
|
106
|
private static Iterable<String> parseParams(final String s) {
|
107
|
return Splitter.on("@@@").omitEmptyStrings().trimResults().split(s);
|
108
|
}
|
109
|
|
110
|
private static StringTemplate getTemplate(final Resource res) {
|
111
|
try {
|
112
|
return new StringTemplate(IOUtils.toString(res.getInputStream()));
|
113
|
} catch (IOException e) {
|
114
|
log.error("unable to get template", e);
|
115
|
throw new RuntimeException(e);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
public static String asSqlValue(final String s) {
|
120
|
return s == null ? "NULL" : "'" + StringEscapeUtils.escapeSql(s) + "'";
|
121
|
}
|
122
|
|
123
|
public static String asSqlValue(final Boolean b) {
|
124
|
return b == null ? "NULL" : b.toString();
|
125
|
}
|
126
|
|
127
|
private static String asSqlValue(final Double d) {
|
128
|
return d == null ? "NULL" : d.toString();
|
129
|
}
|
130
|
|
131
|
private static Object asSqlValue(final Date date) {
|
132
|
if (date == null) { return "NULL"; }
|
133
|
final long millis = date.getTime();
|
134
|
final java.sql.Date sqlDate = new java.sql.Date(millis);
|
135
|
return "'" + sqlDate.toString() + "'";
|
136
|
}
|
137
|
|
138
|
public static boolean verifyCompliance(final Document doc) {
|
139
|
|
140
|
for (Object o : doc.selectNodes("//INTERFACE")) {
|
141
|
final String compliance = ((Node) o).valueOf("./INTERFACE_EXTRA_FIELD[@name='" + DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD + "']");
|
142
|
if (!StringUtils.isEmpty(compliance)) {
|
143
|
if (!"unknown".equalsIgnoreCase(compliance) && !"notCompatible".equalsIgnoreCase(compliance)) { return true; }
|
144
|
} else {
|
145
|
final String compliance2 = ((Node) o).valueOf("@compliance");
|
146
|
if (!"unknown".equalsIgnoreCase(compliance2) && !"notCompatible".equalsIgnoreCase(compliance2)) { return true; }
|
147
|
}
|
148
|
}
|
149
|
|
150
|
return false;
|
151
|
}
|
152
|
|
153
|
@SuppressWarnings("unchecked")
|
154
|
public static DatasourceDesc xmlToDatasourceDesc(final Document doc) throws DatasourceManagerServiceException {
|
155
|
|
156
|
final DatasourceDesc ds = new DatasourceDesc();
|
157
|
|
158
|
ds.setId(findValue(doc, "id", String.class));
|
159
|
ds.setOfficialName(findValue(doc, "officialname", String.class));
|
160
|
ds.setEnglishName(findValue(doc, "englishname", String.class));
|
161
|
ds.setWebsiteUrl(findValue(doc, "websiteurl", String.class));
|
162
|
ds.setLogoUrl(findValue(doc, "logourl", String.class));
|
163
|
ds.setCountryCode(findValue(doc, "countrycode", String.class));
|
164
|
ds.setCountryName(findValue(doc, "countryname", String.class));
|
165
|
ds.setOrganization(findValue(doc, "organization", String.class));
|
166
|
ds.setContactEmail(findValue(doc, "contactemail", String.class));
|
167
|
ds.setLatitude(findValue(doc, "latitude", Double.class));
|
168
|
ds.setLongitude(findValue(doc, "longitude", Double.class));
|
169
|
ds.setTimezone(findValue(doc, "timezone", Double.class));
|
170
|
ds.setNamespacePrefix(findValue(doc, "namespaceprefix", String.class));
|
171
|
ds.setOdNumberOfItems(findValue(doc, "od_numberofitems", String.class));
|
172
|
ds.setOdNumberOfItemsDate(findValue(doc, "od_numberofitemsdate", String.class));
|
173
|
ds.setOdPolicies(findValue(doc, "od_policies", String.class));
|
174
|
ds.setOdLanguages(findValue(doc, "od_languages", String.class));
|
175
|
ds.setOdContentTypes(findValue(doc, "od_contenttypes", String.class));
|
176
|
ds.setCollectedFrom(findValue(doc, "collectedfrom", String.class));
|
177
|
ds.setInferred(findValue(doc, "inferred", Boolean.class));
|
178
|
ds.setDeletedByInference(findValue(doc, "deletedbyinference", Boolean.class));
|
179
|
ds.setTrust(findValue(doc, "trust", Double.class));
|
180
|
ds.setInferenceProvenance(findValue(doc, "inferenceprovenance", String.class));
|
181
|
ds.setDateOfValidation(findValue(doc, "dateofvalidation", Date.class));
|
182
|
ds.setRegisteredBy(findValue(doc, "registeredby", String.class));
|
183
|
ds.setDatasourceClass(findValue(doc, "datasourceclass", String.class));
|
184
|
ds.setProvenanceActionClass(findValue(doc, "provenanceactionclass", String.class));
|
185
|
ds.setDateOfCollection(findValue(doc, "dateofcollection", Date.class));
|
186
|
ds.setTypology(findValue(doc, "typology", String.class));
|
187
|
ds.setActivationId(findValue(doc, "activationid", String.class));
|
188
|
ds.setMergehomonyms(findValue(doc, "mergehomonyms", Boolean.class));
|
189
|
ds.setDescription(findValue(doc, "description", String.class));
|
190
|
ds.setReleaseStartDate(findValue(doc, "releasestartdate", Date.class));
|
191
|
ds.setReleaseEndDate(findValue(doc, "releaseenddate", Date.class));
|
192
|
ds.setMissionStatementUrl(findValue(doc, "missionstatementurl", String.class));
|
193
|
ds.setDataProvider(findValue(doc, "dataprovider", Boolean.class));
|
194
|
ds.setServiceProvider(findValue(doc, "serviceprovider", Boolean.class));
|
195
|
ds.setDatabaseAccessType(findValue(doc, "databaseaccesstype", String.class));
|
196
|
ds.setDataUploadType(findValue(doc, "datauploadtype", String.class));
|
197
|
ds.setDatabaseAccessRestriction(findValue(doc, "databaseaccessrestriction", String.class));
|
198
|
ds.setDataUploadRestriction(findValue(doc, "datauploadrestriction", String.class));
|
199
|
ds.setVersioning(findValue(doc, "versioning", Boolean.class));
|
200
|
ds.setCitationGuidelineUrl(findValue(doc, "citationguidelineurl", String.class));
|
201
|
ds.setQualityManagementKind(findValue(doc, "qualitymanagementkind", String.class));
|
202
|
ds.setPidSystems(findValue(doc, "pidsystems", String.class));
|
203
|
ds.setCertificates(findValue(doc, "certificates", String.class));
|
204
|
ds.setAggregator(findValue(doc, "aggregator", String.class));
|
205
|
ds.setIssn(findValue(doc, "issn", String.class));
|
206
|
ds.setEissn(findValue(doc, "eissn", String.class));
|
207
|
ds.setLissn(findValue(doc, "lissn", String.class));
|
208
|
ds.setInterfaces(findValue(doc, "accessinfopackage", List.class));
|
209
|
|
210
|
return ds;
|
211
|
}
|
212
|
|
213
|
@SuppressWarnings("unchecked")
|
214
|
private static <T> T findValue(final Document doc, final String elem, final Class<T> clazz) throws DatasourceManagerServiceException {
|
215
|
|
216
|
if (clazz == List.class) {
|
217
|
if (elem.equalsIgnoreCase("accessinfopackage")) {
|
218
|
final List<IfaceDesc> ifaces = Lists.newArrayList();
|
219
|
for (Object o : doc.selectNodes("//FIELD[@name='accessinfopackage']/ITEM")) {
|
220
|
final IfaceDesc ifaceDesc = generateIfaceDesc(((Node) o).getText());
|
221
|
if (ifaceDesc != null) {
|
222
|
ifaces.add(ifaceDesc);
|
223
|
}
|
224
|
}
|
225
|
return (T) ifaces;
|
226
|
} else {
|
227
|
throw new DatasourceManagerServiceException("Invalid List element " + elem);
|
228
|
}
|
229
|
} else {
|
230
|
final String val = doc.valueOf("//FIELD[@name='" + elem + "' and not(@isNull='true')]");
|
231
|
|
232
|
if (clazz == String.class) {
|
233
|
return (T) val;
|
234
|
} else if (val == null || val.isEmpty()) {
|
235
|
return null;
|
236
|
} else if (clazz == Boolean.class) {
|
237
|
return (T) new Boolean(val);
|
238
|
} else if (clazz == Double.class) {
|
239
|
return (T) new Double(val);
|
240
|
} else if (clazz == Date.class) {
|
241
|
try {
|
242
|
return (T) new SimpleDateFormat("yyyy-MM-dd").parse(val);
|
243
|
} catch (ParseException e) {
|
244
|
throw new DatasourceManagerServiceException("Invalid date: " + val);
|
245
|
}
|
246
|
} else {
|
247
|
throw new DatasourceManagerServiceException("Invalid type " + clazz + " for element " + elem);
|
248
|
}
|
249
|
}
|
250
|
|
251
|
}
|
252
|
|
253
|
public static Map<String, Object> asMapOfSqlValues(final DatasourceDesc ds) {
|
254
|
final Map<String, Object> map = Maps.newHashMap();
|
255
|
map.put("id", asSqlValue(ds.getId()));
|
256
|
map.put("officialName", asSqlValue(ds.getOfficialName()));
|
257
|
map.put("englishName", asSqlValue(ds.getEnglishName()));
|
258
|
map.put("websiteUrl", asSqlValue(ds.getWebsiteUrl()));
|
259
|
map.put("logoUrl", asSqlValue(ds.getLogoUrl()));
|
260
|
map.put("countryCode", asSqlValue(ds.getCountryCode()));
|
261
|
map.put("countryName", asSqlValue(ds.getCountryName()));
|
262
|
map.put("organization", asSqlValue(ds.getOrganization()));
|
263
|
map.put("contactEmail", asSqlValue(ds.getContactEmail()));
|
264
|
map.put("latitude", asSqlValue(ds.getLatitude()));
|
265
|
map.put("longitude", asSqlValue(ds.getLongitude()));
|
266
|
map.put("timezone", asSqlValue(ds.getTimezone()));
|
267
|
map.put("namespacePrefix", asSqlValue(ds.getNamespacePrefix()));
|
268
|
map.put("odNumberOfItems", asSqlValue(ds.getOdNumberOfItems()));
|
269
|
map.put("odNumberOfItemsDate", asSqlValue(ds.getOdNumberOfItemsDate()));
|
270
|
map.put("odPolicies", asSqlValue(ds.getOdPolicies()));
|
271
|
map.put("odLanguages", asSqlValue(ds.getOdLanguages()));
|
272
|
map.put("odContentTypes", asSqlValue(ds.getOdContentTypes()));
|
273
|
map.put("collectedFrom", asSqlValue(ds.getCollectedFrom()));
|
274
|
map.put("inferred", asSqlValue(ds.isInferred()));
|
275
|
map.put("deletedByInference", asSqlValue(ds.isDeletedByInference()));
|
276
|
map.put("trust", asSqlValue(ds.getTrust()));
|
277
|
map.put("inferenceProvenance", asSqlValue(ds.getInferenceProvenance()));
|
278
|
map.put("dateOfValidation", asSqlValue(ds.getDateOfValidation()));
|
279
|
map.put("registeredBy", asSqlValue(ds.getRegisteredBy()));
|
280
|
map.put("datasourceClass", asSqlValue(ds.getDatasourceClass()));
|
281
|
map.put("provenanceActionClass", asSqlValue(ds.getProvenanceActionClass()));
|
282
|
map.put("dateOfCollection", asSqlValue(ds.getDateOfCollection()));
|
283
|
map.put("typology", asSqlValue(ds.getTypology()));
|
284
|
map.put("activationId", asSqlValue(ds.getActivationId()));
|
285
|
map.put("mergehomonyms", asSqlValue(ds.isMergehomonyms()));
|
286
|
map.put("description", asSqlValue(ds.getDescription()));
|
287
|
map.put("releaseStartDate", asSqlValue(ds.getReleaseStartDate()));
|
288
|
map.put("releaseEndDate", asSqlValue(ds.getReleaseEndDate()));
|
289
|
map.put("missionStatementUrl", asSqlValue(ds.getMissionStatementUrl()));
|
290
|
map.put("dataProvider", asSqlValue(ds.isDataProvider()));
|
291
|
map.put("serviceProvider", asSqlValue(ds.isServiceProvider()));
|
292
|
map.put("databaseAccessType", asSqlValue(ds.getDatabaseAccessType()));
|
293
|
map.put("dataUploadType", asSqlValue(ds.getDataUploadType()));
|
294
|
map.put("databaseAccessRestriction", asSqlValue(ds.getDatabaseAccessRestriction()));
|
295
|
map.put("dataUploadRestriction", asSqlValue(ds.getDataUploadRestriction()));
|
296
|
map.put("versioning", asSqlValue(ds.isVersioning()));
|
297
|
map.put("citationGuidelineUrl", asSqlValue(ds.getCitationGuidelineUrl()));
|
298
|
map.put("qualityManagementKind", asSqlValue(ds.getQualityManagementKind()));
|
299
|
map.put("pidSystems", asSqlValue(ds.getPidSystems()));
|
300
|
map.put("certificates", asSqlValue(ds.getCertificates()));
|
301
|
map.put("aggregator", asSqlValue(ds.getAggregator()));
|
302
|
map.put("issn", asSqlValue(ds.getIssn()));
|
303
|
map.put("eissn", asSqlValue(ds.getEissn()));
|
304
|
map.put("lissn", asSqlValue(ds.getLissn()));
|
305
|
|
306
|
final ArrayList<Map<String, Object>> ifaces = new ArrayList<>();
|
307
|
|
308
|
if (ds.getInterfaces() != null) {
|
309
|
|
310
|
for (IfaceDesc iface : ds.getInterfaces()) {
|
311
|
if (iface.getId() != null && !iface.getId().trim().isEmpty()) {
|
312
|
final Map<String, Object> mapIface = Maps.newHashMap();
|
313
|
|
314
|
mapIface.put("id", asSqlValue(iface.getId()));
|
315
|
mapIface.put("typology", asSqlValue(iface.getTypology()));
|
316
|
mapIface.put("compliance", asSqlValue(iface.getCompliance()));
|
317
|
mapIface.put("contentDescription", asSqlValue(iface.getContentDescription()));
|
318
|
mapIface.put("accessProtocol", asSqlValue(iface.getAccessProtocol()));
|
319
|
mapIface.put("baseUrl", asSqlValue(iface.getBaseUrl()));
|
320
|
|
321
|
final Map<String, String> extraFields = Maps.newHashMap();
|
322
|
if (iface.getExtraFields() != null) {
|
323
|
for (String k : iface.getExtraFields().keySet()) {
|
324
|
if (k != null && !k.trim().isEmpty()) {
|
325
|
extraFields.put(asSqlValue(k), asSqlValue(iface.getExtraFields().get(k)));
|
326
|
}
|
327
|
}
|
328
|
}
|
329
|
mapIface.put("extraFields", extraFields);
|
330
|
|
331
|
final Map<String, String> accessParams = Maps.newHashMap();
|
332
|
if (iface.getAccessParams() != null) {
|
333
|
for (String k : iface.getAccessParams().keySet()) {
|
334
|
if (k != null && !k.trim().isEmpty()) {
|
335
|
accessParams.put(asSqlValue(k), asSqlValue(iface.getAccessParams().get(k)));
|
336
|
}
|
337
|
}
|
338
|
}
|
339
|
mapIface.put("accessParams", accessParams);
|
340
|
|
341
|
ifaces.add(mapIface);
|
342
|
}
|
343
|
}
|
344
|
|
345
|
map.put("interfaces", ifaces);
|
346
|
}
|
347
|
|
348
|
return map;
|
349
|
}
|
350
|
|
351
|
public static Map<String, Object> asMapOfSqlValues(final Map<String, String> input) {
|
352
|
final Map<String, Object> map = Maps.newHashMap();
|
353
|
for (Map.Entry<String, String> e : input.entrySet()) {
|
354
|
map.put(asSqlValue(e.getKey()), asSqlValue(e.getValue()));
|
355
|
}
|
356
|
return map;
|
357
|
}
|
358
|
|
359
|
}
|