1
|
package eu.dnetlib.validator.service.impls.providers;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
|
7
|
import javax.xml.parsers.DocumentBuilder;
|
8
|
import javax.xml.parsers.DocumentBuilderFactory;
|
9
|
import javax.xml.parsers.ParserConfigurationException;
|
10
|
import javax.xml.xpath.XPath;
|
11
|
import javax.xml.xpath.XPathConstants;
|
12
|
import javax.xml.xpath.XPathExpression;
|
13
|
import javax.xml.xpath.XPathExpressionException;
|
14
|
import javax.xml.xpath.XPathFactory;
|
15
|
|
16
|
import org.w3c.dom.Document;
|
17
|
import org.w3c.dom.Element;
|
18
|
import org.w3c.dom.Node;
|
19
|
import org.w3c.dom.ls.DOMImplementationLS;
|
20
|
import org.w3c.dom.ls.LSSerializer;
|
21
|
import org.xml.sax.InputSource;
|
22
|
|
23
|
import eu.dnetlib.api.data.MDStoreService;
|
24
|
import eu.dnetlib.domain.EPR;
|
25
|
import eu.dnetlib.validator.engine.data.DataException;
|
26
|
import eu.dnetlib.validator.engine.data.Provider;
|
27
|
import eu.dnetlib.validator.engine.data.ResultSet;
|
28
|
import eu.dnetlib.validator.engine.execution.ValidationObject;
|
29
|
import eu.dnetlib.validator.service.impls.valobjs.XMLTextValidationObject;
|
30
|
import gr.uoa.di.driver.enabling.resultset.ResultSetFactory;
|
31
|
import gr.uoa.di.driver.util.ServiceLocator;
|
32
|
|
33
|
public class DnetProvider extends Provider{
|
34
|
|
35
|
private static final long serialVersionUID = -4280319954962194170L;
|
36
|
|
37
|
private static ServiceLocator<MDStoreService> mdStoreServiceLocator;
|
38
|
|
39
|
private static ResultSetFactory rsFactory;
|
40
|
|
41
|
public static final String DATASOURCE = "DATASOURCE";
|
42
|
|
43
|
public static final String BATCH_SIZE = "BATCH_SIZE";
|
44
|
|
45
|
public static final String RECORDS = "RECORDS";
|
46
|
|
47
|
public static final String MDSTORE_ID = "MDSTORE_ID";
|
48
|
|
49
|
public static final String FROM = "FROM";
|
50
|
|
51
|
public static final String BEGIN_RECORD = "BEGIN_RECORD";
|
52
|
|
53
|
public static final String UNTIL = "UNTIL";
|
54
|
|
55
|
public static final String RECORD_FILTER = "RECORD_FILTER";
|
56
|
|
57
|
public static final String WORKER_ID = "WORKER_ID";
|
58
|
|
59
|
public static final String WORKERS = "WORKERS";
|
60
|
|
61
|
|
62
|
|
63
|
public DnetProvider() {
|
64
|
super(3);
|
65
|
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public ResultSet<ValidationObject> getValidationObjects() throws ProviderException {
|
70
|
return new DnetRecordResultSet();
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public ResultSet<String> getValidationObjectIds() throws ProviderException,
|
75
|
UnsupportedOperationException {
|
76
|
// TODO Auto-generated method stub
|
77
|
return null;
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public ValidationObject getValidationObject(String valObjId)
|
82
|
throws ProviderException, UnsupportedOperationException {
|
83
|
// TODO Auto-generated method stub
|
84
|
return null;
|
85
|
}
|
86
|
|
87
|
public static void printXmlDocument(Document document) {
|
88
|
DOMImplementationLS domImplementationLS =
|
89
|
(DOMImplementationLS) document.getImplementation();
|
90
|
LSSerializer lsSerializer =
|
91
|
domImplementationLS.createLSSerializer();
|
92
|
String string = lsSerializer.writeToString(document);
|
93
|
System.out.println(string);
|
94
|
}
|
95
|
|
96
|
private class DnetResultSet {
|
97
|
|
98
|
gr.uoa.di.driver.enabling.resultset.ResultSet<String> rs = null;
|
99
|
|
100
|
protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
101
|
protected DocumentBuilder builder;
|
102
|
protected XPathFactory xfactory = XPathFactory.newInstance();
|
103
|
protected List<Node> records = null;
|
104
|
protected int records_sum = 0;
|
105
|
protected int index = -1;
|
106
|
protected int pointer = 0;
|
107
|
protected int recordsNum = 0;
|
108
|
protected int workers = 0;
|
109
|
protected int workerId = 0;
|
110
|
protected int beginRecord = 0;
|
111
|
protected int endRecord = 0;
|
112
|
protected String error = null;
|
113
|
|
114
|
public DnetResultSet() {
|
115
|
|
116
|
try {
|
117
|
builder = factory.newDocumentBuilder();
|
118
|
log.debug("Retrieving the datasource..");
|
119
|
|
120
|
log.debug("RECORDS " + pros.getProperty(RECORDS));
|
121
|
log.debug("MDSTORE_ID " + pros.getProperty(MDSTORE_ID));
|
122
|
log.debug("DATASOURCE: " + pros.getProperty(DATASOURCE));
|
123
|
log.debug("WORKER_ID: " + pros.getProperty(WORKER_ID));
|
124
|
workerId = Integer.parseInt(pros.getProperty(WORKER_ID));
|
125
|
workers = Integer.parseInt(pros.getProperty(WORKERS));
|
126
|
// log.debug("BEGIN RECORD: " + pros.getProperty(BEGIN_RECORD));
|
127
|
// EPR epr = mdStoreServiceServiceLocator.getService().deliverMDRecords(pros.getProperty(MD_ID), pros.getProperty(FROM), pros.getProperty(UNTIL), pros.getProperty(RECORD_FILTER));
|
128
|
log.debug("Issuing request on mdstore: " + pros.getProperty(MDSTORE_ID));
|
129
|
EPR epr = mdStoreServiceLocator.getService().deliverMDRecords(pros.getProperty(MDSTORE_ID), null, null, null);
|
130
|
|
131
|
rs = rsFactory.createResultSet(epr);
|
132
|
|
133
|
log.debug("rs created");
|
134
|
records_sum = rs.size();
|
135
|
log.debug("Number of records in ResultSet: " + records_sum);
|
136
|
if (pros.getProperty(RECORDS).equalsIgnoreCase("-1") ) {
|
137
|
pros.setProperty(RECORDS,Integer.toString(records_sum));
|
138
|
} else if (Integer.parseInt(pros.getProperty(RECORDS)) > records_sum) {
|
139
|
pros.setProperty(RECORDS,Integer.toString(records_sum));
|
140
|
}
|
141
|
recordsNum = Integer.parseInt(pros.getProperty(RECORDS));
|
142
|
log.error("W"+ workerId + "# RECORDS TO TEST: " + recordsNum);
|
143
|
log.error("W"+ workerId + "# WORKERS: " + workers);
|
144
|
// pros.setProperty(DnetProvider.BEGIN_RECORD, Integer.toString(Integer.parseInt(pros.getProperty(WORKER_ID)) * (Integer.parseInt(pros.getProperty(RECORDS))/Integer.parseInt(pros.getProperty(WORKERS)))));
|
145
|
beginRecord = workerId * (recordsNum/workers);
|
146
|
endRecord = (recordsNum/workers) + beginRecord;
|
147
|
// endRecord = Integer.parseInt(pros.getProperty(RECORDS))/Integer.parseInt(pros.getProperty(WORKERS)) + Integer.parseInt(pros.getProperty(BEGIN_RECORD));
|
148
|
if (workerId == workers-1)
|
149
|
endRecord += recordsNum % workers;
|
150
|
log.error("W"+ workerId + "# BEGIN RECORD: " + beginRecord);
|
151
|
log.error("W"+ workerId + "# END RECORD: " + endRecord);
|
152
|
pointer = beginRecord;
|
153
|
} catch (ParserConfigurationException e) {
|
154
|
log.error("", e);
|
155
|
} catch (Exception e) {
|
156
|
log.error("", e);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
protected List<Node> getRecords() throws DataException {
|
161
|
List<Node> records = new ArrayList<Node>();
|
162
|
|
163
|
try {
|
164
|
int to = pointer + Integer.parseInt(pros.getProperty(BATCH_SIZE)) -1 ;
|
165
|
log.error("to : " + to + " and limit: " + endRecord);
|
166
|
if (to < endRecord) {
|
167
|
log.error("Issuing request for records. From : " + pointer + " to : " + to);
|
168
|
List<String> tempRecords = rs.get(pointer, to);
|
169
|
pointer += Integer.parseInt(pros.getProperty(BATCH_SIZE));
|
170
|
for (String record : tempRecords) {
|
171
|
// log.debug("record from resultSet is : " + record);
|
172
|
InputSource is = new InputSource(new StringReader(record));
|
173
|
Document doc = builder.parse(is);
|
174
|
XPath xpath = xfactory.newXPath();
|
175
|
XPathExpression expr = xpath.compile("//*[local-name()='record']");
|
176
|
records.add((Node) expr.evaluate(doc, XPathConstants.NODE));
|
177
|
}
|
178
|
}
|
179
|
if (records.size() == 0) {
|
180
|
log.debug("There are no records. ");
|
181
|
error = "There are no records";
|
182
|
log.debug("Error: "+ error);
|
183
|
}
|
184
|
|
185
|
} catch (Exception e) {
|
186
|
log.error("", e);
|
187
|
throw new DataException();
|
188
|
}
|
189
|
return records;
|
190
|
}
|
191
|
}
|
192
|
|
193
|
private class DnetRecordResultSet extends DnetResultSet implements ResultSet<ValidationObject> {
|
194
|
|
195
|
@Override
|
196
|
public String getError() {
|
197
|
if (error != null)
|
198
|
log.debug("An error occured "+ this.error);
|
199
|
else
|
200
|
log.debug("No errors on request");
|
201
|
return this.error;
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
public boolean next() throws DataException {
|
206
|
index++;
|
207
|
log.debug("Moving cursor to result "+index);
|
208
|
if (records == null || index >= records.size()) {
|
209
|
if (records != null && (records.size() == 0))
|
210
|
return false;
|
211
|
index = -1;
|
212
|
records = getRecords();
|
213
|
return next();
|
214
|
}
|
215
|
return true;
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
public ValidationObject get() throws DataException {
|
220
|
XMLTextValidationObject ret = null;
|
221
|
|
222
|
Document newXmlDocument;
|
223
|
try {
|
224
|
newXmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
225
|
|
226
|
Element root = newXmlDocument.createElement("root");
|
227
|
newXmlDocument.appendChild(root);
|
228
|
Node node = records.get(index);
|
229
|
Node copyNode = newXmlDocument.importNode(node, true);
|
230
|
root.appendChild(copyNode);
|
231
|
printXmlDocument(newXmlDocument);
|
232
|
ret = new XMLTextValidationObject(newXmlDocument);
|
233
|
XPathFactory factory = XPathFactory.newInstance();
|
234
|
XPath xPath = factory.newXPath();
|
235
|
ret.setId(xPath.evaluate("//*[local-name()='header']/*[name()='dri:recordIdentifier']/text()", records.get(index)));
|
236
|
ret.setStatus(xPath.evaluate("//*[local-name()='header']/@status", records.get(index)));
|
237
|
log.debug("record id: " + ret.getId());
|
238
|
log.debug("record status: " + ret.getStatus());
|
239
|
|
240
|
} catch (ParserConfigurationException e) {
|
241
|
log.error("error getting object"+ e);
|
242
|
} catch (XPathExpressionException e) {
|
243
|
log.error("error getting object"+ e);
|
244
|
}
|
245
|
return ret;
|
246
|
}
|
247
|
|
248
|
|
249
|
}
|
250
|
|
251
|
|
252
|
public static ResultSetFactory getRsFactory() {
|
253
|
return rsFactory;
|
254
|
}
|
255
|
|
256
|
public static void setRsFactory(ResultSetFactory rsFactory) {
|
257
|
DnetProvider.rsFactory = rsFactory;
|
258
|
}
|
259
|
|
260
|
public static ServiceLocator<MDStoreService> getMdStoreServiceLocator() {
|
261
|
return mdStoreServiceLocator;
|
262
|
}
|
263
|
|
264
|
public static void setMdStoreServiceLocator(
|
265
|
ServiceLocator<MDStoreService> mdStoreServiceLocator) {
|
266
|
DnetProvider.mdStoreServiceLocator = mdStoreServiceLocator;
|
267
|
}
|
268
|
|
269
|
@Override
|
270
|
public ResultSet<ValidationObject> getValidationObjects(String entity)
|
271
|
throws ProviderException {
|
272
|
// TODO Auto-generated method stub
|
273
|
return null;
|
274
|
}
|
275
|
|
276
|
|
277
|
}
|