> entitySet = new HashSet<>();
55 |
56 | for (final String basePackage : this.getMappingBasePackages()) {
57 | entitySet.addAll(this.scanEntities(basePackage));
58 | }
59 |
60 | return entitySet;
61 | }
62 |
63 | @Bean
64 | public GremlinMappingContext gremlinMappingContext() throws ClassNotFoundException {
65 | final GremlinMappingContext context = new GremlinMappingContext();
66 |
67 | context.setInitialEntitySet(this.getInitialEntitySet());
68 |
69 | return context;
70 | }
71 |
72 | }
73 |
74 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.result;
7 |
8 | import com.microsoft.spring.data.gremlin.common.Constants;
9 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
10 | import lombok.NoArgsConstructor;
11 | import org.springframework.lang.NonNull;
12 | import org.springframework.util.Assert;
13 |
14 | import java.util.ArrayList;
15 | import java.util.LinkedHashMap;
16 | import java.util.Map;
17 |
18 | @NoArgsConstructor
19 | // TODO: seems only for Vertex.
20 | public abstract class AbstractGremlinResultReader {
21 |
22 | /**
23 | * properties's organization is a little complicated.
24 | *
25 | * properties is LinkedHashMap
26 | * K is String
27 | * V is ArrayList
28 | * T is LinkedHashMap
29 | */
30 | private Object readProperty(@NonNull Object value) {
31 | Assert.isInstanceOf(ArrayList.class, value, "should be instance of ArrayList");
32 |
33 | @SuppressWarnings("unchecked") final ArrayList> mapList
34 | = (ArrayList>) value;
35 |
36 | Assert.isTrue(mapList.size() == 1, "should be only 1 element in ArrayList");
37 |
38 | return mapList.get(0).get(Constants.PROPERTY_VALUE);
39 | }
40 |
41 | protected void readResultProperties(@NonNull Map properties, @NonNull GremlinSource source) {
42 | source.getProperties().clear();
43 | properties.forEach((key, value) -> source.setProperty(key, this.readProperty(value)));
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.result;
7 |
8 | import com.microsoft.spring.data.gremlin.common.GremlinUtils;
9 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
10 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSourceVertex;
11 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
12 | import lombok.NoArgsConstructor;
13 | import org.apache.tinkerpop.gremlin.driver.Result;
14 | import org.springframework.lang.NonNull;
15 | import org.springframework.util.Assert;
16 |
17 | import java.util.List;
18 | import java.util.Map;
19 |
20 | import static com.microsoft.spring.data.gremlin.common.Constants.*;
21 |
22 | @NoArgsConstructor
23 | public class GremlinResultVertexReader extends AbstractGremlinResultReader implements GremlinResultsReader {
24 |
25 | private void validate(List results, GremlinSource source) {
26 | Assert.notNull(results, "Results should not be null.");
27 | Assert.notNull(source, "GremlinSource should not be null.");
28 | Assert.isTrue(results.size() == 1, "Vertex should contain only one result.");
29 |
30 | final Result result = results.get(0);
31 |
32 | Assert.isInstanceOf(Map.class, result.getObject(), "should be one instance of Map");
33 |
34 | @SuppressWarnings("unchecked") final Map map = (Map) result.getObject();
35 |
36 | Assert.isTrue(map.containsKey(PROPERTY_ID), "should contain id property");
37 | Assert.isTrue(map.containsKey(PROPERTY_LABEL), "should contain label property");
38 | Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain type property");
39 | Assert.isTrue(map.containsKey(PROPERTY_PROPERTIES), "should contain properties property");
40 | Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_VERTEX), "must be vertex type");
41 |
42 | Assert.isInstanceOf(Map.class, map.get(PROPERTY_PROPERTIES), "should be one instance of Map");
43 | }
44 |
45 | @Override
46 | @SuppressWarnings("unchecked")
47 | public void read(@NonNull List results, @NonNull GremlinSource source) {
48 | if (!(source instanceof GremlinSourceVertex)) {
49 | throw new GremlinUnexpectedSourceTypeException("Should be instance of GremlinSourceVertex");
50 | }
51 |
52 | validate(results, source);
53 |
54 | final Map map = (Map) results.get(0).getObject();
55 | final Map properties = (Map) map.get(PROPERTY_PROPERTIES);
56 |
57 | super.readResultProperties(properties, source);
58 |
59 | final String className = source.getProperties().get(GREMLIN_PROPERTY_CLASSNAME).toString();
60 |
61 | source.setIdField(GremlinUtils.getIdField(GremlinUtils.toEntityClass(className)));
62 | source.setId(map.get(PROPERTY_ID));
63 | source.setLabel(map.get(PROPERTY_LABEL).toString());
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultsGraphReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.result;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
9 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSourceEdge;
10 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSourceGraph;
11 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSourceVertex;
12 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedEntityTypeException;
13 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
14 | import org.apache.tinkerpop.gremlin.driver.Result;
15 | import org.springframework.lang.NonNull;
16 | import org.springframework.util.Assert;
17 |
18 | import java.util.List;
19 | import java.util.Map;
20 |
21 | import static com.microsoft.spring.data.gremlin.common.Constants.*;
22 | import static java.util.Collections.singletonList;
23 |
24 | public class GremlinResultsGraphReader extends AbstractGremlinResultReader implements GremlinResultsReader {
25 |
26 | private final GremlinResultVertexReader vertexResultReader;
27 | private final GremlinResultEdgeReader edgeResultReader;
28 |
29 | public GremlinResultsGraphReader() {
30 | vertexResultReader = new GremlinResultVertexReader();
31 | edgeResultReader = new GremlinResultEdgeReader();
32 | }
33 |
34 | @Override
35 | public void read(@NonNull List results, @NonNull GremlinSource source) {
36 | if (!(source instanceof GremlinSourceGraph)) {
37 | throw new GremlinUnexpectedSourceTypeException("Should be instance of GremlinSourceGraph");
38 | }
39 |
40 | final GremlinSourceGraph graphSource = (GremlinSourceGraph) source;
41 |
42 | graphSource.getVertexSet().clear();
43 | graphSource.getEdgeSet().clear();
44 |
45 | results.stream().map(this::processResult).forEach(graphSource::addGremlinSource);
46 | }
47 |
48 | private GremlinSource processResult(Result result) {
49 | final GremlinSource source;
50 | final Object obj = result.getObject();
51 |
52 | Assert.isInstanceOf(Map.class, obj, "should be an instance of Map");
53 | @SuppressWarnings("unchecked") final Map map = (Map) result.getObject();
54 |
55 | Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain a type property");
56 | final String type = (String) map.get(PROPERTY_TYPE);
57 |
58 | switch (type) {
59 | case RESULT_TYPE_VERTEX:
60 | source = new GremlinSourceVertex();
61 | vertexResultReader.read(singletonList(result), source);
62 | break;
63 | case RESULT_TYPE_EDGE:
64 | source = new GremlinSourceEdge();
65 | edgeResultReader.read(singletonList(result), source);
66 | break;
67 | default:
68 | throw new GremlinUnexpectedEntityTypeException("Unexpected result type: " + type);
69 | }
70 |
71 | return source;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultsReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.result;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
9 | import org.apache.tinkerpop.gremlin.driver.Result;
10 |
11 | import java.util.List;
12 |
13 | public interface GremlinResultsReader {
14 | /**
15 | * Read the Gremlin returned Results to GremlinSource.
16 | */
17 | void read(List results, GremlinSource source);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/script/GremlinScriptLiteral.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.script;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * Provider interface to generate different query to gremlin server.
14 | * The scripts return queries in steps, organized by List.
15 | */
16 | public interface GremlinScriptLiteral {
17 | /**
18 | * Generate the insert query from source (Vertex, Edge or Graph).
19 | */
20 | List generateInsertScript(GremlinSource source);
21 |
22 | /**
23 | * Generate the deleteAll query from source (Vertex, Edge or Graph).
24 | */
25 | List generateDeleteAllScript();
26 |
27 | /**
28 | * Generate the deleteAll By Domain Class query from source (Vertex, Edge or Graph).
29 | */
30 | List generateDeleteAllByClassScript(GremlinSource source);
31 |
32 | /**
33 | * Generate the findById query from source (Vertex, Edge).
34 | */
35 | List generateFindByIdScript(GremlinSource source);
36 |
37 | /**
38 | * Generate the update query from source (Vertex, Edge or Graph).
39 | */
40 | List generateUpdateScript(GremlinSource source);
41 |
42 | /**
43 | * Generate the findAll query from source (Vertex, Edge or Graph).
44 | */
45 | List generateFindAllScript(GremlinSource source);
46 |
47 | /**
48 | * Generate the DeleteById query from source (Vertex, Edge or Graph).
49 | */
50 | List generateDeleteByIdScript(GremlinSource source);
51 |
52 | /**
53 | * Generate the Count query from Source (Vertex, Edge)
54 | */
55 | List generateCountScript(GremlinSource source);
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/AbstractGremlinSourceReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.common.GremlinUtils;
9 | import com.microsoft.spring.data.gremlin.exception.GremlinEntityInformationException;
10 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedEntityTypeException;
11 | import lombok.NonNull;
12 | import org.apache.tinkerpop.shaded.jackson.databind.JavaType;
13 | import org.apache.tinkerpop.shaded.jackson.databind.type.TypeFactory;
14 | import org.springframework.data.mapping.PersistentProperty;
15 | import org.springframework.lang.Nullable;
16 | import org.springframework.util.Assert;
17 |
18 | import java.io.IOException;
19 | import java.lang.reflect.Field;
20 | import java.util.Date;
21 |
22 | public abstract class AbstractGremlinSourceReader {
23 |
24 | protected Object readProperty(@NonNull PersistentProperty property, @Nullable Object value) {
25 | final Class> type = property.getTypeInformation().getType();
26 | final JavaType javaType = TypeFactory.defaultInstance().constructType(property.getType());
27 |
28 | if (value == null) {
29 | return null;
30 | } else if (type == int.class || type == Integer.class
31 | || type == Boolean.class || type == boolean.class
32 | || type == String.class) {
33 | return value;
34 | } else if (type == Date.class) {
35 | Assert.isTrue(value instanceof Long, "Date store value must be instance of long");
36 | return new Date((Long) value);
37 | } else {
38 | final Object object;
39 |
40 | try {
41 | object = GremlinUtils.getObjectMapper().readValue(value.toString(), javaType);
42 | } catch (IOException e) {
43 | throw new GremlinUnexpectedEntityTypeException("Failed to read String to Object", e);
44 | }
45 |
46 | return object;
47 | }
48 | }
49 |
50 | protected Object getGremlinSourceId(@NonNull GremlinSource source) {
51 | if (!source.getId().isPresent()) {
52 | return null;
53 | }
54 |
55 | final Object id = source.getId().get();
56 | final Field idField = source.getIdField();
57 |
58 | if (idField.getType() == String.class) {
59 | return id.toString();
60 | } else if (idField.getType() == Integer.class) {
61 | Assert.isTrue(id instanceof Integer, "source Id should be Integer.");
62 | return id;
63 | } else if (idField.getType() == Long.class && id instanceof Integer) {
64 | return Long.valueOf((Integer) id);
65 | } else if (idField.getType() == Long.class && id instanceof Long) {
66 | return id;
67 | }
68 |
69 | throw new GremlinEntityInformationException("unsupported id field type: " + id.getClass().getSimpleName());
70 | }
71 | }
72 |
73 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceEdge.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.result.GremlinResultEdgeReader;
9 | import com.microsoft.spring.data.gremlin.conversion.script.GremlinScriptLiteralEdge;
10 | import lombok.Getter;
11 | import lombok.Setter;
12 |
13 | public class GremlinSourceEdge extends AbstractGremlinSource {
14 |
15 | @Getter
16 | @Setter
17 | private Object vertexIdFrom;
18 |
19 | @Getter
20 | @Setter
21 | private Object vertexIdTo;
22 |
23 | public GremlinSourceEdge() {
24 | super();
25 | initializeGremlinStrategy();
26 | }
27 |
28 | public GremlinSourceEdge(Class domainClass) {
29 | super(domainClass);
30 | initializeGremlinStrategy();
31 | }
32 |
33 | private void initializeGremlinStrategy() {
34 | this.setGremlinScriptStrategy(new GremlinScriptLiteralEdge());
35 | this.setGremlinResultReader(new GremlinResultEdgeReader());
36 | this.setGremlinSourceReader(new GremlinSourceEdgeReader());
37 | this.setGremlinSourceWriter(new GremlinSourceEdgeWriter());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceEdgeReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.annotation.EdgeFrom;
9 | import com.microsoft.spring.data.gremlin.annotation.EdgeTo;
10 | import com.microsoft.spring.data.gremlin.common.GremlinUtils;
11 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
12 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
13 | import com.microsoft.spring.data.gremlin.mapping.GremlinPersistentEntity;
14 | import lombok.NoArgsConstructor;
15 | import org.apache.commons.lang3.reflect.FieldUtils;
16 | import org.springframework.data.annotation.Id;
17 | import org.springframework.data.mapping.PersistentProperty;
18 | import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
19 | import org.springframework.lang.NonNull;
20 | import org.springframework.util.Assert;
21 |
22 | import java.lang.reflect.Field;
23 |
24 | import static com.microsoft.spring.data.gremlin.common.Constants.PROPERTY_ID;
25 |
26 | @NoArgsConstructor
27 | public class GremlinSourceEdgeReader extends AbstractGremlinSourceReader implements GremlinSourceReader {
28 |
29 | @Override
30 | public T read(@NonNull Class domainClass, @NonNull MappingGremlinConverter converter,
31 | @NonNull GremlinSource source) {
32 | if (!(source instanceof GremlinSourceEdge)) {
33 | throw new GremlinUnexpectedSourceTypeException("should be instance of GremlinSourceEdge");
34 | }
35 |
36 | final T domain = GremlinUtils.createInstance(domainClass);
37 | final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain);
38 | final GremlinPersistentEntity persistentEntity = converter.getPersistentEntity(domainClass);
39 |
40 | for (final Field field : FieldUtils.getAllFields(domainClass)) {
41 | final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName());
42 | if (property == null) {
43 | continue;
44 | }
45 | if (field.getName().equals(PROPERTY_ID) || field.getAnnotation(Id.class) != null) {
46 | accessor.setProperty(property, super.getGremlinSourceId(source));
47 | continue;
48 | } else if (field.getAnnotation(EdgeFrom.class) != null || field.getAnnotation(EdgeTo.class) != null) {
49 | // We cannot do that here as the gremlin will not tell more information about vertex except Id. After
50 | // the query of Edge end, we can get the Id of vertex from/to. And then we will do extra 2 query to
51 | // obtain the 2 vertex and complete the edge.
52 | //
53 | // That work will be wrapped in GremlinTemplate insert, and skip the property here.
54 | continue;
55 | }
56 |
57 | final Object value = super.readProperty(property, source.getProperties().get(field.getName()));
58 | accessor.setProperty(property, value);
59 | }
60 |
61 | return domain;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceGraph.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.result.GremlinResultsGraphReader;
9 | import com.microsoft.spring.data.gremlin.conversion.result.GremlinResultsReader;
10 | import com.microsoft.spring.data.gremlin.conversion.script.GremlinScriptLiteralGraph;
11 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
12 | import lombok.Getter;
13 |
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | public class GremlinSourceGraph extends AbstractGremlinSource {
18 |
19 | @Getter
20 | private List vertexSet = new ArrayList<>();
21 |
22 | @Getter
23 | private List edgeSet = new ArrayList<>();
24 |
25 | @Getter
26 | private GremlinResultsReader resultsReader;
27 |
28 | public GremlinSourceGraph() {
29 | super();
30 | initializeGremlinStrategy();
31 | this.setGremlinSourceReader(new GremlinSourceGraphReader());
32 | this.resultsReader = new GremlinResultsGraphReader();
33 | }
34 |
35 | public GremlinSourceGraph(Class domainClass) {
36 | super(domainClass);
37 | initializeGremlinStrategy();
38 | this.setGremlinSourceReader(new GremlinSourceGraphReader());
39 | this.resultsReader = new GremlinResultsGraphReader();
40 | }
41 |
42 | public void addGremlinSource(GremlinSource source) {
43 | if (source instanceof GremlinSourceVertex) {
44 | this.vertexSet.add(source);
45 | } else if (source instanceof GremlinSourceEdge) {
46 | this.edgeSet.add(source);
47 | } else {
48 | throw new GremlinUnexpectedSourceTypeException("source type can only be Vertex or Edge");
49 | }
50 | }
51 |
52 | private void initializeGremlinStrategy() {
53 | this.setGremlinScriptStrategy(new GremlinScriptLiteralGraph());
54 | this.setGremlinSourceWriter(new GremlinSourceGraphWriter());
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
9 |
10 | /**
11 | * Provider Entity type dependent read method.
12 | */
13 | public interface GremlinSourceReader {
14 | /**
15 | * Read data from GremlinSource to domain
16 | */
17 | T read(Class domainClass, MappingGremlinConverter converter, GremlinSource source);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceVertex.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.result.GremlinResultVertexReader;
9 | import com.microsoft.spring.data.gremlin.conversion.script.GremlinScriptLiteralVertex;
10 |
11 | public class GremlinSourceVertex extends AbstractGremlinSource {
12 |
13 | public GremlinSourceVertex() {
14 | super();
15 | initializeGremlinStrategy();
16 | }
17 |
18 | public GremlinSourceVertex(Class domainClass) {
19 | super(domainClass);
20 | initializeGremlinStrategy();
21 | }
22 |
23 | private void initializeGremlinStrategy() {
24 | this.setGremlinScriptStrategy(new GremlinScriptLiteralVertex());
25 | this.setGremlinResultReader(new GremlinResultVertexReader());
26 | this.setGremlinSourceReader(new GremlinSourceVertexReader());
27 | this.setGremlinSourceWriter(new GremlinSourceVertexWriter());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceVertexReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.common.Constants;
9 | import com.microsoft.spring.data.gremlin.common.GremlinUtils;
10 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
11 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
12 | import com.microsoft.spring.data.gremlin.mapping.GremlinPersistentEntity;
13 | import lombok.NoArgsConstructor;
14 | import org.apache.commons.lang3.reflect.FieldUtils;
15 | import org.springframework.data.annotation.Id;
16 | import org.springframework.data.mapping.PersistentProperty;
17 | import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
18 | import org.springframework.lang.NonNull;
19 | import org.springframework.util.Assert;
20 |
21 | import java.lang.reflect.Field;
22 |
23 | @NoArgsConstructor
24 | public class GremlinSourceVertexReader extends AbstractGremlinSourceReader implements GremlinSourceReader {
25 |
26 | @Override
27 | public T read(@NonNull Class domainClass, @NonNull MappingGremlinConverter converter,
28 | @NonNull GremlinSource source) {
29 | if (!(source instanceof GremlinSourceVertex)) {
30 | throw new GremlinUnexpectedSourceTypeException("should be instance of GremlinSourceVertex");
31 | }
32 |
33 | final T domain = GremlinUtils.createInstance(domainClass);
34 | final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain);
35 | final GremlinPersistentEntity persistentEntity = converter.getPersistentEntity(domainClass);
36 |
37 | for (final Field field : FieldUtils.getAllFields(domainClass)) {
38 | final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName());
39 | if (property == null) {
40 | continue;
41 | }
42 |
43 | if (field.getName().equals(Constants.PROPERTY_ID) || field.getAnnotation(Id.class) != null) {
44 | accessor.setProperty(property, super.getGremlinSourceId(source));
45 | } else {
46 | final Object value = super.readProperty(property, source.getProperties().get(field.getName()));
47 | accessor.setProperty(property, value);
48 | }
49 | }
50 |
51 | return domain;
52 | }
53 | }
54 |
55 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceVertexWriter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
9 | import com.microsoft.spring.data.gremlin.exception.GremlinEntityInformationException;
10 | import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException;
11 | import com.microsoft.spring.data.gremlin.mapping.GremlinPersistentEntity;
12 | import lombok.NoArgsConstructor;
13 | import org.apache.commons.lang3.reflect.FieldUtils;
14 | import org.springframework.data.annotation.Id;
15 | import org.springframework.data.mapping.PersistentProperty;
16 | import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
17 | import org.springframework.lang.NonNull;
18 | import org.springframework.util.Assert;
19 |
20 | import java.lang.reflect.Field;
21 |
22 | import static com.microsoft.spring.data.gremlin.common.Constants.GREMLIN_PROPERTY_CLASSNAME;
23 | import static com.microsoft.spring.data.gremlin.common.Constants.PROPERTY_ID;
24 |
25 | @NoArgsConstructor
26 | public class GremlinSourceVertexWriter implements GremlinSourceWriter {
27 |
28 | @Override
29 | public void write(@NonNull Object domain, @NonNull MappingGremlinConverter converter,
30 | @NonNull GremlinSource source) {
31 | if (!(source instanceof GremlinSourceVertex)) {
32 | throw new GremlinUnexpectedSourceTypeException("should be the instance of GremlinSourceVertex");
33 | }
34 |
35 | source.setId(converter.getIdFieldValue(domain));
36 |
37 | final GremlinPersistentEntity> persistentEntity = converter.getPersistentEntity(domain.getClass());
38 | final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain);
39 |
40 | for (final Field field : FieldUtils.getAllFields(domain.getClass())) {
41 | final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName());
42 | if (property == null) {
43 | continue;
44 | }
45 |
46 | if (field.getName().equals(PROPERTY_ID) || field.getAnnotation(Id.class) != null) {
47 | continue;
48 | } else if (field.getName().equals(GREMLIN_PROPERTY_CLASSNAME)) {
49 | throw new GremlinEntityInformationException("Domain Cannot use pre-defined field name: "
50 | + GREMLIN_PROPERTY_CLASSNAME);
51 | }
52 |
53 | source.setProperty(field.getName(), accessor.getProperty(property));
54 | }
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/conversion/source/GremlinSourceWriter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.conversion.source;
7 |
8 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
9 |
10 | /**
11 | * Provider Entity type dependent write method.
12 | */
13 | public interface GremlinSourceWriter {
14 | /**
15 | * Write the domain class information to GremlinSource
16 | */
17 | void write(Object domain, MappingGremlinConverter converter, GremlinSource source);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinEntityInformationException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | import org.springframework.dao.TypeMismatchDataAccessException;
9 |
10 | public class GremlinEntityInformationException extends TypeMismatchDataAccessException {
11 |
12 | public GremlinEntityInformationException(String msg) {
13 | super(msg);
14 | }
15 |
16 | public GremlinEntityInformationException(String msg, Throwable cause) {
17 | super(msg, cause);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinIllegalConfigurationException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | import org.springframework.dao.InvalidDataAccessApiUsageException;
9 |
10 | public class GremlinIllegalConfigurationException extends InvalidDataAccessApiUsageException {
11 |
12 | public GremlinIllegalConfigurationException(String msg) {
13 | super(msg);
14 | }
15 |
16 | public GremlinIllegalConfigurationException(String msg, Throwable cause) {
17 | super(msg, cause);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinInvalidEntityIdFieldException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | public class GremlinInvalidEntityIdFieldException extends GremlinEntityInformationException {
9 |
10 | public GremlinInvalidEntityIdFieldException(String msg) {
11 | super(msg);
12 | }
13 |
14 | public GremlinInvalidEntityIdFieldException(String msg, Throwable cause) {
15 | super(msg, cause);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinQueryException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | import org.springframework.dao.DataAccessResourceFailureException;
9 |
10 | public class GremlinQueryException extends DataAccessResourceFailureException {
11 |
12 | public GremlinQueryException(String msg) {
13 | super(msg);
14 | }
15 |
16 | public GremlinQueryException(String msg, Throwable cause) {
17 | super(msg, cause);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinUnexpectedEntityTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | public class GremlinUnexpectedEntityTypeException extends GremlinEntityInformationException {
9 |
10 | public GremlinUnexpectedEntityTypeException(String msg) {
11 | super(msg);
12 | }
13 |
14 | public GremlinUnexpectedEntityTypeException(String msg, Throwable cause) {
15 | super(msg, cause);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/exception/GremlinUnexpectedSourceTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.exception;
7 |
8 | import org.springframework.dao.TypeMismatchDataAccessException;
9 |
10 | public class GremlinUnexpectedSourceTypeException extends TypeMismatchDataAccessException {
11 |
12 | public GremlinUnexpectedSourceTypeException(String msg) {
13 | super(msg);
14 | }
15 |
16 | public GremlinUnexpectedSourceTypeException(String msg, Throwable cause) {
17 | super(msg, cause);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/mapping/BasicGremlinPersistentEntity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.mapping;
7 |
8 | import org.springframework.beans.BeansException;
9 | import org.springframework.context.ApplicationContext;
10 | import org.springframework.context.ApplicationContextAware;
11 | import org.springframework.context.expression.BeanFactoryAccessor;
12 | import org.springframework.context.expression.BeanFactoryResolver;
13 | import org.springframework.data.mapping.model.BasicPersistentEntity;
14 | import org.springframework.data.util.TypeInformation;
15 | import org.springframework.expression.spel.support.StandardEvaluationContext;
16 |
17 | public class BasicGremlinPersistentEntity extends BasicPersistentEntity
18 | implements GremlinPersistentEntity, ApplicationContextAware {
19 |
20 | private final StandardEvaluationContext context;
21 |
22 | public BasicGremlinPersistentEntity(TypeInformation information) {
23 | super(information);
24 |
25 | this.context = new StandardEvaluationContext();
26 | }
27 |
28 | @Override
29 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
30 | this.context.addPropertyAccessor(new BeanFactoryAccessor());
31 | this.context.setBeanResolver(new BeanFactoryResolver(applicationContext));
32 | this.context.setRootObject(applicationContext);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/mapping/BasicGremlinPersistentProperty.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.mapping;
7 |
8 | import com.microsoft.spring.data.gremlin.common.Constants;
9 | import org.springframework.data.mapping.Association;
10 | import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
11 | import org.springframework.data.mapping.model.Property;
12 | import org.springframework.data.mapping.model.SimpleTypeHolder;
13 |
14 | public class BasicGremlinPersistentProperty extends AnnotationBasedPersistentProperty
15 | implements GremlinPersistentProperty {
16 |
17 | public BasicGremlinPersistentProperty(Property property, GremlinPersistentEntity> owner,
18 | SimpleTypeHolder holder) {
19 | super(property, owner, holder);
20 | }
21 |
22 | @Override
23 | protected Association createAssociation() {
24 | return new Association<>(this, null);
25 | }
26 |
27 | @Override
28 | public boolean isIdProperty() {
29 | return super.isIdProperty() || getName().equals(Constants.PROPERTY_ID);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/mapping/GremlinMappingContext.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.mapping;
7 |
8 | import org.springframework.context.ApplicationContext;
9 | import org.springframework.data.mapping.context.AbstractMappingContext;
10 | import org.springframework.data.mapping.model.Property;
11 | import org.springframework.data.mapping.model.SimpleTypeHolder;
12 | import org.springframework.data.util.TypeInformation;
13 |
14 | public class GremlinMappingContext
15 | extends AbstractMappingContext, GremlinPersistentProperty> {
16 | private ApplicationContext context;
17 |
18 | @Override
19 | public void setApplicationContext(ApplicationContext context) {
20 | this.context = context;
21 | }
22 |
23 | @Override
24 | public GremlinPersistentProperty createPersistentProperty(Property property,
25 | BasicGremlinPersistentEntity> owner,
26 | SimpleTypeHolder holder) {
27 | return new BasicGremlinPersistentProperty(property, owner, holder);
28 | }
29 |
30 | @Override
31 | protected BasicGremlinPersistentEntity createPersistentEntity(TypeInformation typeInformation) {
32 | final BasicGremlinPersistentEntity entity = new BasicGremlinPersistentEntity<>(typeInformation);
33 |
34 | if (this.context != null) {
35 | entity.setApplicationContext(this.context);
36 | }
37 |
38 | return entity;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/mapping/GremlinPersistentEntity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.mapping;
7 |
8 | import org.springframework.data.mapping.PersistentEntity;
9 |
10 | public interface GremlinPersistentEntity extends PersistentEntity {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/mapping/GremlinPersistentProperty.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.mapping;
7 |
8 | import org.springframework.data.mapping.PersistentProperty;
9 |
10 | public interface GremlinPersistentProperty extends PersistentProperty {
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinEntityMetadata.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.query;
7 |
8 | import org.springframework.data.repository.core.EntityMetadata;
9 |
10 | public interface GremlinEntityMetadata extends EntityMetadata {
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinOperations.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.query;
7 |
8 | import com.microsoft.spring.data.gremlin.common.GremlinEntityType;
9 | import com.microsoft.spring.data.gremlin.conversion.MappingGremlinConverter;
10 | import com.microsoft.spring.data.gremlin.conversion.source.GremlinSource;
11 | import com.microsoft.spring.data.gremlin.query.query.GremlinQuery;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * Provider interface for basic Operations with Gremlin
17 | */
18 | public interface GremlinOperations {
19 |
20 | void deleteAll();
21 |
22 | void deleteAll(GremlinEntityType type);
23 |
24 | void deleteAll(GremlinSource source);
25 |
26 | boolean isEmptyGraph(GremlinSource source);
27 |
28 | boolean existsById(Object id, GremlinSource source);
29 |
30 | void deleteById(Object id, GremlinSource source);
31 |
32 | T insert(T object, GremlinSource source);
33 |
34 | T findById(Object id, GremlinSource source);
35 |
36 | T findVertexById(Object id, GremlinSource source);
37 |
38 | T findEdgeById(Object id, GremlinSource source);
39 |
40 | T update(T object, GremlinSource source);
41 |
42 | T save(T object, GremlinSource source);
43 |
44 | List findAll(GremlinSource source);
45 |
46 | long vertexCount();
47 |
48 | long edgeCount();
49 |
50 | List find(GremlinQuery query, GremlinSource source);
51 |
52 | MappingGremlinConverter getMappingConverter();
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/query/SimpleGremlinEntityMetadata.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.query;
7 |
8 | public class SimpleGremlinEntityMetadata implements GremlinEntityMetadata {
9 |
10 | private final Class type;
11 |
12 | public SimpleGremlinEntityMetadata(Class type) {
13 | this.type = type;
14 | }
15 |
16 | public Class getJavaType() {
17 | return this.type;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/microsoft/spring/data/gremlin/query/criteria/Criteria.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) Microsoft Corporation. All rights reserved.
3 | * Licensed under the MIT License. See LICENSE in the project root for
4 | * license information.
5 | */
6 | package com.microsoft.spring.data.gremlin.query.criteria;
7 |
8 | import lombok.Getter;
9 | import org.springframework.lang.NonNull;
10 | import org.springframework.util.Assert;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | @Getter
16 | public class Criteria {
17 |
18 | private String subject;
19 | private List