T
- the bean typepublic class DataCommunicator<T> extends Object implements Serializable
BiFunction
s)
to write JsonObject
s representing each data object to be sent to the
client-side.Modifier and Type | Class and Description |
---|---|
static class |
DataCommunicator.EmptyDataProvider<T1>
In-memory data provider with no items.
|
static class |
DataCommunicator.Filter<F>
Wraps the component's filter object with the meta information whether
this filter changing should trigger the item count change event.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_PAGE_INCREASE_COUNT |
Constructor and Description |
---|
DataCommunicator(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.
|
Modifier and Type | Method and Description |
---|---|
Query |
buildQuery(int offset,
int limit)
Generate a data query with component sorting and filtering.
|
void |
confirmUpdate(int updateId)
Confirm update with the given
updateId . |
protected Stream<T> |
fetchFromProvider(int offset,
int limit)
Fetches a list of items from the DataProvider.
|
List<QuerySortOrder> |
getBackEndSorting()
Returns the
QuerySortOrder to use with backend sorting. |
DataProvider<T,?> |
getDataProvider()
Gets the current data provider from this DataCommunicator.
|
int |
getDataProviderSize()
Getter method for determining the item count of the data.
|
protected Object |
getFilter()
Get the object used for filtering in this data communicator.
|
SerializableComparator<T> |
getInMemorySorting()
Returns the
Comparator to use with in-memory sorting. |
T |
getItem(int index)
Gets the item at the given index from the data available to the
component.
|
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.
|
int |
getItemCountEstimate()
Gets the item count estimate used.
|
int |
getItemCountEstimateIncrease()
Gets the item count estimate increase used.
|
DataKeyMapper<T> |
getKeyMapper()
Gets the
DataKeyMapper used by this DataCommunicator . |
int |
getPageSize()
Returns the page size set to fetch items.
|
protected Set<String> |
getPassivatedKeys(Set<String> oldActive) |
protected void |
handleDataRefreshEvent(DataChangeEvent.DataRefreshEvent<T> event) |
boolean |
isDefinedSize()
Returns whether defined or undefined size is used.
|
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.
|
boolean |
isItemActive(T item)
Returns whether the given item is part of the active items.
|
boolean |
isPagingEnabled()
Returns whether paged queries are enabled or not.
|
void |
refresh(T data)
Informs the DataCommunicator that a data object has been updated.
|
void |
reset()
Resets all the data.
|
void |
setBackEndSorting(List<QuerySortOrder> sortOrder)
Sets the
QuerySortOrder 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.
|
<F> SerializableConsumer<DataCommunicator.Filter<F>> |
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 the
Comparator 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 the
DataKeyMapper used in this DataCommunicator . |
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.
|
public static final int DEFAULT_PAGE_INCREASE_COUNT
public DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode)
dataGenerator
- the data generator functionarrayUpdater
- array updater strategydataUpdater
- data updater strategystateNode
- the state node used to communicate forpublic DataCommunicator(DataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, SerializableConsumer<elemental.json.JsonArray> dataUpdater, StateNode stateNode, boolean fetchEnabled)
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.
dataGenerator
- the data generator functionarrayUpdater
- array updater strategydataUpdater
- data updater strategystateNode
- the state node used to communicate forfetchEnabled
- if fetchEnabled
is true
then the data provider
will be called to fetch the items and/or to get the items
count until it's set to false
public void setRequestedRange(int start, int length)
start
- the start of the requested rangelength
- the end of the requested rangepublic void reset()
It effectively resends all available data.
public void refresh(T data)
data
- updated data object; not null
public void confirmUpdate(int updateId)
updateId
.updateId
- the update identifierpublic DataProvider<T,?> getDataProvider()
public <F> SerializableConsumer<DataCommunicator.Filter<F>> setDataProvider(DataProvider<T,F> dataProvider, F initialFilter, boolean notifiesOnChange)
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.
F
- the filter typedataProvider
- the data provider to set, not null
initialFilter
- the initial filter value to use, or null
to not
use any initial filter valuenotifiesOnChange
- if true
, then the data communicator will fire the item
count change event as soon as filter change modifies the item
count. If false
, the item count change event won't be
fired, even if the item count will be changed as a result of
filtering.public <F> SerializableConsumer<F> setDataProvider(DataProvider<T,F> dataProvider, F initialFilter)
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.
F
- the filter typedataProvider
- the data provider to set, not null
initialFilter
- the initial filter value to use, or null
to not
use any initial filter valuepublic int getItemCount()
public boolean isItemActive(T item)
item
- the item to check, not null
true
if item is active, false
if notpublic T getItem(int index)
Call to the backend is triggered if the item for a requested index is not present in the cached active items.
index
- the index of the item to getIndexOutOfBoundsException
- requested index is outside of the filtered and sorted data
setpublic Query buildQuery(int offset, int limit)
offset
- first index to fetchlimit
- fetched item countQuery
for component statepublic void setPageSize(int pageSize)
pageSize
- the page size to setpublic int getPageSize()
public void setCountCallback(CallbackDataProvider.CountCallback<T,?> countCallback)
countCallback
- the size callback to usepublic void setItemCountEstimate(int itemCountEstimate)
NOTE: setting item count estimate that is less than two pages
(set with setPageSize(int)
) can cause extra requests initially
or after a reset.
itemCountEstimate
- the item count estimate to be usedpublic int getItemCountEstimate()
public void setItemCountEstimateIncrease(int itemCountEstimateIncrease)
setPageSize(int)
or it may cause bad performance.itemCountEstimateIncrease
- the item count estimate step to usepublic int getItemCountEstimateIncrease()
public void setDefinedSize(boolean definedSize)
true
will use the
DataProvider.size(Query)
for getting the size. Calling with
false
will use whatever has been set with
setItemCountEstimate(int)
and increase the count when needed
with setItemCountEstimateIncrease(int)
.definedSize
- true
for defined size, false
for undefined
sizepublic boolean isDefinedSize()
true
for defined size, false
for undefined sizepublic DataKeyMapper<T> getKeyMapper()
DataKeyMapper
used by this DataCommunicator
. Key
mapper can be used to map keys sent to the client-side back to their
respective data objects.protected void setKeyMapper(DataKeyMapper<T> keyMapper)
DataKeyMapper
used in this DataCommunicator
. Key
mapper can be used to map keys sent to the client-side back to their
respective data objects.keyMapper
- the keyMapperpublic void setInMemorySorting(SerializableComparator<T> comparator)
Comparator
to use with in-memory sorting.comparator
- comparator used to sort datapublic SerializableComparator<T> getInMemorySorting()
Comparator
to use with in-memory sorting.public void setBackEndSorting(List<QuerySortOrder> sortOrder)
QuerySortOrder
s to use with backend sorting.sortOrder
- list of sort order information to pass to a querypublic List<QuerySortOrder> getBackEndSorting()
QuerySortOrder
to use with backend sorting.public boolean isPagingEnabled()
When the paged queries are supported, the Query.getPage()
and
Query.getPageSize()
can be used to fetch items from the paged
repositories. Otherwise, one should use Query.getOffset()
and
Query.getLimit()
. Paged queries are enabled by default.
true
for paged queries, false
for offset/limit
queriessetPagingEnabled(boolean)
public void setPagingEnabled(boolean pagingEnabled)
pagingEnabled
- true
for paged queries, false
for offset/limit
queriespublic boolean isFetchEnabled()
true
if the calls to data provider are enabled,
false
otherwisepublic void setFetchEnabled(boolean fetchEnabled)
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.
fetchEnabled
- if true
then the calls to data provider are enabled,
otherwise the data provider won't be called to fetch the
items.public int getDataProviderSize()
This method should be used only with defined size, i.e. when
isDefinedSize()
returns true
.
Can be overridden by a subclass that uses a specific type of DataProvider and/or query.
protected Object getFilter()
protected Stream<T> fetchFromProvider(int offset, int limit)
NOTE: the limit
parameter shows how many items the
client wants to fetch, but the actual number of results may be greater,
and vary from 0 to pages * pageSize
.
offset
- the starting index of the rangelimit
- the desired number of resultsprotected void handleDataRefreshEvent(DataChangeEvent.DataRefreshEvent<T> event)
Copyright © 2022. All rights reserved.