From 6a1910566ff9f87fa229b3cd3802e0150dc41770 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Fri, 13 Aug 2010 01:17:28 +0200 Subject: [PATCH] cleanup, some refactoring, todo managedset --- .../persistence/graph/Relationship.java | 4 +- .../graph/neo4j/Neo4jNodeBacking.aj | 75 +++++++++---------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/springframework/persistence/graph/Relationship.java b/src/main/java/org/springframework/persistence/graph/Relationship.java index aa4489ea2..cf6704ec1 100644 --- a/src/main/java/org/springframework/persistence/graph/Relationship.java +++ b/src/main/java/org/springframework/persistence/graph/Relationship.java @@ -5,6 +5,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.persistence.graph.neo4j.NodeBacked; + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Relationship { @@ -13,6 +15,6 @@ public @interface Relationship { Direction direction(); - Class elementClass() default Void.class; + Class elementClass() default NodeBacked.class; } 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 53ff8cdd7..1a6ea1f93 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Set; +import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.reflect.FieldSignature; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.DynamicRelationshipType; @@ -150,8 +151,8 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields Neo4J relationship with value=[" + newVal + "]"); - relInfo.apply(entity, newVal); - return null; + Object result=relInfo.apply(entity, newVal); + return proceed(entity,result); } catch(NotInTransactionException e) { throw new InvalidDataAccessResourceUsageException("Not in a Neo4j transaction.", e); } @@ -168,7 +169,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields relatedType=(Class)field.getType(); if (relAnnotation != null) { return new SingleRelationshipInfo(DynamicRelationshipType.withName(relAnnotation.type()), - relAnnotation.direction().toNeo4jDir(), field.getType(), graphEntityInstantiator); + relAnnotation.direction().toNeo4jDir(),relatedType, graphEntityInstantiator); } return new SingleRelationshipInfo(DynamicRelationshipType.withName(getNeo4jPropertyName(field)), - Direction.OUTGOING, field.getType(), graphEntityInstantiator); + Direction.OUTGOING, relatedType, graphEntityInstantiator); } if (isOneToNRelationshipField(field)) { - final Relationship relAnnotation = field.getAnnotation(Relationship.class); return new OneToNRelationshipInfo(DynamicRelationshipType.withName(relAnnotation.type()), relAnnotation.direction().toNeo4jDir(), relAnnotation.elementClass(), graphEntityInstantiator); } @@ -200,14 +201,13 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields clazz; + private final Class relatedType; private final EntityInstantiator graphEntityInstantiator; - public SingleRelationshipInfo(RelationshipType type, Direction direction, Class clazz, EntityInstantiator graphEntityInstantiator) { + public SingleRelationshipInfo(RelationshipType type, Direction direction, Class clazz, EntityInstantiator graphEntityInstantiator) { this.type = type; this.direction = direction; - this.clazz = clazz; + this.relatedType = clazz; this.graphEntityInstantiator = graphEntityInstantiator; } - public void apply(NodeBacked entity, Object newVal) { + public Object apply(NodeBacked entity, Object newVal) { if (newVal != null && !(newVal instanceof NodeBacked)) { throw new IllegalArgumentException("New value must be NodeBacked."); } @@ -234,7 +234,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields) clazz); + return graphEntityInstantiator.createEntityFromState(targetNode, relatedType); } } @@ -272,19 +269,20 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields elementClass; + private final Class relatedType; private final EntityInstantiator graphEntityInstantiator; - public OneToNRelationshipInfo(RelationshipType type, Direction direction, Class elementClass, EntityInstantiator graphEntityInstantiator) { + public OneToNRelationshipInfo(RelationshipType type, Direction direction, Class elementClass, EntityInstantiator graphEntityInstantiator) { this.type = type; this.direction = direction; - this.elementClass = elementClass; + this.relatedType = elementClass; this.graphEntityInstantiator = graphEntityInstantiator; } - public void apply(NodeBacked entity, Object newVal) { + public Object apply(NodeBacked entity, Object newVal) { Node entityNode = entity.getUnderlyingNode(); + Set newNodes=new HashSet(); if (newVal != null) { if (!(newVal instanceof Set)) { throw new IllegalArgumentException("New value must be a Set, was: " + newVal.getClass()); @@ -294,28 +292,29 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields) newVal) { - NodeBacked nb = (NodeBacked) obj; - Node targetNode = nb.getUnderlyingNode(); + for (Node newNode : newNodes) { switch(direction) { - case OUTGOING : entityNode.createRelationshipTo(targetNode, type); break; - case INCOMING : targetNode.createRelationshipTo(entityNode, type); break; + case OUTGOING : entityNode.createRelationshipTo(newNode, type); break; + case INCOMING : newNode.createRelationshipTo(entityNode, type); break; default : throw new IllegalArgumentException("invalid direction " + direction); } } - + return newVal; // TODO managedSet that for each mutating method calls this apply (todo use AspectJ to handle that?) } @Override @@ -324,10 +323,10 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields rels = entityNode.getRelationships(type, direction); - Set result = new HashSet(); - for (org.neo4j.graphdb.Relationship rel : rels) { - result.add(graphEntityInstantiator.createEntityFromState(rel.getOtherNode(entityNode), (Class) elementClass)); + Set result = new HashSet(); + for (org.neo4j.graphdb.Relationship rel : entityNode.getRelationships(type, direction)) { + NodeBacked newEntity=graphEntityInstantiator.createEntityFromState(rel.getOtherNode(entityNode), relatedType);; + result.add(newEntity); } return result; }