1
|
package eu.dnetlib.data.actionmanager.actions;
|
2
|
|
3
|
import java.lang.reflect.Type;
|
4
|
|
5
|
import com.google.gson.*;
|
6
|
import com.google.protobuf.InvalidProtocolBufferException;
|
7
|
import com.googlecode.protobuf.format.JsonFormat;
|
8
|
import eu.dnetlib.data.proto.DNGFProtos.DNGF;
|
9
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.Agent;
|
10
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.actions.AbstractActionSerializer;
|
11
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.actions.AtomicAction;
|
12
|
import org.apache.commons.codec.binary.Base64;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
|
16
|
/**
|
17
|
* Created by claudio on 30/09/15.
|
18
|
*/
|
19
|
public class AtomicActionSerialiser extends AbstractActionSerializer implements JsonSerializer<AtomicAction> {
|
20
|
|
21
|
private static final Log log = LogFactory.getLog(AtomicActionSerialiser.class);
|
22
|
|
23
|
public static String toJSON(final AtomicAction action) {
|
24
|
|
25
|
final GsonBuilder gson = new GsonBuilder();
|
26
|
|
27
|
gson.registerTypeAdapter(AtomicAction.class, new AtomicActionSerialiser());
|
28
|
|
29
|
return gson.create().toJson(action);
|
30
|
}
|
31
|
|
32
|
@Override
|
33
|
public JsonElement serialize(final AtomicAction aa, final Type typeOfSrc, final JsonSerializationContext context) {
|
34
|
|
35
|
JsonObject json = new JsonObject();
|
36
|
json.addProperty(targetColumn, aa.getTargetColumn());
|
37
|
json.addProperty(targetColumnFamily, aa.getTargetColumnFamily());
|
38
|
json.addProperty(targetRowKey, aa.getTargetRowKey());
|
39
|
json.addProperty(rawSet, aa.getRawSet());
|
40
|
json.addProperty(rowKey, aa.getRowKey());
|
41
|
json.addProperty(actionType, aa.getActionType().toString());
|
42
|
|
43
|
json.add(agent, getAgentJSON(aa));
|
44
|
|
45
|
//json.addProperty(targetValue, getTargetValueJSON(aa));
|
46
|
|
47
|
final String base64 = new String(Base64.encodeBase64(getTargetValueJSON(aa).getBytes()));
|
48
|
json.addProperty(targetValue, base64);
|
49
|
|
50
|
return json;
|
51
|
}
|
52
|
|
53
|
private JsonElement getAgentJSON(final AtomicAction aa) {
|
54
|
|
55
|
final Agent a = aa.getAgent();
|
56
|
|
57
|
JsonObject json = new JsonObject();
|
58
|
json.addProperty(agent_id, a.getId());
|
59
|
json.addProperty(agent_name, a.getName());
|
60
|
json.addProperty(agent_type, a.getType().toString());
|
61
|
|
62
|
return json;
|
63
|
}
|
64
|
|
65
|
private String getTargetValueJSON(final AtomicAction aa) {
|
66
|
try {
|
67
|
return aa.getTargetValue() != null && aa.getTargetValue().length > 0 ? JsonFormat.printToString(DNGF.parseFrom(aa.getTargetValue())) : "";
|
68
|
} catch (InvalidProtocolBufferException e) {
|
69
|
log.error("unable to parse proto", e);
|
70
|
return null;
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|