Class DataCommunicator<T>
- Type Parameters:
T
- the bean type
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ComboBoxDataCommunicator
,HierarchicalDataCommunicator
,PagelessDataCommunicator
BiFunction
s)
to write JsonObject
s representing each data object to be sent to the
client-side.- Since:
- 1.0
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
In-memory data provider with no items.static final class
Wraps the component's filter object with the meta information whether this filter changing should trigger the item count change event. -
Field Summary
-
Constructor Summary
ConstructorDescriptionDataCommunicator
(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode) Creates a new instance.DataCommunicator
(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode, boolean fetchEnabled) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionbuildQuery
(int offset, int limit) Generate a data query with component sorting and filtering.protected final Range
computeRequestedRange
(int start, int length) Computes the requested range, limiting the number of requested items to a given threshold of ten pages.void
confirmUpdate
(int updateId) Confirm update with the givenupdateId
.void
enablePushUpdates
(Executor executor) Control whether DataCommunicator should push data updates to the component asynchronously or not.fetchFromProvider
(int offset, int limit) Fetches a list of items from the DataProvider.Returns theQuerySortOrder
to use with backend sorting.DataProvider<T,
?> Gets the current data provider from this DataCommunicator.int
Getter method for determining the item count of the data.protected Object
Get the object used for filtering in this data communicator.Returns theComparator
to use with in-memory sorting.getItem
(int index) Gets the item at the given index from the data available to the component.int
This is the latest DataProvider item count informed to the client or fetched from the DataProvider if client data has not been sent.int
Gets the item count estimate used.int
Gets the item count estimate increase used.Gets theDataKeyMapper
used by thisDataCommunicator
.int
Returns the page size set to fetch items.getPassivatedKeys
(Set<String> oldActive) protected void
boolean
Returns whether defined or undefined size is used.boolean
Returns whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.boolean
isItemActive
(T item) Returns whether the given item is part of the active items.boolean
Returns whether paged queries are enabled or not.void
Informs the DataCommunicator that a data object has been updated.void
reset()
Resets all the data.void
setBackEndSorting
(List<QuerySortOrder> sortOrder) Sets theQuerySortOrder
s to use with backend sorting.void
setCountCallback
(CallbackDataProvider.CountCallback<T, ?> countCallback) Sets the size callback to be used and switches the component to exact row count.<F> SerializableConsumer<F>
setDataProvider
(DataProvider<T, F> dataProvider, F initialFilter) Sets the current data provider for this DataCommunicator.setDataProvider
(DataProvider<T, F> dataProvider, F initialFilter, boolean notifiesOnChange) Sets the current data provider for this DataCommunicator.void
setDefinedSize
(boolean definedSize) Changes between defined and undefined size and clears any previously set count callback.void
setFetchEnabled
(boolean fetchEnabled) Sets whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.void
setInMemorySorting
(SerializableComparator<T> comparator) Sets theComparator
to use with in-memory sorting.void
setItemCountEstimate
(int itemCountEstimate) Sets the item count estimate to use and switches component to undefined size.void
setItemCountEstimateIncrease
(int itemCountEstimateIncrease) Sets the item count estimate increase to use and switches the component to undefined size if not yet used.protected void
setKeyMapper
(DataKeyMapper<T> keyMapper) Sets theDataKeyMapper
used in thisDataCommunicator
.void
setPageSize
(int pageSize) Sets the page size that is used to fetch items.void
setPagingEnabled
(boolean pagingEnabled) Sets whether paged queries or offset/limit queries will be used.void
setRequestedRange
(int start, int length) Sets the requested range of data to be sent.
-
Field Details
-
DEFAULT_PAGE_INCREASE_COUNT
public static final int DEFAULT_PAGE_INCREASE_COUNT- See Also:
-
-
Constructor Details
-
DataCommunicator
public DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode) Creates a new instance.- Parameters:
dataGenerator
- the data generator functionarrayUpdater
- array updater strategydataUpdater
- data updater strategystateNode
- the state node used to communicate for
-
DataCommunicator
public DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode, boolean fetchEnabled) Creates a new instance.Allows to setup whether the data communicator will ignore fetch and size queries to data provider until further configuration. This mode is useful when the component needs to postpone the calls to data provider until some event, i.e. dropdown open event of the combo box, but needs to configure the data communicator preliminary.
- Parameters:
dataGenerator
- the data generator functionarrayUpdater
- array updater strategydataUpdater
- data updater strategystateNode
- the state node used to communicate forfetchEnabled
- iffetchEnabled
istrue
then the data provider will be called to fetch the items and/or to get the items count until it's set tofalse
-
-
Method Details
-
setRequestedRange
public void setRequestedRange(int start, int length) Sets the requested range of data to be sent.- Parameters:
start
- the start of the requested rangelength
- the end of the requested range
-
computeRequestedRange
Computes the requested range, limiting the number of requested items to a given threshold of ten pages.- Parameters:
start
- the start of the requested rangelength
- the end of the requested range- Returns:
-
enablePushUpdates
Control whether DataCommunicator should push data updates to the component asynchronously or not. By default the executor service is not defined and updates are done synchronously. Setting to null will disable the feature.Note: This works only with Grid component. If set to true, Push needs to be enabled and set to PushMode.AUTOMATIC in order this to work.
- Parameters:
executor
- The Executor used for async updates.
-
reset
public void reset()Resets all the data.It effectively resends all available data.
-
refresh
Informs the DataCommunicator that a data object has been updated.- Parameters:
data
- updated data object; notnull
-
confirmUpdate
public void confirmUpdate(int updateId) Confirm update with the givenupdateId
.- Parameters:
updateId
- the update identifier
-
getDataProvider
Gets the current data provider from this DataCommunicator.- Returns:
- the data provider
-
setDataProvider
public <F> SerializableConsumer<DataCommunicator.Filter<F>> setDataProvider(DataProvider<T, F> dataProvider, F initialFilter, boolean notifiesOnChange) Sets the current data provider for this DataCommunicator.The returned consumer can be used to set some other filter value that should be included in queries sent to the data provider. It is only valid until another data provider is set.
This method also sets the data communicator to defined size - meaning that the given data provider is queried for size and previous size estimates are discarded.
This method allows to define whether the data communicator notifies about changing of item count when it changes due to filtering.
- Type Parameters:
F
- the filter type- Parameters:
dataProvider
- the data provider to set, notnull
initialFilter
- the initial filter value to use, ornull
to not use any initial filter valuenotifiesOnChange
- iftrue
, then the data communicator will fire the item count change event as soon as filter change modifies the item count. Iffalse
, the item count change event won't be fired, even if the item count will be changed as a result of filtering.- Returns:
- a consumer that accepts a new filter value to use
-
setDataProvider
Sets the current data provider for this DataCommunicator.The returned consumer can be used to set some other filter value that should be included in queries sent to the data provider. It is only valid until another data provider is set.
This method also sets the data communicator to defined size - meaning that the given data provider is queried for size and previous size estimates are discarded.
- Type Parameters:
F
- the filter type- Parameters:
dataProvider
- the data provider to set, notnull
initialFilter
- the initial filter value to use, ornull
to not use any initial filter value- Returns:
- a consumer that accepts a new filter value to use
-
getItemCount
public int getItemCount()This is the latest DataProvider item count informed to the client or fetched from the DataProvider if client data has not been sent.- Returns:
- count of available items
-
isItemActive
Returns whether the given item is part of the active items.- Parameters:
item
- the item to check, notnull
- Returns:
true
if item is active,false
if not
-
getItem
Gets the item at the given index from the data available to the component. Data is filtered and sorted the same way as in the component.Call to the backend is triggered if the item for a requested index is not present in the cached active items.
- Parameters:
index
- the index of the item to get- Returns:
- item on index
- Throws:
IndexOutOfBoundsException
- requested index is outside of the filtered and sorted data set
-
buildQuery
Generate a data query with component sorting and filtering.- Parameters:
offset
- first index to fetchlimit
- fetched item count- Returns:
Query
for component state
-
setPageSize
public void setPageSize(int pageSize) Sets the page size that is used to fetch items. The queries to data provider are a multiple of the page size.- Parameters:
pageSize
- the page size to set
-
getPageSize
public int getPageSize()Returns the page size set to fetch items.- Returns:
- the page size
-
setCountCallback
Sets the size callback to be used and switches the component to exact row count. The new count will be used after this roundtrip.- Parameters:
countCallback
- the size callback to use
-
setItemCountEstimate
public void setItemCountEstimate(int itemCountEstimate) Sets the item count estimate to use and switches component to undefined size. Any previously set count callback is cleared. The new estimate is applied if the actual count has not been discovered and if the estimate is greater than the number of requested items. Otherwise it is not applied until there has been a reset.NOTE: setting item count estimate that is less than two pages (set with
setPageSize(int)
) can cause extra requests initially or after a reset.- Parameters:
itemCountEstimate
- the item count estimate to be used
-
getItemCountEstimate
public int getItemCountEstimate()Gets the item count estimate used.- Returns:
- the item count estimate used
-
setItemCountEstimateIncrease
public void setItemCountEstimateIncrease(int itemCountEstimateIncrease) Sets the item count estimate increase to use and switches the component to undefined size if not yet used. Any previously set count callback is cleared. The step is used the next time that the count is adjusted. NOTE: the increase should be greater than thesetPageSize(int)
or it may cause bad performance.- Parameters:
itemCountEstimateIncrease
- the item count estimate step to use
-
getItemCountEstimateIncrease
public int getItemCountEstimateIncrease()Gets the item count estimate increase used.- Returns:
- the item count estimate increase
-
setDefinedSize
public void setDefinedSize(boolean definedSize) Changes between defined and undefined size and clears any previously set count callback. Calling with valuetrue
will use theDataProvider.size(Query)
for getting the size. Calling withfalse
will use whatever has been set withsetItemCountEstimate(int)
and increase the count when needed withsetItemCountEstimateIncrease(int)
.- Parameters:
definedSize
-true
for defined size,false
for undefined size
-
isDefinedSize
public boolean isDefinedSize()Returns whether defined or undefined size is used.- Returns:
true
for defined size,false
for undefined size
-
getKeyMapper
Gets theDataKeyMapper
used by thisDataCommunicator
. Key mapper can be used to map keys sent to the client-side back to their respective data objects.- Returns:
- key mapper
-
setKeyMapper
Sets theDataKeyMapper
used in thisDataCommunicator
. Key mapper can be used to map keys sent to the client-side back to their respective data objects.- Parameters:
keyMapper
- the keyMapper
-
setInMemorySorting
Sets theComparator
to use with in-memory sorting.- Parameters:
comparator
- comparator used to sort data
-
getInMemorySorting
Returns theComparator
to use with in-memory sorting.- Returns:
- comparator used to sort data
-
setBackEndSorting
Sets theQuerySortOrder
s to use with backend sorting.- Parameters:
sortOrder
- list of sort order information to pass to a query
-
getBackEndSorting
Returns theQuerySortOrder
to use with backend sorting.- Returns:
- an unmodifiable list of sort order information to pass to a query
-
isPagingEnabled
public boolean isPagingEnabled()Returns whether paged queries are enabled or not.When the paged queries are supported, the
Query.getPage()
andQuery.getPageSize()
can be used to fetch items from the paged repositories. Otherwise, one should useQuery.getOffset()
andQuery.getLimit()
. Paged queries are enabled by default.- Returns:
true
for paged queries,false
for offset/limit queries- See Also:
-
setPagingEnabled
public void setPagingEnabled(boolean pagingEnabled) Sets whether paged queries or offset/limit queries will be used.- Parameters:
pagingEnabled
-true
for paged queries,false
for offset/limit queries
-
isFetchEnabled
public boolean isFetchEnabled()Returns whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.- Returns:
true
if the calls to data provider are enabled,false
otherwise
-
setFetchEnabled
public void setFetchEnabled(boolean fetchEnabled) Sets whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.One may need to disable the data provider calls in order to configure the data communicator and to postpone these calls until some event, i.e. dropdown open event of the combo box.
This sets to
true
by default.- Parameters:
fetchEnabled
- iftrue
then the calls to data provider are enabled, otherwise the data provider won't be called to fetch the items.
-
getDataProviderSize
public int getDataProviderSize()Getter method for determining the item count of the data.This method should be used only with defined size, i.e. when
isDefinedSize()
returnstrue
.Can be overridden by a subclass that uses a specific type of DataProvider and/or query.
- Returns:
- the size of data provider with current filter
-
getFilter
Get the object used for filtering in this data communicator.- Returns:
- the filter object of this data communicator
-
fetchFromProvider
Fetches a list of items from the DataProvider.NOTE: the
limit
parameter shows how many items the client wants to fetch, but the actual number of results may be greater, and vary from0 to pages * pageSize
.- Parameters:
offset
- the starting index of the rangelimit
- the desired number of results- Returns:
- the list of items in given range
-
handleDataRefreshEvent
-
getPassivatedKeys
-