1
|
package eu.dnetlib.msro.workflows.nodes;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.io.InputStreamReader;
|
6
|
import java.util.HashMap;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import org.apache.commons.lang3.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.DocumentHelper;
|
14
|
import org.dom4j.Element;
|
15
|
import org.dom4j.Namespace;
|
16
|
import org.dom4j.Node;
|
17
|
import org.dom4j.QName;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.beans.factory.annotation.Required;
|
20
|
import org.springframework.core.io.Resource;
|
21
|
|
22
|
import com.google.gson.Gson;
|
23
|
import com.google.gson.reflect.TypeToken;
|
24
|
|
25
|
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
|
26
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
27
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
28
|
import eu.dnetlib.msro.workflows.procs.Env;
|
29
|
import eu.dnetlib.rmi.common.ResultSet;
|
30
|
import eu.dnetlib.rmi.manager.MSROException;
|
31
|
|
32
|
public class PatchCinecaIdsJobNode extends SimpleJobNode {
|
33
|
|
34
|
@Autowired
|
35
|
private ResultSetFactory resultSetFactory;
|
36
|
|
37
|
private Resource idMapFile;
|
38
|
|
39
|
private String inputEprParam;
|
40
|
private String outputEprParam;
|
41
|
|
42
|
public static final String cinecaOaiPrefix = "oai:iris.cnr.it:";
|
43
|
public static final String peopleOaiPrefix = "oai:it.cnr:prodotti:";
|
44
|
public static final String peopleNamespacePrefix = "people______::";
|
45
|
|
46
|
private static final Namespace ISTI_NS = new Namespace("isti", "http://openportal.isti.cnr.it");
|
47
|
private static final Namespace DRI_NS = new Namespace("dri", "http://www.driver-repository.eu/namespace/dri");
|
48
|
|
49
|
private static final Log log = LogFactory.getLog(PatchCinecaIdsJobNode.class);
|
50
|
|
51
|
@Override
|
52
|
protected String execute(final Env env) throws Exception {
|
53
|
|
54
|
@SuppressWarnings("unchecked")
|
55
|
final ResultSet<String> rsIn = env.getAttribute(inputEprParam, ResultSet.class);
|
56
|
|
57
|
if (rsIn == null) { throw new MSROException("Passed rs is null"); }
|
58
|
|
59
|
final Map<String, String> idMap = loadMap(idMapFile);
|
60
|
|
61
|
log.info("potential mapped ids: " + idMap.size());
|
62
|
|
63
|
final ResultSet<String> rsOut = resultSetFactory.map(rsIn, String.class, xml -> patchId(xml, idMap));
|
64
|
|
65
|
env.setAttribute(outputEprParam, rsOut);
|
66
|
|
67
|
return Arc.DEFAULT_ARC;
|
68
|
}
|
69
|
|
70
|
protected Map<String, String> loadMap(final Resource mapResource) throws IOException {
|
71
|
|
72
|
final Gson gson = new Gson();
|
73
|
|
74
|
final TypeToken<HashMap<String, String>> tt = new TypeToken<HashMap<String, String>>() {};
|
75
|
|
76
|
try (InputStream is = mapResource.getInputStream(); InputStreamReader reader = new InputStreamReader(is)) {
|
77
|
return gson.fromJson(reader, tt.getType());
|
78
|
}
|
79
|
|
80
|
}
|
81
|
|
82
|
protected String patchId(final String xml, final Map<String, String> idMap) {
|
83
|
try {
|
84
|
|
85
|
final Document doc = DocumentHelper.parseText(xml);
|
86
|
|
87
|
final Element header = (Element) doc.selectSingleNode("//*[local-name() = 'header']");
|
88
|
for (final Object o : header.selectNodes("./*[local-name() = 'identifier']")) {
|
89
|
final Node n = (Node) o;
|
90
|
final String oldValue = n.getText().trim();
|
91
|
|
92
|
if (oldValue.startsWith(cinecaOaiPrefix)) {
|
93
|
final String peopleId = idMap.get(StringUtils.substringAfter(oldValue, cinecaOaiPrefix));
|
94
|
|
95
|
if (StringUtils.isNotBlank(peopleId)) {
|
96
|
final String newValue = peopleOaiPrefix + peopleId;
|
97
|
n.setText(newValue);
|
98
|
header.addElement(new QName("objIdentifier", DRI_NS)).setText(peopleNamespacePrefix + Hashing.md5(newValue));
|
99
|
header.addElement(new QName("hiddenIdentifier", ISTI_NS)).setText(oldValue);
|
100
|
log.debug("* MAPPED ID: " + oldValue + " -> " + newValue);
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
|
105
|
return doc.asXML();
|
106
|
} catch (final Exception e) {
|
107
|
throw new RuntimeException(e);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
public String getInputEprParam() {
|
112
|
return inputEprParam;
|
113
|
}
|
114
|
|
115
|
public void setInputEprParam(final String inputEprParam) {
|
116
|
this.inputEprParam = inputEprParam;
|
117
|
}
|
118
|
|
119
|
public String getOutputEprParam() {
|
120
|
return outputEprParam;
|
121
|
}
|
122
|
|
123
|
public void setOutputEprParam(final String outputEprParam) {
|
124
|
this.outputEprParam = outputEprParam;
|
125
|
}
|
126
|
|
127
|
public Resource getIdMapFile() {
|
128
|
return idMapFile;
|
129
|
}
|
130
|
|
131
|
@Required
|
132
|
public void setIdMapFile(final Resource idMapFile) {
|
133
|
this.idMapFile = idMapFile;
|
134
|
}
|
135
|
|
136
|
}
|