Project

General

Profile

1
package eu.dnetlib.msro.workflows.hadoop;
2

    
3
import java.util.List;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9

    
10
import com.google.common.collect.Iterables;
11
import com.googlecode.sarasvati.NodeToken;
12

    
13
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
14
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
15
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
16

    
17
public class FindIndexJobNode extends SimpleJobNode {
18

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

    
24
	@Resource
25
	private UniqueServiceLocator serviceLocator;
26

    
27
	/**
28
	 * if non null, overrides format from env.
29
	 */
30
	private String mdFormat;
31

    
32
	/**
33
	 * if non null, overrides format from env.
34
	 */
35
	private String layout;
36

    
37
	/**
38
	 * if non null, overrides format from env.
39
	 */
40
	private String interpretation;
41

    
42
	/**
43
	 * {@inheritDoc}
44
	 * 
45
	 * @see com.googlecode.sarasvati.mem.MemNode#execute(com.googlecode.sarasvati.Engine, com.googlecode.sarasvati.NodeToken)
46
	 */
47
	@Override
48
	public String execute(final NodeToken token) {
49
		final String envFormat = token.getFullEnv().getAttribute("format");
50
		final String envLayout = token.getFullEnv().getAttribute("layout");
51
		final String envInterpretation = token.getFullEnv().getAttribute("interpretation");
52

    
53
		final String format = handleOverride(token, "format", envFormat, getMdFormat());
54
		final String layout = handleOverride(token, "layout", envLayout, getLayout());
55
		final String interp = handleOverride(token, "interpretation", envInterpretation, getInterpretation());
56

    
57
		String mdRef = format + "-" + layout + "-" + interp;
58
		log.info("searching index for [" + mdRef + "]");
59

    
60
		final String indexId = findIndex(format, layout, interp);
61
		token.getEnv().setAttribute("index_id", indexId);
62

    
63
		if (indexId == null || indexId.isEmpty()) {
64
			log.info("no index was found for [" + mdRef + "]");
65
			return "notFound";
66
		} else {
67
			log.info("index found for [" + mdRef + "]: " + indexId);
68
			return "found";
69
		}
70
	}
71

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

    
90
	private String handleOverride(final NodeToken token, final String name, final String env, final String override) {
91
		if (override != null) {
92
			token.getEnv().setAttribute(name, override);
93
		}
94
		return override != null ? override : env;
95
	}
96

    
97
	public String getMdFormat() {
98
		return mdFormat;
99
	}
100

    
101
	public void setMdFormat(final String mdFormat) {
102
		this.mdFormat = mdFormat;
103
	}
104

    
105
	public String getLayout() {
106
		return layout;
107
	}
108

    
109
	public void setLayout(final String layout) {
110
		this.layout = layout;
111
	}
112

    
113
	public String getInterpretation() {
114
		return interpretation;
115
	}
116

    
117
	public void setInterpretation(final String interpretation) {
118
		this.interpretation = interpretation;
119
	}
120

    
121
}
(4-4/12)