DATAGRAPH-173 fixed verify method for interfaces, added interface support for type-representation strategies

This commit is contained in:
Michael Hunger
2012-01-11 00:26:06 +01:00
parent 788998b95f
commit 67c5ea0a37
8 changed files with 95 additions and 46 deletions

View File

@@ -59,7 +59,7 @@ public class Neo4jPersistentEntityImpl<T> extends BasicPersistentEntity<T, Neo4j
@Override
public void verify() {
super.verify();
if (isManaged()) {
if (isManaged() || getType().isInterface()) {
return;
}
final Neo4jPersistentProperty idProperty = getIdProperty();

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.data.neo4j.support.typerepresentation;
import java.lang.annotation.Annotation;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
@@ -27,6 +25,8 @@ import org.springframework.data.neo4j.support.index.ClosableIndexHits;
import org.springframework.data.neo4j.support.index.IndexProvider;
import org.springframework.data.neo4j.support.index.IndexType;
import java.lang.annotation.Annotation;
public abstract class AbstractIndexingTypeRepresentationStrategy<S extends PropertyContainer> implements
TypeRepresentationStrategy<S> {
@@ -90,15 +90,16 @@ public abstract class AbstractIndexingTypeRepresentationStrategy<S extends Prope
return typeCache.getClassForName(className);
}
protected void addToTypesIndex(S relationshipOrNode, Class<?> 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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -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) {

View File

@@ -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 {
}

View File

@@ -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");

View File

@@ -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<Being> {
}

View File

@@ -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<Being> beings = beingRepository.findAll().as(List.class);
assertEquals(3,beings.size());
}
}