1 |
51852
|
michele.ar
|
package eu.dnetlib.data.mdstore.plugins;
|
2 |
|
|
|
3 |
|
|
import java.io.StringReader;
|
4 |
|
|
import java.net.URISyntaxException;
|
5 |
|
|
import java.util.ArrayList;
|
6 |
|
|
import java.util.HashMap;
|
7 |
|
|
import java.util.List;
|
8 |
|
|
import java.util.Map;
|
9 |
|
|
import java.util.Objects;
|
10 |
|
|
import java.util.regex.Matcher;
|
11 |
|
|
import java.util.regex.Pattern;
|
12 |
|
|
import java.util.stream.Collectors;
|
13 |
|
|
|
14 |
|
|
import org.apache.commons.logging.Log;
|
15 |
|
|
import org.apache.commons.logging.LogFactory;
|
16 |
|
|
import org.dom4j.Document;
|
17 |
|
|
import org.dom4j.DocumentException;
|
18 |
|
|
import org.dom4j.Node;
|
19 |
|
|
import org.dom4j.io.SAXReader;
|
20 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
21 |
|
|
|
22 |
|
|
import eu.dnetlib.data.mdstore.plugins.objects.CnrCollection;
|
23 |
|
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
24 |
|
|
import eu.dnetlib.data.utils.HttpFetcher;
|
25 |
|
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
26 |
|
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
27 |
|
|
|
28 |
|
|
public class EnrichCollectionPlugin extends MdRecordPlugin {
|
29 |
|
|
|
30 |
|
|
private Map<String, CnrCollection> colls = new HashMap<>();
|
31 |
|
|
|
32 |
|
|
private static final Log log = LogFactory.getLog(EnrichCollectionPlugin.class);
|
33 |
|
|
|
34 |
|
|
@Autowired
|
35 |
|
|
private UniqueServiceLocator serviceLocator;
|
36 |
|
|
|
37 |
|
|
@Override
|
38 |
|
|
protected void reconfigure(final Map<String, String> params) {
|
39 |
|
|
|
40 |
|
|
params.entrySet().forEach(e -> log.info(String.format("******************* %s -> %s", e.getKey(), e.getValue())));
|
41 |
|
|
|
42 |
|
|
try {
|
43 |
|
|
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(params.get("dsId"));
|
44 |
|
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
45 |
|
|
final String baseUrl = doc.valueOf("//INTERFACE[@id='" + params.get("dsInterface") + "']/BASE_URL").trim();
|
46 |
|
|
|
47 |
|
|
colls.clear();
|
48 |
|
|
colls.putAll(listOaiCollections(baseUrl + "?verb=ListSets").stream()
|
49 |
|
|
.map(this::createCollection)
|
50 |
|
|
.filter(Objects::nonNull)
|
51 |
56978
|
michele.ar
|
.distinct()
|
52 |
51852
|
michele.ar
|
.collect(Collectors.toMap(CnrCollection::getCode, o -> o)));
|
53 |
|
|
|
54 |
|
|
} catch (final Exception e) {
|
55 |
|
|
log.error("Error evaluating ListSets", e);
|
56 |
|
|
throw new RuntimeException("Error evaluating ListSets", e);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
@Override
|
62 |
60388
|
michele.ar
|
protected boolean updateRecord(final String recordId, final MdRecord record) {
|
63 |
51852
|
michele.ar
|
for (final CnrCollection c : record.getInCollections()) {
|
64 |
|
|
if (colls.containsKey(c.getCode())) {
|
65 |
|
|
c.setName(colls.get(c.getCode()).getName());
|
66 |
|
|
c.setAcronym(colls.get(c.getCode()).getAcronym());
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
return true;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
@SuppressWarnings("unchecked")
|
73 |
|
|
private List<Node> listOaiCollections(final String listSetsUrl) {
|
74 |
62330
|
michele.ar
|
final SAXReader reader = new SAXReader();
|
75 |
51852
|
michele.ar
|
try {
|
76 |
|
|
final String s = HttpFetcher.fetch(listSetsUrl);
|
77 |
|
|
final Document doc = reader.read(new StringReader(s));
|
78 |
|
|
return doc.selectNodes("//*[local-name() = 'ListSets']/*[local-name() = 'set']");
|
79 |
|
|
} catch (final DocumentException | URISyntaxException e) {
|
80 |
|
|
log.error("Error listing sets from url: " + listSetsUrl, e);
|
81 |
62330
|
michele.ar
|
try {
|
82 |
|
|
final Document doc = reader.read(getClass().getResourceAsStream("ListSets.xml"));
|
83 |
|
|
return doc.selectNodes("//*[local-name() = 'ListSets']/*[local-name() = 'set']");
|
84 |
|
|
} catch (final DocumentException e1) {
|
85 |
|
|
log.error("Error listing sets from local file", e1);
|
86 |
|
|
return new ArrayList<>();
|
87 |
|
|
}
|
88 |
51852
|
michele.ar
|
}
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
private CnrCollection createCollection(final Node n) {
|
92 |
|
|
|
93 |
|
|
final CnrCollection c = new CnrCollection();
|
94 |
|
|
final String code = n.valueOf("./*[local-name() = 'setSpec']").trim();
|
95 |
|
|
|
96 |
|
|
c.setCode(n.valueOf("./*[local-name() = 'setSpec']").trim());
|
97 |
|
|
|
98 |
|
|
if (code.equalsIgnoreCase("openaire")) {
|
99 |
|
|
c.setAcronym("openaire");
|
100 |
|
|
c.setName("Openaire Collection");
|
101 |
|
|
} else {
|
102 |
|
|
final String desc = n.valueOf("./*[local-name() = 'setDescription']").trim();
|
103 |
|
|
final Matcher m = Pattern.compile("^Prodotti della ricerca di (.+) \\- (.+)$").matcher(desc);
|
104 |
|
|
if (m.matches()) {
|
105 |
|
|
c.setAcronym(m.group(1));
|
106 |
|
|
c.setName(m.group(2));
|
107 |
|
|
} else {
|
108 |
|
|
c.setAcronym(desc);
|
109 |
|
|
c.setName(desc);
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
return c;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
54968
|
michele.ar
|
@Override
|
116 |
|
|
protected void resetConfiguration() {
|
117 |
|
|
colls.clear();
|
118 |
|
|
}
|
119 |
|
|
|
120 |
51852
|
michele.ar
|
}
|