Project

General

Profile

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

    
3
import java.io.IOException;
4

    
5
import javax.annotation.Resource;
6
import javax.xml.ws.wsaddressing.W3CEndpointReference;
7

    
8
import org.apache.commons.io.IOUtils;
9
import org.apache.commons.lang.StringUtils;
10

    
11
import com.googlecode.sarasvati.Arc;
12
import com.googlecode.sarasvati.NodeToken;
13

    
14
import eu.dnetlib.enabling.database.rmi.DatabaseService;
15
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
16
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
17

    
18
public class QueryDbJobNode extends AsyncJobNode {
19

    
20
	private String db;
21
	private String dbParam;
22
	private String dbProperty;
23

    
24
	private String sql;
25
	private String sqlForSize;
26
	private String xslt;
27
	private String outputEprParam;
28

    
29
	@Resource
30
	private UniqueServiceLocator serviceLocator;
31

    
32
	@Override
33
	protected String execute(final NodeToken token) throws Exception {
34
		final String sqlText = fetchSqlAsText(sql);
35

    
36
		W3CEndpointReference epr = null;
37

    
38
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
39

    
40
		if (StringUtils.isNotBlank(xslt)) {
41
			final String xsltText = IOUtils.toString(getClass().getResourceAsStream(xslt));
42

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

    
56
		token.getEnv().setAttribute(outputEprParam, epr.toString());
57

    
58
		return Arc.DEFAULT_ARC;
59
	}
60

    
61
	private String fetchSqlAsText(final String path) throws IOException {
62
		return IOUtils.toString(getClass().getResourceAsStream(path));
63
	}
64

    
65
	private String findDb(final NodeToken token) {
66
		if (dbParam != null && !dbParam.isEmpty()) {
67
			return token.getEnv().getAttribute(dbParam);
68
		} else if (dbProperty != null && !dbProperty.isEmpty()) {
69
			return getPropertyFetcher().getProperty(dbProperty);
70
		} else {
71
			return db;
72
		}
73
	}
74

    
75
	public String getDb() {
76
		return db;
77
	}
78

    
79
	public void setDb(final String db) {
80
		this.db = db;
81
	}
82

    
83
	public String getDbParam() {
84
		return dbParam;
85
	}
86

    
87
	public void setDbParam(final String dbParam) {
88
		this.dbParam = dbParam;
89
	}
90

    
91
	public String getSql() {
92
		return sql;
93
	}
94

    
95
	public void setSql(final String sql) {
96
		this.sql = sql;
97
	}
98

    
99
	public String getXslt() {
100
		return xslt;
101
	}
102

    
103
	public void setXslt(final String xslt) {
104
		this.xslt = xslt;
105
	}
106

    
107
	public String getOutputEprParam() {
108
		return outputEprParam;
109
	}
110

    
111
	public void setOutputEprParam(final String outputEprParam) {
112
		this.outputEprParam = outputEprParam;
113
	}
114

    
115
	public String getDbProperty() {
116
		return dbProperty;
117
	}
118

    
119
	public void setDbProperty(final String dbProperty) {
120
		this.dbProperty = dbProperty;
121
	}
122

    
123
	public String getSqlForSize() {
124
		return sqlForSize;
125
	}
126

    
127
	public void setSqlForSize(final String sqlForSize) {
128
		this.sqlForSize = sqlForSize;
129
	}
130

    
131
}
(3-3/4)