Project

General

Profile

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
import eu.dnetlib.functionality.index.client.solr.SolrIndexClient;
13
import eu.dnetlib.functionality.index.client.solr.SolrIndexClientFactory;
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.apache.solr.client.solrj.response.QueryResponse;
18
import org.apache.solr.common.SolrDocument;
19
import org.apache.solr.common.SolrDocumentList;
20
import org.dom4j.Attribute;
21
import org.dom4j.Document;
22
import org.dom4j.DocumentException;
23
import org.dom4j.io.SAXReader;
24
import org.springframework.beans.factory.annotation.Autowired;
25

    
26
/**
27
 * The goal of this task is to return the original identifiers of objects merged in a representative object by deduplication. Created by
28
 * alessia on 09/02/16.
29
 */
30
public class OpenaireIdResolver {
31

    
32
	private static final Log log = LogFactory.getLog(OpenaireIdResolver.class);
33
	private final static String RESULT_FIELD = "__result";
34
	private final static String XPATH_TO_MERGED = "//*[local-name()='entity']/*//children/result/@objidentifier";
35
	private final SAXReader saxReader = new SAXReader();
36
	@Autowired
37
	private UniqueServiceLocator serviceLocator;
38
	@Autowired
39
	private SolrIndexClientFactory indexClientFactory;
40

    
41
	public List<String> resolveIdentifier(final String id) {
42
		if (StringUtils.isBlank(id)) return Lists.newArrayList();
43
		else {
44
			return findOriginalIds(id);
45
		}
46
	}
47

    
48
	protected List<String> findOriginalIds(final String id) {
49
		try(final SolrIndexClient client = (SolrIndexClient) indexClientFactory.getClient(getPublicIndexCollection())) {
50

    
51
			final String query = String.format("objidentifier:\"%s\"", id);
52
			final QueryResponse response = client.query(query, 1);
53
			final SolrDocumentList results = response.getResults();
54
			if (results.isEmpty()) {
55
				log.debug("Query " + query + " returned 0 documents");
56
				return Lists.newArrayList();
57
			}
58
			// my results contain the document with the given identifier
59
			final SolrDocument solrDoc = results.get(0);
60
			return extractMergedIdentifiers(solrDoc);
61
		} catch (final Exception e) {
62
			log.error("Can't get original ids for " + id + "\n ", e);
63
			throw new RuntimeException("Can't get original ids for " + id + "\n " + e);
64
		}
65
	}
66

    
67
	@SuppressWarnings("unchecked")
68
	protected List<String> extractMergedIdentifiers(final SolrDocument doc) throws DocumentException {
69
		final String xmlRecord = (String) doc.getFirstValue(RESULT_FIELD);
70
		final Document xmlDoc = this.saxReader.read(new StringReader(xmlRecord));
71
		return Lists.newArrayList(Iterables.transform(xmlDoc.selectNodes(XPATH_TO_MERGED), (Function<Attribute, String>) a -> a.getStringValue()));
72
	}
73

    
74
	protected String getIndexEndpoint() throws ISLookUpException {
75
		return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
76
				"for $x in collection('/db/DRIVER/ServiceResources/IndexServiceResourceType') return $x//PROTOCOL[./@name='solr']/@address/string()");
77
	}
78

    
79
	protected String getPublicIndexCollection() throws ISLookUpException {
80
		return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
81
				"for $x in collection('/db/DRIVER/ServiceResources/SearchServiceResourceType') return $x[.//PROPERTY[@key='infrastructure']/@value='public']//PROPERTY[@key='mdformat']/@value/string()");
82
	}
83

    
84
	public UniqueServiceLocator getServiceLocator() {
85
		return serviceLocator;
86
	}
87

    
88
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
89
		this.serviceLocator = serviceLocator;
90
	}
91

    
92
}
(3-3/3)