All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mantledillusion.vaadin.cotton.component.mixin.HasSimpleDataProviderBuilder Maven / Gradle / Ivy

Go to download

Cotton is a Vaadin extension destined for the ultimate of developer convenience.

There is a newer version: 2.3.4
Show newest version
package com.mantledillusion.vaadin.cotton.component.mixin;

import com.mantledillusion.data.epiphy.Property;
import com.mantledillusion.vaadin.cotton.component.EntityBuilder;
import com.mantledillusion.vaadin.cotton.exception.http900.Http901IllegalArgumentException;
import com.mantledillusion.vaadin.cotton.model.InMemoryDataProviderBinding;
import com.mantledillusion.vaadin.cotton.model.ModelAccessor;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.data.binder.HasDataProvider;
import com.vaadin.flow.data.provider.CallbackDataProvider;
import com.vaadin.flow.data.provider.ConfigurableFilterDataProvider;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.shared.Registration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * {@link EntityBuilder} for {@link HasDataProvider} implementing {@link Component}s.
 *
 * @param  The {@link Component} type implementing {@link HasDataProvider}.
 * @param  The element type of the {@link HasDataProvider}.
 * @param  The type of {@link HasDataProviderBuilder.ConfigurableFilter} that is used to filter the {@link DataProvider}.
 * @param  The final implementation type of {@link HasValueBuilder}.
 */
