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 32639 michele.ar
24
	@Resource
25
	private UniqueServiceLocator serviceLocator;
26
27 29933 michele.ar
	@Override
28 32639 michele.ar
	protected String execute(final NodeToken token) throws Exception {
29
		serviceLocator.getService(DatabaseService.class).updateSQL(findDb(token), fetchSqlAsText(sql));
30
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
		return IOUtils.toString(getClass().getResourceAsStream(path));
36
	}
37 32639 michele.ar
38 29933 michele.ar
	private String findDb(final NodeToken token) {
39
		if (dbParam != null && !dbParam.isEmpty()) {
40
			return token.getEnv().getAttribute(dbParam);
41
		} else if (dbProperty != null && !dbProperty.isEmpty()) {
42
			return getPropertyFetcher().getProperty(dbProperty);
43
		} else {
44
			return db;
45
		}
46
	}
47 32639 michele.ar
48 29933 michele.ar
	public String getDb() {
49
		return db;
50
	}
51
52 32639 michele.ar
	public void setDb(final String db) {
53 29933 michele.ar
		this.db = db;
54
	}
55
56
	public String getDbParam() {
57
		return dbParam;
58
	}
59
60 32639 michele.ar
	public void setDbParam(final String dbParam) {
61 29933 michele.ar
		this.dbParam = dbParam;
62
	}
63
64
	public String getDbProperty() {
65
		return dbProperty;
66
	}
67
68 32639 michele.ar
	public void setDbProperty(final String dbProperty) {
69 29933 michele.ar
		this.dbProperty = dbProperty;
70
	}
71
72
	public String getSql() {
73
		return sql;
74
	}
75
76 32639 michele.ar
	public void setSql(final String sql) {
77 29933 michele.ar
		this.sql = sql;
78
	}
79
80
}