Project

General

Profile

1
package eu.dnetlib.msro.workflows.nodes.functions;
2

    
3
import java.net.URLEncoder;
4
import java.util.function.Function;
5

    
6
import org.antlr.stringtemplate.StringTemplate;
7
import org.apache.commons.io.IOUtils;
8
import org.apache.commons.lang3.StringUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.context.annotation.Scope;
14
import org.springframework.core.io.Resource;
15
import org.springframework.stereotype.Component;
16

    
17
import eu.dnetlib.clients.is.InformationServiceClient;
18
import eu.dnetlib.exceptions.InformationServiceException;
19
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
20
import eu.dnetlib.msro.annotations.ProcessNode;
21
import eu.dnetlib.msro.exceptions.MSROException;
22
import eu.dnetlib.msro.workflows.nodes.SimpleParallelProcessNode;
23

    
24
@Component
25
@Scope("prototype")
26
@ProcessNode("MdBuilder")
27
public class MdBuilderJobNode extends AbstractApplyFunctionJobNode<String, String> {
28

    
29
	private String datasourceId;
30
	private String datasourceInterface;
31

    
32
	@Value("${msro.worker.mdstore.mdbuilder.xslt.template}")
33
	private Resource mdBuilderTemplateXslt;
34

    
35
	@Autowired
36
	private InformationServiceClient isClient;
37

    
38
	private static final Log log = LogFactory.getLog(SimpleParallelProcessNode.class);
39

    
40
	@Override
41
	protected Function<String, String> getFunction() throws MSROException {
42

    
43
		if (StringUtils.isBlank(datasourceId)) { throw new MSROException("Missing datasourceId"); }
44
		if (StringUtils.isBlank(datasourceInterface)) { throw new MSROException("Missing datasourceInterface"); }
45

    
46
		final String xq = "for $x in doc('/db/DRIVER/" + datasourceId + "') "
47
				+ "return concat("
48
				+ "$x//BASE_URL, ' @@@ ', "
49
				+ "$x//EXTRA_FIELDS/FIELD/value[../key='NamespacePrefix'], ' @@@ ', "
50
				+ "$x//INTERFACE[@id='" + datasourceInterface + "']/INTERFACE_EXTRA_FIELD[@name='metadata_identifier_path'], ' @@@ ', "
51
				+ "$x//INTERFACE[@id='" + datasourceInterface + "']/ACCESS_PROTOCOL/@format)";
52
		try {
53
			final String[] arr = isClient.findOne(xq).split("@@@");
54

    
55
			final StringTemplate st = new StringTemplate(IOUtils.toString(mdBuilderTemplateXslt.getInputStream()));
56
			st.setAttribute("datasourceId", datasourceId);
57
			st.setAttribute("baseurl", URLEncoder.encode(arr[0].trim(), "UTF-8"));
58
			st.setAttribute("namespacePrefix", arr[1].trim());
59
			st.setAttribute("xpath", arr[2].trim());
60
			st.setAttribute("metadatanamespace", getMetadataNamespace(arr[3].trim()));
61

    
62
			return new ApplyXslt(st.toString());
63
		} catch (final Exception e) {
64
			log.error("Error preparing MDBuilder function", e);
65
			throw new MSROException("Error preparing MDBuilder function", e);
66
		}
67
	}
68

    
69
	private String getMetadataNamespace(final String format) {
70
		final String xQuery = "collection('/db/DRIVER/conf/mdFormats')//METADATAFORMAT[@name='" + format + "' or @prefix='" + format + "']/@namespace/string()";
71
		try {
72
			return isClient.findOne(xQuery);
73
		} catch (final InformationServiceException e) {
74
			log.warn("Format " + format + " not registered on IS");
75
			return "";
76
		}
77

    
78
	}
79

    
80
	public String getDatasourceId() {
81
		return datasourceId;
82
	}
83

    
84
	public void setDatasourceId(final String datasourceId) {
85
		this.datasourceId = datasourceId;
86
	}
87

    
88
	public String getDatasourceInterface() {
89
		return datasourceInterface;
90
	}
91

    
92
	public void setDatasourceInterface(final String datasourceInterface) {
93
		this.datasourceInterface = datasourceInterface;
94
	}
95

    
96
}
(4-4/5)