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 4c54f8555..e59256a93 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 @@ -56,24 +56,66 @@ public class GraphDatabaseContext { private RelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy; - /** - * @param relationship to remove from indexes and to delete - */ - private void removeRelationship(Relationship relationship) { - removeFromIndexes(relationship); - relationship.delete(); + + + public > Index getIndex(Class type) { + return getIndex(type, null); + } + + public > Index getIndex(Class type, String indexName) { + return getIndex(type, indexName, false); + } + + + public > Index getIndex(Class type, String indexName, boolean fullText) { + if (indexName==null) indexName = Indexed.Name.get(type); + Map config = fullText ? LuceneIndexImplementation.FULLTEXT_CONFIG : null; + if (NodeBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forNodes(indexName, config); + if (RelationshipBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forRelationships(indexName, config); + throw new IllegalArgumentException("Wrong index type supplied: " + type); } /** - * @param relationship to be removed from all indexes, all properties are removed from all indexes + * @return true if a transaction manager is available and a transaction is currently running */ - private void removeFromIndexes(Relationship relationship) { - IndexManager indexManager = graphDatabaseService.index(); - for (String indexName : getIndexManager().relationshipIndexNames()) { - indexManager.forRelationships(indexName).remove(relationship); + public boolean transactionIsRunning() { + try { + return getTxManager().getStatus() != Status.STATUS_NO_TRANSACTION; + } catch (SystemException e) { + log.error("Error accessing TransactionManager", e); + return false; } } + public > Iterable findAll(final Class entityClass) { + return getTypeRepresentationStrategy(entityClass).findAll(entityClass); + } + + public > long count(final Class entityClass) { + return getTypeRepresentationStrategy(entityClass).count(entityClass); + } + + + + public > T createEntityFromStoredType(S state) { + return getTypeRepresentationStrategy(state).createEntity(state); + } + + 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 > void postEntityCreation(S node, Class entityClass) { + getTypeRepresentationStrategy(node, entityClass).postEntityCreation(node, entityClass); + } + + /** * removes the entity by cleaning the relationships first and then removing the node * it removes all of them from all indexes in advance @@ -93,19 +135,29 @@ public class GraphDatabaseContext { node.delete(); } + private void removeFromIndexes(Node node) { + IndexManager indexManager = getIndexManager(); + for (String indexName : indexManager.nodeIndexNames()) { + indexManager.forNodes(indexName).remove(node); + } + } + + // public void removeRelationshipEntity(RelationshipBacked entity) { Relationship relationship = entity.getPersistentState(); if (relationship==null) return; removeRelationship(relationship); } - /** - * @param node to be removed from all indexes, all properties of the node are removed from all indexes - */ - private void removeFromIndexes(Node node) { - IndexManager indexManager = graphDatabaseService.index(); - for (String indexName : getIndexManager().nodeIndexNames()) { - indexManager.forNodes(indexName).remove(node); + private void removeRelationship(Relationship relationship) { + removeFromIndexes(relationship); + relationship.delete(); + } + + private void removeFromIndexes(Relationship relationship) { + IndexManager indexManager = getIndexManager(); + for (String indexName : indexManager.relationshipIndexNames()) { + indexManager.forRelationships(indexName).remove(relationship); } } @@ -113,82 +165,6 @@ public class GraphDatabaseContext { return graphDatabaseService.index(); } - /** - * @param indexName or null, "node" is assumed if null - * @return node index {@link Index} - */ - public > Index getIndex(Class type, String indexName) { - if (indexName==null) indexName = Indexed.Name.get(type); - if (NodeBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forNodes(indexName); - if (RelationshipBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forRelationships(indexName); - throw new IllegalArgumentException("Wrong index type supplied "+type); - } - /** - * @param type type of index requested - either Node.class or Relationship.class - * @param indexName or null, "node" is assumed if null - * @param fullText true if a fulltext queryable index is needed, false for exact match - * @return node index {@link Index} - */ - public > Index getIndex(Class type, String indexName, boolean fullText) { - if (indexName==null) indexName = Indexed.Name.get(type); - Map config = fullText ? LuceneIndexImplementation.FULLTEXT_CONFIG : LuceneIndexImplementation.EXACT_CONFIG; - if (NodeBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forNodes(indexName, config); - if (RelationshipBacked.class.isAssignableFrom(type)) return (Index) getIndexManager().forRelationships(indexName, config); - throw new IllegalArgumentException("Wrong index type supplied "+type); - } - - /** - * delegates to the configured @{link TypeRepresentationStrategy} to iterate over all instances of this type - * @param entityClass type of entity - * @param - * @return - * TODO inheritance handling - */ - public Iterable findAll(final Class entityClass) { - return getTypeRepresentationStrategy(entityClass).findAll(entityClass); - } - - /** - * delegates to the configured @{link TypeRepresentationStrategy} for a count of all instances of this type - * @param entityClass - * @return count of all instances - */ - public long count(final Class entityClass) { - return getTypeRepresentationStrategy(entityClass).count(entityClass); - } - - /** - * @return true if a transaction manager is available and a transaction is currently running - */ - public boolean transactionIsRunning() { - try { - return getTxManager().getStatus() != Status.STATUS_NO_TRANSACTION; - } catch (SystemException e) { - log.error("Error accessing TransactionManager", e); - return false; - } - } - - - - 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")