Project

General

Profile

1 26600 sandro.lab
package eu.dnetlib.msro.workflows.nodes.repohi;
2
3
import javax.annotation.Resource;
4
5
import com.google.common.base.Splitter;
6
import com.googlecode.sarasvati.Arc;
7
import com.googlecode.sarasvati.NodeToken;
8
9 33834 michele.ar
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
10 26600 sandro.lab
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
12
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
13 32639 michele.ar
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14 26600 sandro.lab
import eu.dnetlib.msro.rmi.MSROException;
15
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
16
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
17
18
public class VerifyDatasourceJobNode extends SimpleJobNode {
19
20 32639 michele.ar
	@Resource
21
	private UniqueServiceLocator serviceLocator;
22 26600 sandro.lab
23
	private String expectedInterfaceTypologyPrefixes;
24
25
	private String expectedCompliancePrefixes;
26
27
	@Override
28
	protected String execute(final NodeToken token) throws Exception {
29
		final String dsId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ID);
30
		final String ifaceId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
31 32639 michele.ar
		final ISLookUpService lookupService = serviceLocator.getService(ISLookUpService.class);
32 26600 sandro.lab
33 29884 michele.ar
		String compliance;
34
		try {
35 32639 michele.ar
			compliance = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
36
					+ "']/INTERFACE_EXTRA_FIELD[@name='"
37 33834 michele.ar
					+ DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD + "']/text()");
38 32639 michele.ar
		} catch (ISLookUpDocumentNotFoundException e) {
39
			compliance = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
40
					+ "']/@compliance/string()");
41 29884 michele.ar
		}
42 26600 sandro.lab
43 32639 michele.ar
		final String typology = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
44
				+ "']/@typology/string()");
45
46 29884 michele.ar
		verifyValue(compliance, expectedCompliancePrefixes);
47 32639 michele.ar
		verifyValue(typology, expectedInterfaceTypologyPrefixes);
48 29884 michele.ar
49 32639 michele.ar
		if (isPending(dsId)) {
50
			return "validateDs";
51
		} else {
52
			return Arc.DEFAULT_ARC;
53
		}
54 26600 sandro.lab
	}
55
56 29884 michele.ar
	private void verifyValue(final String value, final String expected) throws Exception {
57 32639 michele.ar
		if (expected != null && !expected.isEmpty()) {
58 26600 sandro.lab
			for (String s : Splitter.on(",").omitEmptyStrings().trimResults().split(expected)) {
59 32639 michele.ar
				if (value.toLowerCase().startsWith(s.toLowerCase())) { return; }
60 26600 sandro.lab
			}
61
			throw new MSROException("Invalid value: " + value + ", Valid term prefixes are: [" + expected + "]");
62
		}
63
	}
64
65
	private boolean isPending(final String id) throws ISLookUpDocumentNotFoundException, ISLookUpException {
66
		final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//RESOURCE_KIND/@value/string()";
67 32639 michele.ar
		final String res = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
68 26600 sandro.lab
		return res.trim().equals("PendingRepositoryResources");
69
	}
70
71
	public String getExpectedInterfaceTypologyPrefixes() {
72
		return expectedInterfaceTypologyPrefixes;
73
	}
74
75
	public void setExpectedInterfaceTypologyPrefixes(final String expectedInterfaceTypologyPrefixes) {
76
		this.expectedInterfaceTypologyPrefixes = expectedInterfaceTypologyPrefixes;
77
	}
78
79
	public String getExpectedCompliancePrefixes() {
80
		return expectedCompliancePrefixes;
81
	}
82
83
	public void setExpectedCompliancePrefixes(final String expectedCompliancePrefixes) {
84
		this.expectedCompliancePrefixes = expectedCompliancePrefixes;
85
	}
86
87
}