com.vaadin.data.provider.

Class HierarchyMapper<T,​F>

  • Type Parameters:

    T - the data type

    F - the filter type

    All Implemented Interfaces:

    DataGenerator<T>, Serializable

    public class HierarchyMapper<T,​F>
    extends Object
    implements DataGenerator<T>

    Mapper for hierarchical data.

    Keeps track of the expanded nodes, and size of of the subtrees for each expanded node.

    This class is framework internal implementation details, and can be changed / moved at any point. This means that you should not directly use this for anything.

    Since:

    8.1

    Author:

    Vaadin Ltd

    See Also:

    Serialized Form

    • Constructor Detail

      • HierarchyMapper

        public HierarchyMapper​(HierarchicalDataProvider<T,​F> provider)

        Constructs a new HierarchyMapper.

        Parameters:

        provider - the hierarchical data provider for this mapper

    • Method Detail

      • getTreeSize

        public int getTreeSize()

        Returns the size of the currently expanded hierarchy.

        Returns:

        the amount of available data

      • getParentIndex

        public Integer getParentIndex​(T item)

        Finds the index of the parent of the item in given target index.

        Parameters:

        item - the item to get the parent of

        Returns:

        the parent index or a negative value if the parent is not found

      • isExpanded

        public boolean isExpanded​(T item)

        Returns whether the given item is expanded.

        Parameters:

        item - the item to test

        Returns:

        true if item is expanded; false if not

      • expand

        public Range expand​(T item,
                            Integer position)

        Expands the given item. Always returns an empty range if this HierarchyMapper hasn't been initialized e.g. by attaching the TreeGrid that uses it (no need to add rows if not attached). The range can also be empty if the item doesn't exist in this hierarchy, it has already been expanded, it doesn't have any children, all its children have been filtered out, or expanding it cannot affect the indexing of any active items (items within the client-side cache). For the sake of simplicity, any items that share a root node with any active item are considered to fall within the last category, even if they are positioned after the last active item.

        Parameters:

        item - the item to expand

        position - the index of the item

        Returns:

        range of rows added by expanding the item, can be empty

      • doExpand

        @Deprecated
        public Range doExpand​(T item,
                              Optional<Integer> position)
        Deprecated.

        Expands the given item.

        Parameters:

        item - the item to expand

        position - the index of item

        Returns:

        range of rows added by expanding the item

      • collapse

        public Range collapse​(T item,
                              Integer position)

        Collapses the given item.

        If this HierarchyMapper uses active data optimization, this method always returns an empty range if the collapsed item is sufficiently far beyond the active data range (not sharing a root node with any active items). The optimization doesn't affect the data before the active data range, since those changes affect the indexes of the active range.

        Parameters:

        item - the item to collapse (cannot be null)

        position - the index of the item

        Returns:

        range of rows removed by collapsing the item, can be empty

        See Also:

        useActiveDataOptimization(boolean)

      • doCollapse

        @Deprecated
        public Range doCollapse​(T item,
                                Optional<Integer> position)
        Deprecated.

        Collapses the given item.

        Parameters:

        item - the item to collapse

        position - the index of item

        Returns:

        range of rows removed by collapsing the item

      • generateData

        public void generateData​(T item,
                                 elemental.json.JsonObject jsonObject)

        Description copied from interface: DataGenerator

        Adds custom data for the given item to its serialized JsonObject representation. This JSON object will be sent to client-side DataProvider.

        Specified by:

        generateData in interface DataGenerator<T>

        Parameters:

        item - the data item being serialized

        jsonObject - the JSON object being sent to the client

      • getItemCollapseAllowedProvider

        public ItemCollapseAllowedProvider<T> getItemCollapseAllowedProvider()

        Gets the current item collapse allowed provider.

        Returns:

        the item collapse allowed provider

      • setItemCollapseAllowedProvider

        public void setItemCollapseAllowedProvider​(ItemCollapseAllowedProvider<T> itemCollapseAllowedProvider)

        Sets the current item collapse allowed provider.

        Parameters:

        itemCollapseAllowedProvider - the item collapse allowed provider

      • getInMemorySorting

        public Comparator<T> getInMemorySorting()

        Gets the current in-memory sorting.

        Returns:

        the in-memory sorting

      • setInMemorySorting

        public void setInMemorySorting​(Comparator<T> inMemorySorting)

        Sets the current in-memory sorting. This will cause the hierarchy to be constructed again.

        Parameters:

        inMemorySorting - the in-memory sorting

      • getBackEndSorting

        public List<QuerySortOrder> getBackEndSorting()

        Gets the current back-end sorting.

        Returns:

        the back-end sorting

      • setBackEndSorting

        public void setBackEndSorting​(List<QuerySortOrder> backEndSorting)

        Sets the current back-end sorting. This will cause the hierarchy to be constructed again.

        Parameters:

        backEndSorting - the back-end sorting

      • getFilter

        public F getFilter()

        Gets the current filter.

        Returns:

        the filter

      • setFilter

        public void setFilter​(Object filter)

        Sets the current filter. This will cause the hierarchy to be constructed again.

        Parameters:

        filter - the filter

      • getDataProvider

        public HierarchicalDataProvider<T,​F> getDataProvider()

        Gets the HierarchicalDataProvider for this HierarchyMapper.

        Returns:

        the hierarchical data provider

      • hasChildren

        public boolean hasChildren​(T item)

        Returns whether given item has children.

        Parameters:

        item - the node to test

        Returns:

        true if node has children; false if not

      • fetchItems

        public Stream<T> fetchItems​(Range range)

        Gets a stream of items in the form of a flattened hierarchy from the back-end and filters the wanted results from the list. The default implementation of this method updates the internal parent-children bookkeeping up to the given range (or throughout the entire data set if active data optimization isn't in use), and marks this HierarchyMapper as initialized.

        NOTE: If your data request is likely to involve I/O channels, see DataProvider.fetch(Query) for instructions on how to handle the stream without risking resource leaks.

        NOTE: For performance and data consistency reasons this method should only be called for the data range that is currently getting sent to the client. If you wish to fetch the items for some other purpose, use fetchItems(Object, Range) for a null parent instead. By default this method is called for the first time when the related component is attached.

        Parameters:

        range - the requested item range

        Returns:

        the stream of items

        See Also:

        setInitialized(boolean), useActiveDataOptimization(boolean)

      • fetchItems

        public Stream<T> fetchItems​(T parent,
                                    Range range)

        Gets a stream of children for the given item in the form of a flattened hierarchy from the back-end and filter the wanted results from the list.

        NOTE: If your data request is likely to involve I/O channels, see DataProvider.fetch(Query) for instructions on how to handle the stream without risking resource leaks.

        Parameters:

        parent - the parent item for the fetch

        range - the requested item range

        Returns:

        the stream of items

      • getParentOfItem

        protected T getParentOfItem​(T item)

        Find parent for the given item among open folders.

        Parameters:

        item - the item

        Returns:

        parent item or null for root items or if the parent is closed

      • removeChildrenRecursively

        protected List<Object> removeChildrenRecursively​(Object id)

        Removes recursively from internal parent-children bookkeeping all children of an item identified by a given id. The default implementation of this method doesn't update the expansion bookkeeping of any of the nodes.

        The internal bookkeeping should be used over data provider when handling a collapse, because it matches the latest data that has been sent to the client. If the data provider gets updated within the same server round-trip as the collapse, the client-side update will still be pending, and using fresh data can lead to incorrect row removals from the client.

        Parameters:

        id - id of the item whose children should be removed

        Returns:

        list of the removed children, can be empty

      • removeChildren

        protected void removeChildren​(Object id)

        Removes from internal parent-children bookkeeping all children of an item identified by a given id. The default implementation of this method doesn't update the expansion bookkeeping of any of the nodes, nor look deeper into the hierarchy -- those are handled by the calling method.

        NOTE: Older implementation of this method also updated the expansion bookkeeping and iterated over the entire hierarchy, as well as suggested overriding this method in subclasses for removing obsolete data to avoid memory leaks. That should no longer be necessary with the new logic.

        Parameters:

        id - the item id

        See Also:

        removeChildrenRecursively(Object)

      • getIndexOf

        public Optional<Integer> getIndexOf​(T target)

        Finds the current index of given object. This is based on a search in flattened version of the hierarchy of each root node in turn until the target is found.

        NOTE: If this HierarchyMapper hasn't been initialized and the items requested for the first time, root node list is still empty and no search is performed.

        Parameters:

        target - the target object to find

        Returns:

        optional index of given object

      • registerChildren

        protected void registerChildren​(T parent,
                                        List<T> childList)

        Register parent and child items into inner structures.

        NOTE: Older implementation of this class suggested overriding this and other methods, mainly for the purpose of avoiding memory leaks. That should no longer be necessary with the new logic.

        This method assumes that any added, moved, or removed data is handled separately (only updating collapsed contents, or refreshing the whole data provider after the update), because making such changes here couldn't update the client side correctly.

        Parameters:

        parent - the parent item

        childList - list of children to be registered for the given parent, can be empty

      • destroyAllData

        public void destroyAllData()

        Description copied from interface: DataGenerator

        Informs the DataGenerator that all data has been dropped. This method should clean up any unneeded information stored for items.

        Specified by:

        destroyAllData in interface DataGenerator<T>

      • destroyData

        public void destroyData​(T item)

        Description copied from interface: DataGenerator

        Informs the DataGenerator that the given data item has been dropped and is no longer needed. This method should clean up any unneeded information stored for this item.

        Specified by:

        destroyData in interface DataGenerator<T>

        Parameters:

        item - the dropped data item

      • useActiveDataOptimization

        public void useActiveDataOptimization​(boolean use)

        Set whether you want to optimize the internal data handling to only consider root nodes that have descendants within the client-side cache. The default value is true.

        Parameters:

        use - true if you want to optimize, false otherwise

        Since:

        8.17

      • setInitialized

        public void setInitialized​(boolean initialize)

        Set the initialization state of this class. By default this is set true when the related component is attached and data communicator makes the first data request for updating the client-side cache, and false when all the data is reset. Before initialization no expand data is sent to client, as the assumption is that the client hasn't been initialized yet either. Collapse doesn't have a corresponding check, because the assumption is that the internal caches are empty and the attempt to collapse would result in an empty row range anyway.

        NOTE: Do not set this class uninitialized for optimization purposes without calling destroyAllData() afterwards, otherwise the internal caches are likely to get out of sync with both data communicator and the client side and be unable to recover gracefully.

        Parameters:

        initialize - true to mark initialized, false to mark requiring initialization

        Since:

        8.17