Class AbstractNestedSetService<T extends INestedSetNode<ID,T>,ID>
java.lang.Object
com.mewebstudio.springboot.jpa.nestedset.AbstractNestedSetService<T,ID>
- Type Parameters:
T- The type of the nested set node.ID- The type of the identifier for the nested set node.
Abstract service class for managing nested set trees.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final JpaNestedSetRepository<T,ID> The repository to be used for database operations. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractNestedSetService(JpaNestedSetRepository<T, ID> repository) Constructor for AbstractNestedSetService. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcloseGapInTree(T entity, int width, List<T> allNodes) Closes the gap in the tree after a node is deleted.protected TcreateNode(List<T> allNodes, T node) Creates a new node in the nested set tree.protected TcreateNode(T node) Creates a new node in the nested set tree.protected voiddeleteNode(T node) Deletes a node from the nested set tree.getAncestors(T entity) Get ancestors of a node.getDescendants(T entity) Get descendants of a node.getNodeGap(List<T> allNodes, INestedSetNode<ID, T> parent) Get the gap for inserting a new node in the nested set tree.protected booleanisDescendant(T ancestor, T descendant) Check if a node is a descendant of another node.Move a node down in the tree.protected TmoveNode(T node, MoveNodeDirection direction) Move a node in the tree.Move a node up in the tree.protected intrebuildTree(T parent, List<T> allNodes) Rebuild the tree structure starting from the root node.protected intrebuildTree(T parent, List<T> allNodes, int currentLeft) Rebuild the tree structure.saveAllNodes(List<T> nodes) Save all nodes in the tree.protected TupdateNode(T node, T newParent) Update a node in the nested set tree.
-
Field Details
-
repository
The repository to be used for database operations.
-
-
Constructor Details
-
AbstractNestedSetService
Constructor for AbstractNestedSetService.- Parameters:
repository- The repository to be used for database operations.
-
-
Method Details
-
getAncestors
Get ancestors of a node.- Parameters:
entity- The node whose ancestors are to be found.- Returns:
- A list of ancestor nodes.
-
getDescendants
Get descendants of a node.- Parameters:
entity- The node whose descendants are to be found.- Returns:
- A list of descendant nodes.
-
moveUp
Move a node up in the tree.- Parameters:
node- The node to be moved up.- Returns:
- The updated node.
-
moveDown
Move a node down in the tree.- Parameters:
node- The node to be moved down.- Returns:
- The updated node.
-
createNode
Creates a new node in the nested set tree.- Parameters:
allNodes- The list of all nodes in the tree.node- T The new node to be created.- Returns:
- T The created node.
-
createNode
Creates a new node in the nested set tree.- Parameters:
node- T The new node to be created.- Returns:
- T The created node.
-
getNodeGap
Get the gap for inserting a new node in the nested set tree.- Parameters:
allNodes- The list of all nodes in the tree.parent- T The parent node under which the new node will be created.- Returns:
- A pair of integers representing the left and right values for the new node.
-
updateNode
Update a node in the nested set tree.- Parameters:
node- T The node to be updated.newParent- T The new parent node under which the node will be moved.- Returns:
- T The updated node.
-
deleteNode
Deletes a node from the nested set tree.- Parameters:
node- T The node to be deleted.
-
closeGapInTree
Closes the gap in the tree after a node is deleted.- Parameters:
entity- T The node that was deleted.width- int The width of the gap to be closed.allNodes- List The list of all nodes in the tree.
-
moveNode
Move a node in the tree.- Parameters:
node- T The node to be moved.direction- MoveNodeDirection The direction in which the node will be moved (up or down).- Returns:
- T The updated node.
-
isDescendant
Check if a node is a descendant of another node.- Parameters:
ancestor- T The potential ancestor node.descendant- T The potential descendant node.- Returns:
- True if the descendant is a child of the ancestor, false otherwise.
-
rebuildTree
Rebuild the tree structure.- Parameters:
parent- T The parent node of the current node being processed.allNodes- List The list of all nodes in the tree.currentLeft- Int The current left value of the node being processed.- Returns:
- Int The right value of the node being processed.
-
rebuildTree
Rebuild the tree structure starting from the root node.- Parameters:
parent- T The root node of the tree.allNodes- List The list of all nodes in the tree.- Returns:
- int The right value of the root node.
-
saveAllNodes
Save all nodes in the tree.- Parameters:
nodes- List The list of nodes to be saved.- Returns:
- List The list of saved nodes.
-