-
public final class DataProvidersKt
-
-
Field Summary
Fields Modifier and Type Field Description private final ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>>dataProviderprivate final DataLoader<T>dataLoader
-
Method Summary
Modifier and Type Method Description final ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>>getDataProvider()final DataLoader<T>getDataLoader()final <T extends Any> ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>>sqlDataProvider(Class<T> clazz, String sql, Map<String, Any> params, Function1<T, Any> idMapper)Allows the coder to write any SQL he wishes. final <T extends Any> UnitsetDataLoader(Grid<T> $self, DataLoader<T> dataLoader, Function1<T, Any> idResolver)Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it as a (configurable) Grid.getDataProvider. final <T extends KEntity<?>> UnitsetDataLoader(Grid<T> $self, DataLoader<T> dataLoader)Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it as a (configurable) Grid.getDataProvider. final <T extends KEntity<?>> ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>>asDataProvider(DataLoader<T> $self)Returns a VokDataProvider which loads data from this DataLoader. -
-
Method Detail
-
getDataProvider
final ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>> getDataProvider()
-
getDataLoader
final DataLoader<T> getDataLoader()
-
sqlDataProvider
final <T extends Any> ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>> sqlDataProvider(Class<T> clazz, String sql, Map<String, Any> params, Function1<T, Any> idMapper)
Allows the coder to write any SQL he wishes. This provider must be simple enough to not to get in the way by smart (complex) Kotlin language features. It should support any SQL select, but should allow for adding custom filters and orderings (since this is plugged into Grid after all).
The provider is bound to a holder class which holds the values (any POJO). vok-orm is used to map the result set to the class. For example:
data class CustomerAddress(val customerName: String, val address: String) val provider = sqlDataProvider(CustomerAddress::class.java, """select c.name as customerName, a.street || ' ' || a.city as address from Customer c inner join Address a on c.address_id=a.id where 1=1 {{WHERE}} order by 1=1{{ORDER}} {{PAGING}}""", idMapper = { it })(Note how select column names must correspond to field names in the
CustomerAddressclass)Now
sqlDataProvidercan hot-patch thewhereclause computed from Grid's filters into{{WHERE}}(as a simple string replacement), and theorder byandoffset/limitinto the{{ORDER}}and{{PAGING}}, as follows:{{WHERE}}will be replaced by something like this:"and name=:pqw5as and age>:p123a"- note the auto-generated parameter names starting withp. If there are no filters, will be replaced by an empty string.{{ORDER}}will be replaced by", customerName ASC, street ASC"or by an empty string if there is no ordering requirement.{{PAGING}}will be replaced by"offset 0 limit 100"or by an empty string if there are no limitations.
Note that the Grid will display fields present in the
CustomerAddressholder class and will also auto-generate filters for them, based on the type of the field.No bloody annotations! Work in progress. It is expected that a holder class is written for every select, tailored to show the outcome of that particular select.
- Parameters:
clazz- the type of the holder class which will hold the resultsql- the select which can map into the holder class (that is, it selects columns whose names match the holder class fields).params- the sql may be parametrized; this map holds all the parameters present in the sql itself.idMapper- returns the primary key which must be unique for every row returned.
-
setDataLoader
final <T extends Any> Unit setDataLoader(Grid<T> $self, DataLoader<T> dataLoader, Function1<T, Any> idResolver)
Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it as a (configurable) Grid.getDataProvider.
- Parameters:
idResolver- provides unique ID for every item.
-
setDataLoader
final <T extends KEntity<?>> Unit setDataLoader(Grid<T> $self, DataLoader<T> dataLoader)
Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it as a (configurable) Grid.getDataProvider.
-
asDataProvider
final <T extends KEntity<?>> ConfigurableFilterDataProvider<T, Filter<T>, Filter<T>> asDataProvider(DataLoader<T> $self)
Returns a VokDataProvider which loads data from this DataLoader.
-
-
-
-