public interface HasSimpleDataProviderBuilder, E,
        F extends HasDataProviderBuilder.ConfigurableFilter,
        B extends HasSimpleDataProviderBuilder>
        extends HasDataProviderBuilder, EntityBuilder {

    /**
     * Builder method, configures a new {@link com.vaadin.flow.data.provider.InMemoryDataProvider} bound to the given
     * {@link Property} using the given {@link ModelAccessor}.
     *
     * @param 
     *            The type of the model to whose property to bind.
     * @param binder
     *            The {@link ModelAccessor} to bind the {@link HasDataProvider} with; might not be null.
     * @param property
     *            The {@link Property} to bind the {@link HasDataProvider} to; might not be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default  B setDataProvider(ModelAccessor binder, Property property) {
        return configure(hasDataProvider -> binder.bindHasDataProvider(hasDataProvider, property, null));
    }

    /**
     * Builder method, configures a new {@link com.vaadin.flow.data.provider.InMemoryDataProvider} bound to the given
     * {@link Property} using the given {@link ModelAccessor}.
     *
     * @param 
     *            The type of the model to whose property to bind.
     * @param binder
     *            The {@link ModelAccessor} to bind the {@link HasDataProvider} with; might not be null.
     * @param property
     *            The {@link Property} to bind the {@link HasDataProvider} to; might not be null.
     * @param filter
     *            The {@link HasDataProviderBuilder.ConfigurableFilter} to use; might be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default  B setDataProvider(ModelAccessor binder, Property property, F filter) {
        return setDataProvider(binder, property, () -> filter);
    }

    /**
     * Builder method, configures a new {@link com.vaadin.flow.data.provider.InMemoryDataProvider} bound to the given
     * {@link Property} using the given {@link ModelAccessor}.
     *
     * @param 
     *            The type of the model to whose property to bind.
     * @param binder
     *            The {@link ModelAccessor} to bind the {@link HasDataProvider} with; might not be null.
     * @param property
     *            The {@link Property} to bind the {@link HasDataProvider} to; might not be null.
     * @param filterSupplier
     *            A {@link Supplier} of {@link HasDataProviderBuilder.ConfigurableFilter}s to use; might be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default  B setDataProvider(ModelAccessor binder, Property property, Supplier filterSupplier) {
        return configure(hasDataProvider -> {
            F filter = filterSupplier != null ? filterSupplier.get() : null;
            set(HasDataProviderBuilder.ConfigurableFilter.class, filter);
            InMemoryDataProviderBinding binding = binder.bindHasDataProvider(hasDataProvider, property, filter);
            if (filter != null) {
                filter.addConfigurationChangedListener(() -> binding.getDataProvider().refreshAll());
            }
        }, true);
    }

    /**
     * Builder method, configures the given {@link DataProvider}.
     *
     * @param dataProvider
     *            The {@link DataProvider} to configure; might not be null.
     * @return this
     */
    default B setDataProvider(DataProvider dataProvider) {
        return configure(hasDataProvider -> hasDataProvider.setDataProvider(dataProvider), true);
    }

    /**
     * Builder method, configures the given {@link DataProvider}.
     *
     * @param dataProvider
     *            The {@link DataProvider} to configure; might not be null.
     * @param filter
     *            The {@link HasDataProviderBuilder.ConfigurableFilter} to use; might not be null.
     * @return this
     */
    default B setDataProvider(DataProvider dataProvider, F filter) {
        return setDataProvider(dataProvider, () -> filter);
    }

    /**
     * Builder method, configures the given {@link DataProvider}.
     *
     * @param dataProvider
     *            The {@link DataProvider} to configure; might not be null.
     * @param filterSupplier
     *            A {@link Supplier} of {@link HasDataProviderBuilder.ConfigurableFilter}s to use; might be null.
     * @return this
     */
    default B setDataProvider(DataProvider dataProvider, Supplier filterSupplier) {
        return configure(hasDataProvider -> {
            F filter = filterSupplier != null ?  filterSupplier.get() : null;
            set(HasDataProviderBuilder.ConfigurableFilter.class, filter);
            if (filter != null) {
                ConfigurableFilterDataProvider configurableFilterDataProvider =
                        dataProvider.withConfigurableFilter();
                configurableFilterDataProvider.setFilter(filter);
                filter.addConfigurationChangedListener(configurableFilterDataProvider::refreshAll);
                hasDataProvider.setDataProvider(configurableFilterDataProvider);
            } else {
                hasDataProvider.setDataProvider(dataProvider);
            }
        }, true);
    }

    /**
     * Builder method, configures a new {@link CallbackDataProvider} that uses the given callbacks.
     *
     * @param countCallback
     *            The {@link CallbackDataProvider.CountCallback} that is able to determine the count of all elements
     *            matching the query; might not be null.
     * @param fetchCallback
     *            The {@link CallbackDataProvider.FetchCallback} that is able to fetch all elements matching the
     *            query; might not be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default B setDataProvider(CallbackDataProvider.CountCallback countCallback,
                              CallbackDataProvider.FetchCallback fetchCallback) {
        return configure(hasDataProvider -> hasDataProvider.setDataProvider(DataProvider.
                fromCallbacks(fetchCallback, countCallback)), true);
    }

    /**
     * Builder method, configures a new {@link CallbackDataProvider} that uses the given callbacks.
     *
     * @param countCallback
     *            The {@link CallbackDataProvider.CountCallback} that is able to determine the count of all elements
     *            matching the query; might not be null.
     * @param fetchCallback
     *            The {@link CallbackDataProvider.FetchCallback} that is able to fetch all elements matching the
     *            query; might not be null.
     * @param filter
     *            The {@link HasDataProviderBuilder.ConfigurableFilter} to use; might be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default B setDataProvider(CallbackDataProvider.CountCallback countCallback,
                              CallbackDataProvider.FetchCallback fetchCallback, F filter) {
        return setDataProvider(countCallback, fetchCallback, () -> filter);
    }

    /**
     * Builder method, configures a new {@link CallbackDataProvider} that uses the given callbacks.
     *
     * @param countCallback
     *            The {@link CallbackDataProvider.CountCallback} that is able to determine the count of all elements
     *            matching the query; might not be null.
     * @param fetchCallback
     *            The {@link CallbackDataProvider.FetchCallback} that is able to fetch all elements matching the
     *            query; might not be null.
     * @param filterSupplier
     *            A {@link Supplier} of {@link HasDataProviderBuilder.ConfigurableFilter}s to use; might be null.
     * @return A new {@link HasDataProvider} instance, fully configured and bound, never null
     */
    default B setDataProvider(CallbackDataProvider.CountCallback countCallback,
                              CallbackDataProvider.FetchCallback fetchCallback, Supplier filterSupplier) {
        if (countCallback == null) {
            throw new Http901IllegalArgumentException("Cannot configure a data provider from a null count callback.");
        } else if (fetchCallback == null) {
            throw new Http901IllegalArgumentException("Cannot configure a data provider from a null fetch callback.");
        }
        return configure(hasDataProvider -> {
            F filter = filterSupplier != null ? filterSupplier.get() : null;
            set(HasDataProviderBuilder.ConfigurableFilter.class, filter);
            CallbackDataProvider callbackDataProvider = DataProvider.fromFilteringCallbacks(fetchCallback, countCallback);
            if (filter == null) {
                hasDataProvider.setDataProvider(callbackDataProvider);
            } else {
                ConfigurableFilterDataProvider configurableFilterDataProvider =
                        callbackDataProvider.withConfigurableFilter();
                configurableFilterDataProvider.setFilter(filter);
                filter.addConfigurationChangedListener(configurableFilterDataProvider::refreshAll);
                hasDataProvider.setDataProvider(configurableFilterDataProvider);
            }
        }, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy