1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.tools;
21
22 import com.fasterxml.jackson.core.JsonFactory;
23 import com.fasterxml.jackson.core.JsonGenerator;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Maps;
27 import com.tinkerpop.blueprints.Direction;
28 import com.tinkerpop.blueprints.Edge;
29 import com.tinkerpop.blueprints.Graph;
30 import com.tinkerpop.blueprints.Vertex;
31 import eu.ehri.project.models.annotations.EntityType;
32 import eu.ehri.project.persistence.Bundle;
33
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42
43
44 public class JsonDataExporter {
45
46 private static final JsonFactory jsonFactory = new JsonFactory();
47 private static final ObjectMapper jsonMapper = new ObjectMapper();
48
49
50
51
52
53
54
55 public static void outputGraph(Graph graph, OutputStream stream)
56 throws IOException {
57
58 try (JsonGenerator g = jsonFactory.createGenerator(stream)) {
59 g.writeStartArray();
60 for (Vertex vertex : graph.getVertices()) {
61
62 Map<String, Object> data = Maps.newHashMap();
63 data.put(Bundle.ID_KEY, vertex.getProperty(EntityType.ID_KEY));
64 data.put(Bundle.TYPE_KEY, vertex.getProperty(EntityType.TYPE_KEY));
65 data.put(Bundle.DATA_KEY, getProperties(vertex));
66 data.put(Bundle.REL_KEY, getRelations(vertex));
67
68 jsonMapper.writeValue(g, data);
69 g.writeRaw('\n');
70 }
71 g.writeEndArray();
72 }
73 }
74
75 private static Map<String, Object> getProperties(Vertex vertex) {
76 Map<String,Object> props = Maps.newHashMap();
77 for (String key : vertex.getPropertyKeys()) {
78 if (!key.startsWith("_")) {
79 props.put(key, vertex.getProperty(key));
80 }
81 }
82 return props;
83 }
84
85 private static Map<String,List<List<String>>> getRelations(Vertex vertex) {
86 Map<String,List<List<String>>> outRels = Maps.newHashMap();
87 for (Edge e : vertex.getEdges(Direction.OUT)) {
88 String label = e.getLabel();
89 Vertex other = e.getVertex(Direction.IN);
90 String oid = other.getProperty(EntityType.ID_KEY);
91 String otype = other.getProperty(EntityType.TYPE_KEY);
92 if (oid != null && otype != null) {
93 List<String> ref = Lists.newArrayList(oid, otype);
94 if (!outRels.containsKey(label)) {
95 outRels.put(label, Lists.<List<String>>newArrayList());
96 }
97
98 outRels.get(label).add(ref);
99 }
100 }
101 return outRels;
102 }
103 }