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

    
24
	@Resource
25
	private UniqueServiceLocator serviceLocator;
26

    
27
	@Override
28
	protected String execute(final NodeToken token) throws Exception {
29
		serviceLocator.getService(DatabaseService.class).updateSQL(findDb(token), fetchSqlAsText(sql));
30

    
31
		return Arc.DEFAULT_ARC;
32
	}
33

    
34
	private String fetchSqlAsText(final String path) throws IOException {
35
		return IOUtils.toString(getClass().getResourceAsStream(path));
36
	}
37

    
38
	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

    
48
	public String getDb() {
49
		return db;
50
	}
51

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

    
56
	public String getDbParam() {
57
		return dbParam;
58
	}
59

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

    
64
	public String getDbProperty() {
65
		return dbProperty;
66
	}
67

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

    
72
	public String getSql() {
73
		return sql;
74
	}
75

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

    
80
}
(2-2/4)