Class HierarchyAccessor<T extends Hierarchy>

  • All Implemented Interfaces:
    java.io.Serializable

    public class HierarchyAccessor<T extends Hierarchy>
    extends java.lang.Object
    implements java.io.Serializable
    HierarchyAccessor for accessing hierarchy entities.
    Since:
    5.17
    See Also:
    Serialized Form
    • Method Detail

      • getInstance

        public static <T extends HierarchyHierarchyAccessor getInstance​(java.lang.Class<T> hierarchyClass,
                                                                          java.lang.String rootHierarchyId,
                                                                          DataAccessUnit dauAccessFrom,
                                                                          boolean createIfNotFound)
                                                                   throws SystemException
        Get a HierarchyAccessor instance.
        Parameters:
        hierarchyClass - hierarchy entity class
        rootHierarchyId - root hierarchy id for this accessor. null for accessing whole hierarchy.
        dauAccessFrom - the DataAccessUnit from which to access the hierarchy entities
        createIfNotFound - whether to create one if not found
        Throws:
        SystemException - throw if an error occurs
      • getInstance

        public static <T extends HierarchyHierarchyAccessor getInstance​(java.lang.Class<T> hierarchyClass,
                                                                          java.lang.String rootHierarchyId,
                                                                          DataAccessUnit dauAccessFrom)
                                                                   throws SystemException
        Get a HierarchyAccessor instance. Create one if not found.
        Parameters:
        hierarchyClass - hierarchy entity class
        rootHierarchyId - root hierarchy id for this accessor. null for accessing whole hierarchy.
        dauAccessFrom - the DataAccessUnit from which to access the hierarchy entities
        Throws:
        SystemException - throw if an error occurs
      • getCachedInstances

        public static java.util.List<HierarchyAccessor> getCachedInstances​(java.lang.Class hierarchyClass,
                                                                           DataAccessUnit dauAccessFrom)
        Get all the cached HierarchyAccessor(s) for the specified hierarchy type and the owner dataAccessUnit
        Parameters:
        hierarchyClass - hierarchy class
        dauAccessFrom - the dataAccessUnit to get the hierarchy type's ownerDataAccessUnit
        Returns:
        a list of HierarchyAccessor(s)
      • clearCache

        public static void clearCache​(java.lang.Class hierarchyClass,
                                      java.lang.String rootHierarchyId,
                                      DataAccessUnit dauAccessFrom)
                               throws SystemException
        Clear the cache of a HierarchyAccessor instance.
        Parameters:
        hierarchyClass - hierarchy entity class
        rootHierarchyId - root hierarchy id for this accessor. null for accessing whole hierarchy.
        dauAccessFrom - the DataAccessUnit from which to access the hierarchy entities
        Throws:
        SystemException
      • clearCache

        public static void clearCache​(DataAccessUnit ownerDataAccessUnit)
        Clear caches of all HierarchyAccessor instances whose hierarchy entities are owned by the ownerDataAccessUnit.
        Parameters:
        ownerDataAccessUnit - owner DataAccessUnit
      • convertHierarchyIdRule

        public static int[] convertHierarchyIdRule​(java.lang.String rootHierarchyId,
                                                   int[] idRule,
                                                   java.lang.String newRootHierarchyId)
                                            throws SystemException
        Convert hierarchy id rule for the descendants of the specified newRootHierarchyId.
        Parameters:
        rootHierarchyId - root hierarchy id (null for empty)
        idRule - id rule for descendants of the rootHierarchyId
        newRootHierarchyId - new root hierarchy id
        Returns:
        new id rule for newRootHierarchyId
        Throws:
        SystemException - throw if newRootHierarchyId does not comply with the IdRule
      • getIdRule

        public int[] getIdRule()
        Get the hierarchy id rule.
        • If rootHierarchyId is not null, root is the entity with the rootHierarchyId.
        • If rootHierarchyId is null, root is a virtual entity (null).
        idRule[0] is the length of top-level hierarchyId under root.
        Returns:
        id rule.
        See Also:
        IdRule
      • matchIdRule

        public boolean matchIdRule​(java.lang.String idRule)
        Does the IdRule of this HierarchyAccessor match the specified idRule?
        Parameters:
        idRule - id rule as string
        Returns:
        true if they match or this idRule is not initialized, false otherwise
      • getChildHierarchySelectItems

        public java.util.List<SelectItem> getChildHierarchySelectItems​(HierarchyAccessor.HierarchyNode hierarchyNode,
                                                                       boolean localIdAsValue,
                                                                       boolean addEmptySelection)
        Get child hierarchy select items. If localIdAsValue is true, only partial hierarchy id is used. For example, parent hierarchyId is US, and a child hierarchyId is USNY, then NY will be the select item value for the child hierarchy.
        Parameters:
        hierarchyNode - a hierarchy node
        localIdAsValue - whether to use local id as select item values.
        addEmptySelection - whether to add empty selection (value is the hierarchyId)
      • getChildHierarchySelectItems

        public java.util.List<SelectItem> getChildHierarchySelectItems​(java.lang.String hierarchyId,
                                                                       boolean localIdAsValue,
                                                                       boolean addEmptySelection)
        Get child hierarchy select items. If localIdAsValue is true, only partial hierarchy id is used. For example, parent hierarchyId is US, and a child hierarchyId is USNY, then NY will be the select item value for the child hierarchy.
        Parameters:
        hierarchyId - parent hierarchy node
        localIdAsValue - whether to use local id as select item values.
        addEmptySelection - whether to add empty selection (value is the hierarchyId)
      • clearCache

        public void clearCache()
        Clear cache including hierarchy node tree and id rule. They will be rebuilt on demand.
      • getHierarchyNode

        public HierarchyAccessor.HierarchyNode getHierarchyNode​(T hierarchy)
        Get the HierarchyNode in the hierarchy tree for the hierarchy entity.
        Parameters:
        hierarchy - hierarchy entity
        Returns:
        HierarchyNode object
      • getHierarchyNode

        public HierarchyAccessor.HierarchyNode getHierarchyNode​(java.lang.String hierarchyId)
        Get the HierarchyNode in the hierarchy tree for the hierarchy with the id.
        Parameters:
        hierarchyId - hierarchy id
        Returns:
        HierarchyNode object
      • getRootHierarchyId

        public java.lang.String getRootHierarchyId()
        Get root hierarchy id. null for the root of whole hierarchy tree.
        Returns:
        root hierarchy id.
      • getRootHierarchyIdLen

        public int getRootHierarchyIdLen()
        Get root hierarchy id length.
        Returns:
        id length
      • getDepth

        public static int getDepth​(java.lang.String hierarchyId,
                                   int rootHierarchyIdLen,
                                   int[] idRule)
                            throws BackingBeanException
        Get hierarchy depth according to the id rule. The root depth is 1.
        Parameters:
        hierarchyId - hierarchy id
        rootHierarchyIdLen - the length of root hierarchy id
        idRule - id rule for descendants of the rootHierarchyIdLen
        Returns:
        the depth of the hierarchy id
        Throws:
        BackingBeanException - throw if the hierarchyId does not comply with the Id Rule.
      • getDepth

        public int getDepth​(Hierarchy hierarchy)
                     throws BackingBeanException
        Get the depth of the hierarchy. The depth of root is 1.
        Parameters:
        hierarchy - a hierarchy entity
        Returns:
        the depth of the hierarchy
        Throws:
        BackingBeanException
      • getDepth

        public int getDepth​(java.lang.String hierarchyId)
                     throws BackingBeanException
        Get the depth of the hierarchy. The depth of root is 1.
        Parameters:
        hierarchyId - hierarchy id
        Returns:
        depth
        Throws:
        BackingBeanException - throw if the hierarchyId does not comply with the Id Rule.
      • getIdLengthAt

        public static int getIdLengthAt​(int[] idRule,
                                        int index)
        Get id length at the specified index. The last digit of id rule will repeat as many times as needed.
        Parameters:
        idRule - Id Rule
        index - zero-based index
        Returns:
        id length at the index
      • getIdLengthAt

        public int getIdLengthAt​(int index)
        Get id length at the specified index. The last digit of id rule will repeat as many times as needed.
        Parameters:
        index - zero-based index
        Returns:
        id length at the index
      • getHierarchyIdLength

        public static int getHierarchyIdLength​(int depth,
                                               int[] idRule)
        Get hierarchy id length of the specified depth according to the id rule. The root depth is 1.
        Parameters:
        depth - hierarchy depth
        idRule - id rule
        Returns:
        hierarchy id length
      • getHierarchyIdLength

        public int getHierarchyIdLength​(int depth)
        Get hierarchy id length at the specific depth according to the id rule. The root depth is 1.
        Parameters:
        depth - hierarchy depth
        Returns:
        hierarchy id length
      • getParentHierarchyId

        public java.lang.String getParentHierarchyId​(java.lang.String hierarchyId)
                                              throws SystemException
        Get parent hierarchy id.
        Parameters:
        hierarchyId - a hierarchy id
        Returns:
        parent hierarchy id. return null for the virtual root.
        Throws:
        SystemException
      • getParentHierarchyId

        public static java.lang.String getParentHierarchyId​(java.lang.String hierarchyId,
                                                            int[] idRule)
                                                     throws SystemException
        Get parent hierarchy id based on the id rule.
        Parameters:
        hierarchyId - hierarchy id
        idRule - hierarchy id rule
        Returns:
        parent hierarchy id. return null if the hierarchy does not have a parent.
        Throws:
        BackingBeanException - throw if the hierarchyId does not comply with the Id rule
        SystemException
      • getChildHierarchyNodes

        public java.util.List<HierarchyAccessor.HierarchyNode> getChildHierarchyNodes​(T hierarchy)
        Get child hierarchy nodes of the hierarchy
        Parameters:
        hierarchy - a hierarchy entity, null for root
        Returns:
        a list of child HierarchyNodes. return null if there is no children.
      • getChildHierarchyCount

        public int getChildHierarchyCount​(T hierarchy)
        Get child hierarchy count of the hierarchy
        Parameters:
        hierarchy - a hierarchy entity, null for root
        Returns:
        a list of child HierarchyNodes. return null if there is no children.
      • getNextAvailableChildHierarchyId

        public java.lang.String getNextAvailableChildHierarchyId​(java.lang.String hierarchyId)
        Get the next available child hierarchy id.
        Parameters:
        hierarchyId - a parent hierarchy id in hierarchy tree
        Returns:
        available child hierarchy id
      • getNextAvailableSiblingHierarchyId

        public java.lang.String getNextAvailableSiblingHierarchyId​(java.lang.String hierarchyId)
        Get the next available sibling hierarchy id.
        Parameters:
        hierarchyId - an existing hierarchy id in hierarchy tree, which can not be null.
        Returns:
        hierarchy id
      • getChildHierarchyCount

        public int getChildHierarchyCount​(java.lang.String hierarchyId)
        Get child hierarchy count of the hierarchy
        Parameters:
        hierarchyId - hierarchy id, null for root
        Returns:
        a list of child HierarchyNodes. return null if there is no children.
      • getPath

        public java.util.List<HierarchyAccessor.HierarchyNode> getPath​(T hierarchy)
        Get the hierarchy path from the root to the hierarchy.
        Parameters:
        hierarchy - a hierarchy entity
        Returns:
        a list of HierarchyNodes from root to the hierarchy
        Throws:
        java.lang.IllegalArgumentException - throw if the hierarchy not found
      • getPath

        public java.util.List<HierarchyAccessor.HierarchyNode> getPath​(java.lang.String hierarchyId)
        Get the hierarchy path from the root to the hierarchy.
        Parameters:
        hierarchyId - a hierarchy id, null for the virtual root
        Returns:
        a list of HierarchyNodes from root to the hierarchy
        Throws:
        java.lang.IllegalArgumentException - throw if the hierarchy not found