From 17ea86b5fd3ce0907e99e7a11c6af605a0c697da Mon Sep 17 00:00:00 2001 From: David Montag Date: Wed, 30 Mar 2011 12:11:43 -0700 Subject: [PATCH] Refactored GraphDatabaseContext a bit. --- ...AbstractNodeRelationshipFieldAccessor.java | 7 +- .../neo4j/support/GraphDatabaseContext.java | 270 +++++++++--------- 2 files changed, 132 insertions(+), 145 deletions(-) diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/fieldaccess/AbstractNodeRelationshipFieldAccessor.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/fieldaccess/AbstractNodeRelationshipFieldAccessor.java index 7c2b95a25..1fc71af85 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/fieldaccess/AbstractNodeRelationshipFieldAccessor.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/fieldaccess/AbstractNodeRelationshipFieldAccessor.java @@ -16,10 +16,7 @@ package org.springframework.data.graph.neo4j.fieldaccess; -import org.neo4j.graphdb.Direction; -import org.neo4j.graphdb.Node; -import org.neo4j.graphdb.Relationship; -import org.neo4j.graphdb.RelationshipType; +import org.neo4j.graphdb.*; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.graph.core.GraphBacked; import org.springframework.data.graph.neo4j.support.GraphDatabaseContext; @@ -31,7 +28,7 @@ import java.util.Set; * @author Michael Hunger * @since 11.09.2010 */ -public abstract class AbstractNodeRelationshipFieldAccessor implements FieldAccessor { +public abstract class AbstractNodeRelationshipFieldAccessor implements FieldAccessor { protected final RelationshipType type; protected final Direction direction; protected final Class relatedType; diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/GraphDatabaseContext.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/GraphDatabaseContext.java index 10d0dd2b4..4c54f8555 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/GraphDatabaseContext.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/GraphDatabaseContext.java @@ -44,55 +44,17 @@ import java.util.Map; */ public class GraphDatabaseContext { + private static final Log log = LogFactory.getLog(GraphDatabaseContext.class); public static final String DEFAULT_NODE_INDEX_NAME = "node"; public static final String DEFAULT_RELATIONSHIP_INDEX_NAME = "relationship"; private GraphDatabaseService graphDatabaseService; - private ConversionService conversionService; - private NodeTypeRepresentationStrategy nodeTypeRepresentationStrategy; - private RelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy; - private Validator validator; + private NodeTypeRepresentationStrategy nodeTypeRepresentationStrategy; - private final static Log log = LogFactory.getLog(GraphDatabaseContext.class); - - public GraphDatabaseService getGraphDatabaseService() { - return graphDatabaseService; - } - - public void setGraphDatabaseService(GraphDatabaseService graphDatabaseService) { - this.graphDatabaseService = graphDatabaseService; - } - - public NodeTypeRepresentationStrategy getNodeTypeRepresentationStrategy() { - return nodeTypeRepresentationStrategy; - } - - public void setNodeTypeRepresentationStrategy(NodeTypeRepresentationStrategy nodeTypeRepresentationStrategy) { - this.nodeTypeRepresentationStrategy = nodeTypeRepresentationStrategy; - } - - public RelationshipTypeRepresentationStrategy getRelationshipTypeRepresentationStrategy() { - return relationshipTypeRepresentationStrategy; - } - - public void setRelationshipTypeRepresentationStrategy(RelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy) { - this.relationshipTypeRepresentationStrategy = relationshipTypeRepresentationStrategy; - } - - public ConversionService getConversionService() { - return conversionService; - } - - public void setConversionService(ConversionService conversionService) { - this.conversionService = conversionService; - } - - public Node createNode() { - return graphDatabaseService.createNode(); - } + private RelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy; /** * @param relationship to remove from indexes and to delete @@ -147,22 +109,6 @@ public class GraphDatabaseContext { } } - /** - * Creates either a node or relationship entity by delegating the creation to the appropriate @{link EntityInstantiator} - * @param state Node or Relationship - * @param type target entity type - * @return an instance of the entity type - */ - public T createEntityFromState(final S state, final Class type) { - if (state==null) throw new IllegalArgumentException("state has to be either a Node or Relationship, not null"); - if (state instanceof Node && NodeBacked.class.isAssignableFrom(type)) - return (T) nodeTypeRepresentationStrategy.createEntity((Node) state, (Class) type); -// return (T) graphEntityInstantiator.createEntityFromState((Node) state, typeRepresentationStrategy.confirmType((Node)state, (Class)type)); - else - return (T) relationshipTypeRepresentationStrategy.createEntity((Relationship) state, (Class) type); -// return (T) relationshipEntityInstantiator.createEntityFromState((Relationship) state, (Class) type); - } - private IndexManager getIndexManager() { return graphDatabaseService.index(); } @@ -191,51 +137,15 @@ public class GraphDatabaseContext { throw new IllegalArgumentException("Wrong index type supplied "+type); } - /** - * @param nodeId - * @return Node - * @throws NotFoundException - */ - public Node getNodeById(final long nodeId) { - return graphDatabaseService.getNodeById(nodeId); - } - - /** - * delegates to the configured @{link TypeRepresentationStrategy} for after entity creation operations - * @param node - * @param entityClass - */ - public void postEntityCreation(Node node, final Class entityClass) { - nodeTypeRepresentationStrategy.postEntityCreation(node, entityClass); - } - /** - * delegates to the configured @{link TypeRepresentationStrategy} for after entity creation operations - * @param relationship - * @param entityClass - */ - public void postEntityCreation(Relationship relationship, final Class entityClass) { - relationshipTypeRepresentationStrategy.postEntityCreation(relationship, entityClass); - } - /** * delegates to the configured @{link TypeRepresentationStrategy} to iterate over all instances of this type - * @param clazz type of entity + * @param entityClass type of entity * @param * @return * TODO inheritance handling */ - public Iterable findAll(final Class clazz) { - if (checkIsNodeBacked(clazz)) { - return (Iterable) nodeTypeRepresentationStrategy.findAll((Class) clazz); - } - return (Iterable) relationshipTypeRepresentationStrategy.findAll((Class) clazz); - } - - /** - * class base check for nodebacked subclasses - */ - private boolean checkIsNodeBacked(Class clazz) { - return NodeBacked.class.isAssignableFrom(clazz); + public Iterable findAll(final Class entityClass) { + return getTypeRepresentationStrategy(entityClass).findAll(entityClass); } /** @@ -243,38 +153,8 @@ public class GraphDatabaseContext { * @param entityClass * @return count of all instances */ - public long count(final Class entityClass) { - if (checkIsNodeBacked(entityClass)) { - return nodeTypeRepresentationStrategy.count((Class)entityClass); - } - return relationshipTypeRepresentationStrategy.count((Class) entityClass); - } - - /** - * delegates to the configured @{link TypeRepresentationStrategy} to lookup the type information for the given node - * @param node - * @param - * @return entity type of the node - * @throws IllegalStateException for nodes that are not instance backing nodes of a known type - */ - public Class getJavaType(final Node node) { - return nodeTypeRepresentationStrategy.getJavaType(node); - } - - /** - * @return reference node of the graph database - */ - public Node getReferenceNode() { - return graphDatabaseService.getReferenceNode(); - } - - - /** - * @return Neo4j Transaction manager - */ - public TransactionManager getTxManager() { - - return ((AbstractGraphDatabase) graphDatabaseService).getConfig().getTxModule().getTxManager(); + public long count(final Class entityClass) { + return getTypeRepresentationStrategy(entityClass).count(entityClass); } /** @@ -289,35 +169,146 @@ public class GraphDatabaseContext { } } + + + public > T createEntityFromState(S state, Class type) { + if (state==null) throw new IllegalArgumentException("state has to be either a Node or Relationship, not null"); + return getTypeRepresentationStrategy(state, type).createEntity(state, type); + } + + public > T projectTo(GraphBacked entity, Class targetType) { + S state = entity.getPersistentState(); + return getTypeRepresentationStrategy(state, targetType).projectEntity(state, targetType); + } + + public T createEntityFromStoredType(Node node) { + return nodeTypeRepresentationStrategy.createEntity(node); + } + + public > void postEntityCreation(S node, Class entityClass) { + getTypeRepresentationStrategy(node, entityClass).postEntityCreation(node, entityClass); + } + + + + @SuppressWarnings("unchecked") + private > + TypeRepresentationStrategy getTypeRepresentationStrategy(Class type) { + if (NodeBacked.class.isAssignableFrom(type)) { + return (TypeRepresentationStrategy) nodeTypeRepresentationStrategy; + } else if (RelationshipBacked.class.isAssignableFrom(type)) { + return (TypeRepresentationStrategy) relationshipTypeRepresentationStrategy; + } + throw new IllegalArgumentException("Type is not NodeBacked nor RelationshipBacked."); + } + + @SuppressWarnings("unchecked") + private > + TypeRepresentationStrategy getTypeRepresentationStrategy(S state, Class type) { + if (state instanceof Node && NodeBacked.class.isAssignableFrom(type)) { + return (TypeRepresentationStrategy) nodeTypeRepresentationStrategy; + } else if (state instanceof Relationship && RelationshipBacked.class.isAssignableFrom(type)) { + return (TypeRepresentationStrategy) relationshipTypeRepresentationStrategy; + } + throw new IllegalArgumentException("Type is not NodeBacked nor RelationshipBacked."); + } + + @SuppressWarnings("unchecked") + private > + TypeRepresentationStrategy getTypeRepresentationStrategy(S state) { + if (state instanceof Node) { + return (TypeRepresentationStrategy) nodeTypeRepresentationStrategy; + } else if (state instanceof Relationship) { + return (TypeRepresentationStrategy) relationshipTypeRepresentationStrategy; + } + throw new IllegalArgumentException("Type is not NodeBacked nor RelationshipBacked."); + } + + + /** - * delegates to @{link GraphDatabaseService} + * @return Neo4j Transaction manager + */ + public TransactionManager getTxManager() { + return ((AbstractGraphDatabase) graphDatabaseService).getConfig().getTxModule().getTxManager(); + } + + /** + * Delegates to {@link GraphDatabaseService} + */ + public Node createNode() { + return graphDatabaseService.createNode(); + } + + /** + * Delegates to {@link GraphDatabaseService} + */ + public Node getNodeById(final long nodeId) { + return graphDatabaseService.getNodeById(nodeId); + } + + /** + * Delegates to {@link GraphDatabaseService} + */ + public Node getReferenceNode() { + return graphDatabaseService.getReferenceNode(); + } + + /** + * Delegates to {@link GraphDatabaseService} */ public Iterable getAllNodes() { return graphDatabaseService.getAllNodes(); } /** - * delegates to @{link GraphDatabaseService} + * Delegates to {@link GraphDatabaseService} */ public Transaction beginTx() { return graphDatabaseService.beginTx(); } /** - * delegates to @{link GraphDatabaseService} + * Delegates to {@link GraphDatabaseService} */ public Relationship getRelationshipById(final long id) { return graphDatabaseService.getRelationshipById(id); } - public T projectTo(GraphBacked entity, Class targetType) { - final Object state = entity.getPersistentState(); - if (state instanceof Node) - return (T) nodeTypeRepresentationStrategy.projectEntity((Node) state, (Class) targetType); - else - return (T) relationshipTypeRepresentationStrategy.projectEntity((Relationship) state, (Class) targetType); + + + public GraphDatabaseService getGraphDatabaseService() { + return graphDatabaseService; + } + + public void setGraphDatabaseService(GraphDatabaseService graphDatabaseService) { + this.graphDatabaseService = graphDatabaseService; + } + + public NodeTypeRepresentationStrategy getNodeTypeRepresentationStrategy() { + return nodeTypeRepresentationStrategy; } + public void setNodeTypeRepresentationStrategy(NodeTypeRepresentationStrategy nodeTypeRepresentationStrategy) { + this.nodeTypeRepresentationStrategy = nodeTypeRepresentationStrategy; + } + + public RelationshipTypeRepresentationStrategy getRelationshipTypeRepresentationStrategy() { + return relationshipTypeRepresentationStrategy; + } + + public void setRelationshipTypeRepresentationStrategy(RelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy) { + this.relationshipTypeRepresentationStrategy = relationshipTypeRepresentationStrategy; + } + + public ConversionService getConversionService() { + return conversionService; + } + + public void setConversionService(ConversionService conversionService) { + this.conversionService = conversionService; + } + public Validator getValidator() { return validator; } @@ -326,8 +317,7 @@ public class GraphDatabaseContext { this.validator = validatorFactory; } - public T createEntityFromStoredType(Node node) { - return nodeTypeRepresentationStrategy.createEntity(node); - } + + }