1
|
package eu.dnetlib.msro.workflows.nodes.transform;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
|
12
|
import com.google.common.collect.Maps;
|
13
|
|
14
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
15
|
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
|
16
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
17
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
18
|
import eu.dnetlib.msro.workflows.procs.Env;
|
19
|
import eu.dnetlib.rmi.common.ResultSet;
|
20
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
21
|
import eu.dnetlib.rmi.manager.MSROException;
|
22
|
import groovy.lang.GroovyShell;
|
23
|
import groovy.util.GroovyScriptEngine;
|
24
|
|
25
|
public class GroovyJobNode extends SimpleJobNode {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(GroovyJobNode.class);
|
28
|
|
29
|
/**
|
30
|
* used to transform the records using Groovy.
|
31
|
*/
|
32
|
@Autowired
|
33
|
private ResultSetFactory resultSetFactory;
|
34
|
|
35
|
private String inputEprParam;
|
36
|
private String outputEprParam;
|
37
|
private String transformationRuleId;
|
38
|
private Map<String, String> groovyParams;
|
39
|
|
40
|
@Resource
|
41
|
private UniqueServiceLocator serviceLocator;
|
42
|
|
43
|
private Map<String, String> retrieveGroovyParameter() {
|
44
|
final Map<String, String> out = Maps.newHashMap();
|
45
|
|
46
|
final String query = "for $x in collection('/db/DRIVER/GroovyProcessingDSResource/GroovyProcessingDSResourceType')"
|
47
|
+ "where $x[.//RESOURCE_IDENTIFIER/@value='" + this.transformationRuleId + "']"
|
48
|
+ "return concat($x//GROOVY_CLASSPATH/text(),':::',$x//GROOVY_DNETCLASS/text())";
|
49
|
try {
|
50
|
final String result = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query).get(0);
|
51
|
if (result == null) { return null; }
|
52
|
final String[] data = result.trim().split(":::");
|
53
|
if (data.length == 2) {
|
54
|
out.put("classpath", data[0]);
|
55
|
out.put("mainClass", data[1]);
|
56
|
}
|
57
|
|
58
|
return out;
|
59
|
} catch (final Exception e) {
|
60
|
log.error(e);
|
61
|
return null;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
protected String execute(final Env env) throws Exception {
|
67
|
final ResultSet<?> rsIn = env.getAttribute(this.inputEprParam, ResultSet.class);
|
68
|
if (rsIn == null) { throw new MSROException("InputEprParam (" + this.inputEprParam + ") not found in ENV"); }
|
69
|
|
70
|
String groovyClasspath, groovyDnetClass;
|
71
|
final Map<String, String> prop = retrieveGroovyParameter();
|
72
|
groovyClasspath = prop.get("classpath");
|
73
|
groovyDnetClass = prop.get("mainClass");
|
74
|
|
75
|
final ResultSet<?> rsOut = transformGroovy(rsIn, groovyClasspath, groovyDnetClass, this.groovyParams);
|
76
|
env.setAttribute(this.outputEprParam, rsOut);
|
77
|
|
78
|
return Arc.DEFAULT_ARC;
|
79
|
}
|
80
|
|
81
|
private ResultSet<?> transformGroovy(final ResultSet<?> source,
|
82
|
final String groovyClasspath,
|
83
|
final String groovyDnetClass,
|
84
|
final Map<String, String> params) throws ClassNotFoundException, IOException {
|
85
|
|
86
|
final GroovyScriptEngine gse = new GroovyScriptEngine(groovyClasspath);
|
87
|
gse.getGroovyClassLoader().loadClass(groovyDnetClass);
|
88
|
log.info("***********************************************");
|
89
|
log.info("Loaded Groovy classes:");
|
90
|
for (final Class<?> c : gse.getGroovyClassLoader().getLoadedClasses()) {
|
91
|
log.info(c.getCanonicalName());
|
92
|
}
|
93
|
log.info("***********************************************");
|
94
|
final GroovyShell groovyShell = new GroovyShell(gse.getGroovyClassLoader());
|
95
|
|
96
|
final Object go = groovyShell.evaluate("new " + groovyDnetClass + "()");
|
97
|
if (go instanceof GroovyUnaryFunction) {
|
98
|
final GroovyUnaryFunction groovyUnaryFunction = (GroovyUnaryFunction) go;
|
99
|
if (params != null) {
|
100
|
groovyUnaryFunction.setParams(params);
|
101
|
}
|
102
|
return this.resultSetFactory.map(source, String.class, groovyUnaryFunction);
|
103
|
} else {
|
104
|
throw new RuntimeException("Groovy object " + go + " is not supported");
|
105
|
}
|
106
|
}
|
107
|
|
108
|
public String getInputEprParam() {
|
109
|
return this.inputEprParam;
|
110
|
}
|
111
|
|
112
|
public void setInputEprParam(final String inputEprParam) {
|
113
|
this.inputEprParam = inputEprParam;
|
114
|
}
|
115
|
|
116
|
public String getOutputEprParam() {
|
117
|
return this.outputEprParam;
|
118
|
}
|
119
|
|
120
|
public void setOutputEprParam(final String outputEprParam) {
|
121
|
this.outputEprParam = outputEprParam;
|
122
|
}
|
123
|
|
124
|
public String getTransformationRuleId() {
|
125
|
return this.transformationRuleId;
|
126
|
}
|
127
|
|
128
|
public void setTransformationRuleId(final String transformationRuleId) {
|
129
|
this.transformationRuleId = transformationRuleId;
|
130
|
}
|
131
|
|
132
|
public Map<String, String> getGroovyParams() {
|
133
|
return this.groovyParams;
|
134
|
}
|
135
|
|
136
|
public void setGroovyParams(final Map<String, String> groovyParams) {
|
137
|
this.groovyParams = groovyParams;
|
138
|
}
|
139
|
|
140
|
}
|