1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.hostedby;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import javax.annotation.Resource;
|
7
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
8
|
|
9
|
import org.apache.commons.lang.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.DocumentException;
|
14
|
import org.dom4j.io.SAXReader;
|
15
|
import org.springframework.beans.factory.annotation.Required;
|
16
|
import org.springframework.beans.factory.annotation.Value;
|
17
|
|
18
|
import com.google.common.collect.Maps;
|
19
|
import com.googlecode.sarasvati.Arc;
|
20
|
import com.googlecode.sarasvati.NodeToken;
|
21
|
|
22
|
import eu.dnetlib.enabling.database.rmi.DatabaseException;
|
23
|
import eu.dnetlib.enabling.database.rmi.DatabaseService;
|
24
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
25
|
import eu.dnetlib.enabling.resultset.MappedResultSetFactory;
|
26
|
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
|
27
|
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
|
28
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
29
|
|
30
|
public class PatchHostedByJobNode extends SimpleJobNode {
|
31
|
|
32
|
private static final Log log = LogFactory.getLog(PatchHostedByJobNode.class);
|
33
|
private String inputEprParam;
|
34
|
private String outputEprParam;
|
35
|
@Value("${dnet.openaire.db.name}")
|
36
|
private String dbName;
|
37
|
private String countersParam;
|
38
|
private String hostedbyMapTable;
|
39
|
private String xpathEntry;
|
40
|
private String overrideDataSourceId;
|
41
|
private String keyTypeFilter;
|
42
|
@Resource
|
43
|
private UniqueServiceLocator serviceLocator;
|
44
|
private MappedResultSetFactory mappedResultSetFactory;
|
45
|
private ResultSetClientFactory resultSetClientFactory;
|
46
|
|
47
|
/**
|
48
|
* {@inheritDoc}
|
49
|
*/
|
50
|
@Override
|
51
|
protected String execute(final NodeToken token) throws Exception {
|
52
|
final W3CEndpointReference inputEpr = new EPRUtils().getEpr(token.getEnv().getAttribute(inputEprParam));
|
53
|
final HostedByCounters counters = new HostedByCounters();
|
54
|
String datasourceId;
|
55
|
if (StringUtils.isEmpty(overrideDataSourceId)) {
|
56
|
datasourceId = token.getEnv().getAttribute("parentDatasourceId");
|
57
|
} else {
|
58
|
datasourceId = getOverrideDataSourceId();
|
59
|
}
|
60
|
|
61
|
Map<String, HostedByEntry> hostedByEntryMap;
|
62
|
|
63
|
if (!StringUtils.isBlank(keyTypeFilter)) {
|
64
|
hostedByEntryMap = loadHostedByMapFromType(keyTypeFilter);
|
65
|
} else {
|
66
|
hostedByEntryMap = loadHostedByMap(datasourceId);
|
67
|
}
|
68
|
|
69
|
final W3CEndpointReference epr = mappedResultSetFactory.createMappedResultSet(inputEpr, new PatchHostedBy(hostedByEntryMap,
|
70
|
getXpathEntry(), counters));
|
71
|
|
72
|
token.getEnv().setAttribute(outputEprParam, epr.toString());
|
73
|
token.getEnv().setTransientAttribute(countersParam, counters);
|
74
|
|
75
|
return Arc.DEFAULT_ARC;
|
76
|
}
|
77
|
|
78
|
private Map<String, HostedByEntry> loadHostedByMap(final String datasourceId) throws DocumentException, DatabaseException {
|
79
|
final String sql = "SELECT d.id, d.officialname, p.entry from %s p JOIN dsm_datasources d ON (p.datasourceid = d.id) WHERE p.oa_source_id= '%s'";
|
80
|
return getHostedByEntryMapByQuery(datasourceId, sql);
|
81
|
}
|
82
|
|
83
|
private Map<String, HostedByEntry> loadHostedByMapFromType(final String keyType) throws DocumentException, DatabaseException {
|
84
|
final String sql = "SELECT d.id, d.officialname, p.entry from %s p JOIN dsm_datasources d ON (p.datasourceid = d.id) WHERE p.key_type= '%s'";
|
85
|
return getHostedByEntryMapByQuery(keyType, sql);
|
86
|
}
|
87
|
|
88
|
private Map<String, HostedByEntry> getHostedByEntryMapByQuery(final String datasourceId, final String sql)
|
89
|
throws DatabaseException, DocumentException {
|
90
|
final Map<String, HostedByEntry> map = Maps.newHashMap();
|
91
|
final W3CEndpointReference epr = serviceLocator.getService(DatabaseService.class).searchSQL(getDbName(),
|
92
|
String.format(sql, getHostedbyMapTable(), datasourceId));
|
93
|
final SAXReader reader = new SAXReader();
|
94
|
for (final String s : resultSetClientFactory.getClient(epr)) {
|
95
|
final Document doc = reader.read(new StringReader(s));
|
96
|
final String entry = doc.valueOf("//FIELD[@name='entry']");
|
97
|
final String dsId = doc.valueOf("//FIELD[@name='id']");
|
98
|
final String dsName = doc.valueOf("//FIELD[@name='officialname']");
|
99
|
map.put(entry, new HostedByEntry(dsId, dsName));
|
100
|
}
|
101
|
log.info(String.format("built hostedByMap from dsId '%s', size: '%s'", datasourceId, map.size()));
|
102
|
return map;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Getter for property 'inputEprParam'.
|
107
|
*
|
108
|
* @return Value for property 'inputEprParam'.
|
109
|
*/
|
110
|
public String getInputEprParam() {
|
111
|
return inputEprParam;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Setter for property 'inputEprParam'.
|
116
|
*
|
117
|
* @param inputEprParam
|
118
|
* Value to set for property 'inputEprParam'.
|
119
|
*/
|
120
|
public void setInputEprParam(final String inputEprParam) {
|
121
|
this.inputEprParam = inputEprParam;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Getter for property 'outputEprParam'.
|
126
|
*
|
127
|
* @return Value for property 'outputEprParam'.
|
128
|
*/
|
129
|
public String getOutputEprParam() {
|
130
|
return outputEprParam;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Setter for property 'outputEprParam'.
|
135
|
*
|
136
|
* @param outputEprParam
|
137
|
* Value to set for property 'outputEprParam'.
|
138
|
*/
|
139
|
public void setOutputEprParam(final String outputEprParam) {
|
140
|
this.outputEprParam = outputEprParam;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Getter for property 'dbName'.
|
145
|
*
|
146
|
* @return Value for property 'dbName'.
|
147
|
*/
|
148
|
public String getDbName() {
|
149
|
return dbName;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Setter for property 'dbName'.
|
154
|
*
|
155
|
* @param dbName
|
156
|
* Value to set for property 'dbName'.
|
157
|
*/
|
158
|
public void setDbName(final String dbName) {
|
159
|
this.dbName = dbName;
|
160
|
}
|
161
|
|
162
|
public String getKeyTypeFilter() {
|
163
|
return keyTypeFilter;
|
164
|
}
|
165
|
|
166
|
public void setKeyTypeFilter(final String keyTypeFilter) {
|
167
|
this.keyTypeFilter = keyTypeFilter;
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Getter for property 'mappedResultSetFactory'.
|
172
|
*
|
173
|
* @return Value for property 'mappedResultSetFactory'.
|
174
|
*/
|
175
|
public MappedResultSetFactory getMappedResultSetFactory() {
|
176
|
return mappedResultSetFactory;
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Setter for property 'mappedResultSetFactory'.
|
181
|
*
|
182
|
* @param mappedResultSetFactory
|
183
|
* Value to set for property 'mappedResultSetFactory'.
|
184
|
*/
|
185
|
@Required
|
186
|
public void setMappedResultSetFactory(final MappedResultSetFactory mappedResultSetFactory) {
|
187
|
this.mappedResultSetFactory = mappedResultSetFactory;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Getter for property 'resultSetClientFactory'.
|
192
|
*
|
193
|
* @return Value for property 'resultSetClientFactory'.
|
194
|
*/
|
195
|
public ResultSetClientFactory getResultSetClientFactory() {
|
196
|
return resultSetClientFactory;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Setter for property 'resultSetClientFactory'.
|
201
|
*
|
202
|
* @param resultSetClientFactory
|
203
|
* Value to set for property 'resultSetClientFactory'.
|
204
|
*/
|
205
|
@Required
|
206
|
public void setResultSetClientFactory(final ResultSetClientFactory resultSetClientFactory) {
|
207
|
this.resultSetClientFactory = resultSetClientFactory;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Getter for property 'countersParam'.
|
212
|
*
|
213
|
* @return Value for property 'countersParam'.
|
214
|
*/
|
215
|
public String getCountersParam() {
|
216
|
return countersParam;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Setter for property 'countersParam'.
|
221
|
*
|
222
|
* @param countersParam
|
223
|
* Value to set for property 'countersParam'.
|
224
|
*/
|
225
|
public void setCountersParam(final String countersParam) {
|
226
|
this.countersParam = countersParam;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* @return the hostedbyMapTable
|
231
|
*/
|
232
|
public String getHostedbyMapTable() {
|
233
|
return hostedbyMapTable;
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* @param hostedbyMapTable
|
238
|
* the hostedbyMapTable to set
|
239
|
*/
|
240
|
public void setHostedbyMapTable(final String hostedbyMapTable) {
|
241
|
this.hostedbyMapTable = hostedbyMapTable;
|
242
|
}
|
243
|
|
244
|
public String getXpathEntry() {
|
245
|
return xpathEntry;
|
246
|
}
|
247
|
|
248
|
public void setXpathEntry(final String xpathEntry) {
|
249
|
this.xpathEntry = xpathEntry;
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* @return the overrideDataSourceId
|
254
|
*/
|
255
|
public String getOverrideDataSourceId() {
|
256
|
return overrideDataSourceId;
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* @param overrideDataSourceId
|
261
|
* the overrideDataSourceId to set
|
262
|
*/
|
263
|
public void setOverrideDataSourceId(final String overrideDataSourceId) {
|
264
|
this.overrideDataSourceId = overrideDataSourceId;
|
265
|
}
|
266
|
}
|