Project

General

Profile

1
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
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
9
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
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.msro.rmi.MSROException;
14
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
15
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
16

    
17
public class VerifyDatasourceJobNode extends SimpleJobNode {
18

    
19
	@Resource
20
	private UniqueServiceLocator serviceLocator;
21

    
22
	private String expectedInterfaceTypologyPrefixes;
23

    
24
	private String expectedCompliancePrefixes;
25

    
26
	@Override
27
	protected String execute(final NodeToken token) throws Exception {
28
		final String dsId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ID);
29
		final String ifaceId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
30
		final ISLookUpService lookupService = serviceLocator.getService(ISLookUpService.class);
31

    
32
		String compliance;
33
		try {
34
			compliance = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
35
					+ "']/INTERFACE_EXTRA_FIELD[@name='"
36
					+ DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD + "']/text()");
37
		} catch (ISLookUpDocumentNotFoundException e) {
38
			compliance = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
39
					+ "']/@compliance/string()");
40
		}
41

    
42
		final String typology = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
43
				+ "']/@typology/string()");
44

    
45
		verifyValue(compliance, expectedCompliancePrefixes);
46
		verifyValue(typology, expectedInterfaceTypologyPrefixes);
47
		token.getFullEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE_COMPLIANCE, compliance);
48

    
49
		if (isPending(dsId)) {
50
			return "validateDs";
51
		} else {
52
			return Arc.DEFAULT_ARC;
53
		}
54
	}
55

    
56
	private void verifyValue(final String value, final String expected) throws Exception {
57
		if (expected != null && !expected.isEmpty()) {
58
			for (String s : Splitter.on(",").omitEmptyStrings().trimResults().split(expected)) {
59
				if (value.toLowerCase().startsWith(s.toLowerCase())) { return; }
60
			}
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
		final String res = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
68
		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
}
(7-7/7)