1
|
package eu.dnetlib.openaire.blacklist;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.common.base.Function;
|
7
|
import com.google.common.collect.Iterables;
|
8
|
import com.google.common.collect.Lists;
|
9
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
10
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
11
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12
|
|
13
|
import eu.dnetlib.functionality.index.client.solr.SolrIndexClient;
|
14
|
import eu.dnetlib.functionality.index.client.solr.SolrIndexClientFactory;
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
19
|
import org.apache.solr.common.SolrDocument;
|
20
|
import org.apache.solr.common.SolrDocumentList;
|
21
|
import org.dom4j.Attribute;
|
22
|
import org.dom4j.Document;
|
23
|
import org.dom4j.DocumentException;
|
24
|
import org.dom4j.io.SAXReader;
|
25
|
import org.springframework.beans.factory.annotation.Autowired;
|
26
|
|
27
|
/**
|
28
|
* The goal of this task is to return the original identifiers of objects merged in a representative object by deduplication. Created by
|
29
|
* alessia on 09/02/16.
|
30
|
*/
|
31
|
public class OpenaireIdResolver {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(OpenaireIdResolver.class);
|
34
|
public final static String SOLR_COLLECTION_POSTFIX = "-index-openaire";
|
35
|
private final static String RESULT_FIELD = "__result";
|
36
|
private final static String XPATH_TO_MERGED = "//*[local-name()='entity']/*//children/result/@objidentifier";
|
37
|
private final SAXReader saxReader = new SAXReader();
|
38
|
@Autowired
|
39
|
private UniqueServiceLocator serviceLocator;
|
40
|
@Autowired
|
41
|
private SolrIndexClientFactory indexClientFactory;
|
42
|
|
43
|
public List<String> resolveIdentifier(final String id) {
|
44
|
if (StringUtils.isBlank(id)) return Lists.newArrayList();
|
45
|
else {
|
46
|
return findOriginalIds(id);
|
47
|
}
|
48
|
}
|
49
|
|
50
|
protected List<String> findOriginalIds(final String id) {
|
51
|
try(final SolrIndexClient client = (SolrIndexClient) indexClientFactory.getClient(getPublicIndexCollection()+SOLR_COLLECTION_POSTFIX)) {
|
52
|
|
53
|
final String query = String.format("objidentifier:\"%s\"", id);
|
54
|
final QueryResponse response = client.query(query, 1);
|
55
|
final SolrDocumentList results = response.getResults();
|
56
|
if (results.isEmpty()) {
|
57
|
log.debug("Query " + query + " returned 0 documents");
|
58
|
return Lists.newArrayList();
|
59
|
}
|
60
|
// my results contain the document with the given identifier
|
61
|
final SolrDocument solrDoc = results.get(0);
|
62
|
return extractMergedIdentifiers(solrDoc);
|
63
|
} catch (final Exception e) {
|
64
|
log.error("Can't get original ids for " + id + "\n ", e);
|
65
|
throw new RuntimeException("Can't get original ids for " + id + "\n " + e);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
@SuppressWarnings("unchecked")
|
70
|
protected List<String> extractMergedIdentifiers(final SolrDocument doc) throws DocumentException {
|
71
|
final String xmlRecord = (String) doc.getFirstValue(RESULT_FIELD);
|
72
|
final Document xmlDoc = this.saxReader.read(new StringReader(xmlRecord));
|
73
|
return Lists.newArrayList(Iterables.transform(xmlDoc.selectNodes(XPATH_TO_MERGED), (Function<Attribute, String>) a -> a.getStringValue()));
|
74
|
}
|
75
|
|
76
|
protected String getIndexEndpoint() throws ISLookUpException {
|
77
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
78
|
"for $x in collection('/db/DRIVER/ServiceResources/IndexServiceResourceType') return $x//PROTOCOL[./@name='solr']/@address/string()");
|
79
|
}
|
80
|
|
81
|
protected String getPublicIndexCollection() throws ISLookUpException {
|
82
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
83
|
"for $x in collection('/db/DRIVER/ServiceResources/SearchServiceResourceType') return $x[.//PROPERTY[@key='infrastructure']/@value='public']//PROPERTY[@key='mdformat']/@value/string()");
|
84
|
}
|
85
|
|
86
|
public UniqueServiceLocator getServiceLocator() {
|
87
|
return serviceLocator;
|
88
|
}
|
89
|
|
90
|
public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
|
91
|
this.serviceLocator = serviceLocator;
|
92
|
}
|
93
|
|
94
|
}
|