Package science.aist.neo4j.repository
Class AbstractNeo4JNodeRepositoyImpl<S>
- java.lang.Object
-
- science.aist.neo4j.repository.AbstractNeo4JRepository<S,Long>
-
- science.aist.neo4j.repository.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 Summary
Fields Modifier and Type Field Description protected String
BULK_RELATIONSHIP_BULK_STATEMENT
Handles relationships in save all that are 1..*protected String
BULK_RELATIONSHIP_STATEMENT
Handles relationships in save all that are between two nodesprotected static String
CLASSNAME
protected String
CREATE_BULK_STATEMENT
Create a list of objects with all properties setprotected String
CREATE_STATEMENT
Create an object with all properties setprotected String
DELETE_ALL_STATEMENT
deletes all objects of this classprotected String
FIND_ALL_STATEMENT
Searches for all objects of this classprotected String
FIND_BY_ID_STATEMENT
Searches object by idprotected String
FIND_BY_WHERE_STATEMENT
Searches for a given _WHERE_.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 searchprotected static org.slf4j.Logger
logger
protected String
RELATIONSHIP_BULK_STATEMENT
Finds or creates relationships from one node to 1..* nodesprotected String
RELATIONSHIP_STATEMENT
Finds or creates the relationship between two existing nodesprotected String
UPDATE_BULK_STATEMENT
Updates all given nodesprotected String
UPDATE_STATEMENT
Update an existing object
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractNeo4JNodeRepositoyImpl(TransactionManager manager)
empty constructor for child-implementations.protected
AbstractNeo4JNodeRepositoyImpl(TransactionManager manager, String className)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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 DomainObjectvoid
deleteAll()
Deletes all science.neo4j.nodes in neo4j that are of type Sprotected S
execute(String query, org.neo4j.driver.Value parameters, org.neo4j.driver.AccessMode accessMode)
Helper function that simply executes a given queryTypedprotected Iterable<S>
executeAll(String query, org.neo4j.driver.Value parameters, org.neo4j.driver.AccessMode accessMode)
Helper function that simply executes a given queryTypedIterable<S>
findAll()
Returns all node in neo4j that are of type SIterable<S>
findAllBy(String condition, org.neo4j.driver.Value parameters)
Returns ALL science.neo4j.nodes that satisfies the given condition.S
findBy(String condition, org.neo4j.driver.Value parameters)
Returns the first node that satisfies the given condition.S
findById(Long id)
Returns the node with the given id, or null if no such node existsS
findSubtree(Long id)
Returns the entire subtree of the node with the given id, or null if no such node existsS
findSubtree(Long id, int depth)
Returns the entire subtree of the node with the given id, or null if no such node existsS
findSubtree(Long id, int depth, List<String> relationships)
Returns the entire subtree of the node with the given id, or null if no such node existsprotected abstract Long
getId(S node)
Helper method for finding the id of a nodeprotected abstract <T extends S>
Collection<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 methodprotected abstract <T extends S>
ThandleRelationships(T node)
Helper method that is responsible for storing all relationships.protected void
init(String className, String labels)
protected abstract Map<String,Object>
objectify(S node)
Turns all properties of a Node into something to be used by the queries<T> T
query(String query, org.neo4j.driver.Value parameters, Class<T> clazz)
Method for executing a query with a single result in a own transaction<T> Stream<T>
queryAll(String query, org.neo4j.driver.Value parameters, Class<T> clazz)
Method for executing a query with multiple results in a own transactionprotected <T> Stream<T>
queryAllHelp(String query, org.neo4j.driver.Value parameters, Class<T> clazz)
protected <X> Stream<X>
queryAllNormalOrTyped(String query, org.neo4j.driver.Value parameters, Class<X> clazz, Map<Class,AbstractNeo4JRepository> repositories)
Iterable<S>
queryAllTyped(String query, org.neo4j.driver.Value parameters)
Convenience function that allows calling any user defined queryTyped.protected <T> T
queryHelp(String query, org.neo4j.driver.Value parameters, Class<T> clazz)
protected <X> X
queryNormalOrTyped(String query, org.neo4j.driver.Value parameters, Class<X> clazz, Map<Class,AbstractNeo4JRepository> repositories)
S
queryTyped(String query, org.neo4j.driver.Value parameters)
Convenience function that allows calling any user defined queryTyped.<T extends S>
Tsave(T node)
Stores the node in the database.<T extends S>
Iterable<T>saveAll(Iterable<T> nodes)
Saves all given science.neo4j.nodes (create or update)protected void
saveBulkRelationship(String targetClass, String relationshipName, org.neo4j.driver.Value tuples)
Creates or deletes a 1..1 relationship for a multitude of nodesprotected void
saveBulkRelationshipBulk(String targetClass, String relationshipName, org.neo4j.driver.Value sources)
Creates or deletes a 1..* relationship for a multitude of nodesprotected void
saveRelationship(String targetClass, String relationshipName, Long sourceId, Long targetId)
Creates or deletes a 0..1 relationship between two nodesprotected void
saveRelationshipBulk(String targetClass, String relationshipName, Long sourceId, long[] targetIds)
Creates or deletes a 0..* relationship between two nodesprotected abstract S
setId(S node, Long id)
Helper method for setting the id when a node was stored to the database-
Methods inherited from class science.aist.neo4j.repository.AbstractNeo4JRepository
buildParameters, buildWhere, findAllBy, findAllBy, findBy, findBy, getProvidedRepository, getTransactionManager, provideRepository
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface science.aist.neo4j.Neo4jRepository
findAllAsStream
-
-
-
-
Field Detail
-
CLASSNAME
protected static final String CLASSNAME
- See Also:
- Constant Field Values
-
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
-
queryNormalOrTyped
protected <X> X queryNormalOrTyped(String query, org.neo4j.driver.Value parameters, Class<X> clazz, Map<Class,AbstractNeo4JRepository> repositories)
-
queryAllNormalOrTyped
protected <X> Stream<X> queryAllNormalOrTyped(String query, org.neo4j.driver.Value parameters, Class<X> clazz, Map<Class,AbstractNeo4JRepository> repositories)
-
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 interfaceNeo4jQueryRepository
- Type Parameters:
T
- Result type- Parameters:
query
- The query textparameters
- The query parametersclazz
- 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 interfaceNeo4jQueryRepository
- Type Parameters:
T
- Result type- Parameters:
query
- The query textparameters
- The query parametersclazz
- 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 rootrelationships
- the relationships in the subgraphnodes
- 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 idid
- 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 S> Collection<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 runparameters
- to be added to the queryTypedaccessMode
- 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 runparameters
- to be added to the queryTypedaccessMode
- 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 haverelationshipName
- Name of the relationship that will be created/deletedsourceId
- 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 haverelationshipName
- Name of the relationship that will be created/deletedsourceId
- 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 haverelationshipName
- Name of the relationship that will be created/deletedtuples
- 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 haverelationshipName
- Name of the relationship that will be created/deletedsources
- 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 interfaceNeo4jRepository<S,Long>
- Type Parameters:
T
- subtype of S- Parameters:
node
- node to be stored- Returns:
- node with ID
-
saveAll
public <T extends S> Iterable<T> saveAll(Iterable<T> nodes)
Description copied from interface:Neo4jRepository
Saves all given science.neo4j.nodes (create or update)- Specified by:
saveAll
in interfaceNeo4jRepository<S,Long>
- Type Parameters:
T
- subtype of S- Parameters:
nodes
- to be saved- Returns:
- science.neo4j.nodes with id
-
deleteAll
public void deleteAll()
Description copied from interface:Neo4jRepository
Deletes all science.neo4j.nodes in neo4j that are of type S- Specified by:
deleteAll
in interfaceNeo4jRepository<S,Long>
-
findAll
public Iterable<S> findAll()
Description copied from interface:Neo4jRepository
Returns all node in neo4j that are of type S- Specified by:
findAll
in interfaceNeo4jRepository<S,Long>
- Returns:
- iterator over all S
-
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 interfaceNeo4jRepository<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 interfaceNeo4jRepository<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 interfaceNeo4jRepository<S,Long>
- Parameters:
id
- id of nodedepth
- 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 interfaceNeo4jRepository<S,Long>
- Parameters:
id
- id of nodedepth
- 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 classAbstractNeo4JRepository<S,Long>
- Parameters:
condition
- any condition that would be after the "WHERE" in a cypher queryTypedparameters
- 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 classAbstractNeo4JRepository<S,Long>
- Parameters:
condition
- any condition that would be after the "WHERE" in a cypher queryTypedparameters
- 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 runparameters
- 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 runparameters
- to be set- Returns:
- node that matches the query
-
-