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 org.apache.commons.lang.StringUtils;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.apache.solr.client.solrj.SolrQuery;
16
import org.apache.solr.client.solrj.impl.CloudSolrServer;
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.
28
 * Created by alessia on 09/02/16.
29
 */
30
public class OpenaireIdResolver {
31

    
32
	public final static String SOLR_COLLECTION_POSTFIX = "-index-openaire";
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

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

    
47
	protected List<String> findOriginalIds(final String id) {
48
		CloudSolrServer solrCore = null;
49
		SolrQuery q = new SolrQuery("objidentifier:\"" + id + "\"");
50
		QueryResponse response = null;
51
		try {
52
			solrCore = new CloudSolrServer(getIndexEndpoint());
53
			solrCore.setDefaultCollection(getPublicIndexCollection() + SOLR_COLLECTION_POSTFIX);
54
			response = solrCore.query(q);
55
			SolrDocumentList results = response.getResults();
56
			if (results.isEmpty()) {
57
				log.debug("Query " + q + " returned 0 documents");
58
				return Lists.newArrayList();
59
			}
60
			//my results contain the document with the given identifier
61
			SolrDocument solrDoc = results.get(0);
62
			return extractMergedIdentifiers(solrDoc);
63
		} catch (Exception e) {
64
			throw new RuntimeException("Can't get original ids for " + id + "\n " + e);
65
		} finally {
66
			solrCore.shutdown();
67
		}
68
	}
69

    
70
	protected List<String> extractMergedIdentifiers(final SolrDocument doc) throws DocumentException {
71
		List<String> originals = Lists.newArrayList();
72
		String xmlRecord = (String) doc.getFirstValue(RESULT_FIELD);
73
		Document xmlDoc = this.saxReader.read(new StringReader(xmlRecord));
74
		return Lists.newArrayList(Iterables.transform(xmlDoc.selectNodes(XPATH_TO_MERGED), new Function<Attribute, String>() {
75
			@Override
76
			public String apply(final Attribute a) {
77
				return a.getStringValue();
78
			}
79
		}));
80
	}
81

    
82
	protected String getIndexEndpoint() throws ISLookUpException {
83
		return this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(
84
				"for $x in collection('/db/DRIVER/ServiceResources/IndexServiceResourceType') return $x//PROTOCOL[./@name='solr']/@address/string()");
85
	}
86

    
87
	protected String getPublicIndexCollection() throws ISLookUpException {
88
		return this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(
89
				"for $x in collection('/db/DRIVER/ServiceResources/SearchServiceResourceType') return $x[.//PROPERTY[@key='infrastructure']/@value='public']//PROPERTY[@key='mdformat']/@value/string()");
90
	}
91

    
92
	public UniqueServiceLocator getServiceLocator() {
93
		return serviceLocator;
94
	}
95

    
96
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
97
		this.serviceLocator = serviceLocator;
98
	}
99

    
100
}
(3-3/3)