Class DataCommunicator<T>
- Type Parameters:
T- the bean type
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ComboBoxDataCommunicator,HierarchicalDataCommunicator,PagelessDataCommunicator
BiFunctions)
to write JsonObjects representing each data object to be sent to the
client-side.- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classIn-memory data provider with no items.static final classWraps the component's filter object with the meta information whether this filter changing should trigger the item count change event. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int -
Constructor Summary
ConstructorsConstructorDescriptionDataCommunicator(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 RangecomputeRequestedRange(int start, int length) Computes the requested range, limiting the number of requested items to a given threshold of ten pages.voidconfirmUpdate(int updateId) Confirm update with the givenupdateId.protected voiddoUnregister(Integer updateId) voidenablePushUpdates(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 theQuerySortOrderto use with backend sorting.DataProvider<T,?> Gets the current data provider from this DataCommunicator.intGetter method for determining the item count of the data.protected ObjectGet the object used for filtering in this data communicator.Returns theComparatorto use with in-memory sorting.getItem(int index) Gets the item at the given index from the data available to the component.intThis is the latest DataProvider item count informed to the client or fetched from the DataProvider if client data has not been sent.intGets the item count estimate used.intGets the item count estimate increase used.Gets theDataKeyMapperused by thisDataCommunicator.intReturns the page size set to fetch items.getPassivatedKeys(Set<String> oldActive) protected voidbooleanReturns whether defined or undefined size is used.booleanReturns whether the data communicator will call Data Provider for fetching the items and/or getting the items count, or ignore such a calls.booleanisItemActive(T item) Returns whether the given item is part of the active items.booleanReturns whether paged queries are enabled or not.voidInforms the DataCommunicator that a data object has been updated.voidreset()Resets all the data.voidsetBackEndSorting(List<QuerySortOrder> sortOrder) Sets theQuerySortOrders to use with backend sorting.voidsetCountCallback(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.voidsetDefinedSize(boolean definedSize) Changes between defined and undefined size and clears any previously set count callback.voidsetFetchEnabled(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.voidsetInMemorySorting(SerializableComparator<T> comparator) Sets theComparatorto use with in-memory sorting.voidsetItemCountEstimate(int itemCountEstimate) Sets the item count estimate to use and switches component to undefined size.voidsetItemCountEstimateIncrease(int itemCountEstimateIncrease) Sets the item count estimate increase to use and switches the component to undefined size if not yet used.protected voidsetKeyMapper(DataKeyMapper<T> keyMapper) Sets theDataKeyMapperused in thisDataCommunicator.voidsetPageSize(int pageSize) Sets the page size that is used to fetch items.voidsetPagingEnabled(boolean pagingEnabled) Sets whether paged queries or offset/limit queries will be used.voidsetRequestedRange(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:
-
passivatedByUpdate
-
-
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- iffetchEnabledistruethen 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, notnullinitialFilter- the initial filter value to use, ornullto 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, notnullinitialFilter- the initial filter value to use, ornullto 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:
trueif item is active,falseif 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:
Queryfor 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 valuetruewill use theDataProvider.size(Query)for getting the size. Calling withfalsewill use whatever has been set withsetItemCountEstimate(int)and increase the count when needed withsetItemCountEstimateIncrease(int).- Parameters:
definedSize-truefor defined size,falsefor undefined size
-
isDefinedSize
public boolean isDefinedSize()Returns whether defined or undefined size is used.- Returns:
truefor defined size,falsefor undefined size
-
getKeyMapper
Gets theDataKeyMapperused 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 theDataKeyMapperused 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 theComparatorto use with in-memory sorting.- Parameters:
comparator- comparator used to sort data
-
getInMemorySorting
Returns theComparatorto use with in-memory sorting.- Returns:
- comparator used to sort data
-
setBackEndSorting
Sets theQuerySortOrders to use with backend sorting.- Parameters:
sortOrder- list of sort order information to pass to a query
-
getBackEndSorting
Returns theQuerySortOrderto 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:
truefor paged queries,falsefor offset/limit queries- See Also:
-
setPagingEnabled
public void setPagingEnabled(boolean pagingEnabled) Sets whether paged queries or offset/limit queries will be used.- Parameters:
pagingEnabled-truefor paged queries,falsefor 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:
trueif the calls to data provider are enabled,falseotherwise
-
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
trueby default.- Parameters:
fetchEnabled- iftruethen 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
limitparameter 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
-
doUnregister
-
getPassivatedKeys
-