Project

General

Profile

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

    
3
import java.io.IOException;
4

    
5
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
6
import eu.dnetlib.msro.workflows.graph.Arc;
7
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
8
import eu.dnetlib.msro.workflows.procs.Env;
9
import eu.dnetlib.rmi.data.DatabaseService;
10
import org.apache.commons.io.IOUtils;
11
import org.springframework.beans.factory.annotation.Autowired;
12

    
13
public class ExecuteSqlJobNode extends AsyncJobNode {
14

    
15
	private String db;
16
	private String dbParam;
17

    
18
	private String sql;
19

    
20
	@Autowired
21
	private UniqueServiceLocator serviceLocator;
22

    
23
	@Override
24
	protected String execute(final Env env) throws Exception {
25
		this.serviceLocator.getService(DatabaseService.class).updateSQL(findDb(env), fetchSqlAsText(this.sql));
26
		return Arc.DEFAULT_ARC;
27
	}
28

    
29
	private String fetchSqlAsText(final String path) throws IOException {
30
		return IOUtils.toString(getClass().getResourceAsStream(path));
31
	}
32

    
33
	private String findDb(final Env env) {
34
		if (this.dbParam != null && !this.dbParam.isEmpty()) {
35
			return env.getAttribute(this.dbParam, String.class);
36
		} else {
37
			return this.db;
38
		}
39
	}
40

    
41
	public String getDb() {
42
		return this.db;
43
	}
44

    
45
	public void setDb(final String db) {
46
		this.db = db;
47
	}
48

    
49
	public String getDbParam() {
50
		return this.dbParam;
51
	}
52

    
53
	public void setDbParam(final String dbParam) {
54
		this.dbParam = dbParam;
55
	}
56

    
57
	public String getSql() {
58
		return this.sql;
59
	}
60

    
61
	public void setSql(final String sql) {
62
		this.sql = sql;
63
	}
64

    
65
}
(1-1/3)