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