Revision 33828
Added by Michele Artini over 9 years ago
DatasourceManagerServiceImpl.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.datasources; |
2 | 2 |
|
3 | 3 |
import java.io.StringReader; |
4 |
import java.util.Collections; |
|
4 | 5 |
import java.util.Date; |
5 | 6 |
import java.util.List; |
6 | 7 |
import java.util.Map; |
7 | 8 |
|
8 | 9 |
import javax.annotation.Resource; |
9 | 10 |
|
11 |
import org.antlr.stringtemplate.StringTemplate; |
|
12 |
import org.apache.commons.io.IOUtils; |
|
13 |
import org.apache.commons.lang.StringEscapeUtils; |
|
10 | 14 |
import org.apache.commons.lang.StringUtils; |
15 |
import org.apache.commons.lang.math.NumberUtils; |
|
11 | 16 |
import org.apache.commons.logging.Log; |
12 | 17 |
import org.apache.commons.logging.LogFactory; |
13 | 18 |
import org.dom4j.Document; |
14 | 19 |
import org.dom4j.Element; |
15 | 20 |
import org.dom4j.Node; |
16 | 21 |
import org.dom4j.io.SAXReader; |
22 |
import org.springframework.beans.factory.annotation.Required; |
|
23 |
import org.springframework.core.io.ClassPathResource; |
|
17 | 24 |
|
25 |
import com.google.common.base.Function; |
|
26 |
import com.google.common.collect.Lists; |
|
18 | 27 |
import com.google.common.collect.Maps; |
28 |
import com.google.common.collect.Sets; |
|
19 | 29 |
|
30 |
import eu.dnetlib.enabling.datasources.rmi.BrowsableField; |
|
31 |
import eu.dnetlib.enabling.datasources.rmi.BrowseTerm; |
|
20 | 32 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants; |
21 | 33 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc; |
22 | 34 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerService; |
23 | 35 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException; |
24 | 36 |
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc; |
37 |
import eu.dnetlib.enabling.datasources.rmi.RepositoryMapEntry; |
|
38 |
import eu.dnetlib.enabling.datasources.rmi.SearchInterfacesEntry; |
|
39 |
import eu.dnetlib.enabling.datasources.rmi.SimpleDatasourceDesc; |
|
40 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
25 | 41 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
42 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException; |
|
26 | 43 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService; |
27 | 44 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
28 | 45 |
import eu.dnetlib.enabling.tools.AbstractBaseService; |
... | ... | |
33 | 50 |
|
34 | 51 |
private static final String REPOSITORY_RESOURCE_TYPE = "RepositoryServiceResourceType"; |
35 | 52 |
|
53 |
private List<XmlBrowsableField> browsableFields; |
|
54 |
|
|
55 |
private ClassPathResource findReposQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findRepos.xquery.st"); |
|
56 |
|
|
57 |
private ClassPathResource browseRepoApisQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/browseRepoApis.xquery.st"); |
|
58 |
private ClassPathResource findRepoApisQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findRepoApis.xquery.st"); |
|
59 |
private ClassPathResource findReposMapQuery = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findReposMap.xquery"); |
|
60 |
private ClassPathResource simpleFindReposQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/simpleFindRepos.xquery.st"); |
|
61 |
|
|
36 | 62 |
@Resource |
37 | 63 |
private UniqueServiceLocator serviceLocator; |
38 | 64 |
|
39 | 65 |
@Override |
40 | 66 |
public boolean addDatasource(final DatasourceDesc ds) throws DatasourceManagerServiceException { |
41 |
// TODO Auto-generated method stub |
|
42 |
return false; |
|
67 |
try { |
|
68 |
final String profile = DatasourceDescToProfile.convert(ds); |
|
69 |
serviceLocator.getService(ISRegistryService.class).registerProfile(profile); |
|
70 |
return true; |
|
71 |
} catch (Exception e) { |
|
72 |
log.error("Error registering profile", e); |
|
73 |
throw new DatasourceManagerServiceException("Error registering profile", e); |
|
74 |
} |
|
43 | 75 |
} |
44 | 76 |
|
45 | 77 |
@Override |
46 | 78 |
public boolean deleteDatasource(final String dsId) throws DatasourceManagerServiceException { |
47 |
// TODO Auto-generated method stub |
|
48 |
return false; |
|
79 |
try { |
|
80 |
return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId); |
|
81 |
} catch (ISRegistryException e) { |
|
82 |
log.error("Error deleting profile " + dsId, e); |
|
83 |
throw new DatasourceManagerServiceException("Error deleting profile " + dsId, e); |
|
84 |
} |
|
49 | 85 |
} |
50 | 86 |
|
51 | 87 |
@Override |
52 | 88 |
public DatasourceDesc getDatasource(final String dsId) throws DatasourceManagerServiceException { |
53 |
// TODO Auto-generated method stub |
|
54 |
return null; |
|
89 |
try { |
|
90 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId); |
|
91 |
return ProfileToDatasourceDesc.convert(profile); |
|
92 |
} catch (Exception e) { |
|
93 |
log.error("Error accessing profile " + dsId, e); |
|
94 |
throw new DatasourceManagerServiceException("Error accessing profile " + dsId, e); |
|
95 |
} |
|
55 | 96 |
} |
56 | 97 |
|
57 | 98 |
@Override |
58 | 99 |
public List<DatasourceDesc> listAllDatasources() throws DatasourceManagerServiceException { |
59 |
// TODO Auto-generated method stub |
|
60 |
return null; |
|
100 |
return listDatasourcesUsingFilter(null, null, null, null); |
|
61 | 101 |
} |
62 | 102 |
|
63 | 103 |
@Override |
... | ... | |
66 | 106 |
final String iisProcessingWorkflow, |
67 | 107 |
final String collectedFrom) |
68 | 108 |
throws DatasourceManagerServiceException { |
69 |
// TODO Auto-generated method stub |
|
70 |
return null; |
|
109 |
try { |
|
110 |
final StringTemplate st = new StringTemplate(); |
|
111 |
st.setTemplate(IOUtils.toString(findReposQueryTmpl.getInputStream())); |
|
112 |
|
|
113 |
if (StringUtils.isNotBlank(compliance)) { |
|
114 |
st.setAttribute("compliance", compliance); |
|
115 |
} |
|
116 |
if (StringUtils.isNotBlank(contentDescription)) { |
|
117 |
st.setAttribute("contentDescription", contentDescription); |
|
118 |
} |
|
119 |
if (StringUtils.isNotBlank(iisProcessingWorkflow)) { |
|
120 |
st.setAttribute("iisProcessingWorkflow", iisProcessingWorkflow); |
|
121 |
} |
|
122 |
if (StringUtils.isNotBlank(collectedFrom)) { |
|
123 |
st.setAttribute("collectedFrom", collectedFrom); |
|
124 |
} |
|
125 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString()); |
|
126 |
|
|
127 |
return Lists.transform(list, new ProfileToDatasourceDesc()); |
|
128 |
} catch (Exception e) { |
|
129 |
log.error("Error listing datasources", e); |
|
130 |
throw new DatasourceManagerServiceException("Error listing datasources", e); |
|
131 |
} |
|
71 | 132 |
} |
72 | 133 |
|
73 | 134 |
@Override |
... | ... | |
134 | 195 |
|
135 | 196 |
@Override |
136 | 197 |
public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException { |
137 |
// TODO Auto-generated method stub |
|
138 |
return false; |
|
198 |
throw new DatasourceManagerServiceException("NOT IMPLEMENTED"); |
|
139 | 199 |
} |
140 | 200 |
|
141 | 201 |
@Override |
... | ... | |
260 | 320 |
return true; |
261 | 321 |
} |
262 | 322 |
|
323 |
@Override |
|
324 |
public List<BrowsableField> listBrowsableFields() throws DatasourceManagerServiceException { |
|
325 |
return Lists.transform(getBrowsableFields(), new Function<XmlBrowsableField, BrowsableField>() { |
|
326 |
|
|
327 |
@Override |
|
328 |
public BrowsableField apply(final XmlBrowsableField f) { |
|
329 |
return new BrowsableField(f.getId(), f.getLabel()); |
|
330 |
} |
|
331 |
}); |
|
332 |
} |
|
333 |
|
|
334 |
@Override |
|
335 |
public List<BrowseTerm> browseField(final String f) throws DatasourceManagerServiceException { |
|
336 |
|
|
337 |
final List<BrowseTerm> list = Lists.newArrayList(); |
|
338 |
|
|
339 |
try { |
|
340 |
final XmlBrowsableField field = findBrowseField(f); |
|
341 |
if (field != null) { |
|
342 |
final StringTemplate st = new StringTemplate(IOUtils.toString(browseRepoApisQueryTmpl.getInputStream())); |
|
343 |
st.setAttribute("xpath", field.getXpath()); |
|
344 |
|
|
345 |
final Map<String, String> terms = fetchVocabularyTerms(field.getVocabulary()); |
|
346 |
|
|
347 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString())) { |
|
348 |
final String[] arr = s.split("@-@-@"); |
|
349 |
final String id = arr[0].trim(); |
|
350 |
final int count = NumberUtils.toInt(arr[1].trim(), 0); |
|
351 |
list.add(new BrowseTerm(id, findLabel(id, terms), count)); |
|
352 |
} |
|
353 |
} |
|
354 |
} catch (Exception e) { |
|
355 |
log.error("Error browsing field " + f, e); |
|
356 |
} |
|
357 |
|
|
358 |
return list; |
|
359 |
} |
|
360 |
|
|
361 |
private String findLabel(final String code, final Map<String, String> terms) { |
|
362 |
return terms.containsKey(code) ? terms.get(code) : code; |
|
363 |
} |
|
364 |
|
|
365 |
public Map<String, String> fetchVocabularyTerms(final String voc) throws ISLookUpException { |
|
366 |
final String xquery = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code = '" |
|
367 |
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)"; |
|
368 |
|
|
369 |
final Map<String, String> map = Maps.newHashMap(); |
|
370 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) { |
|
371 |
final String[] arr = s.split("@@@"); |
|
372 |
map.put(arr[0].trim(), arr[1].trim()); |
|
373 |
} |
|
374 |
return map; |
|
375 |
} |
|
376 |
|
|
377 |
private XmlBrowsableField findBrowseField(final String id) { |
|
378 |
for (XmlBrowsableField f : getBrowsableFields()) { |
|
379 |
if (f.getId().equals(id)) { return f; } |
|
380 |
} |
|
381 |
return null; |
|
382 |
} |
|
383 |
|
|
384 |
@Override |
|
385 |
public List<SearchInterfacesEntry> searchInterface(final String field, final String value) throws DatasourceManagerServiceException { |
|
386 |
try { |
|
387 |
|
|
388 |
final StringTemplate st = new StringTemplate(); |
|
389 |
st.setTemplate(IOUtils.toString(findRepoApisQueryTmpl.getInputStream())); |
|
390 |
|
|
391 |
if (field.equalsIgnoreCase("__search__")) { |
|
392 |
st.setAttribute("cond", "contains(../..//(*|@*)/lower-case(.), '" + StringEscapeUtils.escapeXml(value.toLowerCase()) + "')"); |
|
393 |
} else { |
|
394 |
final XmlBrowsableField f = findBrowseField(field); |
|
395 |
if (f != null) { |
|
396 |
st.setAttribute("cond", f.getXpath() + "='" + StringEscapeUtils.escapeXml(value) + "'"); |
|
397 |
} else { |
|
398 |
return Lists.newArrayList(); |
|
399 |
} |
|
400 |
} |
|
401 |
|
|
402 |
final String query = st.toString(); |
|
403 |
|
|
404 |
final SAXReader reader = new SAXReader(); |
|
405 |
|
|
406 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query); |
|
407 |
|
|
408 |
return Lists.transform(list, new Function<String, SearchInterfacesEntry>() { |
|
409 |
|
|
410 |
@Override |
|
411 |
public SearchInterfacesEntry apply(final String s) { |
|
412 |
final SearchInterfacesEntry iface = new SearchInterfacesEntry(); |
|
413 |
try { |
|
414 |
final Document doc = reader.read(new StringReader(s)); |
|
415 |
final String country = doc.valueOf("//REPO/@country"); |
|
416 |
|
|
417 |
iface.setRepoId(doc.valueOf("//REPO/@id")); |
|
418 |
iface.setRepoCountry(StringUtils.isEmpty(country) ? "-" : country.toUpperCase()); |
|
419 |
iface.setRepoName(doc.valueOf("//REPO/@name")); |
|
420 |
iface.setRepoPrefix(doc.valueOf("//REPO/@prefix")); |
|
421 |
|
|
422 |
final Node ifcNode = doc.selectSingleNode("//INTERFACE"); |
|
423 |
|
|
424 |
iface.setId(ifcNode.valueOf("./@id")); |
|
425 |
iface.setActive(Boolean.valueOf(ifcNode.valueOf("./@active"))); |
|
426 |
iface.setProtocol(ifcNode.valueOf("./ACCESS_PROTOCOL/text()")); |
|
427 |
|
|
428 |
final String overCompliance = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='overriding_compliance']"); |
|
429 |
if (StringUtils.isEmpty(overCompliance)) { |
|
430 |
iface.setCompliance(ifcNode.valueOf("@compliance")); |
|
431 |
} else { |
|
432 |
iface.setCompliance(overCompliance); |
|
433 |
} |
|
434 |
|
|
435 |
final String lastAggregationDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_date']"); |
|
436 |
if (!StringUtils.isEmpty(lastAggregationDate)) { |
|
437 |
iface.setAggrDate(lastAggregationDate); |
|
438 |
} else { |
|
439 |
final String lastDownloadDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_date']"); |
|
440 |
if (!StringUtils.isEmpty(lastDownloadDate)) { |
|
441 |
iface.setAggrDate(lastDownloadDate); |
|
442 |
} |
|
443 |
} |
|
444 |
final String lastAggregationTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_total']"); |
|
445 |
if (StringUtils.isEmpty(lastAggregationTotal)) { |
|
446 |
final String lastDownloadTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_total']"); |
|
447 |
if (StringUtils.isEmpty(lastDownloadTotal)) { |
|
448 |
iface.setAggrTotal(0); |
|
449 |
} else { |
|
450 |
iface.setAggrTotal(NumberUtils.toInt(lastDownloadTotal, 0)); |
|
451 |
} |
|
452 |
} else { |
|
453 |
iface.setAggrTotal(NumberUtils.toInt(lastAggregationTotal, 0)); |
|
454 |
} |
|
455 |
} catch (Exception e) { |
|
456 |
log.error(e); |
|
457 |
} |
|
458 |
return iface; |
|
459 |
} |
|
460 |
}); |
|
461 |
} catch (Exception e) { |
|
462 |
log.error("Error searching field " + field + " - value: " + value, e); |
|
463 |
} |
|
464 |
return Lists.newArrayList(); |
|
465 |
} |
|
466 |
|
|
467 |
@Override |
|
468 |
public List<RepositoryMapEntry> getRepositoryMap() throws DatasourceManagerServiceException { |
|
469 |
try { |
|
470 |
final SAXReader reader = new SAXReader(); |
|
471 |
|
|
472 |
final String query = IOUtils.toString(findReposMapQuery.getInputStream()); |
|
473 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query); |
|
474 |
return Lists.transform(list, new Function<String, RepositoryMapEntry>() { |
|
475 |
|
|
476 |
@Override |
|
477 |
public RepositoryMapEntry apply(final String s) { |
|
478 |
final RepositoryMapEntry r = new RepositoryMapEntry(); |
|
479 |
|
|
480 |
try { |
|
481 |
final Document doc = reader.read(new StringReader(s)); |
|
482 |
r.setId(doc.valueOf("//dsId")); |
|
483 |
r.setName(doc.valueOf("//name")); |
|
484 |
r.setLat(Float.parseFloat(doc.valueOf("//lat"))); |
|
485 |
r.setLng(Float.parseFloat(doc.valueOf("//lng"))); |
|
486 |
} catch (Exception e) { |
|
487 |
log.error(e); |
|
488 |
} |
|
489 |
return r; |
|
490 |
} |
|
491 |
}); |
|
492 |
} catch (Exception e) { |
|
493 |
log.error("Error obtaing repo map entries", e); |
|
494 |
} |
|
495 |
return Lists.newArrayList(); |
|
496 |
} |
|
497 |
|
|
498 |
private List<XmlBrowsableField> getBrowsableFields() { |
|
499 |
return browsableFields; |
|
500 |
} |
|
501 |
|
|
502 |
@Required |
|
503 |
public void setBrowsableFields(final List<XmlBrowsableField> browsableFields) { |
|
504 |
this.browsableFields = browsableFields; |
|
505 |
} |
|
506 |
|
|
507 |
@Override |
|
508 |
public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) throws DatasourceManagerServiceException { |
|
509 |
try { |
|
510 |
final List<SimpleDatasourceDesc> list = Lists.newArrayList(); |
|
511 |
|
|
512 |
final StringTemplate st = new StringTemplate(IOUtils.toString(simpleFindReposQueryTmpl.getInputStream())); |
|
513 |
st.setAttribute("type", type); |
|
514 |
|
|
515 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString())) { |
|
516 |
final SimpleDatasourceDesc r = new SimpleDatasourceDesc(); |
|
517 |
final String[] arr = s.split("@=@"); |
|
518 |
r.setId(arr[0].trim()); |
|
519 |
r.setName(arr[1].trim()); |
|
520 |
r.setOrigId(arr[2].trim()); |
|
521 |
r.setApis(Sets.newHashSet(arr[3].replaceAll("\\s", "").split(","))); |
|
522 |
r.setValid("true".equals(arr[4].trim())); |
|
523 |
r.setTypology(type); |
|
524 |
list.add(r); |
|
525 |
} |
|
526 |
|
|
527 |
Collections.sort(list); |
|
528 |
|
|
529 |
return list; |
|
530 |
} catch (Exception e) { |
|
531 |
log.error("Error listing repos", e); |
|
532 |
} |
|
533 |
return Lists.newArrayList(); |
|
534 |
} |
|
535 |
|
|
263 | 536 |
} |
Also available in: Unified diff
partial implementation