From f3452ba2e02d34afe13b892c55d45dee511f0fcb Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 29 Apr 2013 18:36:41 +0200 Subject: [PATCH] DATAGRAPH-351 - Let Neo4jPersistentProperty extend AnnotationBasedPersistentProperty. --- .../JpaIdFieldAccessListenerFactory.java | 65 +++++---- ...oreNodeDelegatingFieldAccessorFactory.java | 14 +- .../RelationshipNodeFieldAccessorFactory.java | 13 +- .../mapping/Neo4jPersistentProperty.java | 14 +- .../mapping/Neo4JPersistentPropertyImpl.java | 127 +++++++++++------- .../mapping/Neo4jPersistentEntityImpl.java | 23 ++-- 6 files changed, 143 insertions(+), 113 deletions(-) diff --git a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java index c420e85b2..7ddf7de8f 100644 --- a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java +++ b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/fieldaccess/JpaIdFieldAccessListenerFactory.java @@ -16,52 +16,51 @@ package org.springframework.data.neo4j.cross_store.fieldaccess; +import javax.persistence.Id; + import org.springframework.data.neo4j.fieldaccess.FieldAccessListener; import org.springframework.data.neo4j.fieldaccess.FieldAccessorListenerFactory; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; import org.springframework.data.neo4j.support.Neo4jTemplate; -import javax.persistence.Id; - /** * @author Michael Hunger * @since 12.09.2010 */ -public class JpaIdFieldAccessListenerFactory implements FieldAccessorListenerFactory -{ - private final Neo4jTemplate template; +public class JpaIdFieldAccessListenerFactory implements FieldAccessorListenerFactory { + private final Neo4jTemplate template; - public JpaIdFieldAccessListenerFactory(Neo4jTemplate template) { - this.template = template; - } + public JpaIdFieldAccessListenerFactory(Neo4jTemplate template) { + this.template = template; + } - @Override - public boolean accept(final Neo4jPersistentProperty property) { - return property.isAnnotationPresent(Id.class); - } + @Override + public boolean accept(final Neo4jPersistentProperty property) { + return property.getAnnotation(Id.class) != null; + } - @Override - public FieldAccessListener forField(final Neo4jPersistentProperty property) { - return new JpaIdFieldListener(property, template); - } + @Override + public FieldAccessListener forField(final Neo4jPersistentProperty property) { + return new JpaIdFieldListener(property, template); + } - public static class JpaIdFieldListener implements FieldAccessListener { - protected final Neo4jPersistentProperty property; - private final Neo4jTemplate template; + public static class JpaIdFieldListener implements FieldAccessListener { + protected final Neo4jPersistentProperty property; + private final Neo4jTemplate template; - public JpaIdFieldListener(final Neo4jPersistentProperty property, Neo4jTemplate template) { - this.property = property; - this.template = template; - } + public JpaIdFieldListener(final Neo4jPersistentProperty property, Neo4jTemplate template) { + this.property = property; + this.template = template; + } - @Override - public void valueChanged(Object entity, Object oldVal, Object newVal) { - if (newVal != null) { - template.save(entity); -/* TODO EntityState entityState = entity.getEntityState(); - entityState.persist(); -*/ - } - } - } + @Override + public void valueChanged(Object entity, Object oldVal, Object newVal) { + if (newVal != null) { + template.save(entity); + /* TODO EntityState entityState = entity.getEntityState(); + entityState.persist(); + */ + } + } + } } diff --git a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java index 462d28356..5412c08b7 100644 --- a/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java +++ b/spring-data-neo4j-cross-store/src/main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeDelegatingFieldAccessorFactory.java @@ -15,6 +15,9 @@ */ package org.springframework.data.neo4j.cross_store.support.node; +import java.util.Arrays; +import java.util.Collection; + import org.springframework.data.neo4j.annotation.GraphProperty; import org.springframework.data.neo4j.annotation.RelatedTo; import org.springframework.data.neo4j.cross_store.fieldaccess.JpaIdFieldAccessListenerFactory; @@ -35,9 +38,6 @@ import org.springframework.data.neo4j.fieldaccess.TraversalFieldAccessorFactory; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; import org.springframework.data.neo4j.support.Neo4jTemplate; -import java.util.Arrays; -import java.util.Collection; - /** * @author mh * @since 30.04.12 @@ -57,7 +57,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel newConvertingNodePropertyFieldAccessorFactory()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.isAnnotationPresent(GraphProperty.class) && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }, new JpaIdFieldAccessListenerFactory(template)); @@ -75,7 +75,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel new RelatedToSingleFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.isAnnotationPresent(RelatedTo.class) && super.accept(property); + return property.getAnnotation(RelatedTo.class) != null && super.accept(property); } }, new RelatedToCollectionFieldAccessorFactory(getTemplate()), @@ -89,7 +89,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel return new ConvertingNodePropertyFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.isAnnotationPresent(GraphProperty.class) && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }; } @@ -98,7 +98,7 @@ public class CrossStoreNodeDelegatingFieldAccessorFactory extends DelegatingFiel return new PropertyFieldAccessorFactory(getTemplate()) { @Override public boolean accept(Neo4jPersistentProperty property) { - return property.isAnnotationPresent(GraphProperty.class) && super.accept(property); + return property.getAnnotation(GraphProperty.class) != null && super.accept(property); } }; } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/RelationshipNodeFieldAccessorFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/RelationshipNodeFieldAccessorFactory.java index fe86f8a5d..8486a58d7 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/RelationshipNodeFieldAccessorFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/RelationshipNodeFieldAccessorFactory.java @@ -16,21 +16,18 @@ package org.springframework.data.neo4j.fieldaccess; +import static org.springframework.data.neo4j.support.DoReturn.*; + import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.data.neo4j.annotation.EndNode; -import org.springframework.data.neo4j.annotation.StartNode; - - import org.springframework.data.neo4j.mapping.MappingPolicy; import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; import org.springframework.data.neo4j.support.Neo4jTemplate; -import static org.springframework.data.neo4j.support.DoReturn.doReturn; - /** * @author Michael Hunger + * @author Oliver Gierke * @since 21.09.2010 */ public class RelationshipNodeFieldAccessorFactory implements FieldAccessorFactory { @@ -48,11 +45,11 @@ public class RelationshipNodeFieldAccessorFactory implements FieldAccessorFactor } private boolean isEndNodeField(final Neo4jPersistentProperty f) { - return f.isAnnotationPresent(EndNode.class); + return f.isEndNode(); } private boolean isStartNodeField(final Neo4jPersistentProperty f) { - return f.isAnnotationPresent(StartNode.class); + return f.isStartNode(); } @Override diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java index 3bff3e0c8..a9af0667a 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java @@ -16,12 +16,12 @@ package org.springframework.data.neo4j.mapping; -import org.springframework.core.convert.ConversionService; -import org.springframework.data.mapping.PersistentProperty; - import java.lang.annotation.Annotation; import java.util.Collection; +import org.springframework.core.convert.ConversionService; +import org.springframework.data.mapping.PersistentProperty; + /** * Interface for Neo4J specific {@link PersistentProperty}s. Declares additional metadata to lookup relationship * information. @@ -67,8 +67,12 @@ public interface Neo4jPersistentProperty extends PersistentProperty getAnnotations(); T getAnnotation(Class annotationType); - - boolean isAnnotationPresent(Class annotationType); + + boolean isStartNode(); + + boolean isEndNode(); + + boolean isRelationshipType(); void setValue(Object entity, Object newValue); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java index e7714dbc9..554d1698c 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java @@ -16,26 +16,6 @@ package org.springframework.data.neo4j.support.mapping; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.convert.ConversionService; -import org.springframework.data.annotation.Transient; -import org.springframework.data.annotation.Version; -import org.springframework.data.mapping.Association; -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.model.AbstractPersistentProperty; -import org.springframework.data.mapping.model.MappingException; -import org.springframework.data.mapping.model.SimpleTypeHolder; -import org.springframework.data.neo4j.annotation.*; -import org.springframework.data.neo4j.mapping.IndexInfo; -import org.springframework.data.neo4j.mapping.ManagedEntity; -import org.springframework.data.neo4j.mapping.MappingPolicy; -import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity; -import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; -import org.springframework.data.neo4j.mapping.RelationshipInfo; -import org.springframework.data.neo4j.support.DoReturn; -import org.springframework.data.util.TypeInformation; - import java.beans.PropertyDescriptor; import java.lang.annotation.Annotation; import java.lang.reflect.Field; @@ -45,12 +25,42 @@ import java.util.IdentityHashMap; import java.util.Iterator; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.convert.ConversionService; +import org.springframework.data.annotation.Transient; +import org.springframework.data.mapping.Association; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; +import org.springframework.data.mapping.model.MappingException; +import org.springframework.data.mapping.model.SimpleTypeHolder; +import org.springframework.data.neo4j.annotation.EndNode; +import org.springframework.data.neo4j.annotation.Fetch; +import org.springframework.data.neo4j.annotation.GraphId; +import org.springframework.data.neo4j.annotation.GraphProperty; +import org.springframework.data.neo4j.annotation.Indexed; +import org.springframework.data.neo4j.annotation.NodeEntity; +import org.springframework.data.neo4j.annotation.Query; +import org.springframework.data.neo4j.annotation.RelatedTo; +import org.springframework.data.neo4j.annotation.RelatedToVia; +import org.springframework.data.neo4j.annotation.RelationshipEntity; +import org.springframework.data.neo4j.annotation.RelationshipType; +import org.springframework.data.neo4j.annotation.StartNode; +import org.springframework.data.neo4j.mapping.IndexInfo; +import org.springframework.data.neo4j.mapping.ManagedEntity; +import org.springframework.data.neo4j.mapping.MappingPolicy; +import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity; +import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty; +import org.springframework.data.neo4j.mapping.RelationshipInfo; +import org.springframework.data.neo4j.support.DoReturn; +import org.springframework.data.util.TypeInformation; + /** * Implementation of {@link org.springframework.data.neo4j.mapping.Neo4jPersistentProperty}. * * @author Oliver Gierke */ -class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty implements +class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty implements Neo4jPersistentProperty { private final static Logger log = LoggerFactory.getLogger(Neo4jPersistentProperty.class); @@ -67,25 +77,22 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty owner, SimpleTypeHolder simpleTypeHolder, Neo4jMappingContext ctx) { super(field, propertyDescriptor, owner, simpleTypeHolder); this.hash = getField().hashCode(); - this.annotations = extractAnnotations(field); this.relationshipInfo = extractRelationshipInfo(field, ctx); + this.annotations = extractAnnotations(field); this.propertyType = extractPropertyType(); this.isAssociation = ctx.isReference(this); this.isNeo4jEntityType = isNeo4jPropertyType(getType()); this.neo4jPropertyName = createNeo4jPropertyName(); - this.indexInfo = extractIndexInfo(); - this.isIdProperty = annotations.containsKey(GraphId.class); + this.isIdProperty = super.isIdProperty() || getAnnotation(GraphId.class) != null; this.defaultValue = extractDefaultValue(); this.myAssociation = isAssociation() ? super.getAssociation() == null ? createAssociation() : super.getAssociation() : null; this.query = extractQuery(); - this.isVersionProperty = isAnnotationPresent(Version.class); } private String extractQuery() { @@ -131,12 +138,6 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty A findAnnotation(Class annotationType) { - return (A) annotations.get(annotationType); - } - - private RelationshipInfo extractRelationshipInfo(final Field field, Neo4jMappingContext ctx) { if (isAnnotationPresent(RelatedTo.class)) { return RelationshipInfo.fromField(field, getAnnotation(RelatedTo.class), getTypeInformation(), ctx); @@ -151,9 +152,6 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty boolean isAnnotationPresent(Class annotationType) { - return annotations.containsKey(annotationType); - } @Override public void setValue(Object entity, Object newValue) { @@ -169,16 +167,6 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty createAssociation() { return new Association(this, null); @@ -273,6 +261,11 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty getAnnotations() { + + if (annotations == null) { + + } + return annotations.values(); } @@ -328,7 +321,7 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty getOwner() { - return (Neo4jPersistentEntity)super.getOwner(); + return (Neo4jPersistentEntity) super.getOwner(); } @Override public boolean isEntity() { @@ -392,11 +385,7 @@ class Neo4jPersistentPropertyImpl extends AbstractPersistentProperty extends BasicPersistentEntity extends BasicPersistentEntity neo4jPersistentPropertyAssociation) { super.addAssociation(neo4jPersistentPropertyAssociation); final Neo4jPersistentProperty property = neo4jPersistentPropertyAssociation.getInverse(); - if (property.isAnnotationPresent(StartNode.class)) { + if (property.isStartNode()) { this.startNodeProperty = property; } - if (property.isAnnotationPresent(EndNode.class)) { + if (property.isEndNode()) { this.endNodeProperty = property; } }