1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.datacite;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.dom4j.Document;
|
9
|
import org.dom4j.Element;
|
10
|
import org.dom4j.io.SAXReader;
|
11
|
|
12
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
13
|
|
14
|
public class PatchHostedBy implements UnaryFunction<String, String> {
|
15
|
private Map<String, HostedByEntry> setSpecHostedByMap;
|
16
|
private DataciteHostedByCounters counters;
|
17
|
|
18
|
private final SAXReader reader = new SAXReader();
|
19
|
private static final Log log = LogFactory.getLog(PatchHostedBy.class);
|
20
|
|
21
|
public PatchHostedBy(final Map<String, HostedByEntry> setSpecHostedByMap, final DataciteHostedByCounters counters) {
|
22
|
this.setSpecHostedByMap = setSpecHostedByMap;
|
23
|
this.counters = counters;
|
24
|
if (log.isDebugEnabled()) {
|
25
|
log.debug("****************************************");
|
26
|
log.debug("SetSpec/hostedBy map:");
|
27
|
for (Map.Entry<String, HostedByEntry> e : setSpecHostedByMap.entrySet()) {
|
28
|
log.debug(" " + e.getKey() + " -> " + e.getValue());
|
29
|
}
|
30
|
log.debug("****************************************");
|
31
|
}
|
32
|
}
|
33
|
|
34
|
@Override
|
35
|
public String evaluate(final String record) {
|
36
|
try {
|
37
|
final Document doc = reader.read(new StringReader(record));
|
38
|
final Element node = (Element) doc.selectSingleNode("//*[local-name()='hostedBy']");
|
39
|
if (node != null) {
|
40
|
final HostedByEntry ds = findHostedBy(doc);
|
41
|
if (ds != null) {
|
42
|
node.addAttribute("id", ds.getId());
|
43
|
node.addAttribute("name", ds.getName());
|
44
|
counters.increaseCounter(ds.getId());
|
45
|
}
|
46
|
return doc.asXML();
|
47
|
} else if (log.isDebugEnabled()) {
|
48
|
log.debug(" -- Missing hostedBy --");
|
49
|
}
|
50
|
} catch(Throwable e) {
|
51
|
log.error("Error adding hosted by to " + record);
|
52
|
}
|
53
|
return record;
|
54
|
}
|
55
|
|
56
|
private HostedByEntry findHostedBy(final Document doc) {
|
57
|
for(Object o : doc.selectNodes("//*[local-name()='header']/*[local-name()='setSpec']")) {
|
58
|
final String set = ((Element) o).getText().trim();
|
59
|
if (setSpecHostedByMap.containsKey(set)) {
|
60
|
if (log.isInfoEnabled()) {
|
61
|
log.info(set + " -> " + setSpecHostedByMap.get(set));
|
62
|
}
|
63
|
return setSpecHostedByMap.get(set);
|
64
|
} else if (log.isDebugEnabled()) {
|
65
|
log.debug(set + " -> UNKNOWN REPO");
|
66
|
}
|
67
|
}
|
68
|
return null;
|
69
|
}
|
70
|
|
71
|
}
|