1 |
44356
|
claudio.at
|
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 |
49184
|
sandro.lab
|
import eu.dnetlib.pid.resolver.AbstractPIDResolver;
|
12 |
|
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
13 |
49288
|
sandro.lab
|
import eu.dnetlib.pid.resolver.model.SubjectType;
|
14 |
44356
|
claudio.at
|
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 |
47294
|
sandro.lab
|
public static final String DATACITE_NS_PREFIX = "datacite____";
|
24 |
44356
|
claudio.at
|
|
25 |
47294
|
sandro.lab
|
/**
|
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 |
44356
|
claudio.at
|
|
30 |
47294
|
sandro.lab
|
/**
|
31 |
|
|
* The Constant baseURL.
|
32 |
|
|
*/
|
33 |
|
|
private final static String baseURL = "http://search.datacite.org/api?wt=json&q=doi:";
|
34 |
44356
|
claudio.at
|
|
35 |
49184
|
sandro.lab
|
private DLIResolvedObject parseResponse(final String response) {
|
36 |
47294
|
sandro.lab
|
if (response == null) return null;
|
37 |
|
|
JsonElement jElement = new JsonParser().parse(response);
|
38 |
|
|
JsonObject jobject = jElement.getAsJsonObject();
|
39 |
|
|
if (jobject.has("response")) {
|
40 |
44356
|
claudio.at
|
|
41 |
47294
|
sandro.lab
|
long total = jobject.get("response").getAsJsonObject().get("numFound").getAsLong();
|
42 |
44356
|
claudio.at
|
|
43 |
47294
|
sandro.lab
|
if (total == 0) return null;
|
44 |
49184
|
sandro.lab
|
DLIResolvedObject responseObj = new DLIResolvedObject();
|
45 |
44356
|
claudio.at
|
|
46 |
47294
|
sandro.lab
|
JsonElement hits = ((JsonObject) jobject.get("response")).get("docs");
|
47 |
44356
|
claudio.at
|
|
48 |
47294
|
sandro.lab
|
JsonArray hitsObject = hits.getAsJsonArray();
|
49 |
44356
|
claudio.at
|
|
50 |
47294
|
sandro.lab
|
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 |
44356
|
claudio.at
|
|
59 |
|
|
|
60 |
47294
|
sandro.lab
|
// 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 |
44356
|
claudio.at
|
|
70 |
47294
|
sandro.lab
|
// 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 |
44356
|
claudio.at
|
|
80 |
47294
|
sandro.lab
|
//ADDING DESCRIPTION
|
81 |
44356
|
claudio.at
|
|
82 |
47294
|
sandro.lab
|
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 |
44356
|
claudio.at
|
|
89 |
47294
|
sandro.lab
|
// 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 |
49184
|
sandro.lab
|
final DLIObjectProvenance provenance = new DLIObjectProvenance();
|
108 |
|
|
DLIPIDResolver.setDatasourceProvenance(provenance, DATACITE_NS_PREFIX);
|
109 |
44356
|
claudio.at
|
|
110 |
47294
|
sandro.lab
|
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 |
44356
|
claudio.at
|
|
126 |
47294
|
sandro.lab
|
}
|
127 |
44356
|
claudio.at
|
|
128 |
47294
|
sandro.lab
|
@Override
|
129 |
|
|
protected boolean canResolvePid(final String pidType) {
|
130 |
|
|
return (pidType != null) && ("doi".equals(pidType.toLowerCase().trim()) || "handle".equals(pidType.toLowerCase().trim()));
|
131 |
44356
|
claudio.at
|
|
132 |
47294
|
sandro.lab
|
}
|
133 |
44356
|
claudio.at
|
|
134 |
47294
|
sandro.lab
|
@Override
|
135 |
49184
|
sandro.lab
|
protected DLIResolvedObject resolve(final String pid, final String pidType) {
|
136 |
47294
|
sandro.lab
|
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 |
44356
|
claudio.at
|
}
|