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.

public abstract class AbstractNestedSetService<T extends INestedSetNode<ID,T>,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

      protected T createNode(List<T> allNodes, T node)
      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

      protected T createNode(T node)
      Creates a new node in the nested set tree.
      Parameters:
      node - T The new node to be created.
      Returns:
      T The created node.
    • getNodeGap

      protected Pair<Integer,Integer> getNodeGap(List<T> allNodes, INestedSetNode<ID,T> parent)
      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

      protected T updateNode(T node, T newParent)
      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

      protected void deleteNode(T node)
      Deletes a node from the nested set tree.
      Parameters:
      node - T The node to be deleted.
    • closeGapInTree

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

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

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

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

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

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