Revision 52968
Added by Sandro La Bruzzo over 5 years ago
DataciteResolver.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.dli.resolver; |
2 | 2 |
|
3 |
import java.util.Arrays; |
|
4 |
import java.util.Collections; |
|
3 | 5 |
import java.util.List; |
6 |
import java.util.stream.Collectors; |
|
4 | 7 |
|
5 | 8 |
import com.google.common.collect.Lists; |
6 | 9 |
import com.google.gson.JsonArray; |
... | ... | |
8 | 11 |
import com.google.gson.JsonObject; |
9 | 12 |
import com.google.gson.JsonParser; |
10 | 13 |
import eu.dnetlib.dli.resolver.model.*; |
14 |
import eu.dnetlib.enabling.tools.DnetStreamSupport; |
|
11 | 15 |
import eu.dnetlib.pid.resolver.AbstractPIDResolver; |
12 | 16 |
import eu.dnetlib.pid.resolver.model.ObjectType; |
13 | 17 |
import eu.dnetlib.pid.resolver.model.SubjectType; |
... | ... | |
30 | 34 |
/** |
31 | 35 |
* The Constant baseURL. |
32 | 36 |
*/ |
33 |
private final static String baseURL = "http://search.datacite.org/api?wt=json&q=doi:";
|
|
37 |
private final static String baseURL = "https://api.datacite.org/works/";
|
|
34 | 38 |
|
35 |
private DLIResolvedObject parseResponse(final String response) { |
|
39 |
private static String getStringValue(final JsonObject root, final String key, final String defaultValue) { |
|
40 |
|
|
41 |
if (root.has(key) && !root.get(key).isJsonNull()) { |
|
42 |
return root.get(key).getAsString(); |
|
43 |
} |
|
44 |
return defaultValue; |
|
45 |
} |
|
46 |
|
|
47 |
public static DLIResolvedObject parseResponse(final String response) { |
|
36 | 48 |
if (response == null) return null; |
37 | 49 |
JsonElement jElement = new JsonParser().parse(response); |
38 | 50 |
JsonObject jobject = jElement.getAsJsonObject(); |
39 |
if (jobject.has("response")) {
|
|
51 |
if (jobject.has("data")) {
|
|
40 | 52 |
|
41 |
long total = jobject.get("response").getAsJsonObject().get("numFound").getAsLong(); |
|
42 |
|
|
43 |
if (total == 0) return null; |
|
44 | 53 |
DLIResolvedObject responseObj = new DLIResolvedObject(); |
54 |
final JsonObject root = jobject.getAsJsonObject("data").getAsJsonObject("attributes"); |
|
45 | 55 |
|
46 |
JsonElement hits = ((JsonObject) jobject.get("response")).get("docs"); |
|
56 |
final String doi = root.get("doi").getAsString(); |
|
57 |
responseObj.setPid(doi); |
|
58 |
responseObj.setPidType("doi"); |
|
59 |
responseObj.setType(ObjectType.dataset); |
|
47 | 60 |
|
48 |
JsonArray hitsObject = hits.getAsJsonArray();
|
|
61 |
final List<String> authors = Lists.newArrayList();
|
|
49 | 62 |
|
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(); |
|
63 |
// ADDING TITLES |
|
58 | 64 |
|
65 |
if (root.has("title") && root.get("title").isJsonArray()) { |
|
66 |
responseObj.setTitles(DnetStreamSupport.generateStreamFromIterator(root.get("title").getAsJsonArray().iterator()) |
|
67 |
.map(JsonElement::toString) |
|
68 |
.collect(Collectors.toList())); |
|
69 |
} else if (root.has("title") && !root.get("title").isJsonNull()) { |
|
70 |
responseObj.setTitles(Collections.singletonList(root.get("title").getAsString())); |
|
71 |
} |
|
59 | 72 |
|
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); |
|
73 |
//ADDING DESCRIPTION |
|
74 |
if (root.has("description") && !root.get("description").isJsonNull()) |
|
75 |
responseObj.setDescription(root.get("description").getAsString()); |
|
69 | 76 |
|
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); |
|
77 |
// ADDING AUTHORS |
|
78 |
if (root.has("author") && root.get("author").isJsonArray()) { |
|
79 | 79 |
|
80 |
//ADDING DESCRIPTION |
|
80 |
DnetStreamSupport.generateStreamFromIterator(root.getAsJsonArray("author").iterator()) |
|
81 |
.map(JsonElement::getAsJsonObject) |
|
82 |
.forEach(it -> { |
|
83 |
if (it.has("literal")) { |
|
84 |
authors.add(it.get("literal").getAsString()); |
|
85 |
} else { |
|
86 |
authors.add(getStringValue(it, "given", " ") + " " + getStringValue(it, "family", " ")); |
|
87 |
} |
|
88 |
}); |
|
89 |
} |
|
90 |
responseObj.setAuthors(authors); |
|
81 | 91 |
|
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 |
} |
|
92 |
final String date = getStringValue(root, "registered", ""); |
|
93 |
responseObj.setDate(date); |
|
88 | 94 |
|
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); |
|
95 |
final DLIObjectProvenance provenance = new DLIObjectProvenance(); |
|
96 |
DLIPIDResolver.setDatasourceProvenance(provenance, DATACITE_NS_PREFIX); |
|
106 | 97 |
|
107 |
final DLIObjectProvenance provenance = new DLIObjectProvenance(); |
|
108 |
DLIPIDResolver.setDatasourceProvenance(provenance, DATACITE_NS_PREFIX); |
|
109 | 98 |
|
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; |
|
99 |
responseObj.setDatasourceProvenance(Lists.newArrayList(provenance)); |
|
100 |
responseObj.setCompletionStatus(CompletionStatus.complete.toString()); |
|
101 |
return responseObj; |
|
123 | 102 |
} |
124 | 103 |
return null; |
125 | 104 |
|
105 |
|
|
126 | 106 |
} |
127 | 107 |
|
108 |
|
|
128 | 109 |
@Override |
129 | 110 |
protected boolean canResolvePid(final String pidType) { |
130 | 111 |
return (pidType != null) && ("doi".equals(pidType.toLowerCase().trim()) || "handle".equals(pidType.toLowerCase().trim())); |
... | ... | |
132 | 113 |
} |
133 | 114 |
|
134 | 115 |
@Override |
135 |
protected DLIResolvedObject resolve(final String pid, final String pidType) {
|
|
116 |
public DLIResolvedObject resolve(final String pid, final String pidType) {
|
|
136 | 117 |
try { |
137 | 118 |
final String response = requestURL(baseURL + pid.replace(" ", "%20")); |
138 | 119 |
return parseResponse(response); |
Also available in: Unified diff
changed Datacite Resolver