1
+ package org .soujava .demos .arangodb .graph ;
2
+
3
+ import com .arangodb .tinkerpop .gremlin .utils .ArangoDBConfigurationBuilder ;
4
+ import org .apache .commons .configuration .BaseConfiguration ;
5
+ import org .eclipse .jnosql .communication .Settings ;
6
+ import org .apache .tinkerpop .gremlin .structure .Graph ;
7
+ import org .apache .tinkerpop .gremlin .structure .util .GraphFactory ;
8
+ import org .eclipse .jnosql .databases .tinkerpop .communication .GraphConfiguration ;
9
+
10
+ import java .util .Objects ;
11
+
12
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .EDGE ;
13
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .EDGE_RELATIONSHIP ;
14
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .GRAPH ;
15
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .HOST ;
16
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .PASSWORD ;
17
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .USER ;
18
+ import static org .soujava .demos .arangodb .graph .ArangoDBGraphConfigurations .VERTEX ;
19
+
20
+ public class ArangoDBGraphConfiguration implements GraphConfiguration {
21
+
22
+
23
+ @ Override
24
+ public Graph apply (Settings settings ) {
25
+ Objects .requireNonNull (settings , "settings is required" );
26
+ ArangoDBConfigurationBuilder builder = new ArangoDBConfigurationBuilder ();
27
+
28
+ settings .prefix (HOST )
29
+ .stream ()
30
+ .map (Object ::toString )
31
+ .forEach (builder ::arangoHosts );
32
+
33
+ settings .prefix (VERTEX )
34
+ .stream ()
35
+ .map (Object ::toString )
36
+ .forEach (builder ::withVertexCollection );
37
+
38
+ settings .prefix (EDGE )
39
+ .stream ()
40
+ .map (Object ::toString )
41
+ .forEach (builder ::withEdgeCollection );
42
+
43
+
44
+ settings .get (USER )
45
+ .map (Object ::toString )
46
+ .ifPresent (builder ::arangoUser );
47
+
48
+ settings .get (PASSWORD )
49
+ .map (Object ::toString )
50
+ .ifPresent (builder ::arangoPassword );
51
+
52
+ settings .get (GRAPH )
53
+ .map (Object ::toString )
54
+ .ifPresent (builder ::graph );
55
+
56
+ settings .prefix (EDGE_RELATIONSHIP )
57
+ .stream ()
58
+ .map (EdgeConfiguration ::parse )
59
+ .forEach (e -> e .add (builder ));
60
+ BaseConfiguration configuration = builder .build ();
61
+
62
+ var conf2 = new org .apache .commons .configuration2 .BaseConfiguration ();
63
+ configuration .getKeys ().forEachRemaining (k -> {
64
+ conf2 .addProperty (k , configuration .getProperty (k ));
65
+ });
66
+ return GraphFactory .open (conf2 );
67
+ }
68
+
69
+
70
+ private static class EdgeConfiguration {
71
+
72
+ private final String edge ;
73
+
74
+ private final String source ;
75
+
76
+ private final String target ;
77
+
78
+ private EdgeConfiguration (String source , String edge , String target ) {
79
+ this .edge = edge ;
80
+ this .source = source ;
81
+ this .target = target ;
82
+ }
83
+
84
+ static EdgeConfiguration parse (Object value ) {
85
+ final String [] values = value .toString ().split ("\\ |" );
86
+ if (values .length != 3 ) {
87
+ throw new IllegalArgumentException ("The element is valid it must have" +
88
+ " three element split by pipe: " + value );
89
+ }
90
+ return new EdgeConfiguration (values [0 ], values [1 ], values [2 ]);
91
+ }
92
+
93
+ private void add (ArangoDBConfigurationBuilder builder ) {
94
+ builder .configureEdge (edge , source , target );
95
+ }
96
+ }
97
+ }
0 commit comments