|
1 |
package eu.dnetlib.msro.puma.nodes;
|
|
2 |
|
|
3 |
/**
|
|
4 |
* Created by sandro on 2/3/16.
|
|
5 |
*/
|
|
6 |
|
|
7 |
import java.util.List;
|
|
8 |
import javax.annotation.Resource;
|
|
9 |
|
|
10 |
import com.google.common.collect.Iterables;
|
|
11 |
import com.googlecode.sarasvati.NodeToken;
|
|
12 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
|
13 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
|
14 |
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
|
15 |
import org.apache.commons.logging.Log;
|
|
16 |
import org.apache.commons.logging.LogFactory;
|
|
17 |
|
|
18 |
public class FindIndexJobNode extends SimpleJobNode {
|
|
19 |
|
|
20 |
/**
|
|
21 |
* logger.
|
|
22 |
*/
|
|
23 |
private static final Log log = LogFactory.getLog(FindIndexJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
|
|
24 |
|
|
25 |
@Resource
|
|
26 |
private UniqueServiceLocator serviceLocator;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* if non null, overrides format from env.
|
|
30 |
*/
|
|
31 |
private String mdFormat;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* if non null, overrides format from env.
|
|
35 |
*/
|
|
36 |
private String layout;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* if non null, overrides format from env.
|
|
40 |
*/
|
|
41 |
private String interpretation;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* {@inheritDoc}
|
|
45 |
*
|
|
46 |
* @see com.googlecode.sarasvati.mem.MemNode#execute(com.googlecode.sarasvati.Engine, NodeToken)
|
|
47 |
*/
|
|
48 |
@Override
|
|
49 |
public String execute(final NodeToken token) {
|
|
50 |
final String envFormat = token.getFullEnv().getAttribute("format");
|
|
51 |
final String envLayout = token.getFullEnv().getAttribute("layout");
|
|
52 |
final String envInterpretation = token.getFullEnv().getAttribute("interpretation");
|
|
53 |
|
|
54 |
final String format = handleOverride(token, "format", envFormat, getMdFormat());
|
|
55 |
final String layout = handleOverride(token, "layout", envLayout, getLayout());
|
|
56 |
final String interp = handleOverride(token, "interpretation", envInterpretation, getInterpretation());
|
|
57 |
|
|
58 |
String mdRef = format + "-" + layout + "-" + interp;
|
|
59 |
log.info("searching index for [" + mdRef + "]");
|
|
60 |
|
|
61 |
final String indexId = findIndex(format, layout, interp);
|
|
62 |
token.getEnv().setAttribute("index_id", indexId);
|
|
63 |
|
|
64 |
if (indexId == null || indexId.isEmpty()) {
|
|
65 |
log.info("no index was found for [" + mdRef + "]");
|
|
66 |
return "notFound";
|
|
67 |
} else {
|
|
68 |
log.info("index found for [" + mdRef + "]: " + indexId);
|
|
69 |
return "found";
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
private String findIndex(final String format, final String layout, final String interp) {
|
|
74 |
final String xquery =
|
|
75 |
"for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType']" + "return $x[.//METADATA_FORMAT = '" + format
|
|
76 |
+ "' and .//METADATA_FORMAT_LAYOUT='" + layout + "' and .//METADATA_FORMAT_INTERPRETATION = '" + interp
|
|
77 |
+ "']//RESOURCE_IDENTIFIER/@value/string()";
|
|
78 |
try {
|
|
79 |
log.info("xquery: " + xquery);
|
|
80 |
List<String> ids = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
|
81 |
log.info("found indexDS ids: " + ids);
|
|
82 |
if (ids == null || ids.isEmpty()) { return null; }
|
|
83 |
if (ids.size() > 1) {
|
|
84 |
throw new IllegalStateException("found more than one index of given format: " + format + ", layout: " + layout
|
|
85 |
+ ", interpretation: " + interp);
|
|
86 |
}
|
|
87 |
return Iterables.getOnlyElement(ids);
|
|
88 |
} catch (Exception e) {
|
|
89 |
return null;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
private String handleOverride(final NodeToken token, final String name, final String env, final String override) {
|
|
94 |
if (override != null) {
|
|
95 |
token.getEnv().setAttribute(name, override);
|
|
96 |
}
|
|
97 |
return override != null ? override : env;
|
|
98 |
}
|
|
99 |
|
|
100 |
public String getMdFormat() {
|
|
101 |
return mdFormat;
|
|
102 |
}
|
|
103 |
|
|
104 |
public void setMdFormat(final String mdFormat) {
|
|
105 |
this.mdFormat = mdFormat;
|
|
106 |
}
|
|
107 |
|
|
108 |
public String getLayout() {
|
|
109 |
return layout;
|
|
110 |
}
|
|
111 |
|
|
112 |
public void setLayout(final String layout) {
|
|
113 |
this.layout = layout;
|
|
114 |
}
|
|
115 |
|
|
116 |
public String getInterpretation() {
|
|
117 |
return interpretation;
|
|
118 |
}
|
|
119 |
|
|
120 |
public void setInterpretation(final String interpretation) {
|
|
121 |
this.interpretation = interpretation;
|
|
122 |
}
|
|
123 |
|
|
124 |
}
|
Added FindIndex Node