1
|
package eu.dnetlib.msro.workflows.nodes.hostedby;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
import java.util.function.UnaryOperator;
|
6
|
|
7
|
import com.google.common.collect.Maps;
|
8
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
9
|
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
|
10
|
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
|
11
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
12
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
13
|
import eu.dnetlib.msro.workflows.procs.Env;
|
14
|
import eu.dnetlib.rmi.common.ResultSet;
|
15
|
import eu.dnetlib.rmi.data.DatabaseException;
|
16
|
import eu.dnetlib.rmi.data.DatabaseService;
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.dom4j.Document;
|
21
|
import org.dom4j.DocumentException;
|
22
|
import org.dom4j.io.SAXReader;
|
23
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
import org.springframework.beans.factory.annotation.Value;
|
25
|
|
26
|
public class PatchHostedByJobNode extends SimpleJobNode {
|
27
|
|
28
|
private static final Log log = LogFactory.getLog(PatchHostedByJobNode.class);
|
29
|
private String inputEprParam;
|
30
|
private String outputEprParam;
|
31
|
@Value("${dnet.openaire.db.name}")
|
32
|
private String dbName;
|
33
|
private String countersParam;
|
34
|
private String hostedbyMapTable;
|
35
|
private String xpathEntry;
|
36
|
private String overrideDataSourceId;
|
37
|
@Autowired
|
38
|
private UniqueServiceLocator serviceLocator;
|
39
|
@Autowired
|
40
|
private ResultSetFactory resultSetFactory;
|
41
|
@Autowired
|
42
|
private ResultSetClient resultSetClient;
|
43
|
|
44
|
/**
|
45
|
* {@inheritDoc}
|
46
|
*/
|
47
|
@Override
|
48
|
protected String execute(final Env env) throws Exception {
|
49
|
final ResultSet<?> inputEpr = env.getAttribute(this.inputEprParam, ResultSet.class);
|
50
|
final HostedByCounters counters = new HostedByCounters();
|
51
|
String datasourceId;
|
52
|
if (StringUtils.isEmpty(this.overrideDataSourceId)) {
|
53
|
datasourceId = env.getAttribute("parentDatasourceId", String.class);
|
54
|
} else {
|
55
|
datasourceId = getOverrideDataSourceId();
|
56
|
}
|
57
|
|
58
|
final UnaryOperator<String> mapper = new PatchHostedBy(loadHostedByMap(datasourceId), getXpathEntry(), counters);
|
59
|
final ResultSet<String> epr = this.resultSetFactory.map(inputEpr, String.class, mapper);
|
60
|
|
61
|
env.setAttribute(this.outputEprParam, epr);
|
62
|
env.setAttribute(this.countersParam, counters);
|
63
|
|
64
|
return Arc.DEFAULT_ARC;
|
65
|
}
|
66
|
|
67
|
private Map<String, HostedByEntry> loadHostedByMap(final String datasourceId) throws DocumentException, DatabaseException {
|
68
|
final Map<String, HostedByEntry> map = Maps.newHashMap();
|
69
|
|
70
|
final String sql = "SELECT d.id, d.officialname, p.entry from %s p JOIN datasources d ON (p.datasourceid = d.id) WHERE p.oa_source_id= '%s'";
|
71
|
|
72
|
final ResultSet<String> epr = this.serviceLocator.getService(DatabaseService.class).searchSQL(getDbName(),
|
73
|
String.format(sql, getHostedbyMapTable(), datasourceId));
|
74
|
|
75
|
final SAXReader reader = new SAXReader();
|
76
|
for (final String s : this.resultSetClient.iter(epr, String.class)) {
|
77
|
final Document doc = reader.read(new StringReader(s));
|
78
|
final String entry = doc.valueOf("//FIELD[@name='entry']");
|
79
|
final String dsId = doc.valueOf("//FIELD[@name='id']");
|
80
|
final String dsName = doc.valueOf("//FIELD[@name='officialname']");
|
81
|
map.put(entry, new HostedByEntry(dsId, dsName));
|
82
|
}
|
83
|
|
84
|
log.info(String.format("built hostedByMap from dsId '%s', size: '%s'", datasourceId, map.size()));
|
85
|
|
86
|
return map;
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Getter for property 'inputEprParam'.
|
91
|
*
|
92
|
* @return Value for property 'inputEprParam'.
|
93
|
*/
|
94
|
public String getInputEprParam() {
|
95
|
return this.inputEprParam;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Setter for property 'inputEprParam'.
|
100
|
*
|
101
|
* @param inputEprParam Value to set for property 'inputEprParam'.
|
102
|
*/
|
103
|
public void setInputEprParam(final String inputEprParam) {
|
104
|
this.inputEprParam = inputEprParam;
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Getter for property 'outputEprParam'.
|
109
|
*
|
110
|
* @return Value for property 'outputEprParam'.
|
111
|
*/
|
112
|
public String getOutputEprParam() {
|
113
|
return this.outputEprParam;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Setter for property 'outputEprParam'.
|
118
|
*
|
119
|
* @param outputEprParam Value to set for property 'outputEprParam'.
|
120
|
*/
|
121
|
public void setOutputEprParam(final String outputEprParam) {
|
122
|
this.outputEprParam = outputEprParam;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Getter for property 'dbName'.
|
127
|
*
|
128
|
* @return Value for property 'dbName'.
|
129
|
*/
|
130
|
public String getDbName() {
|
131
|
return this.dbName;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Setter for property 'dbName'.
|
136
|
*
|
137
|
* @param dbName Value to set for property 'dbName'.
|
138
|
*/
|
139
|
public void setDbName(final String dbName) {
|
140
|
this.dbName = dbName;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Getter for property 'countersParam'.
|
145
|
*
|
146
|
* @return Value for property 'countersParam'.
|
147
|
*/
|
148
|
public String getCountersParam() {
|
149
|
return this.countersParam;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Setter for property 'countersParam'.
|
154
|
*
|
155
|
* @param countersParam Value to set for property 'countersParam'.
|
156
|
*/
|
157
|
public void setCountersParam(final String countersParam) {
|
158
|
this.countersParam = countersParam;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* @return the hostedbyMapTable
|
163
|
*/
|
164
|
public String getHostedbyMapTable() {
|
165
|
return this.hostedbyMapTable;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* @param hostedbyMapTable the hostedbyMapTable to set
|
170
|
*/
|
171
|
public void setHostedbyMapTable(final String hostedbyMapTable) {
|
172
|
this.hostedbyMapTable = hostedbyMapTable;
|
173
|
}
|
174
|
|
175
|
public String getXpathEntry() {
|
176
|
return this.xpathEntry;
|
177
|
}
|
178
|
|
179
|
public void setXpathEntry(final String xpathEntry) {
|
180
|
this.xpathEntry = xpathEntry;
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* @return the overrideDataSourceId
|
185
|
*/
|
186
|
public String getOverrideDataSourceId() {
|
187
|
return this.overrideDataSourceId;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* @param overrideDataSourceId the overrideDataSourceId to set
|
192
|
*/
|
193
|
public void setOverrideDataSourceId(final String overrideDataSourceId) {
|
194
|
this.overrideDataSourceId = overrideDataSourceId;
|
195
|
}
|
196
|
}
|