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.common.ResultSet;
|
10
|
import eu.dnetlib.rmi.data.DatabaseService;
|
11
|
import org.apache.commons.io.IOUtils;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
|
15
|
public class QueryDbJobNode extends AsyncJobNode {
|
16
|
|
17
|
private String db;
|
18
|
private String dbParam;
|
19
|
|
20
|
private String sql;
|
21
|
private String sqlForSize;
|
22
|
private String xslt;
|
23
|
private String outputEprParam;
|
24
|
|
25
|
@Autowired
|
26
|
private UniqueServiceLocator serviceLocator;
|
27
|
|
28
|
@Override
|
29
|
protected String execute(final Env env) throws Exception {
|
30
|
final String sqlText = fetchSqlAsText(this.sql);
|
31
|
|
32
|
ResultSet<String> rs = null;
|
33
|
|
34
|
final DatabaseService dbService = this.serviceLocator.getService(DatabaseService.class);
|
35
|
|
36
|
if (StringUtils.isNotBlank(this.xslt)) {
|
37
|
final String xsltText = IOUtils.toString(getClass().getResourceAsStream(this.xslt));
|
38
|
|
39
|
if (StringUtils.isBlank(this.sqlForSize)) {
|
40
|
rs = dbService.xsltSearchSQL(findDb(env), sqlText, xsltText);
|
41
|
} else {
|
42
|
rs = dbService.alternativeXsltSearchSQL(findDb(env), sqlText, fetchSqlAsText(this.sqlForSize), xsltText);
|
43
|
}
|
44
|
} else {
|
45
|
if (StringUtils.isBlank(this.sqlForSize)) {
|
46
|
rs = dbService.searchSQL(findDb(env), sqlText);
|
47
|
} else {
|
48
|
rs = dbService.alternativeSearchSQL(findDb(env), sqlText, fetchSqlAsText(this.sqlForSize));
|
49
|
}
|
50
|
}
|
51
|
|
52
|
env.setAttribute(this.outputEprParam, rs);
|
53
|
|
54
|
return Arc.DEFAULT_ARC;
|
55
|
}
|
56
|
|
57
|
private String fetchSqlAsText(final String path) throws IOException {
|
58
|
return IOUtils.toString(getClass().getResourceAsStream(path));
|
59
|
}
|
60
|
|
61
|
private String findDb(final Env env) {
|
62
|
if (this.dbParam != null && !this.dbParam.isEmpty()) {
|
63
|
return env.getAttribute(this.dbParam, String.class);
|
64
|
} else {
|
65
|
return this.db;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public String getDb() {
|
70
|
return this.db;
|
71
|
}
|
72
|
|
73
|
public void setDb(final String db) {
|
74
|
this.db = db;
|
75
|
}
|
76
|
|
77
|
public String getDbParam() {
|
78
|
return this.dbParam;
|
79
|
}
|
80
|
|
81
|
public void setDbParam(final String dbParam) {
|
82
|
this.dbParam = dbParam;
|
83
|
}
|
84
|
|
85
|
public String getSql() {
|
86
|
return this.sql;
|
87
|
}
|
88
|
|
89
|
public void setSql(final String sql) {
|
90
|
this.sql = sql;
|
91
|
}
|
92
|
|
93
|
public String getXslt() {
|
94
|
return this.xslt;
|
95
|
}
|
96
|
|
97
|
public void setXslt(final String xslt) {
|
98
|
this.xslt = xslt;
|
99
|
}
|
100
|
|
101
|
public String getOutputEprParam() {
|
102
|
return this.outputEprParam;
|
103
|
}
|
104
|
|
105
|
public void setOutputEprParam(final String outputEprParam) {
|
106
|
this.outputEprParam = outputEprParam;
|
107
|
}
|
108
|
|
109
|
public String getSqlForSize() {
|
110
|
return this.sqlForSize;
|
111
|
}
|
112
|
|
113
|
public void setSqlForSize(final String sqlForSize) {
|
114
|
this.sqlForSize = sqlForSize;
|
115
|
}
|
116
|
|
117
|
}
|