Project

General

Profile

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

    
3
import java.util.List;
4
import javax.annotation.Resource;
5

    
6
import com.google.common.collect.Iterables;
7
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
8
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
9
import eu.dnetlib.msro.workflows.procs.Env;
10
import eu.dnetlib.rmi.enabling.ISLookUpService;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13

    
14
public class FindIndexJobNode extends SimpleJobNode {
15

    
16
	/**
17
	 * logger.
18
	 */
19
	private static final Log log = LogFactory.getLog(FindIndexJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
20

    
21
	@Resource
22
	private UniqueServiceLocator serviceLocator;
23

    
24
	/**
25
	 * if non null, overrides format from env.
26
	 */
27
	private String mdFormat;
28

    
29
	/**
30
	 * if non null, overrides format from env.
31
	 */
32
	private String layout;
33

    
34
	/**
35
	 * if non null, overrides format from env.
36
	 */
37
	private String interpretation;
38

    
39
	/**
40
	 * {@inheritDoc}
41
	 */
42
	@Override
43
	public String execute(final Env env) {
44
		final String envFormat = env.getAttribute("format", String.class);
45
		final String envLayout = env.getAttribute("layout", String.class);
46
		final String envInterpretation = env.getAttribute("interpretation", String.class);
47

    
48
		final String format = handleOverride(env, "format", envFormat, getMdFormat());
49
		final String layout = handleOverride(env, "layout", envLayout, getLayout());
50
		final String interp = handleOverride(env, "interpretation", envInterpretation, getInterpretation());
51

    
52
		String mdRef = format + "-" + layout + "-" + interp;
53
		log.info("searching index for [" + mdRef + "]");
54

    
55
		final String indexId = findIndex(format, layout, interp);
56
		env.setAttribute("index_id", indexId);
57

    
58
		if (indexId == null || indexId.isEmpty()) {
59
			log.info("no index was found for [" + mdRef + "]");
60
			return "notFound";
61
		} else {
62
			log.info("index found for [" + mdRef + "]: " + indexId);
63
			return "found";
64
		}
65
	}
66

    
67
	private String findIndex(final String format, final String layout, final String interp) {
68
		final String xquery =
69
				"for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType']" + "return $x[.//METADATA_FORMAT = '" + format
70
						+ "' and .//METADATA_FORMAT_LAYOUT='" + layout + "' and .//METADATA_FORMAT_INTERPRETATION = '" + interp
71
						+ "']//RESOURCE_IDENTIFIER/@value/string()";
72
		try {
73
			log.info("xquery: " + xquery);
74
			List<String> ids = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
75
			log.info("found indexDS ids: " + ids);
76
			if (ids == null || ids.isEmpty()) { return null; }
77
			if (ids.size() > 1) {
78
				throw new IllegalStateException("found more than one index of given format: " + format + ", layout: " + layout
79
						+ ", interpretation: " + interp);
80
			}
81
			return Iterables.getOnlyElement(ids);
82
		} catch (Exception e) {
83
			return null;
84
		}
85
	}
86

    
87
	private String handleOverride(final Env env, final String name, final String envParam, final String override) {
88
		if (override != null) {
89
			env.setAttribute(name, override);
90
		}
91
		return override != null ? override : envParam;
92
	}
93

    
94
	public String getMdFormat() {
95
		return mdFormat;
96
	}
97

    
98
	public void setMdFormat(final String mdFormat) {
99
		this.mdFormat = mdFormat;
100
	}
101

    
102
	public String getLayout() {
103
		return layout;
104
	}
105

    
106
	public void setLayout(final String layout) {
107
		this.layout = layout;
108
	}
109

    
110
	public String getInterpretation() {
111
		return interpretation;
112
	}
113

    
114
	public void setInterpretation(final String interpretation) {
115
		this.interpretation = interpretation;
116
	}
117

    
118
}
    (1-1/1)