Project

General

Profile

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

    
3
import java.io.IOException;
4
import javax.annotation.Resource;
5
import javax.xml.ws.wsaddressing.W3CEndpointReference;
6

    
7
import eu.dnetlib.enabling.database.rmi.DatabaseService;
8
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
9
import eu.dnetlib.msro.workflows.graph.Arc;
10
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
11
import eu.dnetlib.msro.workflows.procs.Env;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.lang.StringUtils;
14

    
15
public class QueryDbJobNode extends AsyncJobNode {
16

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

    
21
	private String sql;
22
	private String sqlForSize;
23
	private String xslt;
24
	private String outputEprParam;
25

    
26
	@Resource
27
	private UniqueServiceLocator serviceLocator;
28

    
29
	@Override
30
	protected String execute(final Env env) throws Exception {
31
		final String sqlText = fetchSqlAsText(sql);
32

    
33
		W3CEndpointReference epr = null;
34

    
35
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
36

    
37
		if (StringUtils.isNotBlank(xslt)) {
38
			final String xsltText = IOUtils.toString(getClass().getResourceAsStream(xslt));
39

    
40
			if (StringUtils.isBlank(sqlForSize)) {
41
				epr = dbService.xsltSearchSQL(findDb(env), sqlText, xsltText);
42
			} else {
43
				epr = dbService.alternativeXsltSearchSQL(findDb(env), sqlText, fetchSqlAsText(sqlForSize), xsltText);
44
			}
45
		} else {
46
			if (StringUtils.isBlank(sqlForSize)) {
47
				epr = dbService.searchSQL(findDb(env), sqlText);
48
			} else {
49
				epr = dbService.alternativeSearchSQL(findDb(env), sqlText, fetchSqlAsText(sqlForSize));
50
			}
51
		}
52

    
53
		env.setAttribute(outputEprParam, epr.toString());
54

    
55
		return Arc.DEFAULT_ARC;
56
	}
57

    
58
	private String fetchSqlAsText(final String path) throws IOException {
59
		return IOUtils.toString(getClass().getResourceAsStream(path));
60
	}
61

    
62
	private String findDb(final Env env) {
63
		if (dbParam != null && !dbParam.isEmpty()) {
64
			return env.getAttribute(dbParam, String.class);
65
		} else if (dbProperty != null && !dbProperty.isEmpty()) {
66
			return getPropertyFetcher().getProperty(dbProperty);
67
		} else {
68
			return db;
69
		}
70
	}
71

    
72
	public String getDb() {
73
		return db;
74
	}
75

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

    
80
	public String getDbParam() {
81
		return dbParam;
82
	}
83

    
84
	public void setDbParam(final String dbParam) {
85
		this.dbParam = dbParam;
86
	}
87

    
88
	public String getSql() {
89
		return sql;
90
	}
91

    
92
	public void setSql(final String sql) {
93
		this.sql = sql;
94
	}
95

    
96
	public String getXslt() {
97
		return xslt;
98
	}
99

    
100
	public void setXslt(final String xslt) {
101
		this.xslt = xslt;
102
	}
103

    
104
	public String getOutputEprParam() {
105
		return outputEprParam;
106
	}
107

    
108
	public void setOutputEprParam(final String outputEprParam) {
109
		this.outputEprParam = outputEprParam;
110
	}
111

    
112
	public String getDbProperty() {
113
		return dbProperty;
114
	}
115

    
116
	public void setDbProperty(final String dbProperty) {
117
		this.dbProperty = dbProperty;
118
	}
119

    
120
	public String getSqlForSize() {
121
		return sqlForSize;
122
	}
123

    
124
	public void setSqlForSize(final String sqlForSize) {
125
		this.sqlForSize = sqlForSize;
126
	}
127

    
128
}
(2-2/3)