Project

General

Profile

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

    
3
import java.io.IOException;
4

    
5
import javax.annotation.Resource;
6

    
7
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
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
15

    
16
public class ExecuteSqlJobNode extends AsyncJobNode {
17

    
18
	private String db;
19
	private String dbParam;
20
	private String dbProperty;
21

    
22
	private String sql;
23
	private boolean sqlInClasspath = true;
24

    
25
	@Resource
26
	private UniqueServiceLocator serviceLocator;
27

    
28
	@Override
29
	protected String execute(final NodeToken token) throws Exception {
30
		serviceLocator.getService(DatabaseService.class).updateSQL(findDb(token), fetchSqlAsText(sql));
31
		return Arc.DEFAULT_ARC;
32
	}
33

    
34
	private String fetchSqlAsText(final String path) throws IOException {
35
		if(sqlInClasspath)
36
			return IOUtils.toString(getClass().getResourceAsStream(path));
37
		else return sql;
38
	}
39

    
40
	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

    
50
	public String getDb() {
51
		return db;
52
	}
53

    
54
	public void setDb(final String db) {
55
		this.db = db;
56
	}
57

    
58
	public String getDbParam() {
59
		return dbParam;
60
	}
61

    
62
	public void setDbParam(final String dbParam) {
63
		this.dbParam = dbParam;
64
	}
65

    
66
	public String getDbProperty() {
67
		return dbProperty;
68
	}
69

    
70
	public void setDbProperty(final String dbProperty) {
71
		this.dbProperty = dbProperty;
72
	}
73

    
74
	public String getSql() {
75
		return sql;
76
	}
77

    
78
	public void setSql(final String sql) {
79
		this.sql = sql;
80
	}
81

    
82
	public boolean isSqlInClasspath() { return sqlInClasspath; }
83

    
84
	public void setSqlInClasspath(boolean sqlInClasspath) { this.sqlInClasspath = sqlInClasspath; }
85
}
(2-2/4)