1
|
package eu.dnetlib.data.actionmanager.actions;
|
2
|
|
3
|
import java.lang.reflect.Type;
|
4
|
|
5
|
import com.google.gson.*;
|
6
|
import com.googlecode.protobuf.format.JsonFormat;
|
7
|
import eu.dnetlib.data.proto.OafProtos;
|
8
|
import eu.dnetlib.rmi.data.actionmanager.Agent;
|
9
|
import org.apache.commons.codec.binary.Base64;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
13
|
/**
|
14
|
* Created by claudio on 30/09/15.
|
15
|
*/
|
16
|
public class AtomicActionDeserialiser extends AbstractActionSerializer implements JsonDeserializer<AtomicAction> {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(AtomicActionDeserialiser.class);
|
19
|
|
20
|
public static AtomicAction fromJSON(String s) {
|
21
|
final GsonBuilder gson = new GsonBuilder();
|
22
|
|
23
|
gson.registerTypeAdapter(AtomicAction.class, new AtomicActionDeserialiser());
|
24
|
|
25
|
return gson.create().fromJson(s, AtomicAction.class);
|
26
|
}
|
27
|
|
28
|
@Override
|
29
|
public AtomicAction deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
30
|
|
31
|
if (json.isJsonObject()) {
|
32
|
final JsonObject j = (JsonObject) json;
|
33
|
|
34
|
final Agent a = new Agent();
|
35
|
|
36
|
final JsonObject aj = j.get(agent).getAsJsonObject();
|
37
|
a.setId(aj.get(agent_id).getAsString());
|
38
|
a.setName(aj.get(agent_name).getAsString());
|
39
|
a.setType(Agent.AGENT_TYPE.valueOf(aj.get(agent_type).getAsString()));
|
40
|
|
41
|
AtomicAction aa = new AtomicAction(j.get(rawSet).getAsString(), a);
|
42
|
|
43
|
aa.setTargetColumn(j.get(targetColumn).getAsString());
|
44
|
aa.setTargetColumnFamily(j.get(targetColumnFamily).getAsString());
|
45
|
aa.setTargetRowKey(j.get(targetRowKey).getAsString());
|
46
|
|
47
|
aa.setRowKey(j.get(rowKey).getAsString());
|
48
|
aa.setTargetValue(decodeTargetValue(j.get(targetValue).getAsString()));
|
49
|
|
50
|
return aa;
|
51
|
}
|
52
|
|
53
|
throw new JsonParseException("input is not a json object");
|
54
|
}
|
55
|
|
56
|
private byte[] decodeTargetValue(final String s) {
|
57
|
try {
|
58
|
final String json = new String(Base64.decodeBase64(s.getBytes()));
|
59
|
|
60
|
OafProtos.Oaf.Builder oaf = OafProtos.Oaf.newBuilder();
|
61
|
JsonFormat.merge(json, oaf);
|
62
|
|
63
|
return oaf.build().toByteArray();
|
64
|
} catch (JsonFormat.ParseException e) {
|
65
|
log.error("unable to parse proto", e);
|
66
|
return null;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
}
|
71
|
|