1
|
package eu.dnetlib.enabling.datasources;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.text.ParseException;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Arrays;
|
7
|
import java.util.Collections;
|
8
|
import java.util.Date;
|
9
|
import java.util.HashMap;
|
10
|
import java.util.List;
|
11
|
import java.util.Map;
|
12
|
import java.util.Objects;
|
13
|
import java.util.Optional;
|
14
|
import java.util.stream.Collectors;
|
15
|
|
16
|
import org.antlr.stringtemplate.StringTemplate;
|
17
|
import org.apache.commons.io.IOUtils;
|
18
|
import org.apache.commons.lang3.StringEscapeUtils;
|
19
|
import org.apache.commons.lang3.StringUtils;
|
20
|
import org.apache.commons.lang3.math.NumberUtils;
|
21
|
import org.apache.commons.logging.Log;
|
22
|
import org.apache.commons.logging.LogFactory;
|
23
|
import org.dom4j.Document;
|
24
|
import org.dom4j.Element;
|
25
|
import org.dom4j.Node;
|
26
|
import org.dom4j.io.SAXReader;
|
27
|
import org.quartz.CronExpression;
|
28
|
import org.springframework.beans.factory.annotation.Autowired;
|
29
|
import org.springframework.core.io.ClassPathResource;
|
30
|
import org.springframework.stereotype.Component;
|
31
|
|
32
|
import com.google.common.collect.Sets;
|
33
|
|
34
|
import eu.dnetlib.clients.dsManager.BrowsableField;
|
35
|
import eu.dnetlib.clients.dsManager.BrowseTerm;
|
36
|
import eu.dnetlib.clients.dsManager.DatasourceDesc;
|
37
|
import eu.dnetlib.clients.dsManager.DatasourcePoint;
|
38
|
import eu.dnetlib.clients.dsManager.IfaceDesc;
|
39
|
import eu.dnetlib.clients.dsManager.SearchInterfacesEntry;
|
40
|
import eu.dnetlib.clients.dsManager.SimpleDatasourceDesc;
|
41
|
import eu.dnetlib.enabling.is.DnetInformationServiceException;
|
42
|
import eu.dnetlib.enabling.is.InformationServiceController;
|
43
|
|
44
|
@Component
|
45
|
public class SimpleDatasourceManagerCore implements DatasourceManagerCore {
|
46
|
|
47
|
private static final Log log = LogFactory.getLog(SimpleDatasourceManagerCore.class);
|
48
|
|
49
|
@Autowired
|
50
|
private InformationServiceController is;
|
51
|
|
52
|
private static final List<BrowsableField> browsableFields = Arrays.asList(
|
53
|
new XmlBrowsableField("country", "Datasource countries", "../../COUNTRY/text()", "dnet:countries"),
|
54
|
new XmlBrowsableField("type", "API typologies", "@typology/string()", "dnet:datasource_typologies"),
|
55
|
new XmlBrowsableField("protocol", "API protocols", "ACCESS_PROTOCOL/text()", "dnet:protocols"),
|
56
|
new XmlBrowsableField("compliance", "API compatibility levels",
|
57
|
"concat(substring(./INTERFACE_EXTRA_FIELD[@name='overriding_compliance'], 1, number(boolean(./INTERFACE_EXTRA_FIELD[@name='overriding_compliance'])) * string-length(./INTERFACE_EXTRA_FIELD[@name='overriding_compliance'])), substring(@compliance, 1, number(not(boolean(./INTERFACE_EXTRA_FIELD[@name='overriding_compliance']))) * string-length(@compliance)))",
|
58
|
"dnet:compatibilityLevel"));
|
59
|
|
60
|
private ClassPathResource findReposQueryTmpl = new ClassPathResource("/templates/findRepos.xquery.st");
|
61
|
private ClassPathResource browseRepoApisQueryTmpl = new ClassPathResource("/templates/browseRepoApis.xquery.st");
|
62
|
private ClassPathResource findRepoApisQueryTmpl = new ClassPathResource("/templates/findRepoApis.xquery.st");
|
63
|
private ClassPathResource findReposMapQuery = new ClassPathResource("/templates/findReposMap.xquery");
|
64
|
private ClassPathResource simpleFindReposQueryTmpl = new ClassPathResource("/templates/simpleFindRepos.xquery.st");
|
65
|
|
66
|
public static final String OVERRIDING_COMPLIANCE_FIELD = "overriding_compliance";
|
67
|
|
68
|
@Override
|
69
|
public String addDatasource(final DatasourceDesc datasourceDescription) throws DatasourceManagerServiceException {
|
70
|
try {
|
71
|
final String profile = DatasourceDescToProfile.convert(datasourceDescription);
|
72
|
return is.registerProfile(profile);
|
73
|
} catch (final Exception e) {
|
74
|
log.error("Error registering profile", e);
|
75
|
throw new DatasourceManagerServiceException("Error registering profile", e);
|
76
|
}
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public List<DatasourceDesc> listDatasources(final String compliance,
|
81
|
final String contentDescription,
|
82
|
final String collectedFrom) throws DatasourceManagerServiceException {
|
83
|
try {
|
84
|
final StringTemplate st = new StringTemplate();
|
85
|
st.setTemplate(IOUtils.toString(findReposQueryTmpl.getInputStream()));
|
86
|
|
87
|
final Map<String, String> conds = new HashMap<>();
|
88
|
|
89
|
if (StringUtils.isNotBlank(compliance)) {
|
90
|
conds.put("//INTERFACE/@compliance", compliance);
|
91
|
}
|
92
|
if (StringUtils.isNotBlank(contentDescription)) {
|
93
|
conds.put("//INTERFACE/@contentDescription", contentDescription);
|
94
|
}
|
95
|
if (StringUtils.isNotBlank(collectedFrom)) {
|
96
|
// NOT USED
|
97
|
}
|
98
|
|
99
|
if (!conds.isEmpty()) {
|
100
|
st.setAttribute("conds", conds);
|
101
|
}
|
102
|
|
103
|
final List<String> list = is.find(st.toString());
|
104
|
|
105
|
return list.stream().map(new ProfileToDatasourceDesc()).collect(Collectors.toList());
|
106
|
} catch (final Exception e) {
|
107
|
log.error("Error listing datasources", e);
|
108
|
throw new DatasourceManagerServiceException("Error listing datasources", e);
|
109
|
}
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException {
|
114
|
throw new DatasourceManagerServiceException("NOT IMPLEMENTED");
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public boolean bulkUpdateApiExtraFields(final String dsId, final String ifaceId, final Map<String, String> fields)
|
119
|
throws DatasourceManagerServiceException {
|
120
|
try {
|
121
|
final String profile = obtainProfile(dsId);
|
122
|
|
123
|
final SAXReader reader = new SAXReader();
|
124
|
final Document doc = reader.read(new StringReader(profile));
|
125
|
|
126
|
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
127
|
if (iface != null) {
|
128
|
|
129
|
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) {
|
130
|
iface.selectSingleNode("./INTERFACE_EXTRA_FIELD").detach();
|
131
|
}
|
132
|
|
133
|
fields.entrySet().forEach(e -> {
|
134
|
if ((e.getValue() != null) && !e.getValue().isEmpty()) {
|
135
|
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD");
|
136
|
field.addAttribute("name", e.getKey());
|
137
|
field.addText(e.getValue());
|
138
|
}
|
139
|
});
|
140
|
is.updateProfile(dsId, doc.asXML());
|
141
|
} else {
|
142
|
log.error("Invalid interface: " + ifaceId);
|
143
|
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
144
|
}
|
145
|
} catch (final Exception e) {
|
146
|
log.error("Error updating API of profile: " + dsId);
|
147
|
throw new DatasourceManagerServiceException("Error updating API of profile: " + dsId, e);
|
148
|
}
|
149
|
return true;
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public List<BrowsableField> listBrowsableFields() throws DatasourceManagerServiceException {
|
154
|
return browsableFields;
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public List<BrowseTerm> browseField(final String f) throws DatasourceManagerServiceException {
|
159
|
final List<BrowseTerm> list = new ArrayList<>();
|
160
|
|
161
|
try {
|
162
|
final XmlBrowsableField field = findBrowseField(f);
|
163
|
if (field != null) {
|
164
|
final StringTemplate st = new StringTemplate(IOUtils.toString(browseRepoApisQueryTmpl.getInputStream()));
|
165
|
st.setAttribute("xpath", field.getXpath());
|
166
|
|
167
|
final Map<String, String> terms = fetchVocabularyTerms(field.getVocabulary());
|
168
|
|
169
|
for (final String s : is.find(st.toString())) {
|
170
|
final String[] arr = s.split("@-@-@");
|
171
|
final String id = arr[0].trim();
|
172
|
final int count = NumberUtils.toInt(arr[1].trim(), 0);
|
173
|
list.add(new BrowseTerm(id, findLabel(id, terms), count));
|
174
|
}
|
175
|
}
|
176
|
} catch (final Exception e) {
|
177
|
log.error("Error browsing field " + f, e);
|
178
|
}
|
179
|
|
180
|
return list;
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public List<SearchInterfacesEntry> searchInterface(final String field, final String value)
|
185
|
throws DatasourceManagerServiceException {
|
186
|
try {
|
187
|
|
188
|
final StringTemplate st = new StringTemplate();
|
189
|
st.setTemplate(IOUtils.toString(findRepoApisQueryTmpl.getInputStream()));
|
190
|
|
191
|
if (field.equalsIgnoreCase("__search__")) {
|
192
|
st.setAttribute("cond", "contains(../..//(*|@*)/lower-case(.), '" + StringEscapeUtils.escapeXml(value.toLowerCase()) + "')");
|
193
|
} else {
|
194
|
final XmlBrowsableField f = findBrowseField(field);
|
195
|
if (f != null) {
|
196
|
st.setAttribute("cond", f.getXpath() + "='" + StringEscapeUtils.escapeXml(value) + "'");
|
197
|
} else {
|
198
|
return new ArrayList<>();
|
199
|
}
|
200
|
}
|
201
|
|
202
|
final String query = st.toString();
|
203
|
|
204
|
final SAXReader reader = new SAXReader();
|
205
|
|
206
|
return is.find(query).stream()
|
207
|
.map(it -> convertSearchInterface(it, reader)).collect(Collectors.toList());
|
208
|
|
209
|
} catch (final Exception e) {
|
210
|
log.error("Error searching field " + field + " - value: " + value, e);
|
211
|
}
|
212
|
return new ArrayList<>();
|
213
|
}
|
214
|
|
215
|
@Override
|
216
|
public List<DatasourcePoint> getDatasourceMap() throws DatasourceManagerServiceException {
|
217
|
try {
|
218
|
final SAXReader reader = new SAXReader();
|
219
|
|
220
|
final String query = IOUtils.toString(findReposMapQuery.getInputStream());
|
221
|
return is.find(query)
|
222
|
.stream()
|
223
|
.map(s -> {
|
224
|
try {
|
225
|
final Document doc = reader.read(new StringReader(s));
|
226
|
return new DatasourcePoint(
|
227
|
doc.valueOf("//dsId"),
|
228
|
doc.valueOf("//name"),
|
229
|
Float.parseFloat(doc.valueOf("//lat")),
|
230
|
Float.parseFloat(doc.valueOf("//lng")));
|
231
|
} catch (final Exception e) {
|
232
|
log.error(e);
|
233
|
return null;
|
234
|
}
|
235
|
})
|
236
|
.filter(Objects::nonNull)
|
237
|
.collect(Collectors.toList());
|
238
|
|
239
|
} catch (final Exception e) {
|
240
|
log.error("Error obtaing repo map entries", e);
|
241
|
}
|
242
|
return new ArrayList<>();
|
243
|
}
|
244
|
|
245
|
@Override
|
246
|
public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) throws DatasourceManagerServiceException {
|
247
|
try {
|
248
|
final List<SimpleDatasourceDesc> list = new ArrayList<>();
|
249
|
|
250
|
final StringTemplate st = new StringTemplate(IOUtils.toString(simpleFindReposQueryTmpl.getInputStream()));
|
251
|
st.setAttribute("type", type);
|
252
|
|
253
|
for (final String s : is.find(st.toString())) {
|
254
|
final SimpleDatasourceDesc r = new SimpleDatasourceDesc();
|
255
|
final String[] arr = s.split("@=@");
|
256
|
r.setId(arr[0].trim());
|
257
|
r.setName(arr[1].trim());
|
258
|
r.setOrigId(arr[2].trim());
|
259
|
r.setApis(Sets.newHashSet(arr[3].replaceAll("\\s", "").split(",")));
|
260
|
r.setTypology(type);
|
261
|
list.add(r);
|
262
|
}
|
263
|
|
264
|
Collections.sort(list);
|
265
|
|
266
|
return list;
|
267
|
} catch (final Exception e) {
|
268
|
log.error("Error listing repos", e);
|
269
|
}
|
270
|
return new ArrayList<>();
|
271
|
}
|
272
|
|
273
|
@Override
|
274
|
public boolean deleteDatasource(final String dsId) throws DatasourceManagerServiceException {
|
275
|
try {
|
276
|
return is.deleteProfile(fixDsId(dsId));
|
277
|
} catch (final DnetInformationServiceException e) {
|
278
|
log.error("Error deleting profile " + dsId, e);
|
279
|
throw new DatasourceManagerServiceException("Error deleting profile " + dsId, e);
|
280
|
}
|
281
|
}
|
282
|
|
283
|
private String fixDsId(final String dsId) throws DatasourceManagerServiceException {
|
284
|
if (dsId.startsWith("entity/datasource/")) {
|
285
|
return dsId;
|
286
|
} else {
|
287
|
try {
|
288
|
return is.find("for $x in collection('/db/DRIVER/entity/datasource') "
|
289
|
+ "where $x//DATASOURCE_ORIGINAL_ID = '" + dsId + "' "
|
290
|
+ "return $x//RESOURCE_IDENTIFIER/@value/string()").get(0);
|
291
|
} catch (final Throwable e) {
|
292
|
throw new DatasourceManagerServiceException("Invalid id: " + dsId, e);
|
293
|
}
|
294
|
}
|
295
|
}
|
296
|
|
297
|
@Override
|
298
|
public DatasourceDesc getDatasource(final String dsId) throws DatasourceManagerServiceException {
|
299
|
try {
|
300
|
final String profile = obtainProfile(fixDsId(dsId));
|
301
|
return ProfileToDatasourceDesc.convert(profile);
|
302
|
} catch (final Exception e) {
|
303
|
log.error("Error accessing profile: " + dsId, e);
|
304
|
throw new DatasourceManagerServiceException("Error accessing profile " + dsId, e);
|
305
|
}
|
306
|
}
|
307
|
|
308
|
@Override
|
309
|
public boolean updateLevelOfCompliance(final String dsId, final String ifaceId, final String level, final boolean override)
|
310
|
throws DatasourceManagerServiceException {
|
311
|
try {
|
312
|
if (override) {
|
313
|
|
314
|
final String xq =
|
315
|
String.format("for $x in doc('/db/DRIVER/%s')//INTERFACE[@id = '%s']/@compliance return update replace $x with '%s'",
|
316
|
fixDsId(dsId), ifaceId, level);
|
317
|
return is.xupdate(xq);
|
318
|
} else {
|
319
|
|
320
|
final String profId = fixDsId(dsId);
|
321
|
|
322
|
try {
|
323
|
final String profile = obtainProfile(profId);
|
324
|
|
325
|
final SAXReader reader = new SAXReader();
|
326
|
final Document doc = reader.read(new StringReader(profile));
|
327
|
|
328
|
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
329
|
if (iface != null) {
|
330
|
final Map<String, String> fields = new HashMap<>();
|
331
|
|
332
|
if (!StringUtils.isEmpty(level)) {
|
333
|
fields.put(OVERRIDING_COMPLIANCE_FIELD, level);
|
334
|
}
|
335
|
|
336
|
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) {
|
337
|
final Node node = iface.selectSingleNode("./INTERFACE_EXTRA_FIELD");
|
338
|
final String name = node.valueOf("@name");
|
339
|
|
340
|
if (!name.equals(OVERRIDING_COMPLIANCE_FIELD)) {
|
341
|
fields.put(node.valueOf("@name"), node.getText());
|
342
|
}
|
343
|
node.detach();
|
344
|
}
|
345
|
|
346
|
for (final Map.Entry<String, String> e : fields.entrySet()) {
|
347
|
if ((e.getValue() != null) && !e.getValue().isEmpty()) {
|
348
|
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD");
|
349
|
field.addAttribute("name", e.getKey());
|
350
|
field.addText(e.getValue());
|
351
|
}
|
352
|
}
|
353
|
is.updateProfile(profId, doc.asXML());
|
354
|
} else {
|
355
|
log.error("Invalid interface: " + ifaceId);
|
356
|
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
357
|
}
|
358
|
} catch (final Exception e) {
|
359
|
log.error("Error updating API of profile: " + profId);
|
360
|
throw new DatasourceManagerServiceException("Error updating API of profile: " + profId, e);
|
361
|
}
|
362
|
|
363
|
return true;
|
364
|
}
|
365
|
} catch (final Exception e) {
|
366
|
log.error("Error updating profile: " + dsId, e);
|
367
|
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
368
|
}
|
369
|
}
|
370
|
|
371
|
@Override
|
372
|
public boolean updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException {
|
373
|
try {
|
374
|
final String xq =
|
375
|
String.format("for $x in doc('/db/DRIVER/%s')//INTERFACE[@id = '%s']/BASE_URL return update replace $x with <BASE_URL>%s</BASE_URL>",
|
376
|
fixDsId(dsId), ifaceId, baseUrl);
|
377
|
return is.xupdate(xq);
|
378
|
} catch (final Exception e) {
|
379
|
log.error("Error updating profile: " + dsId, e);
|
380
|
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
381
|
}
|
382
|
}
|
383
|
|
384
|
@Override
|
385
|
public boolean updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException {
|
386
|
try {
|
387
|
final String xq =
|
388
|
String.format("for $x in doc('/db/DRIVER/%s')//INTERFACE[@id = '%s']/@active return update replace $x with '%s'",
|
389
|
fixDsId(dsId), ifaceId, active);
|
390
|
return is.xupdate(xq);
|
391
|
} catch (final Exception e) {
|
392
|
log.error("Error updating profile: " + dsId, e);
|
393
|
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
394
|
}
|
395
|
}
|
396
|
|
397
|
@Override
|
398
|
public boolean updateContentDescription(final String dsId, final String ifaceId, final String desc) throws DatasourceManagerServiceException {
|
399
|
try {
|
400
|
final String xq =
|
401
|
String.format("for $x in doc('/db/DRIVER/%s')//INTERFACE[@id = '%s']/@contentDescription return update replace $x with '%s'",
|
402
|
fixDsId(dsId), ifaceId, desc);
|
403
|
return is.xupdate(xq);
|
404
|
} catch (final Exception e) {
|
405
|
log.error("Error updating profile: " + dsId, e);
|
406
|
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
407
|
}
|
408
|
}
|
409
|
|
410
|
@Override
|
411
|
public boolean updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
412
|
throws DatasourceManagerServiceException {
|
413
|
|
414
|
final String profId = fixDsId(dsId);
|
415
|
try {
|
416
|
final String profile = obtainProfile(profId);
|
417
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
418
|
|
419
|
final Node ifcNode = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']");
|
420
|
final Node node = ifcNode.selectSingleNode("./INTERFACE_EXTRA_FIELD[@name = '" + field + "']");
|
421
|
if (node != null) {
|
422
|
node.setText(value);
|
423
|
} else {
|
424
|
final Element e = ((Element) ifcNode).addElement("INTERFACE_EXTRA_FIELD");
|
425
|
e.addAttribute("name", field);
|
426
|
e.setText(value);
|
427
|
}
|
428
|
return is.updateProfile(profId, doc.asXML());
|
429
|
} catch (final Exception e) {
|
430
|
log.error("Error updating profile: " + dsId, e);
|
431
|
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
432
|
}
|
433
|
}
|
434
|
|
435
|
@Override
|
436
|
public boolean updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
437
|
throws DatasourceManagerServiceException {
|
438
|
final String profId = fixDsId(dsId);
|
439
|
|
440
|
try {
|
441
|
|
442
|
final String profile = obtainProfile(profId);
|
443
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
444
|
|
445
|
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/ACCESS_PROTOCOL");
|
446
|
((Element) node).addAttribute(field, value);
|
447
|
|
448
|
return is.updateProfile(profId, doc.asXML());
|
449
|
} catch (final Exception e) {
|
450
|
log.error("Error updating profile: " + profId, e);
|
451
|
throw new DatasourceManagerServiceException("Error updating profile: " + profId, e);
|
452
|
}
|
453
|
}
|
454
|
|
455
|
@Override
|
456
|
public boolean deleteAccessParam(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
457
|
final String profId = fixDsId(dsId);
|
458
|
|
459
|
try {
|
460
|
|
461
|
final String profile = obtainProfile(profId);
|
462
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
463
|
|
464
|
final Node ap = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/ACCESS_PROTOCOL/@" + field);
|
465
|
if (ap != null) {
|
466
|
ap.detach();
|
467
|
|
468
|
}
|
469
|
return is.updateProfile(profId, doc.asXML());
|
470
|
} catch (final Exception e) {
|
471
|
log.error("Error updating profile: " + profId, e);
|
472
|
throw new DatasourceManagerServiceException("Error updating profile: " + profId, e);
|
473
|
}
|
474
|
}
|
475
|
|
476
|
@Override
|
477
|
public boolean deleteExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
478
|
final String profId = fixDsId(dsId);
|
479
|
|
480
|
try {
|
481
|
|
482
|
final String profile = obtainProfile(profId);
|
483
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
484
|
|
485
|
final Node ef = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/INTERFACE_EXTRA_FIELD[@name = '" + field + "']");
|
486
|
if (ef != null) {
|
487
|
ef.detach();
|
488
|
}
|
489
|
return is.updateProfile(profId, doc.asXML());
|
490
|
} catch (final Exception e) {
|
491
|
log.error("Error updating profile: " + profId, e);
|
492
|
throw new DatasourceManagerServiceException("Error updating profile: " + profId, e);
|
493
|
}
|
494
|
}
|
495
|
|
496
|
@Override
|
497
|
public boolean addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException {
|
498
|
final String profId = fixDsId(dsId);
|
499
|
|
500
|
try {
|
501
|
final String profile = obtainProfile(profId);
|
502
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
503
|
|
504
|
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + iface.getId() + "']");
|
505
|
if (node != null) {
|
506
|
node.detach();
|
507
|
}
|
508
|
|
509
|
((Element) doc.selectSingleNode("//INTERFACES")).add(DatasourceDescToProfile.ifaceDescToNode(iface));
|
510
|
|
511
|
return is.updateProfile(profId, doc.asXML());
|
512
|
} catch (final Exception e) {
|
513
|
log.error("Error updating profile: " + profId, e);
|
514
|
throw new DatasourceManagerServiceException("Error updating profile: " + profId, e);
|
515
|
}
|
516
|
}
|
517
|
|
518
|
@Override
|
519
|
public boolean deleteInterface(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
520
|
final String profId = fixDsId(dsId);
|
521
|
|
522
|
try {
|
523
|
final String profile = obtainProfile(profId);
|
524
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
525
|
|
526
|
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']");
|
527
|
if (node != null) {
|
528
|
node.detach();
|
529
|
}
|
530
|
|
531
|
return is.updateProfile(profId, doc.asXML());
|
532
|
} catch (final Exception e) {
|
533
|
log.error("Error updating profile: " + profId, e);
|
534
|
throw new DatasourceManagerServiceException("Error updating profile: " + profId, e);
|
535
|
}
|
536
|
}
|
537
|
|
538
|
@Override
|
539
|
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
540
|
final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()";
|
541
|
try {
|
542
|
final List<String> cronExpressions = is.find(xquery);
|
543
|
return cronExpressions.isEmpty() ? null : (new CronExpression(cronExpressions.get(0))).getNextValidTimeAfter(new Date());
|
544
|
} catch (final DnetInformationServiceException e) {
|
545
|
log.error("Error in xquery: " + xquery, e);
|
546
|
throw new DatasourceManagerServiceException("Error in xquery: " + xquery, e);
|
547
|
} catch (final ParseException e) {
|
548
|
log.error("Error parsing cron expression", e);
|
549
|
throw new DatasourceManagerServiceException("Error parsing cron expression", e);
|
550
|
}
|
551
|
}
|
552
|
|
553
|
@Override
|
554
|
public boolean bulkUpdateApiAccessParams(final String dsId, final String ifaceId, final Map<String, String> params)
|
555
|
throws DatasourceManagerServiceException {
|
556
|
|
557
|
final String profId = fixDsId(dsId);
|
558
|
|
559
|
try {
|
560
|
final String profile = obtainProfile(profId);
|
561
|
|
562
|
final SAXReader reader = new SAXReader();
|
563
|
final Document doc = reader.read(new StringReader(profile));
|
564
|
|
565
|
final Element accessNode = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/ACCESS_PROTOCOL");
|
566
|
if (accessNode != null) {
|
567
|
while (accessNode.attributes().size() > 0) {
|
568
|
accessNode.selectSingleNode("@*").detach();
|
569
|
}
|
570
|
for (final Map.Entry<String, String> e : params.entrySet()) {
|
571
|
if ((e.getValue() != null) && !e.getValue().isEmpty()) {
|
572
|
if (e.getKey().equalsIgnoreCase("baseUrl")) {
|
573
|
doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/BASE_URL").setText(e.getValue());
|
574
|
} else {
|
575
|
accessNode.addAttribute(e.getKey(), e.getValue());
|
576
|
}
|
577
|
}
|
578
|
}
|
579
|
is.updateProfile(profId, doc.asXML());
|
580
|
} else {
|
581
|
log.error("Invalid interface: " + ifaceId);
|
582
|
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
583
|
}
|
584
|
} catch (final Exception e) {
|
585
|
log.error("Error updating API of profile: " + profId);
|
586
|
throw new DatasourceManagerServiceException("Error updating API of profile: " + profId, e);
|
587
|
}
|
588
|
return true;
|
589
|
}
|
590
|
|
591
|
private XmlBrowsableField findBrowseField(final String id) {
|
592
|
|
593
|
final Optional<XmlBrowsableField> res = browsableFields.stream()
|
594
|
.filter(f -> f.getId().equals(id) && (f instanceof XmlBrowsableField))
|
595
|
.map(f -> (XmlBrowsableField) f)
|
596
|
.findFirst();
|
597
|
|
598
|
return res.isPresent() ? res.get() : null;
|
599
|
}
|
600
|
|
601
|
public Map<String, String> fetchVocabularyTerms(final String voc) throws DnetInformationServiceException {
|
602
|
final String xquery = "for $x in collection('/db/DRIVER/conf/vocabulary')[.//VOCABULARY_NAME/@code = '"
|
603
|
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
|
604
|
|
605
|
final Map<String, String> map = new HashMap<>();
|
606
|
for (final String s : is.find(xquery)) {
|
607
|
final String[] arr = s.split("@@@");
|
608
|
map.put(arr[0].trim(), arr[1].trim());
|
609
|
}
|
610
|
return map;
|
611
|
}
|
612
|
|
613
|
private String findLabel(final String code, final Map<String, String> terms) {
|
614
|
return terms.containsKey(code) ? terms.get(code) : code;
|
615
|
}
|
616
|
|
617
|
private SearchInterfacesEntry convertSearchInterface(final String s, final SAXReader reader) {
|
618
|
final SearchInterfacesEntry iface = new SearchInterfacesEntry();
|
619
|
try {
|
620
|
final Document doc = reader.read(new StringReader(s));
|
621
|
final String country = doc.valueOf("//REPO/@country");
|
622
|
|
623
|
iface.setDsId(doc.valueOf("//REPO/@id"));
|
624
|
iface.setCountry(StringUtils.isEmpty(country) ? "-" : country.toUpperCase());
|
625
|
iface.setDsName(doc.valueOf("//REPO/@name"));
|
626
|
iface.setPrefix(doc.valueOf("//REPO/@prefix"));
|
627
|
|
628
|
final Node ifcNode = doc.selectSingleNode("//INTERFACE");
|
629
|
|
630
|
iface.setId(ifcNode.valueOf("./@id"));
|
631
|
iface.setActive(Boolean.valueOf(ifcNode.valueOf("./@active")));
|
632
|
iface.setProtocol(ifcNode.valueOf("./ACCESS_PROTOCOL/text()"));
|
633
|
|
634
|
final String overCompliance = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='overriding_compliance']");
|
635
|
if (StringUtils.isEmpty(overCompliance)) {
|
636
|
iface.setCompliance(ifcNode.valueOf("@compliance"));
|
637
|
} else {
|
638
|
iface.setCompliance(overCompliance);
|
639
|
}
|
640
|
|
641
|
final String lastAggregationDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_date']");
|
642
|
if (!StringUtils.isEmpty(lastAggregationDate)) {
|
643
|
iface.setAggrDate(lastAggregationDate);
|
644
|
} else {
|
645
|
final String lastDownloadDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_date']");
|
646
|
if (!StringUtils.isEmpty(lastDownloadDate)) {
|
647
|
iface.setAggrDate(lastDownloadDate);
|
648
|
}
|
649
|
}
|
650
|
final String lastAggregationTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_total']");
|
651
|
if (StringUtils.isEmpty(lastAggregationTotal)) {
|
652
|
final String lastDownloadTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_total']");
|
653
|
if (StringUtils.isEmpty(lastDownloadTotal)) {
|
654
|
iface.setAggrTotal(0);
|
655
|
} else {
|
656
|
iface.setAggrTotal(NumberUtils.toInt(lastDownloadTotal, 0));
|
657
|
}
|
658
|
} else {
|
659
|
iface.setAggrTotal(NumberUtils.toInt(lastAggregationTotal, 0));
|
660
|
}
|
661
|
} catch (final Exception e) {
|
662
|
log.error(e);
|
663
|
}
|
664
|
return iface;
|
665
|
}
|
666
|
|
667
|
private String obtainProfile(final String dsId) throws DatasourceManagerServiceException {
|
668
|
try {
|
669
|
return dsId.startsWith("entity/datasource")
|
670
|
? is.getProfile(dsId)
|
671
|
: is.find("collection('/db/DRIVER/entity/datasource')/*[.//DATASOURCE_ORIGINAL_ID='" + dsId + "']").get(0);
|
672
|
} catch (final Throwable e) {
|
673
|
log.error("Error accessing profile: " + dsId, e);
|
674
|
throw new DatasourceManagerServiceException("Error accessing profile " + dsId, e);
|
675
|
}
|
676
|
}
|
677
|
|
678
|
}
|