Project

General

Profile

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

    
3
import org.springframework.beans.factory.annotation.Autowired;
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.datasources.common.Api;
10
import eu.dnetlib.enabling.datasources.common.Datasource;
11
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
12
import eu.dnetlib.msro.rmi.MSROException;
13
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
14
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
15

    
16
public class VerifyDatasourceJobNode extends SimpleJobNode {
17

    
18
	@Autowired
19
	private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
20

    
21
	private String expectedInterfaceTypologyPrefixes;
22

    
23
	private String expectedCompliancePrefixes;
24

    
25
	@Override
26
	protected String execute(final NodeToken token) throws Exception {
27
		final String dsId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ID);
28
		final String ifaceId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
29

    
30
		final Api<?> api = dsManager.getApis(dsId)
31
			.stream()
32
			.filter(a -> a.getId().equals(ifaceId))
33
			.findFirst()
34
			.orElseThrow(() -> new MSROException("Intervace not found: " + ifaceId));;
35

    
36
		final String compliance = api.getCompatibility();
37
		final String typology = api.getTypology();
38

    
39
		verifyValue(compliance, expectedCompliancePrefixes);
40
		verifyValue(typology, expectedInterfaceTypologyPrefixes);
41
		token.getFullEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE_COMPLIANCE, compliance);
42

    
43
		return Arc.DEFAULT_ARC;
44
	}
45

    
46
	private void verifyValue(final String value, final String expected) throws Exception {
47
		if (expected != null && !expected.isEmpty()) {
48
			for (final String s : Splitter.on(",").omitEmptyStrings().trimResults().split(expected)) {
49
				if (value.toLowerCase().startsWith(s.toLowerCase())) { return; }
50
			}
51
			throw new MSROException("Invalid value: " + value + ", Valid term prefixes are: [" + expected + "]");
52
		}
53
	}
54

    
55
	public String getExpectedInterfaceTypologyPrefixes() {
56
		return expectedInterfaceTypologyPrefixes;
57
	}
58

    
59
	public void setExpectedInterfaceTypologyPrefixes(final String expectedInterfaceTypologyPrefixes) {
60
		this.expectedInterfaceTypologyPrefixes = expectedInterfaceTypologyPrefixes;
61
	}
62

    
63
	public String getExpectedCompliancePrefixes() {
64
		return expectedCompliancePrefixes;
65
	}
66

    
67
	public void setExpectedCompliancePrefixes(final String expectedCompliancePrefixes) {
68
		this.expectedCompliancePrefixes = expectedCompliancePrefixes;
69
	}
70

    
71
}
(7-7/7)