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

    
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='overriding_compliance']/text()");
36
		} catch (final ISLookUpDocumentNotFoundException e) {
37
			compliance = lookupService.getResourceProfileByQuery("/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//INTERFACE[@id = '" + ifaceId
38
					+ "']/@compliance/string()");
39
		}
40

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

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

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

    
55
	private void verifyValue(final String value, final String expected) throws Exception {
56
		if (expected != null && !expected.isEmpty()) {
57
			for (final String s : Splitter.on(",").omitEmptyStrings().trimResults().split(expected)) {
58
				if (value.toLowerCase().startsWith(s.toLowerCase())) { return; }
59
			}
60
			throw new MSROException("Invalid value: " + value + ", Valid term prefixes are: [" + expected + "]");
61
		}
62
	}
63

    
64
	private boolean isPending(final String id) throws ISLookUpDocumentNotFoundException, ISLookUpException {
65
		final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//RESOURCE_KIND/@value/string()";
66
		final String res = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
67
		return res.trim().equals("PendingRepositoryResources");
68
	}
69

    
70
	public String getExpectedInterfaceTypologyPrefixes() {
71
		return expectedInterfaceTypologyPrefixes;
72
	}
73

    
74
	public void setExpectedInterfaceTypologyPrefixes(final String expectedInterfaceTypologyPrefixes) {
75
		this.expectedInterfaceTypologyPrefixes = expectedInterfaceTypologyPrefixes;
76
	}
77

    
78
	public String getExpectedCompliancePrefixes() {
79
		return expectedCompliancePrefixes;
80
	}
81

    
82
	public void setExpectedCompliancePrefixes(final String expectedCompliancePrefixes) {
83
		this.expectedCompliancePrefixes = expectedCompliancePrefixes;
84
	}
85

    
86
}
(7-7/7)