1
|
package eu.dnetlib.msro.workflows.nodes.transform;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.URLEncoder;
|
5
|
import java.nio.charset.Charset;
|
6
|
import java.util.List;
|
7
|
|
8
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
9
|
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
|
10
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
11
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
12
|
import eu.dnetlib.msro.workflows.procs.Env;
|
13
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
14
|
import eu.dnetlib.rmi.common.ResultSet;
|
15
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
16
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
17
|
import eu.dnetlib.rmi.manager.MSROException;
|
18
|
import org.antlr.stringtemplate.StringTemplate;
|
19
|
import org.apache.commons.io.IOUtils;
|
20
|
import org.apache.commons.logging.Log;
|
21
|
import org.apache.commons.logging.LogFactory;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
import org.springframework.beans.factory.annotation.Required;
|
24
|
import org.springframework.core.io.Resource;
|
25
|
|
26
|
public class MdBuilderJobNode extends SimpleJobNode {
|
27
|
|
28
|
private static final Log log = LogFactory.getLog(MdBuilderJobNode.class);
|
29
|
|
30
|
private Resource mdBuilderTemplateXslt;
|
31
|
|
32
|
private String inputEprParam;
|
33
|
private String outputEprParam;
|
34
|
private String datasourceId;
|
35
|
private String datasourceInterface;
|
36
|
private String datasourceName;
|
37
|
|
38
|
@Autowired
|
39
|
private ResultSetFactory resultSetFactory;
|
40
|
|
41
|
@javax.annotation.Resource
|
42
|
private UniqueServiceLocator serviceLocator;
|
43
|
|
44
|
@Override
|
45
|
protected String execute(final Env env) throws MSROException {
|
46
|
final ResultSet<?> rsIn = env.getAttribute(this.inputEprParam, ResultSet.class);
|
47
|
|
48
|
if (rsIn == null) { throw new MSROException("InputEprParam (" + this.inputEprParam + ") not found in ENV"); }
|
49
|
StringTemplate st = null;
|
50
|
try {
|
51
|
st = new StringTemplate(IOUtils.toString(getMdBuilderTemplateXslt().getInputStream(), Charset.forName("UTF-8")));
|
52
|
st.setAttribute("datasourceId", this.datasourceId);
|
53
|
st.setAttribute("datasourceName", this.datasourceName);
|
54
|
st.setAttribute("datasourceApi", this.datasourceInterface);
|
55
|
st.setAttribute("xpath", getMetadataIdentifierPath().replace("\"", "'"));
|
56
|
st.setAttribute("baseurl", URLEncoder.encode(getBaseUrl(), "UTF-8"));
|
57
|
st.setAttribute("metadatanamespace", getMetadataNamespace());
|
58
|
List<String> namespacePrefix;
|
59
|
String xQuery = "/*[.//RESOURCE_IDENTIFIER/@value='" + this.datasourceId + "']//EXTRA_FIELDS/FIELD/value[../key='NamespacePrefix']/string()";
|
60
|
namespacePrefix = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xQuery);
|
61
|
if (namespacePrefix != null && namespacePrefix.size() != 0) {
|
62
|
st.setAttribute("namespacePrefix", namespacePrefix.get(0));
|
63
|
}
|
64
|
final ResultSet<String> rsOut = this.resultSetFactory.xsltMap(rsIn, st.toString());
|
65
|
|
66
|
env.setAttribute(this.outputEprParam, rsOut);
|
67
|
|
68
|
if (datasourceInterface != null) {
|
69
|
env.setAttribute(WorkflowsConstants.LOG_DATASOURCE_INTERFACE, this.datasourceInterface);
|
70
|
}
|
71
|
|
72
|
return Arc.DEFAULT_ARC;
|
73
|
} catch (final ISLookUpException e) {
|
74
|
throw new MSROException("Error while initializing mdBuilder template (" + getMdBuilderTemplateXslt().getFilename() + ") for datasource "
|
75
|
+ this.datasourceId, e);
|
76
|
} catch (final IOException e) {
|
77
|
throw new MSROException("Error parsing template: " + getMdBuilderTemplateXslt().getFilename(), e);
|
78
|
} catch (final Exception e) {
|
79
|
log.error(st.toString());
|
80
|
throw new MSROException(e);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
private String getBaseUrl() throws ISLookUpException {
|
85
|
String xQuery = "/*[.//RESOURCE_IDENTIFIER/@value='{datasourceId}']//INTERFACE[@id='{interfaceId}']//BASE_URL/string()";
|
86
|
xQuery = xQuery.replace("{interfaceId}", this.datasourceInterface).replace("{datasourceId}", this.datasourceId);
|
87
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xQuery);
|
88
|
}
|
89
|
|
90
|
private String getMetadataIdentifierPath() throws ISLookUpException {
|
91
|
String xQuery = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') "
|
92
|
+ "return $x//INTERFACE[@id='{interfaceId}']/INTERFACE_EXTRA_FIELD[@name='metadata_identifier_path']/string()";
|
93
|
xQuery = xQuery.replace("{interfaceId}", this.datasourceInterface);
|
94
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xQuery);
|
95
|
}
|
96
|
|
97
|
private String getMetadataNamespace() {
|
98
|
try {
|
99
|
String xQuery = "let $x := /*[.//RESOURCE_IDENTIFIER/@value='{datasourceId}']//INTERFACE[@id='{interfaceId}']/ACCESS_PROTOCOL/@format/string() "
|
100
|
+ "return /*[.//RESOURCE_TYPE/@value='MetadataFormatDSResourceType']//METADATAFORMAT[@Prefix=$x]/@NameSpace/string()";
|
101
|
xQuery = xQuery.replace("{interfaceId}", this.datasourceInterface).replace("{datasourceId}", this.datasourceId);
|
102
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xQuery);
|
103
|
} catch (final ISLookUpException e) {
|
104
|
log.error("The interface is not OAI or the format is not found in the MetadataFormatDSResourceType, thus metadata format in the <about> section "
|
105
|
+ "cannot be managed here and it will be left incomplete (for the time being)");
|
106
|
return "";
|
107
|
}
|
108
|
}
|
109
|
|
110
|
public String getInputEprParam() {
|
111
|
return this.inputEprParam;
|
112
|
}
|
113
|
|
114
|
public void setInputEprParam(final String inputEprParam) {
|
115
|
this.inputEprParam = inputEprParam;
|
116
|
}
|
117
|
|
118
|
public String getOutputEprParam() {
|
119
|
return this.outputEprParam;
|
120
|
}
|
121
|
|
122
|
public void setOutputEprParam(final String outputEprParam) {
|
123
|
this.outputEprParam = outputEprParam;
|
124
|
}
|
125
|
|
126
|
public String getDatasourceId() {
|
127
|
return this.datasourceId;
|
128
|
}
|
129
|
|
130
|
public void setDatasourceId(final String datasourceId) {
|
131
|
this.datasourceId = datasourceId;
|
132
|
}
|
133
|
|
134
|
public String getDatasourceInterface() {
|
135
|
return this.datasourceInterface;
|
136
|
}
|
137
|
|
138
|
public void setDatasourceInterface(final String datasourceInterface) {
|
139
|
this.datasourceInterface = datasourceInterface;
|
140
|
}
|
141
|
|
142
|
public String getDatasourceName() {
|
143
|
return datasourceName;
|
144
|
}
|
145
|
|
146
|
public void setDatasourceName(final String datasourceName) {
|
147
|
this.datasourceName = datasourceName;
|
148
|
}
|
149
|
|
150
|
public Resource getMdBuilderTemplateXslt() {
|
151
|
return this.mdBuilderTemplateXslt;
|
152
|
}
|
153
|
|
154
|
@Required
|
155
|
public void setMdBuilderTemplateXslt(final Resource mdBuilderTemplateXslt) {
|
156
|
this.mdBuilderTemplateXslt = mdBuilderTemplateXslt;
|
157
|
}
|
158
|
|
159
|
}
|