Project

General

Profile

1 37183 claudio.at
package eu.dnetlib.msro.workflows.dedup;
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
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
13
import com.googlecode.sarasvati.Arc;
14
import com.googlecode.sarasvati.NodeToken;
15
16
import eu.dnetlib.enabling.database.rmi.DatabaseService;
17
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
18
import eu.dnetlib.msro.workflows.dedup.conf.DedupConfigurationOrchestration;
19
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
20
21
public class QueryUserActionDbJobNode extends AsyncJobNode {
22
23
	/**
24
	 * logger.
25
	 */
26
	private static final Log log = LogFactory.getLog(QueryUserActionDbJobNode.class);
27
28
	private String db;
29
	private String dbParam;
30
	private String dbProperty;
31
32
	private String sql;
33
	private String sqlForSize;
34
	private String xslt;
35
	private String outputEprParam;
36
37
	@Resource
38
	private UniqueServiceLocator serviceLocator;
39
40
	/** The dedup config sequence param. */
41
	private String dedupConfigSequenceParam;
42
43
	@Override
44
	protected String execute(final NodeToken token) throws Exception {
45
46
		final String actionSetId = getActionSetId(token);
47
		if (StringUtils.isBlank(actionSetId)) throw new IllegalArgumentException("empty action set id");
48
49
		final String sqlText = String.format(readFromClasspath(getSql()), actionSetId);
50
		log.info("executing query: " + sqlText);
51
52
		final String sqlTextForsize = String.format(StringUtils.isBlank(getSqlForSize()) ? "" : readFromClasspath(getSqlForSize()), actionSetId);
53
		if (!sqlTextForsize.isEmpty()) {
54
			log.info("using sql for size: " + sqlTextForsize);
55
		}
56
57
		W3CEndpointReference epr = null;
58
59
		final DatabaseService dbService = serviceLocator.getService(DatabaseService.class);
60
61
		if (StringUtils.isNotBlank(xslt)) {
62
			final String xsltText = IOUtils.toString(getClass().getResourceAsStream(xslt));
63
64
			if (StringUtils.isBlank(sqlForSize)) {
65
				epr = dbService.xsltSearchSQL(findDb(token), sqlText, xsltText);
66
			} else {
67
				epr = dbService.alternativeXsltSearchSQL(findDb(token), sqlText, sqlTextForsize, xsltText);
68
			}
69
		} else {
70
			if (StringUtils.isBlank(sqlForSize)) {
71
				epr = dbService.searchSQL(findDb(token), sqlText);
72
			} else {
73
				epr = dbService.alternativeSearchSQL(findDb(token), sqlText, sqlTextForsize);
74
			}
75
		}
76
77
		token.getEnv().setAttribute(outputEprParam, epr.toString());
78
79
		return Arc.DEFAULT_ARC;
80
	}
81
82
	private String findDb(final NodeToken token) {
83
		if ((dbParam != null) && !dbParam.isEmpty()) return token.getEnv().getAttribute(dbParam);
84
		else if ((dbProperty != null) && !dbProperty.isEmpty()) return getPropertyFetcher().getProperty(dbProperty);
85
		else return db;
86
	}
87
88
	private String readFromClasspath(final String path) throws IOException {
89
		return IOUtils.toString(getClass().getResourceAsStream(path));
90
	}
91
92
	/**
93
	 * Gets the action set id.
94
	 *
95
	 * @param token
96
	 *            the token
97
	 * @return the action set id
98
	 */
99
	private String getActionSetId(final NodeToken token) {
100
		final String json = token.getEnv().getAttribute(getDedupConfigSequenceParam());
101
		final DedupConfigurationOrchestration dco = DedupConfigurationOrchestration.fromJSON(json);
102
		final String actionSetId = dco.getActionSetId();
103
		log.info("found actionSetId in workflow env: " + actionSetId);
104
		return actionSetId;
105
	}
106
107
	public String getDb() {
108
		return db;
109
	}
110
111
	public void setDb(final String db) {
112
		this.db = db;
113
	}
114
115
	public String getDbParam() {
116
		return dbParam;
117
	}
118
119
	public void setDbParam(final String dbParam) {
120
		this.dbParam = dbParam;
121
	}
122
123
	public String getSql() {
124
		return sql;
125
	}
126
127
	public void setSql(final String sql) {
128
		this.sql = sql;
129
	}
130
131
	public String getXslt() {
132
		return xslt;
133
	}
134
135
	public void setXslt(final String xslt) {
136
		this.xslt = xslt;
137
	}
138
139
	public String getOutputEprParam() {
140
		return outputEprParam;
141
	}
142
143
	public void setOutputEprParam(final String outputEprParam) {
144
		this.outputEprParam = outputEprParam;
145
	}
146
147
	public String getDbProperty() {
148
		return dbProperty;
149
	}
150
151
	public void setDbProperty(final String dbProperty) {
152
		this.dbProperty = dbProperty;
153
	}
154
155
	public String getSqlForSize() {
156
		return sqlForSize;
157
	}
158
159
	public void setSqlForSize(final String sqlForSize) {
160
		this.sqlForSize = sqlForSize;
161
	}
162
163
	public String getDedupConfigSequenceParam() {
164
		return dedupConfigSequenceParam;
165
	}
166
167
	public void setDedupConfigSequenceParam(final String dedupConfigSequenceParam) {
168
		this.dedupConfigSequenceParam = dedupConfigSequenceParam;
169
	}
170
171
}