Skip to content

Commit 46662d0

Browse files
committed
feat: include database configuration
Signed-off-by: Otavio Santana <[email protected]>
1 parent e8e2282 commit 46662d0

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.soujava.demos.arangodb.graph;
2+
3+
import java.util.function.Supplier;
4+
5+
public enum ArangoDBGraphConfigurations implements Supplier<String> {
6+
7+
/**
8+
* The edge collection. It uses as a prefix. E.g.:jnosql.arangodb.graph.edge.1=edge
9+
*/
10+
EDGE("jnosql.arangodb.graph.edge"),
11+
/**
12+
* Edge collection, the source vertex collection and the target vertex collection split by pipe.
13+
* It hou,It uses as a prefix.
14+
* E.g.: jnosql.arangodb.graph.relationship.1=Person|knows|Person
15+
*/
16+
EDGE_RELATIONSHIP("jnosql.arangodb.graph.relationship"),
17+
/**
18+
* The vertex collection. It uses as a prefix. E.g.: jnosql.arangodb.graph.vertex.1=vertex
19+
*/
20+
VERTEX("jnosql.arangodb.graph.vertex"),
21+
/**
22+
* Name of the graph to use.
23+
*/
24+
GRAPH("jnosql.arangodb.graph.graph"),
25+
/**
26+
* The database host.
27+
*/
28+
HOST("jnosql.arangodb.graph.host"),
29+
/**
30+
* The user's credential.
31+
*/
32+
USER("jnosql.arangodb.graph.user"),
33+
/**
34+
* The password's credential.
35+
*/
36+
PASSWORD("jnosql.arangodb.graph.password");
37+
38+
private final String value;
39+
40+
ArangoDBGraphConfigurations(String value) {
41+
this.value = value;
42+
}
43+
44+
@Override
45+
public String get() {
46+
return value;
47+
}
48+
}

0 commit comments

Comments
 (0)