Project

General

Profile

1
package eu.dnetlib.resolver;
2

    
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import eu.dnetlib.dli.DLIUtils;
8
import eu.dnetlib.resolver.model.CompletionStatus;
9
import eu.dnetlib.resolver.model.ObjectProvenance;
10
import eu.dnetlib.resolver.model.ObjectProvisionMode;
11
import eu.dnetlib.resolver.model.ResolvedObject;
12
import net.sf.ehcache.Cache;
13
import net.sf.ehcache.Element;
14
import org.apache.commons.lang3.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.apache.http.HttpEntity;
18
import org.apache.http.client.ResponseHandler;
19
import org.apache.http.client.methods.HttpGet;
20
import org.apache.http.impl.client.CloseableHttpClient;
21
import org.apache.http.impl.client.HttpClients;
22
import org.apache.http.util.EntityUtils;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Required;
25

    
26
/**
27
 * Created by sandro on 9/8/16.
28
 */
29
public abstract class AbstractPIDResolver implements PIDResolver {
30

    
31
	public static final Map<String, String> resolvedTypes = new HashMap<String, String>() {
32
		{
33
			put("pdb", "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s");
34
			put("ncbi-n", "http://www.ncbi.nlm.nih.gov/gquery/?term=%s");
35
			put("pmid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
36
			put("pmcid", "http://www.ncbi.nlm.nih.gov/pmc/articles/%s");
37
			put("pubmedid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
38
			put("doi", "http://dx.doi.org/%s");
39
			put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
40
			put("nuccore", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
41
			put("swiss-prot", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
42
			put("arrayexpress", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
43
			put("biomodels", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
44
			put("bmrb", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
45
			put("ena", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
46
			put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
47
			put("geo", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
48
			put("ensembl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
49
			put("mgi", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
50
			put("bind", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
51
			put("pride", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
52
			put("ddbj", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
53
			put("bioproject", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
54
			put("embl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
55
			put("sra", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
56
		}
57

    
58
	};
59
	private static final Log log = LogFactory.getLog(AbstractPIDResolver.class);
60
	private Cache cache;
61
	private String name;
62
	@Autowired
63
	private ResolverStore storeManager;
64
	private ResponseHandler<String> responseHandler = response -> {
65
		int status = response.getStatusLine().getStatusCode();
66
		if (status >= 200 && status < 300) {
67
			HttpEntity entity = response.getEntity();
68
			return entity != null ? EntityUtils.toString(entity) : null;
69
		} else {
70
			log.debug("Unexpected response status: " + status);
71
			return null;
72
		}
73
	};
74

    
75
	public static void setDatasourceProvenace(final ObjectProvenance provenance, final String namespacePrefix) {
76
		try {
77
			provenance.setDatasourceId(DLIUtils.getIdFromDataSourcePrefix(namespacePrefix));
78
			provenance.setDatasource(DLIUtils.getNameFromDataSourcePrefix(namespacePrefix));
79
			provenance.setCompletionStatus(CompletionStatus.complete.toString());
80
			provenance.setProvisionMode(ObjectProvisionMode.resolved.toString());
81
		} catch (Throwable e) {
82
			log.error("Error on assignin datasource ");
83
		}
84
	}
85

    
86
	public ResolvedObject retrievePID(final String pid, final String pidType) {
87

    
88
		if (!canResolvePid(pidType)) return null;
89
		final Element item = cache.get(storeManager.generateDNetIdentifier(pid, pidType));
90

    
91
		if (item != null) return (ResolvedObject) item.getObjectValue();
92

    
93
		ResolvedObject resolvedObject = storeManager.getRecord(pid, pidType);
94

    
95
		if (resolvedObject != null) {
96
			cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
97
			return resolvedObject;
98
		}
99
		resolvedObject = resolve(pid, pidType);
100
		if (resolvedObject != null) {
101
			storeManager.insertRecord(getName(), resolvedObject);
102
			cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
103
			return resolvedObject;
104
		}
105
		return null;
106
	}
107

    
108
	public String retrieveDnetID(final String pid, final String pidType) {
109

    
110
		if (!canResolvePid(pidType)) return null;
111
		final String dnetIdentifier = storeManager.generateDNetIdentifier(pid, pidType);
112
		final Element item = cache.get(dnetIdentifier);
113

    
114
		if (item != null) return dnetIdentifier;
115

    
116
		String resolvedIdentifier = storeManager.getRecordIdentifier(pid, pidType);
117

    
118
		if (resolvedIdentifier != null && !StringUtils.isBlank(resolvedIdentifier))
119
			return resolvedIdentifier;
120

    
121
		ResolvedObject resolvedObject = resolve(pid, pidType);
122
		if (resolvedObject != null) {
123
			storeManager.insertRecord(getName(), resolvedObject);
124
			cache.put(new Element(dnetIdentifier, resolvedObject));
125
			return dnetIdentifier;
126
		}
127

    
128
		return null;
129
	}
130

    
131
	public String getName() {
132
		return this.getClass().getSimpleName();
133
	}
134

    
135
	public void setName(final String name) {
136
		this.name = name;
137
	}
138

    
139
	protected abstract boolean canResolvePid(final String pidType);
140

    
141
	protected abstract ResolvedObject resolve(final String pid, final String pidType);
142

    
143
	public Cache getCache() {
144
		return cache;
145
	}
146

    
147
	@Required
148
	public void setCache(final Cache cache) {
149
		this.cache = cache;
150
	}
151

    
152
	protected String requestURL(final String url) {
153
		final CloseableHttpClient httpclient = HttpClients.createDefault();
154
		try {
155

    
156
			HttpGet httpGet = new HttpGet(url);
157
			return httpclient.execute(httpGet, responseHandler);
158
		} catch (Throwable e) {
159
			log.debug("Error in getting url " + url, e);
160
			return null;
161
		} finally {
162
			try {
163
				httpclient.close();
164
			} catch (IOException e) {
165
				log.error("Error on closing httpclient", e);
166
			}
167
		}
168
	}
169
}
(2-2/14)