1
|
package eu.dnetlib.pid.resolver.model.serializer;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonDeserializer;
|
5
|
import com.google.gson.JsonElement;
|
6
|
import com.google.gson.JsonObject;
|
7
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
8
|
import eu.dnetlib.pid.resolver.model.ResolvedObject;
|
9
|
import eu.dnetlib.pid.resolver.model.SubjectType;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
public abstract class ResolvedObjectDeserializer implements JsonDeserializer<ResolvedObject> {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(ResolvedObjectDeserializer.class);
|
19
|
|
20
|
protected void partialDeserialization(final JsonObject root, final ResolvedObject resolvedObject) {
|
21
|
|
22
|
log.debug("called partialDeserialization");
|
23
|
|
24
|
|
25
|
try {
|
26
|
final JsonElement pid = root.get("pid");
|
27
|
resolvedObject.setPid(pid != null ? pid.getAsString() : "");
|
28
|
final JsonElement pidType = root.get("pidType");
|
29
|
resolvedObject.setPidType(pidType != null ? pidType.getAsString() : "");
|
30
|
final JsonElement type = root.get("type");
|
31
|
if (type != null)
|
32
|
switch (type.getAsString()) {
|
33
|
case "dataset":
|
34
|
resolvedObject.setType(ObjectType.dataset);
|
35
|
break;
|
36
|
case "publication":
|
37
|
resolvedObject.setType(ObjectType.publication);
|
38
|
break;
|
39
|
default:
|
40
|
resolvedObject.setType(ObjectType.unknown);
|
41
|
break;
|
42
|
}
|
43
|
if (root.has("subjects") && root.get("subjects").isJsonArray()) {
|
44
|
final JsonArray subjects = root.getAsJsonArray("subjects");
|
45
|
final List<SubjectType> extractedSubjects = new ArrayList<>();
|
46
|
if (subjects != null)
|
47
|
subjects.forEach(sub -> {
|
48
|
if (sub.isJsonObject()) {
|
49
|
final JsonObject subject = sub.getAsJsonObject();
|
50
|
if (subject.has("scheme") && subject.has("term"))
|
51
|
extractedSubjects.add(new SubjectType(subject.get("scheme").getAsString(), subject.get("term").getAsString()));
|
52
|
}
|
53
|
});
|
54
|
resolvedObject.setSubjects(extractedSubjects);
|
55
|
}
|
56
|
} catch (Throwable e) {
|
57
|
log.error("ERROR on partial deserialization " + root, e);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
|
62
|
protected String getValue(final JsonObject input, final String field) {
|
63
|
return input.has(field) ? input.get(field).getAsString() : "";
|
64
|
}
|
65
|
|
66
|
protected List<String> extractListValues(final JsonObject root, final String field) {
|
67
|
final List<String> output = new ArrayList<>();
|
68
|
if (root.has(field) && root.get(field).isJsonArray()) {
|
69
|
root.getAsJsonArray(field)
|
70
|
.forEach(it -> output.add(it.getAsString()));
|
71
|
}
|
72
|
return output;
|
73
|
|
74
|
|
75
|
}
|
76
|
}
|