Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.util.Map;
5

    
6
import com.google.common.collect.Maps;
7
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
8
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
9
import eu.dnetlib.msro.workflows.graph.Arc;
10
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
11
import eu.dnetlib.msro.workflows.procs.Env;
12
import eu.dnetlib.rmi.common.ResultSet;
13
import eu.dnetlib.rmi.enabling.ISLookUpService;
14
import eu.dnetlib.rmi.manager.MSROException;
15
import groovy.lang.GroovyShell;
16
import groovy.util.GroovyScriptEngine;
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.springframework.beans.factory.annotation.Autowired;
20

    
21
public class GroovyJobNode extends SimpleJobNode {
22

    
23
	private static final Log log = LogFactory.getLog(GroovyJobNode.class);
24

    
25
	/**
26
	 * used to transform the records using Groovy.
27
	 */
28
	@Autowired
29
	private ResultSetFactory resultSetFactory;
30

    
31
	private String inputEprParam;
32
	private String outputEprParam;
33
	private String transformationRuleId;
34
	private Map<String, String> groovyParams;
35

    
36
	@Autowired
37
	private UniqueServiceLocator serviceLocator;
38

    
39
	private Map<String, String> retrieveGroovyParameter() {
40
		final Map<String, String> out = Maps.newHashMap();
41

    
42
		final String query = "for $x in collection('/db/DRIVER/GroovyProcessingDSResource/GroovyProcessingDSResourceType')"
43
				+ "where $x[.//RESOURCE_IDENTIFIER/@value='" + this.transformationRuleId + "']"
44
				+ "return concat($x//GROOVY_CLASSPATH/text(),':::',$x//GROOVY_DNETCLASS/text())";
45
		try {
46
			final String result = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query).get(0);
47
			if (result == null) { return null; }
48
			final String[] data = result.trim().split(":::");
49
			if (data.length == 2) {
50
				out.put("classpath", data[0]);
51
				out.put("mainClass", data[1]);
52
			}
53

    
54
			return out;
55
		} catch (final Exception e) {
56
			log.error(e);
57
			return null;
58
		}
59
	}
60

    
61
	@Override
62
	protected String execute(final Env env) throws Exception {
63
		final ResultSet<?> rsIn = env.getAttribute(this.inputEprParam, ResultSet.class);
64
		if (rsIn == null) { throw new MSROException("InputEprParam (" + this.inputEprParam + ") not found in ENV"); }
65

    
66
		String groovyClasspath, groovyDnetClass;
67
		final Map<String, String> prop = retrieveGroovyParameter();
68
		groovyClasspath = prop.get("classpath");
69
		groovyDnetClass = prop.get("mainClass");
70

    
71
		final ResultSet<?> rsOut = transformGroovy(rsIn, groovyClasspath, groovyDnetClass, this.groovyParams);
72
		env.setAttribute(this.outputEprParam, rsOut);
73

    
74
		return Arc.DEFAULT_ARC;
75
	}
76

    
77
	private ResultSet<?> transformGroovy(final ResultSet<?> source,
78
			final String groovyClasspath,
79
			final String groovyDnetClass,
80
			final Map<String, String> params) throws ClassNotFoundException, IOException {
81

    
82
		final GroovyScriptEngine gse = new GroovyScriptEngine(groovyClasspath);
83
		gse.getGroovyClassLoader().loadClass(groovyDnetClass);
84
		log.info("***********************************************");
85
		log.info("Loaded Groovy classes:");
86
		for (final Class<?> c : gse.getGroovyClassLoader().getLoadedClasses()) {
87
			log.info(c.getCanonicalName());
88
		}
89
		log.info("***********************************************");
90
		final GroovyShell groovyShell = new GroovyShell(gse.getGroovyClassLoader());
91

    
92
		final Object go = groovyShell.evaluate("new " + groovyDnetClass + "()");
93
		if (go instanceof GroovyUnaryFunction) {
94
			final GroovyUnaryFunction groovyUnaryFunction = (GroovyUnaryFunction) go;
95
			if (params != null) {
96
				groovyUnaryFunction.setParams(params);
97
			}
98
			return this.resultSetFactory.map(source, String.class, groovyUnaryFunction);
99
		} else {
100
			throw new RuntimeException("Groovy object " + go + " is not supported");
101
		}
102
	}
103

    
104
	public String getInputEprParam() {
105
		return this.inputEprParam;
106
	}
107

    
108
	public void setInputEprParam(final String inputEprParam) {
109
		this.inputEprParam = inputEprParam;
110
	}
111

    
112
	public String getOutputEprParam() {
113
		return this.outputEprParam;
114
	}
115

    
116
	public void setOutputEprParam(final String outputEprParam) {
117
		this.outputEprParam = outputEprParam;
118
	}
119

    
120
	public String getTransformationRuleId() {
121
		return this.transformationRuleId;
122
	}
123

    
124
	public void setTransformationRuleId(final String transformationRuleId) {
125
		this.transformationRuleId = transformationRuleId;
126
	}
127

    
128
	public Map<String, String> getGroovyParams() {
129
		return this.groovyParams;
130
	}
131

    
132
	public void setGroovyParams(final Map<String, String> groovyParams) {
133
		this.groovyParams = groovyParams;
134
	}
135

    
136
}
(3-3/6)