From 3f119a28c150b41498bf6db80fa0ddb71653e344 Mon Sep 17 00:00:00 2001 From: David Montag Date: Tue, 24 Aug 2010 13:53:37 +0200 Subject: [PATCH] Added support for read-only views of relationships (private Iterable readOnlyField) --- .../graph/neo4j/Neo4jNodeBacking.aj | 62 ++++++++++++++++--- .../persistence/test/Group.java | 12 ++++ .../persistence/test/Person.java | 4 -- .../test/graph/Neo4jGraphPersistenceTest.java | 18 ++++++ 4 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj index 39558ae6f..69e92affb 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj @@ -217,6 +217,10 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields relatedType; + private final EntityInstantiator graphEntityInstantiator; + + public ReadOnlyOneToNRelationshipInfo(RelationshipType type, Direction direction, Class elementClass, EntityInstantiator graphEntityInstantiator) { + this.type = type; + this.direction = direction; + this.relatedType = elementClass; + this.graphEntityInstantiator = graphEntityInstantiator; + } + + public Object apply(final NodeBacked entity, final Object newVal) { + throw new InvalidDataAccessApiUsageException("Cannot set read-only relationship entity field."); + } + + @Override + public Object readObject(NodeBacked entity) { + Node entityNode = entity.getUnderlyingNode(); + if (entityNode == null) { + throw new IllegalStateException("Entity must have a backing Node"); + } + Set result = new HashSet(); + for (Relationship rel : entityNode.getRelationships(type, direction)) { + result.add(graphEntityInstantiator.createEntityFromState(rel.getOtherNode(entityNode), relatedType)); + } + return new ManagedSet(entity, result, this); + } + + } + public static class OneToNRelationshipEntityInfo implements RelationshipInfo { private final RelationshipType type; diff --git a/src/test/java/org/springframework/persistence/test/Group.java b/src/test/java/org/springframework/persistence/test/Group.java index 7c0694347..20b2c6282 100644 --- a/src/test/java/org/springframework/persistence/test/Group.java +++ b/src/test/java/org/springframework/persistence/test/Group.java @@ -4,12 +4,16 @@ import org.springframework.persistence.graph.Direction; import org.springframework.persistence.graph.Graph; import java.util.Collection; +import java.util.HashSet; @Graph.Entity public class Group { @Graph.Entity.Relationship(type = "persons", direction = Direction.OUTGOING, elementClass = Person.class) private Collection persons; + + @Graph.Entity.Relationship(type = "persons", elementClass = Person.class) + private Iterable readOnlyPersons; public void setPersons(Collection persons) { this.persons = persons; @@ -23,4 +27,12 @@ public class Group { return persons; } + public Iterable getReadOnlyPersons() { + return readOnlyPersons; + } + + public void setReadOnlyPersons(Iterable p) { + readOnlyPersons = p; + } + } diff --git a/src/test/java/org/springframework/persistence/test/Person.java b/src/test/java/org/springframework/persistence/test/Person.java index a00452351..6fd6a8528 100644 --- a/src/test/java/org/springframework/persistence/test/Person.java +++ b/src/test/java/org/springframework/persistence/test/Person.java @@ -1,7 +1,5 @@ package org.springframework.persistence.test; -import java.util.Collection; - import org.neo4j.graphdb.DynamicRelationshipType; import org.neo4j.graphdb.Relationship; import org.springframework.persistence.graph.Direction; @@ -32,13 +30,11 @@ public class Person { // @Property(serialize=SerializationPolicy.STRING, index=true, queryable=true, removeOnReset=true) // Date birthday; - public Person(String name, int age) { this.name = name; this.age = age; } - public String getName() { return name; } diff --git a/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java b/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java index 914fd0d94..f9dc765d2 100644 --- a/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java +++ b/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java @@ -342,4 +342,22 @@ public class Neo4jGraphPersistenceTest { Person p = new Person("Michael", 35); p.setFriendships(new HashSet()); } + + @Test + @Transactional + public void testOneToManyReadOnly() { + Person michael = new Person("Michael", 35); + Person david = new Person("David", 25); + Group group = new Group(); + Set persons = new HashSet(Arrays.asList(michael, david)); + group.setPersons(persons); + Assert.assertEquals(persons, IteratorUtil.addToCollection(group.getReadOnlyPersons().iterator(), new HashSet())); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + @Transactional + public void testOneToManyReadOnlyShouldThrowExceptionOnSet() { + Group group = new Group(); + group.setReadOnlyPersons(new HashSet()); + } }