Project

General

Profile

1 29933 michele.ar
package eu.dnetlib.msro.workflows.nodes.db;
2
3
import java.io.IOException;
4
5 32639 michele.ar
import javax.annotation.Resource;
6
7 29933 michele.ar
import org.apache.commons.io.IOUtils;
8
9
import com.googlecode.sarasvati.Arc;
10
import com.googlecode.sarasvati.NodeToken;
11
12
import eu.dnetlib.enabling.database.rmi.DatabaseService;
13 32639 michele.ar
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14 29933 michele.ar
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
15
16
public class ExecuteSqlJobNode extends AsyncJobNode {
17 32639 michele.ar
18 29933 michele.ar
	private String db;
19
	private String dbParam;
20
	private String dbProperty;
21
22
	private String sql;
23 58646 alessia.ba
	private boolean sqlInClasspath = true;
24 32639 michele.ar
25
	@Resource
26
	private UniqueServiceLocator serviceLocator;
27
28 29933 michele.ar
	@Override
29 32639 michele.ar
	protected String execute(final NodeToken token) throws Exception {
30
		serviceLocator.getService(DatabaseService.class).updateSQL(findDb(token), fetchSqlAsText(sql));
31 29933 michele.ar
		return Arc.DEFAULT_ARC;
32
	}
33 32639 michele.ar
34 29933 michele.ar
	private String fetchSqlAsText(final String path) throws IOException {
35 58646 alessia.ba
		if(sqlInClasspath)
36
			return IOUtils.toString(getClass().getResourceAsStream(path));
37
		else return sql;
38 29933 michele.ar
	}
39 32639 michele.ar
40 29933 michele.ar
	private String findDb(final NodeToken token) {
41
		if (dbParam != null && !dbParam.isEmpty()) {
42
			return token.getEnv().getAttribute(dbParam);
43
		} else if (dbProperty != null && !dbProperty.isEmpty()) {
44
			return getPropertyFetcher().getProperty(dbProperty);
45
		} else {
46
			return db;
47
		}
48
	}
49 32639 michele.ar
50 29933 michele.ar
	public String getDb() {
51
		return db;
52
	}
53
54 32639 michele.ar
	public void setDb(final String db) {
55 29933 michele.ar
		this.db = db;
56
	}
57
58
	public String getDbParam() {
59
		return dbParam;
60
	}
61
62 32639 michele.ar
	public void setDbParam(final String dbParam) {
63 29933 michele.ar
		this.dbParam = dbParam;
64
	}
65
66
	public String getDbProperty() {
67
		return dbProperty;
68
	}
69
70 32639 michele.ar
	public void setDbProperty(final String dbProperty) {
71 29933 michele.ar
		this.dbProperty = dbProperty;
72
	}
73
74
	public String getSql() {
75
		return sql;
76
	}
77
78 32639 michele.ar
	public void setSql(final String sql) {
79 29933 michele.ar
		this.sql = sql;
80
	}
81
82 58646 alessia.ba
	public boolean isSqlInClasspath() { return sqlInClasspath; }
83
84
	public void setSqlInClasspath(boolean sqlInClasspath) { this.sqlInClasspath = sqlInClasspath; }
85 29933 michele.ar
}