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.

public abstract class AbstractNestedSetService<T extends INestedSetNode<ID>,ID> extends Object
Abstract service class for managing nested set trees.
  • Field Details

  • Constructor Details

    • AbstractNestedSetService

      protected AbstractNestedSetService(JpaNestedSetRepository<T,ID> repository)
      Constructor for AbstractNestedSetService.
      Parameters:
      repository - The repository to be used for database operations.
  • Method Details

    • getAncestors

      public List<T> getAncestors(T entity)
      Get ancestors of a node.
      Parameters:
      entity - The node whose ancestors are to be found.
      Returns:
      A list of ancestor nodes.
    • getDescendants

      public List<T> getDescendants(T entity)
      Get descendants of a node.
      Parameters:
      entity - The node whose descendants are to be found.
      Returns:
      A list of descendant nodes.
    • moveUp

      public T moveUp(T node)
      Move a node up in the tree.
      Parameters:
      node - The node to be moved up.
      Returns:
      The updated node.
    • moveDown

      public T moveDown(T node)
      Move a node down in the tree.
      Parameters:
      node - The node to be moved down.
      Returns:
      The updated node.
    • createNode

      public org.apache.commons.lang3.tuple.Pair<Integer,Integer> createNode(List<T> allNodes, T parent)
      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

      protected void closeGapInTree(T entity, int width, List<T> allNodes)
      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

      protected T moveNode(T node, MoveNodeDirection direction)
      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

      protected boolean isDescendant(T ancestor, T descendant)
      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

      protected int rebuildTree(T parent, List<T> allNodes, int currentLeft)
      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

      protected int rebuildTree(T parent, List<T> allNodes)
      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

      protected List<T> saveAllNodes(List<T> nodes)
      Save all nodes in the tree.
      Parameters:
      nodes - The list of nodes to be saved.
      Returns:
      The list of saved nodes.