Project

General

Profile

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

    
3
import com.google.common.base.Splitter;
4
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
5
import eu.dnetlib.msro.workflows.graph.Arc;
6
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
7
import eu.dnetlib.msro.workflows.procs.Env;
8
import eu.dnetlib.msro.workflows.procs.ProcessAware;
9
import eu.dnetlib.msro.workflows.procs.WorkflowProcess;
10
import eu.dnetlib.rmi.datasource.DatasourceConstants;
11
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
12
import eu.dnetlib.rmi.enabling.ISLookUpException;
13
import eu.dnetlib.rmi.enabling.ISLookUpService;
14
import eu.dnetlib.rmi.manager.MSROException;
15
import org.springframework.beans.factory.annotation.Autowired;
16

    
17
public class VerifyDatasourceJobNode extends SimpleJobNode implements ProcessAware {
18

    
19
	@Autowired
20
	private UniqueServiceLocator serviceLocator;
21

    
22
	private String expectedInterfaceTypologyPrefixes;
23

    
24
	private String expectedCompliancePrefixes;
25

    
26
	private WorkflowProcess process;
27

    
28
	@Override
29
	protected String execute(final Env env) throws Exception {
30
		final String dsId = this.process.getDsId();
31
		final String ifaceId = this.process.getDsInterface();
32
		final ISLookUpService lookupService = this.serviceLocator.getService(ISLookUpService.class);
33

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

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

    
47
		verifyValue(compliance, this.expectedCompliancePrefixes);
48
		verifyValue(typology, this.expectedInterfaceTypologyPrefixes);
49

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

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

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

    
72
	public String getExpectedInterfaceTypologyPrefixes() {
73
		return this.expectedInterfaceTypologyPrefixes;
74
	}
75

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

    
80
	public String getExpectedCompliancePrefixes() {
81
		return this.expectedCompliancePrefixes;
82
	}
83

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

    
88
	public WorkflowProcess getProcess() {
89
		return this.process;
90
	}
91

    
92
	@Override
93
	public void setProcess(final WorkflowProcess process) {
94
		this.process = process;
95
	}
96

    
97
}
(6-6/6)