Class AbstractNeo4JNodeRepositoyImpl<S>

  • Type Parameters:
    S - Type of class
    All Implemented Interfaces:
    Neo4jQueryRepository, Neo4jRepository<S,​Long>
    Direct Known Subclasses:
    ReflectiveNeo4JNodeRepositoryImpl

    public abstract class AbstractNeo4JNodeRepositoyImpl<S>
    extends AbstractNeo4JRepository<S,​Long>
    implements Neo4jQueryRepository
    Quick notes about usage:
    • This class should handle pretty much all the default stuff
    • On a save all nodes without an id will be saved (propagating to ALL nodes through EVERY relationship!)
    • We are currently only capable of handling (source)-[DIRECTED]->(target) unidirectional relationships from S to T.
    • On a load the node plus all related nodes are returned.
    Since:
    1.0
    Author:
    Oliver Krauss
    • Field Detail

      • CREATE_STATEMENT

        protected String CREATE_STATEMENT
        Create an object with all properties set
      • CREATE_BULK_STATEMENT

        protected String CREATE_BULK_STATEMENT
        Create a list of objects with all properties set
      • UPDATE_STATEMENT

        protected String UPDATE_STATEMENT
        Update an existing object
      • UPDATE_BULK_STATEMENT

        protected String UPDATE_BULK_STATEMENT
        Updates all given nodes
      • FIND_BY_ID_STATEMENT

        protected String FIND_BY_ID_STATEMENT
        Searches object by id
      • FIND_BY_WHERE_STATEMENT

        protected String FIND_BY_WHERE_STATEMENT
        Searches for a given _WHERE_. The _WHERE_ should be replaced with the specific statement, such as "WHERE n.fieldThatIsString CONTAINS $substring
      • FIND_ALL_STATEMENT

        protected String FIND_ALL_STATEMENT
        Searches for all objects of this class
      • DELETE_ALL_STATEMENT

        protected String DELETE_ALL_STATEMENT
        deletes all objects of this class
      • RELATIONSHIP_STATEMENT

        protected String RELATIONSHIP_STATEMENT
        Finds or creates the relationship between two existing nodes
      • RELATIONSHIP_BULK_STATEMENT

        protected String RELATIONSHIP_BULK_STATEMENT
        Finds or creates relationships from one node to 1..* nodes
      • BULK_RELATIONSHIP_STATEMENT

        protected String BULK_RELATIONSHIP_STATEMENT
        Handles relationships in save all that are between two nodes
      • BULK_RELATIONSHIP_BULK_STATEMENT

        protected String BULK_RELATIONSHIP_BULK_STATEMENT
        Handles relationships in save all that are 1..*
      • FIND_SUBTREE_STATEMENT

        protected String FIND_SUBTREE_STATEMENT
        Loads a node and all outgoing relationships from that node and child nodes up to DEPTH Note: RELATIONSHIPS and DEPTH must be replaced on the fly, as neo4j does not support either search
      • logger

        protected static org.slf4j.Logger logger
    • Constructor Detail

      • AbstractNeo4JNodeRepositoyImpl

        protected AbstractNeo4JNodeRepositoyImpl​(TransactionManager manager)
        empty constructor for child-implementations. If you use this one, you MUST call the init function
        Parameters:
        manager - used to handle transactions
      • AbstractNeo4JNodeRepositoyImpl

        protected AbstractNeo4JNodeRepositoyImpl​(TransactionManager manager,
                                                 String className)
    • Method Detail

      • init

        protected void init​(String className,
                            String labels)
      • query

        public <T> T query​(String query,
                           org.neo4j.driver.Value parameters,
                           Class<T> clazz)
        Description copied from interface: Neo4jQueryRepository
        Method for executing a query with a single result in a own transaction
        Specified by:
        query in interface Neo4jQueryRepository
        Type Parameters:
        T - Result type
        Parameters:
        query - The query text
        parameters - The query parameters
        clazz - The result clazz type
        Returns:
        Single query result
      • queryHelp

        protected <T> T queryHelp​(String query,
                                  org.neo4j.driver.Value parameters,
                                  Class<T> clazz)
      • queryAll

        public <T> Stream<T> queryAll​(String query,
                                      org.neo4j.driver.Value parameters,
                                      Class<T> clazz)
        Description copied from interface: Neo4jQueryRepository
        Method for executing a query with multiple results in a own transaction
        Specified by:
        queryAll in interface Neo4jQueryRepository
        Type Parameters:
        T - Result type
        Parameters:
        query - The query text
        parameters - The query parameters
        clazz - The result clazz type
        Returns:
        Stream of results
      • queryAllHelp

        protected <T> Stream<T> queryAllHelp​(String query,
                                             org.neo4j.driver.Value parameters,
                                             Class<T> clazz)
      • cast

        protected abstract S cast​(org.neo4j.driver.Value value,
                                  org.neo4j.driver.Value relationships,
                                  org.neo4j.driver.Value nodes)
        Casts the result of the queryTyped into a DomainObject
        Parameters:
        value - the value to be casted as root
        relationships - the relationships in the subgraph
        nodes - the relationships are pointing to
        Returns:
        the casted result
      • setId

        protected abstract S setId​(S node,
                                   Long id)
        Helper method for setting the id when a node was stored to the database
        Parameters:
        node - to be assigned an id
        id - id to be assigned
        Returns:
        node with id
      • getId

        protected abstract Long getId​(S node)
        Helper method for finding the id of a node
        Parameters:
        node - to return the id from
        Returns:
        id of node or null
      • handleRelationships

        protected abstract <T extends S> T handleRelationships​(T node)
        Helper method that is responsible for storing all relationships. Use "saveRelationship" and "saveRelationshipBulk" when implementing this method!
        Type Parameters:
        T - extension of S
        Parameters:
        node - to be saved
        Returns:
        node
      • handleRelationships

        protected abstract <T extends SCollection<T> handleRelationships​(Collection<T> nodes)
        Helper method that is responsible for storing all relationships in a multitude of nodes Use "saveBulkRelationship" and "saveBulkRelationshipBulk" when implementing this method
        Type Parameters:
        T - extension of S
        Parameters:
        nodes - the nodes with relations which should be handled
        Returns:
        the stored nodes
      • objectify

        protected abstract Map<String,​Object> objectify​(S node)
        Turns all properties of a Node into something to be used by the queries
        Parameters:
        node - to be transformed
        Returns:
        a map containing the id, and another map that contains all properties
      • execute

        protected S execute​(String query,
                            org.neo4j.driver.Value parameters,
                            org.neo4j.driver.AccessMode accessMode)
        Helper function that simply executes a given queryTyped
        Parameters:
        query - to be run
        parameters - to be added to the queryTyped
        accessMode - mode to execute the query
        Returns:
        result
      • executeAll

        protected Iterable<S> executeAll​(String query,
                                         org.neo4j.driver.Value parameters,
                                         org.neo4j.driver.AccessMode accessMode)
        Helper function that simply executes a given queryTyped
        Parameters:
        query - to be run
        parameters - to be added to the queryTyped
        accessMode - mode to execute the query
        Returns:
        result
      • saveRelationship

        protected void saveRelationship​(String targetClass,
                                        String relationshipName,
                                        Long sourceId,
                                        Long targetId)
        Creates or deletes a 0..1 relationship between two nodes
        Parameters:
        targetClass - Label that the target must have
        relationshipName - Name of the relationship that will be created/deleted
        sourceId - Id of source node (the node THIS repository is responsible for)
        targetId - Id of the target node (the target of the relationship)
      • saveRelationshipBulk

        protected void saveRelationshipBulk​(String targetClass,
                                            String relationshipName,
                                            Long sourceId,
                                            long[] targetIds)
        Creates or deletes a 0..* relationship between two nodes
        Parameters:
        targetClass - Label that the target must have
        relationshipName - Name of the relationship that will be created/deleted
        sourceId - Id of source node (the node THIS repository is responsible for)
        targetIds - Id of all target nodes (the target of the relationship)
      • saveBulkRelationship

        protected void saveBulkRelationship​(String targetClass,
                                            String relationshipName,
                                            org.neo4j.driver.Value tuples)
        Creates or deletes a 1..1 relationship for a multitude of nodes
        Parameters:
        targetClass - Label that the target must have
        relationshipName - Name of the relationship that will be created/deleted
        tuples - MUST be a List of values with a "source" -> id and "target" -> id for EVERY tuple
      • saveBulkRelationshipBulk

        protected void saveBulkRelationshipBulk​(String targetClass,
                                                String relationshipName,
                                                org.neo4j.driver.Value sources)
        Creates or deletes a 1..* relationship for a multitude of nodes
        Parameters:
        targetClass - Label that target must have
        relationshipName - Name of the relationship that will be created/deleted
        sources - MUST be a List ov values with a "source" -> id and "targets" -> list[id] for EVERY node
      • save

        public <T extends S> T save​(T node)
        Description copied from interface: Neo4jRepository
        Stores the node in the database. If the node has an ID a merge is conducted, if the ID is empty a new node is always created
        Specified by:
        save in interface Neo4jRepository<S,​Long>
        Type Parameters:
        T - subtype of S
        Parameters:
        node - node to be stored
        Returns:
        node with ID
      • saveAll

        public <T extends SIterable<T> saveAll​(Iterable<T> nodes)
        Description copied from interface: Neo4jRepository
        Saves all given science.neo4j.nodes (create or update)
        Specified by:
        saveAll in interface Neo4jRepository<S,​Long>
        Type Parameters:
        T - subtype of S
        Parameters:
        nodes - to be saved
        Returns:
        science.neo4j.nodes with id
      • findById

        public S findById​(Long id)
        Description copied from interface: Neo4jRepository
        Returns the node with the given id, or null if no such node exists
        Specified by:
        findById in interface Neo4jRepository<S,​Long>
        Parameters:
        id - id of node
        Returns:
        node or null
      • findSubtree

        public S findSubtree​(Long id)
        Description copied from interface: Neo4jRepository
        Returns the entire subtree of the node with the given id, or null if no such node exists
        Specified by:
        findSubtree in interface Neo4jRepository<S,​Long>
        Parameters:
        id - id of node
        Returns:
        node with children (infinite depth!) or null
      • findSubtree

        public S findSubtree​(Long id,
                             int depth)
        Description copied from interface: Neo4jRepository
        Returns the entire subtree of the node with the given id, or null if no such node exists
        Specified by:
        findSubtree in interface Neo4jRepository<S,​Long>
        Parameters:
        id - id of node
        depth - depth until the subtree will be loaded (-1 is infinite, 0 is node only)
        Returns:
        node with children up to depth or null
      • findSubtree

        public S findSubtree​(Long id,
                             int depth,
                             List<String> relationships)
        Description copied from interface: Neo4jRepository
        Returns the entire subtree of the node with the given id, or null if no such node exists
        Specified by:
        findSubtree in interface Neo4jRepository<S,​Long>
        Parameters:
        id - id of node
        depth - depth until the subtree will be loaded (-1 is infinite, 0 is node only)
        relationships - that will be loaded (all non mentioned will be omitted)
        Returns:
        node with children up to depth or null
      • findBy

        public S findBy​(String condition,
                        org.neo4j.driver.Value parameters)
        Description copied from class: AbstractNeo4JRepository
        Returns the first node that satisfies the given condition. The condition may access the node with "n" Example: findByCondition("n.name = $name", Values.parameters("name", "Dijkstra"))
        Specified by:
        findBy in class AbstractNeo4JRepository<S,​Long>
        Parameters:
        condition - any condition that would be after the "WHERE" in a cypher queryTyped
        parameters - all parameters defined in the condition (parameters optional)
        Returns:
        first node that satisifes condition with parameters
      • findAllBy

        public Iterable<S> findAllBy​(String condition,
                                     org.neo4j.driver.Value parameters)
        Description copied from class: AbstractNeo4JRepository
        Returns ALL science.neo4j.nodes that satisfies the given condition. The condition may access the node with "n" Example: findByCondition("n.name in $names", Values.parameters("names", Values.parameters("Dijkstra", "Knuth", "Turing")))
        Specified by:
        findAllBy in class AbstractNeo4JRepository<S,​Long>
        Parameters:
        condition - any condition that would be after the "WHERE" in a cypher queryTyped
        parameters - all parameters defined in the condition (parameters optional)
        Returns:
        first node that satisifes condition with parameters
      • queryTyped

        public S queryTyped​(String query,
                            org.neo4j.driver.Value parameters)
        Convenience function that allows calling any user defined queryTyped. Parameters should be defined as "$parametername" The value-list should be defined as Map of parametername (without the $) to value
        Parameters:
        query - to be run
        parameters - to be set
        Returns:
        node that matches the query
      • queryAllTyped

        public Iterable<S> queryAllTyped​(String query,
                                         org.neo4j.driver.Value parameters)
        Convenience function that allows calling any user defined queryTyped. Parameters should be defined as "$parametername" The value-list should be defined as Map of parametername (without the $) to value
        Parameters:
        query - to be run
        parameters - to be set
        Returns:
        node that matches the query