From 67c5ea0a37060a858a731d20c79d58b2c7a09ec6 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Wed, 11 Jan 2012 00:26:06 +0100 Subject: [PATCH] DATAGRAPH-173 fixed verify method for interfaces, added interface support for type-representation strategies --- .../mapping/Neo4jPersistentEntityImpl.java | 2 +- ...actIndexingTypeRepresentationStrategy.java | 23 +++++++------ ...ndexingNodeTypeRepresentationStrategy.java | 14 -------- ...ferenceNodeTypeRepresentationStrategy.java | 34 +++++++++++-------- .../data/neo4j/model/Being.java | 26 ++++++++++++++ .../data/neo4j/model/Person.java | 2 +- .../neo4j/repository/BeingRepository.java | 25 ++++++++++++++ .../neo4j/repository/GraphRepositoryTest.java | 15 +++++--- 8 files changed, 95 insertions(+), 46 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Being.java create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/BeingRepository.java diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jPersistentEntityImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jPersistentEntityImpl.java index c1358d205..9b8aa3e1b 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jPersistentEntityImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jPersistentEntityImpl.java @@ -59,7 +59,7 @@ public class Neo4jPersistentEntityImpl extends BasicPersistentEntity implements TypeRepresentationStrategy { @@ -90,15 +90,16 @@ public abstract class AbstractIndexingTypeRepresentationStrategy entityClass) { - Class type = entityClass; - while (type.getAnnotation(typeEntityClass) != null) { - String value = entityClass.getName(); - if (indexProvider != null) - value = indexProvider.createIndexValueForType(entityClass); - - getTypesIndex().add(relationshipOrNode, INDEX_KEY, value); - type = type.getSuperclass(); + protected void addToTypesIndex(S relationshipOrNode, Class type) { + if (type == null || !type.isAnnotationPresent(typeEntityClass)) return; + String value = type.getName(); + if (indexProvider != null) { + value = indexProvider.createIndexValueForType(type); + } + getTypesIndex().add(relationshipOrNode, INDEX_KEY, value); + addToTypesIndex(relationshipOrNode, type.getSuperclass()); + for (Class anInterface : type.getInterfaces()) { + addToTypesIndex(relationshipOrNode, anInterface); } } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/IndexingNodeTypeRepresentationStrategy.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/IndexingNodeTypeRepresentationStrategy.java index 207860b38..db96e5209 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/IndexingNodeTypeRepresentationStrategy.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/IndexingNodeTypeRepresentationStrategy.java @@ -30,18 +30,4 @@ public class IndexingNodeTypeRepresentationStrategy extends AbstractIndexingType public IndexingNodeTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider) { super(graphDb, indexProvider, INDEX_NAME, Node.class, NodeEntity.class); } - - @Override - protected void addToTypesIndex(Node node, Class entityClass) { - Class klass = entityClass; - while (klass.getAnnotation(NodeEntity.class) != null) { - String value = klass.getName(); - if (indexProvider != null) - value = indexProvider.createIndexValueForType(klass); - - getTypesIndex().add(node, INDEX_KEY, value); - klass = klass.getSuperclass(); - } - } - } \ No newline at end of file diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/SubReferenceNodeTypeRepresentationStrategy.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/SubReferenceNodeTypeRepresentationStrategy.java index 6af59fa81..446a116eb 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/SubReferenceNodeTypeRepresentationStrategy.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/typerepresentation/SubReferenceNodeTypeRepresentationStrategy.java @@ -23,6 +23,7 @@ import org.neo4j.helpers.collection.IterableWrapper; import org.neo4j.kernel.Traversal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.core.GraphDatabase; import org.springframework.data.neo4j.core.NodeTypeRepresentationStrategy; @@ -95,22 +96,27 @@ public class SubReferenceNodeTypeRepresentationStrategy implements NodeTypeRepre incrementAndGetCounter(subReference, SUBREFERENCE_NODE_COUNTER_KEY); - updateSuperClassSubrefs(type, subReference); + updateSuperClassSubrefs(type.getSuperclass(), subReference); + for (Class anInterface : type.getInterfaces()) { + updateSuperClassSubrefs(anInterface, subReference); + } } - private void updateSuperClassSubrefs(Class clazz, Node subReference) { - Class superClass = clazz.getSuperclass(); - if (superClass != null) { - Node superClassSubref = obtainSubreferenceNode(superClass); - if (getSingleOtherNode(subReference, SUBCLASS_OF_RELATIONSHIP_TYPE, Direction.OUTGOING) == null) { - subReference.createRelationshipTo(superClassSubref, SUBCLASS_OF_RELATIONSHIP_TYPE); - } - superClassSubref.setProperty(SUBREF_CLASS_KEY, superClass.getName()); - Integer count = incrementAndGetCounter(superClassSubref, SUBREFERENCE_NODE_COUNTER_KEY); - if (log.isDebugEnabled()) log.debug("count on ref " + superClassSubref + " for class " + superClass.getSimpleName() + " = " + count); - updateSuperClassSubrefs(superClass, superClassSubref); - } - } + private void updateSuperClassSubrefs(Class type, Node subReference) { + if (type == null || !type.isAnnotationPresent(NodeEntity.class)) return; + + Node superClassSubref = obtainSubreferenceNode(type); + if (getSingleOtherNode(subReference, SUBCLASS_OF_RELATIONSHIP_TYPE, Direction.OUTGOING) == null) { + subReference.createRelationshipTo(superClassSubref, SUBCLASS_OF_RELATIONSHIP_TYPE); + } + superClassSubref.setProperty(SUBREF_CLASS_KEY, type.getName()); + Integer count = incrementAndGetCounter(superClassSubref, SUBREFERENCE_NODE_COUNTER_KEY); + if (log.isDebugEnabled()) log.debug("count on ref " + superClassSubref + " for class " + type.getSimpleName() + " = " + count); + updateSuperClassSubrefs(type.getSuperclass(), superClassSubref); + for (Class anInterface : type.getInterfaces()) { + updateSuperClassSubrefs(anInterface, subReference); + } + } @Override public long count(final Class entityClass) { diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Being.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Being.java new file mode 100644 index 000000000..cb0c9014d --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Being.java @@ -0,0 +1,26 @@ +/** + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.model; + +import org.springframework.data.neo4j.annotation.NodeEntity; + +/** + * @author mh + * @since 10.01.12 + */ +@NodeEntity +public interface Being { +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java index 678fdecdd..60ab77d71 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/model/Person.java @@ -30,7 +30,7 @@ import java.util.Set; @NodeEntity -public class Person { +public class Person implements Being { public static final String NAME_INDEX = "name-index"; public static final org.neo4j.graphdb.RelationshipType KNOWS = DynamicRelationshipType.withName("knows"); diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/BeingRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/BeingRepository.java new file mode 100644 index 000000000..1eb8a146a --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/BeingRepository.java @@ -0,0 +1,25 @@ +/** + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.repository; + +import org.springframework.data.neo4j.model.Being; + +/** + * @author mh + * @since 10.01.12 + */ +public interface BeingRepository extends GraphRepository { +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java index 8b64ade27..2a9ce1ca5 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java @@ -28,10 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; -import org.springframework.data.neo4j.model.Friendship; -import org.springframework.data.neo4j.model.Group; -import org.springframework.data.neo4j.model.Person; -import org.springframework.data.neo4j.model.RootEntity; +import org.springframework.data.neo4j.model.*; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.conversion.NoSuchColumnFoundException; import org.springframework.data.neo4j.support.node.Neo4jHelper; @@ -50,12 +47,12 @@ import org.springframework.transaction.support.TransactionTemplate; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Map; import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; -import static org.junit.Assert.assertEquals; import static org.junit.internal.matchers.IsCollectionContaining.hasItem; import static org.junit.internal.matchers.IsCollectionContaining.hasItems; import static org.neo4j.helpers.collection.IteratorUtil.addToCollection; @@ -77,6 +74,8 @@ public class GraphRepositoryTest { @Autowired private PersonRepository personRepository; @Autowired + private BeingRepository beingRepository; + @Autowired GroupRepository groupRepository; @Autowired @@ -320,4 +319,10 @@ public class GraphRepositoryTest { final Person p2 = neo4jTemplate.findOne(person.getId(), Person.class); assertEquals(root.getId(),p2.getRoot().getId()); } + + @Test + public void testUseInterfaceAsPersistentEntity() { + final List beings = beingRepository.findAll().as(List.class); + assertEquals(3,beings.size()); + } }