1
|
package eu.dnetlib.pace.model.gt;
|
2
|
|
3
|
import java.lang.reflect.Type;
|
4
|
|
5
|
import com.google.common.base.Function;
|
6
|
import com.google.common.base.Joiner;
|
7
|
import com.google.common.collect.Iterables;
|
8
|
import com.google.common.collect.Lists;
|
9
|
import com.google.gson.JsonDeserializationContext;
|
10
|
import com.google.gson.JsonDeserializer;
|
11
|
import com.google.gson.JsonElement;
|
12
|
import com.google.gson.JsonObject;
|
13
|
import com.google.gson.JsonParseException;
|
14
|
|
15
|
public class GTAuthorOafSerialiser implements JsonDeserializer<GTAuthor> {
|
16
|
|
17
|
private static final String VALUE = "value";
|
18
|
private static final String SECONDNAMES = "secondnames";
|
19
|
private static final String FIRSTNAME = "firstname";
|
20
|
private static final String FULLNAME = "fullname";
|
21
|
private static final String ID = "id";
|
22
|
private static final String MERGEDPERSON = "mergedperson";
|
23
|
private static final String METADATA = "metadata";
|
24
|
private static final String ANCHOR_ID = "anchorId";
|
25
|
private static final String COAUTHOR = "coauthor";
|
26
|
|
27
|
@Override
|
28
|
public GTAuthor deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
29
|
final GTAuthor gta = new GTAuthor();
|
30
|
|
31
|
gta.setAuthor(getAuthor(json));
|
32
|
gta.setMerged(getMerged(json));
|
33
|
|
34
|
gta.setCoAuthors(getCoAuthors(json));
|
35
|
|
36
|
return gta;
|
37
|
}
|
38
|
|
39
|
private CoAuthors getCoAuthors(final JsonElement json) {
|
40
|
final JsonObject obj = json.getAsJsonObject();
|
41
|
if (!obj.has(COAUTHOR)) return null;
|
42
|
return new CoAuthors(Lists.newArrayList(Iterables.transform(obj.get(COAUTHOR).getAsJsonArray(),
|
43
|
new Function<JsonElement, CoAuthor>() {
|
44
|
|
45
|
@Override
|
46
|
public CoAuthor apply(final JsonElement in) {
|
47
|
final CoAuthor a = new CoAuthor(getAuthor(in));
|
48
|
final JsonObject jsonObject = in.getAsJsonObject();
|
49
|
if (jsonObject.has(ANCHOR_ID)) {
|
50
|
a.setAnchorId(jsonObject.get(ANCHOR_ID).getAsString());
|
51
|
}
|
52
|
return a;
|
53
|
}
|
54
|
})));
|
55
|
}
|
56
|
|
57
|
private Author getAuthor(final JsonElement json) {
|
58
|
|
59
|
final Author a = new Author();
|
60
|
a.setCoauthors(null);
|
61
|
a.setMatches(null);
|
62
|
|
63
|
final JsonObject jso = json.getAsJsonObject();
|
64
|
|
65
|
a.setId(jso.has(ID) ? jso.get(ID).getAsString() : null);
|
66
|
|
67
|
final JsonObject jsonObject = json.getAsJsonObject();
|
68
|
if (jsonObject.has(METADATA)) {
|
69
|
final JsonObject m = jsonObject.get(METADATA).getAsJsonObject();
|
70
|
a.setFullname(getValue(m, FULLNAME));
|
71
|
a.setFirstname(getValue(m, FIRSTNAME));
|
72
|
a.setSecondnames(getValues(m, SECONDNAMES));
|
73
|
}
|
74
|
return a;
|
75
|
}
|
76
|
|
77
|
private Authors getMerged(final JsonElement json) {
|
78
|
final JsonObject obj = json.getAsJsonObject();
|
79
|
if (!obj.has(MERGEDPERSON)) return null;
|
80
|
return new Authors(Lists.newArrayList(Iterables.transform(obj.get(MERGEDPERSON).getAsJsonArray(),
|
81
|
new Function<JsonElement, Author>() {
|
82
|
|
83
|
@Override
|
84
|
public Author apply(final JsonElement in) {
|
85
|
return getAuthor(in);
|
86
|
}
|
87
|
})));
|
88
|
}
|
89
|
|
90
|
private String getValues(final JsonObject m, final String fieldName) {
|
91
|
return m.has(fieldName) ? Joiner.on(" ").join(Iterables.transform(m.get(fieldName).getAsJsonArray(), new Function<JsonElement, String>() {
|
92
|
|
93
|
@Override
|
94
|
public String apply(final JsonElement in) {
|
95
|
return in.getAsJsonObject().get(VALUE).getAsString();
|
96
|
}
|
97
|
})) : null;
|
98
|
}
|
99
|
|
100
|
private String getValue(final JsonObject m, final String fieldName) {
|
101
|
return m.has(fieldName) ? m.get(fieldName).getAsJsonObject().get(VALUE).getAsString() : null;
|
102
|
}
|
103
|
|
104
|
}
|