1
|
package eu.dnetlib.dli.resolver;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import com.google.common.collect.Lists;
|
6
|
import com.google.gson.JsonArray;
|
7
|
import com.google.gson.JsonElement;
|
8
|
import com.google.gson.JsonObject;
|
9
|
import com.google.gson.JsonParser;
|
10
|
import eu.dnetlib.dli.resolver.model.*;
|
11
|
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
|
12
|
import eu.dnetlib.pid.resolver.model.ObjectProvenance;
|
13
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
|
17
|
/**
|
18
|
* The Class DataciteDOIResolver.
|
19
|
*/
|
20
|
|
21
|
public class DataciteResolver extends AbstractPIDResolver {
|
22
|
|
23
|
public static final String DATACITE_NS_PREFIX = "datacite____";
|
24
|
|
25
|
/**
|
26
|
* The Constant log.
|
27
|
*/
|
28
|
private static final Log log = LogFactory.getLog(DataciteResolver.class); // NOPMD by marko on 11/24/08 5:02 PM
|
29
|
|
30
|
/**
|
31
|
* The Constant baseURL.
|
32
|
*/
|
33
|
private final static String baseURL = "http://search.datacite.org/api?wt=json&q=doi:";
|
34
|
|
35
|
private DLIResolvedObject parseResponse(final String response) {
|
36
|
if (response == null) return null;
|
37
|
JsonElement jElement = new JsonParser().parse(response);
|
38
|
JsonObject jobject = jElement.getAsJsonObject();
|
39
|
if (jobject.has("response")) {
|
40
|
|
41
|
long total = jobject.get("response").getAsJsonObject().get("numFound").getAsLong();
|
42
|
|
43
|
if (total == 0) return null;
|
44
|
DLIResolvedObject responseObj = new DLIResolvedObject();
|
45
|
|
46
|
JsonElement hits = ((JsonObject) jobject.get("response")).get("docs");
|
47
|
|
48
|
JsonArray hitsObject = hits.getAsJsonArray();
|
49
|
|
50
|
for (final JsonElement elem : hitsObject) {
|
51
|
String doi = ((JsonObject) elem).get("doi").getAsString();
|
52
|
responseObj.setPid(doi);
|
53
|
responseObj.setPidType("DOI");
|
54
|
responseObj.setType(ObjectType.dataset);
|
55
|
List<String> authors = Lists.newArrayList();
|
56
|
List<String> titles = Lists.newArrayList();
|
57
|
List<SubjectType> subjects = Lists.newArrayList();
|
58
|
|
59
|
|
60
|
// ADDING SUBJECTS
|
61
|
final JsonElement subjectElement = ((JsonObject) elem).get("subject");
|
62
|
if (subjectElement != null) {
|
63
|
JsonArray subjectArray = subjectElement.getAsJsonArray();
|
64
|
for (int i = 0; i < subjectArray.size(); i++) {
|
65
|
subjects.add(new SubjectType("unknown", subjectArray.get(i).getAsString()));
|
66
|
}
|
67
|
}
|
68
|
responseObj.setSubjects(subjects);
|
69
|
|
70
|
// ADDING TITLES
|
71
|
JsonElement titleElement = ((JsonObject) elem).get("title");
|
72
|
if (titleElement != null) {
|
73
|
JsonArray titleArray = titleElement.getAsJsonArray();
|
74
|
for (int i = 0; i < titleArray.size(); i++) {
|
75
|
titles.add(titleArray.get(i).getAsString());
|
76
|
}
|
77
|
}
|
78
|
responseObj.setTitles(titles);
|
79
|
|
80
|
//ADDING DESCRIPTION
|
81
|
|
82
|
JsonElement descriptionElement = ((JsonObject) elem).get("description");
|
83
|
if (descriptionElement != null) {
|
84
|
JsonArray descriptionArray = descriptionElement.getAsJsonArray();
|
85
|
if (descriptionArray.size() > 0)
|
86
|
responseObj.setDescription(descriptionArray.get(0).getAsString());
|
87
|
}
|
88
|
|
89
|
// ADDING AUTHORS
|
90
|
JsonElement contributorElement = ((JsonObject) elem).get("contributor");
|
91
|
if (contributorElement != null) {
|
92
|
JsonArray contributorArray = contributorElement.getAsJsonArray();
|
93
|
for (int i = 0; i < contributorArray.size(); i++) {
|
94
|
authors.add(contributorArray.get(i).getAsString());
|
95
|
}
|
96
|
}
|
97
|
// ADDING AUTHORS
|
98
|
JsonElement creatorElement = ((JsonObject) elem).get("creator");
|
99
|
if (creatorElement != null) {
|
100
|
JsonArray creatorArray = creatorElement.getAsJsonArray();
|
101
|
for (int i = 0; i < creatorArray.size(); i++) {
|
102
|
authors.add(creatorArray.get(i).getAsString());
|
103
|
}
|
104
|
}
|
105
|
responseObj.setAuthors(authors);
|
106
|
|
107
|
final DLIObjectProvenance provenance = new DLIObjectProvenance();
|
108
|
DLIPIDResolver.setDatasourceProvenance(provenance, DATACITE_NS_PREFIX);
|
109
|
|
110
|
final String dataCenter_symbol = ((JsonObject) elem).get("datacentre_symbol").getAsString();
|
111
|
final String datacenter = ((JsonObject) elem).get("datacentre").getAsString();
|
112
|
if (datacenter != null) {
|
113
|
final String publisher = datacenter.replace(dataCenter_symbol, "").replace("-", "").trim();
|
114
|
if (publisher != null && publisher.length() > 0) {
|
115
|
provenance.setPublisher(publisher);
|
116
|
}
|
117
|
}
|
118
|
responseObj.setDatasourceProvenance(Lists.newArrayList(provenance));
|
119
|
responseObj.setCompletionStatus(CompletionStatus.complete.toString());
|
120
|
return responseObj;
|
121
|
}
|
122
|
return null;
|
123
|
}
|
124
|
return null;
|
125
|
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
protected boolean canResolvePid(final String pidType) {
|
130
|
return (pidType != null) && ("doi".equals(pidType.toLowerCase().trim()) || "handle".equals(pidType.toLowerCase().trim()));
|
131
|
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
protected DLIResolvedObject resolve(final String pid, final String pidType) {
|
136
|
try {
|
137
|
final String response = requestURL(baseURL + pid.replace(" ", "%20"));
|
138
|
return parseResponse(response);
|
139
|
} catch (Throwable e) {
|
140
|
log.error("Error on getting item from url " + baseURL + pid.replace(" ", "%20"));
|
141
|
}
|
142
|
return null;
|
143
|
}
|
144
|
}
|