1
|
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
|
.distinct()
|
52
|
.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
|
protected boolean updateRecord(final String recordId, final MdRecord record) {
|
63
|
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
|
try {
|
75
|
final SAXReader reader = new SAXReader();
|
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
|
return new ArrayList<>();
|
82
|
}
|
83
|
}
|
84
|
|
85
|
private CnrCollection createCollection(final Node n) {
|
86
|
|
87
|
final CnrCollection c = new CnrCollection();
|
88
|
final String code = n.valueOf("./*[local-name() = 'setSpec']").trim();
|
89
|
|
90
|
c.setCode(n.valueOf("./*[local-name() = 'setSpec']").trim());
|
91
|
|
92
|
if (code.equalsIgnoreCase("openaire")) {
|
93
|
c.setAcronym("openaire");
|
94
|
c.setName("Openaire Collection");
|
95
|
} else {
|
96
|
final String desc = n.valueOf("./*[local-name() = 'setDescription']").trim();
|
97
|
final Matcher m = Pattern.compile("^Prodotti della ricerca di (.+) \\- (.+)$").matcher(desc);
|
98
|
if (m.matches()) {
|
99
|
c.setAcronym(m.group(1));
|
100
|
c.setName(m.group(2));
|
101
|
} else {
|
102
|
c.setAcronym(desc);
|
103
|
c.setName(desc);
|
104
|
}
|
105
|
}
|
106
|
return c;
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
protected void resetConfiguration() {
|
111
|
colls.clear();
|
112
|
}
|
113
|
|
114
|
}
|