Revision 39370
Added by Alessia Bardi almost 9 years ago
BlacklistManager.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.openaire.blacklist; |
2 | 2 |
|
3 | 3 |
import java.io.StringReader; |
4 |
import java.util.Iterator; |
|
5 |
import java.util.List; |
|
4 | 6 |
|
5 | 7 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
6 |
import javax.xml.xpath.XPath; |
|
7 |
import javax.xml.xpath.XPathExpressionException; |
|
8 |
import javax.xml.xpath.XPathFactory; |
|
9 | 8 |
|
10 | 9 |
import org.apache.commons.lang.StringUtils; |
11 | 10 |
import org.apache.commons.logging.Log; |
12 | 11 |
import org.apache.commons.logging.LogFactory; |
12 |
import org.dom4j.Document; |
|
13 |
import org.dom4j.DocumentException; |
|
14 |
import org.dom4j.Element; |
|
15 |
import org.dom4j.io.SAXReader; |
|
13 | 16 |
import org.springframework.beans.factory.annotation.Autowired; |
14 | 17 |
import org.springframework.beans.factory.annotation.Value; |
15 |
import org.xml.sax.InputSource; |
|
16 | 18 |
|
17 | 19 |
import eu.dnetlib.enabling.database.rmi.DatabaseService; |
18 | 20 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
... | ... | |
41 | 43 |
@Autowired |
42 | 44 |
private ResultSetClientFactory resultSetClientFactory; |
43 | 45 |
|
44 |
public Iterable<BlacklistEntry> getBlacklist() {
|
|
46 |
public Iterator<BlacklistEntry> getBlacklist() {
|
|
45 | 47 |
String sqlQuery = "SELECT * from blacklist"; |
46 | 48 |
// TODO: ask the database service to get the blacklist entries and translate them into pojo instances. |
47 | 49 |
W3CEndpointReference epr = serviceLocator.getService(DatabaseService.class).searchSQL(blacklistDatabaseName, sqlQuery); |
48 | 50 |
|
49 | 51 |
IterableResultSetClient iter = resultSetClientFactory.getClient(epr); |
52 |
return this.getBlacklistIterator(iter); |
|
53 |
} |
|
50 | 54 |
|
51 |
return new MappedCollection<BlacklistEntry, String>(iter, new UnaryFunction<BlacklistEntry, String>() { |
|
55 |
public Iterator<BlacklistEntry> getBlacklistIterator(final Iterable<String> xmlEntries) { |
|
56 |
return new MappedCollection<BlacklistEntry, String>(xmlEntries, new UnaryFunction<BlacklistEntry, String>() { |
|
52 | 57 |
|
53 | 58 |
@Override |
54 | 59 |
public BlacklistEntry evaluate(final String dbEntry) { |
60 |
SAXReader saxReader = new SAXReader(); |
|
55 | 61 |
BlacklistEntry be = new BlacklistEntry(); |
62 |
Document doc; |
|
56 | 63 |
try { |
57 |
|
|
58 |
InputSource doc = new InputSource(new StringReader(dbEntry)); |
|
59 |
XPath xpath = XPathFactory.newInstance().newXPath(); |
|
60 |
String provenance = xpath.evaluate("//FIELD[./@name='provenance']", doc); |
|
64 |
doc = saxReader.read(new StringReader(dbEntry)); |
|
65 |
be.setCreationDate(doc.selectSingleNode("//FIELD[./@name='creation_time']").getText()); |
|
66 |
be.setLastUpdateDate(doc.selectSingleNode("//FIELD[./@name='last_update_time']").getText()); |
|
67 |
be.setNote(doc.selectSingleNode("//FIELD[./@name='note']").getText()); |
|
68 |
be.setSemanticClass(doc.selectSingleNode(".//FIELD[./@name='semantic_class']").getText()); |
|
69 |
be.setSemanticScheme(doc.selectSingleNode("//FIELD[./@name='semantic_scheme']").getText()); |
|
70 |
be.setStatus(STATUS.valueOf(StringUtils.upperCase(doc.selectSingleNode("//FIELD[./@name='status']").getText()))); |
|
71 |
be.setTicketId(doc.selectSingleNode("//FIELD[./@name='ticket_id']").getText()); |
|
72 |
be.setUser(doc.selectSingleNode("//FIELD[./@name='userid']").getText()); |
|
73 |
be.setSourceObject(doc.selectSingleNode("//FIELD[./@name='source_object']").getText()); |
|
74 |
be.setTargetObject(doc.selectSingleNode("//FIELD[./@name='target_object']").getText()); |
|
75 |
String provenance = doc.selectSingleNode("//FIELD[./@name='provenance']").getText(); |
|
61 | 76 |
be.setProvenance(provenance); |
62 | 77 |
if (provenance.equalsIgnoreCase("iis")) { |
63 |
be.setIisModule(xpath.evaluate("//FIELD[./@name='iis_module']", doc));
|
|
64 |
be.setIisStatus(IIS_STATUS.valueOf(StringUtils.upperCase(xpath.evaluate("//FIELD[./@name='iis_status']", doc))));
|
|
78 |
be.setIisModule(doc.selectSingleNode("//FIELD[./@name='iis_module']").getText());
|
|
79 |
be.setIisStatus(IIS_STATUS.valueOf(StringUtils.upperCase(doc.selectSingleNode("//FIELD[./@name='iis_status']").getText())));
|
|
65 | 80 |
} |
66 |
// xpath.evaluate("//*[local-name()='objIdentifier']", doc) |
|
67 |
be.setCreationDate(xpath.evaluate("//FIELD[./@name='creation_time']", doc)); |
|
68 |
be.setLastUpdateDate(xpath.evaluate("//FIELD[./@name='last_update_time']", doc)); |
|
69 |
be.setNote(xpath.evaluate("//FIELD[./@name='note']", doc)); |
|
70 |
be.setSemanticClass(xpath.evaluate("//FIELD[./@name='semantic_class']", doc)); |
|
71 |
be.setSemanticScheme(xpath.evaluate("//FIELD[./@name='semantic_scheme']", doc)); |
|
72 |
be.setStatus(STATUS.valueOf(StringUtils.upperCase(xpath.evaluate("//FIELD[./@name='note']", doc)))); |
|
73 |
be.setTicketId(xpath.evaluate("//FIELD[./@name='ticket_id']", doc)); |
|
74 |
be.setUser(xpath.evaluate("//FIELD[./@name='userid']", doc)); |
|
75 |
be.setSourceObject(xpath.evaluate("//FIELD[./@name='source_object']", doc)); |
|
76 |
be.setTargetObject(xpath.evaluate("//FIELD[./@name='target_object']", doc)); |
|
77 |
// TODO: get the array of original identifiers |
|
78 |
// xpath.evaluate(("//FIELD[./@name='original_source_objects']/", doc,XPathConstants.NODESET); |
|
79 |
// xpath.evaluate(("//FIELD[./@name='target_source_objects']/", doc,XPathConstants.NODESET); |
|
81 |
// get the array of original identifiers |
|
82 |
List<Element> sources = doc.selectNodes("//FIELD[./@name='original_source_objects']/ITEM"); |
|
83 |
if ((sources != null) && !sources.isEmpty()) { |
|
84 |
for (Element e : sources) { |
|
85 |
be.getOriginalSourceObjects().add(e.getText()); |
|
86 |
} |
|
87 |
} |
|
80 | 88 |
|
81 |
} catch (XPathExpressionException e) { |
|
82 |
log.error("Can't parse blacklist entry from database:\n" + dbEntry); |
|
83 |
e.printStackTrace(); |
|
89 |
List<Element> targets = doc.selectNodes("//FIELD[./@name='original_target_objects']/ITEM"); |
|
90 |
if ((targets != null) && !targets.isEmpty()) { |
|
91 |
for (Element e : targets) { |
|
92 |
be.getOriginalTargetObjects().add(e.getText()); |
|
93 |
} |
|
94 |
} |
|
95 |
} catch (DocumentException e) { |
|
96 |
log.error(e); |
|
97 |
throw new RuntimeException(e); |
|
84 | 98 |
} |
85 | 99 |
return be; |
86 | 100 |
} |
87 |
}); |
|
101 |
}).iterator();
|
|
88 | 102 |
} |
89 | 103 |
} |
Also available in: Unified diff
Basic entry point to get the blacklist as json list