map = elementToMap(vertex);
41 | return map;
42 | }
43 | else {
44 | return null;
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/spring-data-gremlin-core/src/main/java/org/springframework/data/gremlin/schema/GremlinEdgeSchema.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.schema;
2 |
3 | import org.apache.tinkerpop.gremlin.structure.Direction;
4 | import org.springframework.data.gremlin.schema.property.GremlinAdjacentProperty;
5 | import org.springframework.data.gremlin.schema.property.GremlinProperty;
6 | import org.springframework.data.gremlin.schema.property.accessor.GremlinFieldPropertyAccessor;
7 |
8 |
9 | /**
10 | *
11 | * Defines the schema of a mapped Class. Each GremlinSchema holds the {@code className}, {@code classType},
12 | * {@code schemaType} (VERTEX, EDGE) and the identifying {@link GremlinFieldPropertyAccessor}.
13 | *
14 | *
15 | * The GremlinSchema contains the high level logic for converting Vertices to mapped classes.
16 | *
17 | *
18 | * @author Gman
19 | */
20 | public class GremlinEdgeSchema extends GremlinSchema {
21 |
22 | public GremlinEdgeSchema(Class classType, GremlinSchema super V> superSchema) {
23 | super(classType, superSchema);
24 | }
25 |
26 | private GremlinAdjacentProperty outProperty;
27 | private GremlinAdjacentProperty inProperty;
28 |
29 | public void addProperty(GremlinProperty property) {
30 | super.addProperty(property);
31 | if (property instanceof GremlinAdjacentProperty) {
32 | GremlinAdjacentProperty adjacentProperty = (GremlinAdjacentProperty) property;
33 | if (adjacentProperty.getDirection() == Direction.OUT) {
34 | outProperty = adjacentProperty;
35 | } else {
36 | inProperty = adjacentProperty;
37 | }
38 | }
39 | }
40 |
41 | public GremlinAdjacentProperty getOutProperty() {
42 | return outProperty;
43 | }
44 |
45 | public GremlinAdjacentProperty getInProperty() {
46 | return inProperty;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/spring-data-gremlin-core/src/main/java/org/springframework/data/gremlin/schema/property/accessor/GremlinFieldPropertyAccessor.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.schema.property.accessor;
2 |
3 | import org.springframework.data.gremlin.schema.LazyInitializationHandler;
4 |
5 | import java.lang.reflect.Field;
6 |
7 | /**
8 | * A concrete {@link AbstractGremlinFieldPropertyAccessor} for basic Fields.
9 | *
10 | * @author Gman
11 | */
12 | public class GremlinFieldPropertyAccessor extends AbstractGremlinFieldPropertyAccessor {
13 |
14 | public GremlinFieldPropertyAccessor(Field field) {
15 | super(field);
16 | }
17 |
18 | public GremlinFieldPropertyAccessor(Field field, GremlinFieldPropertyAccessor parentAccessor) {
19 | super(field, parentAccessor);
20 | }
21 |
22 | @Override
23 | public V get(Object object) {
24 | try {
25 | LazyInitializationHandler.initProxy(object);
26 | object = getEmbeddedObject(object, false);
27 | V result = null;
28 | if (object != null) {
29 | result = (V) field.get(object);
30 | }
31 | return result;
32 | } catch (IllegalAccessException e) {
33 | throw new IllegalStateException(e.getMessage(), e);
34 | }
35 | }
36 |
37 | @Override
38 | public void set(Object object, V val) {
39 |
40 | try {
41 | object = getEmbeddedObject(object, true);
42 | if (object != null) {
43 | field.set(object, val);
44 | }
45 | } catch (IllegalAccessException e) {
46 | throw new IllegalStateException(e.getMessage(), e);
47 | }
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "GremlinFieldPropertyAccessor{"
53 | + "field=" + field
54 | + ", embeddedAccessor=" + embeddedAccessor +
55 | '}';
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/spring-data-gremlin-test/src/main/java/org/springframework/data/gremlin/object/jpa/repository/AbstractAddressRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.object.jpa.repository;
2 |
3 | import org.apache.commons.collections4.CollectionUtils;
4 | import org.junit.Assert;
5 | import org.junit.Test;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.gremlin.object.jpa.domain.Address;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | import static org.junit.Assert.assertEquals;
13 | import static org.junit.Assert.assertNotNull;
14 |
15 | @SuppressWarnings("SpringJavaAutowiringInspection")
16 | public abstract class AbstractAddressRepositoryTest extends BaseRepositoryTest {
17 |
18 | @Autowired
19 | protected AddressRepository addressRepository;
20 |
21 | @Test
22 | public void should_find_addresses() throws Exception {
23 | List addresses = new ArrayList();
24 |
25 | CollectionUtils.addAll(addresses, addressRepository.findAll());
26 | assertNotNull(addresses);
27 | assertEquals(2, addresses.size());
28 |
29 | for (Address address : addresses) {
30 | Assert.assertNotNull(address.getPeople());
31 | Assert.assertTrue(address.getPeople().size() > 0);
32 | }
33 | }
34 |
35 | @Test
36 | public void should_save_and_find_embedded_country() throws Exception {
37 | List addresses = new ArrayList<>();
38 |
39 | CollectionUtils.addAll(addresses, addressRepository.findAll());
40 | assertNotNull(addresses);
41 | assertEquals(2, addresses.size());
42 | assertNotNull(addresses.get(0).getCountry());
43 | assertEquals("Australia", addresses.get(0).getCountry().getName());
44 | assertNotNull(addresses.get(1).getCountry());
45 | assertEquals("Australia", addresses.get(1).getCountry().getName());
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/spring-data-gremlin-test/src/main/java/org/springframework/data/gremlin/object/core/repository/AbstractAddressRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.object.core.repository;
2 |
3 | import org.apache.commons.collections4.CollectionUtils;
4 | import org.junit.Assert;
5 | import org.junit.Test;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.gremlin.object.core.domain.Address;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | import static org.junit.Assert.assertEquals;
13 | import static org.junit.Assert.assertNotNull;
14 |
15 | @SuppressWarnings("SpringJavaAutowiringInspection")
16 | public abstract class AbstractAddressRepositoryTest extends BaseRepositoryTest {
17 |
18 | @Autowired
19 | protected AddressRepository addressRepository;
20 |
21 | @Test
22 | public void should_find_addresses() throws Exception {
23 | List addresses = new ArrayList();
24 |
25 | CollectionUtils.addAll(addresses, addressRepository.findAll());
26 | assertNotNull(addresses);
27 | assertEquals(2, addresses.size());
28 |
29 | for (Address address : addresses) {
30 | Assert.assertNotNull(address.getPeople());
31 | Assert.assertTrue(address.getPeople().size() > 0);
32 | }
33 | }
34 |
35 |
36 | @Test
37 | public void should_save_and_find_embedded_country() throws Exception {
38 | List addresses = new ArrayList();
39 |
40 | CollectionUtils.addAll(addresses, addressRepository.findAll());
41 | assertNotNull(addresses);
42 | assertEquals(2, addresses.size());
43 | assertNotNull(addresses.get(0).getCountry());
44 | assertEquals("Australia", addresses.get(0).getCountry().getName());
45 | assertNotNull(addresses.get(1).getCountry());
46 | assertEquals("Australia", addresses.get(1).getCountry().getName());
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/spring-data-gremlin-test/src/main/java/org/springframework/data/gremlin/object/core/domain/Located.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.object.core.domain;
2 |
3 | import org.springframework.data.gremlin.annotation.*;
4 |
5 | import java.util.Date;
6 |
7 | /**
8 | * Created by gman on 3/08/15.
9 | */
10 | @Edge("was_located")
11 | public class Located {
12 |
13 | @Id
14 | private String id;
15 |
16 | @Property("location_date")
17 | private Date date;
18 |
19 | @FromVertex
20 | private Person person;
21 |
22 | @ToVertex
23 | private Location location;
24 |
25 | public Located() {
26 | }
27 |
28 | public Located(Date date, Person person, Location location) {
29 | this.date = date;
30 | this.person = person;
31 | this.location = location;
32 | }
33 |
34 | public String getId() {
35 | return id;
36 | }
37 |
38 | public void setId(String id) {
39 | this.id = id;
40 | }
41 |
42 | public Date getDate() {
43 | return date;
44 | }
45 |
46 | public void setDate(Date date) {
47 | this.date = date;
48 | }
49 |
50 | public Person getPerson() {
51 | return person;
52 | }
53 |
54 | public void setPerson(Person person) {
55 | this.person = person;
56 | }
57 |
58 | public Location getLocation() {
59 | return location;
60 | }
61 |
62 | public void setLocation(Location location) {
63 | this.location = location;
64 | }
65 |
66 | // @Override
67 | // public boolean equals(Object o) {
68 | // if (this == o) {
69 | // return true;
70 | // }
71 | // if (o == null || getClass() != o.getClass()) {
72 | // return false;
73 | // }
74 | //
75 | // Located located = (Located) o;
76 | //
77 | // return !(id != null ? !id.equals(located.id) : located.id != null);
78 | //
79 | // }
80 | //
81 | // @Override
82 | // public int hashCode() {
83 | // return id != null ? id.hashCode() : 0;
84 | // }
85 | }
86 |
--------------------------------------------------------------------------------
/spring-data-gremlin-test/src/main/java/org/springframework/data/gremlin/object/jpa/domain/Address.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.object.jpa.domain;
2 |
3 | import javax.persistence.*;
4 | import java.util.HashSet;
5 | import java.util.Set;
6 |
7 | @Entity
8 | public class Address {
9 |
10 | @Id
11 | private String id;
12 |
13 | @Embedded
14 | private Country country;
15 |
16 | private String city;
17 |
18 | private String street;
19 |
20 | @OneToOne
21 | @Column(name = "of_area")
22 | private Area area;
23 |
24 | @OneToMany(mappedBy = "address")
25 | @Column(name = "lives_at")
26 | private Set people;
27 |
28 | public Address() {}
29 |
30 | public Address(Country country, String city, String street, Area area) {
31 | this.country = country;
32 | this.city = city;
33 | this.street = street;
34 | this.area = area;
35 | }
36 |
37 | public String getId() {
38 | return id;
39 | }
40 |
41 | public void setId(String id) {
42 | this.id = id;
43 | }
44 |
45 | public Country getCountry() {
46 | return country;
47 | }
48 |
49 | public void setCountry(Country country) {
50 | this.country = country;
51 | }
52 |
53 | public String getCity() {
54 | return city;
55 | }
56 |
57 | public void setCity(String city) {
58 | this.city = city;
59 | }
60 |
61 | public String getStreet() {
62 | return street;
63 | }
64 |
65 | public void setStreet(String street) {
66 | this.street = street;
67 | }
68 |
69 | public Area getArea() {
70 | return area;
71 | }
72 |
73 | public void setArea(Area area) {
74 | this.area = area;
75 | }
76 |
77 | public Set getPeople() {
78 | if (people == null) {
79 | people = new HashSet();
80 | }
81 | return people;
82 | }
83 |
84 | public void setPeople(Set people) {
85 | this.people = people;
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/spring-data-gremlin-orientdb/src/main/java/org/springframework/data/gremlin/repository/orientdb/OrientDBGraphAdapter.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.repository.orientdb;
2 |
3 | import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
4 | import org.apache.tinkerpop.gremlin.structure.Element;
5 | import org.apache.tinkerpop.gremlin.structure.Vertex;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.data.gremlin.repository.GremlinGraphAdapter;
9 | import org.springframework.data.gremlin.schema.property.encoder.GremlinPropertyEncoder;
10 | import org.springframework.data.gremlin.schema.property.encoder.orientdb.OrientDbIdEncoder;
11 | import org.springframework.transaction.annotation.Transactional;
12 |
13 | /**
14 | * Created by gman on 27/06/15.
15 | */
16 | public class OrientDBGraphAdapter extends GremlinGraphAdapter {
17 |
18 | private static final Logger LOGGER = LoggerFactory.getLogger(OrientDBGraphAdapter.class);
19 |
20 | private GremlinPropertyEncoder idEncoder = new OrientDbIdEncoder();
21 |
22 | @Override
23 | @Transactional(readOnly = false)
24 | public Vertex createVertex(OrientGraph graph, String className) {
25 | return graph.addVertex(className);
26 | }
27 |
28 | @Override
29 | public Element refresh(Element element) {
30 | //((OrientElement) element).reload();
31 | return element;
32 | }
33 |
34 | public String encodeId(String id) {
35 | if (id == null) {
36 | return null;
37 | }
38 | if (idEncoder != null) {
39 | id = idEncoder.encode(id).toString();
40 | }
41 | return id;
42 | }
43 |
44 | public String decodeId(String id) {
45 | if (id == null) {
46 | return null;
47 | }
48 | if (idEncoder != null) {
49 | id = idEncoder.decode(id).toString();
50 | }
51 | return id;
52 | }
53 |
54 | @Override
55 | public boolean isValidId(String id) {
56 | return super.isValidId(id) && !id.contains("-");
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/spring-data-gremlin-core/src/main/java/org/springframework/data/gremlin/query/execution/CompositeExecution.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.gremlin.query.execution;
2 |
3 | import org.apache.tinkerpop.gremlin.structure.Vertex;
4 | import org.springframework.data.gremlin.query.AbstractGremlinQuery;
5 | import org.springframework.data.gremlin.query.CompositeResult;
6 | import org.springframework.data.gremlin.repository.GremlinGraphAdapter;
7 | import org.springframework.data.gremlin.schema.GremlinSchema;
8 | import org.springframework.data.gremlin.schema.GremlinSchemaFactory;
9 | import org.springframework.data.repository.query.DefaultParameters;
10 |
11 | import java.util.Iterator;
12 | import java.util.Map;
13 |
14 | /**
15 | * Executes the query to return a Map of properties.
16 | *
17 | * @author Gman
18 | */
19 | @SuppressWarnings("unchecked")
20 | public class CompositeExecution extends AbstractGremlinExecution {
21 |
22 | /**
23 | * Instantiates a new {@link CountExecution}.
24 | */
25 | public CompositeExecution(GremlinSchemaFactory schemaFactory, DefaultParameters parameters, GremlinGraphAdapter graphAdapter) {
26 | super(schemaFactory, parameters, graphAdapter);
27 | }
28 |
29 | @Override
30 | protected Object doExecute(AbstractGremlinQuery query, Object[] values) {
31 |
32 | Iterator result = ((Iterable) query.runQuery(parameters, values)).iterator();
33 | Vertex vertex = result.next();
34 | if (vertex == null) {
35 | return null;
36 | }
37 | if (result.hasNext()) {
38 | throw new IllegalArgumentException("The query resulted in multiple Vertices. Expected only one result for this Execution.");
39 | }
40 |
41 | Map map = elementToMap(vertex);
42 |
43 | Class> mappedType = query.getQueryMethod().getReturnedObjectType();
44 | GremlinSchema mapper = schemaFactory.getSchema(mappedType);
45 | Object entity = mapper.loadFromGraph(graphAdapter, vertex);
46 |
47 | return new CompositeResult