Class AbstractNestedSetService<T extends INestedSetNode<ID>,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.createNode(List<T> allNodes, T parent) Creates a new node in the nested set tree.getAncestors(T entity) Get ancestors of a node.getDescendants(T entity) Get descendants of a node.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.
-
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.parent- The parent node under which the new node will be created.- Returns:
- A pair of integers representing the left and right values of the new node.
-
closeGapInTree
Closes the gap in the tree after a node is deleted.- Parameters:
entity- The node that was deleted.width- The width of the gap to be closed.allNodes- The list of all nodes in the tree.
-
moveNode
Move a node in the tree.- Parameters:
node- The node to be moved.direction- 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- The potential ancestor node.descendant- The potential descendant node.- Returns:
- True if the descendant is a child of the ancestor, false otherwise.
-
rebuildTree
Rebuild the tree structure.- Parameters:
parent- The parent node of the current node being processed.allNodes- The list of all nodes in the tree.currentLeft- The current left value of the node being processed.- Returns:
- The right value of the node being processed.
-
rebuildTree
Rebuild the tree structure starting from the root node.- Parameters:
parent- The root node of the tree.allNodes- The list of all nodes in the tree.- Returns:
- The right value of the root node.
-
saveAllNodes
Save all nodes in the tree.- Parameters:
nodes- The list of nodes to be saved.- Returns:
- The list of saved nodes.
-