1
|
package eu.dnetlib.dli.resolver;
|
2
|
|
3
|
import com.google.common.collect.Lists;
|
4
|
import com.google.gson.JsonArray;
|
5
|
import com.google.gson.JsonElement;
|
6
|
import com.google.gson.JsonObject;
|
7
|
import com.google.gson.JsonParser;
|
8
|
import eu.dnetlib.dli.resolver.model.DLIObjectProvenance;
|
9
|
import eu.dnetlib.dli.resolver.model.DLIResolvedObject;
|
10
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
11
|
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
|
15
|
import java.util.List;
|
16
|
|
17
|
/**
|
18
|
* @author sandro
|
19
|
*/
|
20
|
public class PubMedResolver extends AbstractPIDResolver {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(PubMedResolver.class); // NOPMD by marko on 11/24/08 5:02 PM
|
23
|
|
24
|
private static final String query = "http://www.ebi.ac.uk/europepmc/webservices/rest/search/query=%s&format=json";
|
25
|
|
26
|
private static final String PUBMED_NS_PREFIX = "pubmed______";
|
27
|
|
28
|
@Override
|
29
|
protected boolean canResolvePid(final String pidType) {
|
30
|
return pidType != null && (pidType.toLowerCase().trim().equals("pubmedid") || pidType.toLowerCase().trim().equals("pmcid"));
|
31
|
}
|
32
|
|
33
|
@Override
|
34
|
protected DLIResolvedObject resolve(final String pid, final String pidType) {
|
35
|
String requestUrl = null;
|
36
|
String type = null;
|
37
|
if (pidType.toLowerCase().trim().equals("pubmedid")) {
|
38
|
requestUrl = String.format(query, "ext_id:" + pid);
|
39
|
type = "pubmedid";
|
40
|
} else if (pidType.toLowerCase().trim().equals("pmcid")) {
|
41
|
requestUrl = String.format(query, "pmcid:" + pid);
|
42
|
type = "pmcid";
|
43
|
}
|
44
|
if (requestUrl == null)
|
45
|
return null;
|
46
|
try {
|
47
|
String result = requestURL(requestUrl);
|
48
|
|
49
|
return parsingResponse(result, type);
|
50
|
} catch (Throwable e) {
|
51
|
log.error(String.format("Error on resolving pid %s type:%s requestUrl:%s", pid, pidType, requestUrl), e);
|
52
|
}
|
53
|
return null;
|
54
|
}
|
55
|
|
56
|
private DLIResolvedObject parsingResponse(final String response, final String type) {
|
57
|
if (response == null) return null;
|
58
|
JsonElement jElement = new JsonParser().parse(response);
|
59
|
JsonObject jobject = jElement.getAsJsonObject();
|
60
|
if (jobject.has("hitCount")) {
|
61
|
|
62
|
long total = jobject.get("hitCount").getAsLong();
|
63
|
|
64
|
if (total == 0) return null;
|
65
|
DLIResolvedObject responseObj = new DLIResolvedObject();
|
66
|
|
67
|
JsonElement hits = ((JsonObject) jobject.get("resultList")).get("result");
|
68
|
|
69
|
JsonArray hitsObject = hits.getAsJsonArray();
|
70
|
|
71
|
for (JsonElement elem : hitsObject) {
|
72
|
String idType;
|
73
|
if (type.equals("pmcid")) {
|
74
|
idType = type;
|
75
|
} else {
|
76
|
idType = "pmid";
|
77
|
}
|
78
|
|
79
|
if (!((JsonObject) elem).has(idType)) {
|
80
|
continue;
|
81
|
}
|
82
|
|
83
|
String doi = ((JsonObject) elem).get(idType).getAsString();
|
84
|
responseObj.setPid(doi);
|
85
|
responseObj.setPidType(type);
|
86
|
responseObj.setType(ObjectType.publication);
|
87
|
List<String> authors = Lists.newArrayList();
|
88
|
List<String> titles = Lists.newArrayList();
|
89
|
|
90
|
// ADDING TITLES
|
91
|
JsonElement titleElement = ((JsonObject) elem).get("title");
|
92
|
if (titleElement != null) {
|
93
|
String titleString = titleElement.getAsString();
|
94
|
titles.add(titleString);
|
95
|
}
|
96
|
responseObj.setTitles(titles);
|
97
|
// ADDING AUTHORS
|
98
|
JsonElement contributorElement = ((JsonObject) elem).get("authorString");
|
99
|
if (contributorElement != null) {
|
100
|
String contributorString = contributorElement.getAsString();
|
101
|
if (contributorString != null) {
|
102
|
String[] splittedAuthors = contributorString.split(",");
|
103
|
|
104
|
for (int i = 0; i < splittedAuthors.length; i++) {
|
105
|
authors.add(splittedAuthors[i]);
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
responseObj.setAuthors(authors);
|
110
|
|
111
|
JsonElement date = ((JsonObject) elem).get("pubYear");
|
112
|
|
113
|
if (date.isJsonNull() == false) {
|
114
|
responseObj.setDate(date.getAsString());
|
115
|
}
|
116
|
DLIObjectProvenance provenance = new DLIObjectProvenance(PUBMED_NS_PREFIX, "resolved", "complete", null, null, true);
|
117
|
responseObj.fixContribution(provenance);
|
118
|
responseObj.setDatasourceProvenance(Lists.newArrayList(provenance));
|
119
|
return responseObj;
|
120
|
|
121
|
}
|
122
|
|
123
|
return null;
|
124
|
}
|
125
|
return null;
|
126
|
|
127
|
}
|
128
|
}
|