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

    
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
	private final static String RESULT_FIELD = "__result";
35
	private final static String XPATH_TO_MERGED = "//*[local-name()='entity']/*//children/result/@objidentifier";
36
	private final SAXReader saxReader = new SAXReader();
37
	@Autowired
38
	private UniqueServiceLocator serviceLocator;
39
	@Autowired
40
	private SolrIndexClientFactory indexClientFactory;
41

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

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

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

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

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

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

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

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

    
93
}
(3-3/3)