com.vaadin.flow.data.provider.Query Java Examples

The following examples show how to use com.vaadin.flow.data.provider.Query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DemoView.java    From multiselect-combo-box-flow with Apache License 2.0 6 votes vote down vote up
private void addAllowCustomValuesDemo() {
    MultiselectComboBox<String> multiselectComboBox = new MultiselectComboBox();
    multiselectComboBox.setLabel("Allow custom values");
    multiselectComboBox.setPlaceholder("Select existing or input custom value");
    multiselectComboBox.setWidth("100%");
    List<String> items = Arrays.asList("Java", "Go", "Python", "C#");
    multiselectComboBox.setItems(items);

    multiselectComboBox.addCustomValuesSetListener(e -> {
        Set<String> existingSelected = multiselectComboBox.getValue().stream().collect(Collectors.toSet());
        existingSelected.add(e.getDetail());
        List<String> updatedItems = multiselectComboBox.getDataProvider().fetch(new Query<>()).collect(Collectors.toList());
        updatedItems.add(e.getDetail());
        multiselectComboBox.setItems(updatedItems);
        multiselectComboBox.setValue(existingSelected);
    });

    Button getValueBtn = new Button("Get value");
    getValueBtn.addClickListener(
        event -> multiselectComboBoxValueChangeHandler(
            multiselectComboBox));

    add(buildDemoContainer(multiselectComboBox, getValueBtn));
}
 
Example #2
Source File: ReplaceListDataProviderTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIdOfReplacementItem() {
    Assert.assertFalse("Test object was stale before making any changes.",
            dataProvider.isStale(TEST_OBJECT));

    dataProvider.refreshItem(new StrBean("Replacement TestObject", 10, -2));

    StrBean fromDataProvider = dataProvider.fetch(new Query<>()).findFirst()
            .get();
    Object id = dataProvider.getId(fromDataProvider);

    Assert.assertNotEquals("DataProvider did not return the replacement",
            TEST_OBJECT, fromDataProvider);

    Assert.assertEquals("DataProvider not using correct identifier getter",
            TEST_OBJECT.getId(), id);

    Assert.assertTrue("Old test object should be stale",
            dataProvider.isStale(TEST_OBJECT));
}
 
Example #3
Source File: PageableDataProvider.java    From spring-data-provider with Apache License 2.0 6 votes vote down vote up
private <T, F> Sort createSpringSort(Query<T, F> q) {
    List<QuerySortOrder> sortOrders;
    if (q.getSortOrders().isEmpty()) {
        sortOrders = getDefaultSortOrders();
    } else {
        sortOrders = q.getSortOrders();
    }
    List<Order> orders = sortOrders.stream()
            .map(PageableDataProvider::queryOrderToSpringOrder)
            .collect(Collectors.toList());
    if (orders.isEmpty()) {
        return Sort.unsorted();
    } else {
        return Sort.by(orders);
    }
}
 
Example #4
Source File: ConfigurableFilterDataProviderWrapperTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void predicate_combinedFilters() {
    configurablePredicate.setFilter(Integer.valueOf(50));

    Assert.assertEquals("Both filters should be used", 0,
            configurablePredicate.size(new Query<>("Xyz")));

    configurablePredicate.setFilter(null);

    Assert.assertEquals("Only zyz filter should be used", 1,
            configurablePredicate.size(new Query<>("Xyz")));
}
 
Example #5
Source File: ConfigurableFilterDataProviderWrapperTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void predicate_queryFilter() {
    Assert.assertEquals("Query filter should be used", 1,
            configurablePredicate.size(new Query<>("Xyz")));

    Assert.assertEquals("null query filter should return all items", 100,
            configurablePredicate.size(new Query<>()));
}
 
Example #6
Source File: ConfigurableFilterDataProviderWrapperTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void predicate_setFilter() {
    configurablePredicate.setFilter(Integer.valueOf(50));

    Assert.assertEquals("Set filter should be used", 49,
            configurablePredicate.size(new Query<>()));

    configurablePredicate.setFilter(null);

    Assert.assertEquals("null filter should return all items", 100,
            configurablePredicate.size(new Query<>()));
}
 
Example #7
Source File: ConfigurableFilterDataProviderWrapperTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void void_setFilter() {
    configurableVoid.setFilter(xyzFilter);

    Assert.assertEquals("Set filter should be used", 1,
            configurableVoid.size(new Query<>()));

    configurableVoid.setFilter(null);

    Assert.assertEquals("null filter should return all items", 100,
            configurableVoid.size(new Query<>()));
}
 
Example #8
Source File: ReplaceListDataProviderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetIdOfItem() {
    Object id = dataProvider.fetch(new Query<>()).findFirst()
            .map(dataProvider::getId).get();
    Assert.assertEquals("DataProvider not using correct identifier getter",
            TEST_OBJECT.getId(), id);
}
 
Example #9
Source File: HierarchicalDataProvider.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("serial")
default <C> HierarchicalDataProvider<T, C> withConvertedFilter(
        SerializableFunction<C, F> filterConverter) {
    return new HierarchicalFilterDataProviderWrapper<T, C, F>(this) {
        @Override
        protected F getFilter(Query<T, C> query) {
            return FilterUtils.convertFilter(filterConverter, query);
        }
    };
}
 
Example #10
Source File: MainAppLayout.java    From vaadin-app-layout with Apache License 2.0 5 votes vote down vote up
private SearchOverlayButton<TestSearchResult, SerializablePredicate<TestSearchResult>> initSearchOverlayButton() {
    // The data provider that provides the entities for the search
    ListDataProvider<TestSearchResult> listDataProvider = new ListDataProvider<>(Arrays.asList(
            new TestSearchResult("Header1", "Description1"),
            new TestSearchResult("Header2", "Description2"),
            new TestSearchResult("Header3", "Description3"),
            new TestSearchResult("Header4", "Description4"),
            new TestSearchResult("Header5", "Description5"),
            new TestSearchResult("Header6", "Description6"),
            new TestSearchResult("Header7", "Description7"),
            new TestSearchResult("Header8", "Description8"),
            new TestSearchResult("Header9", "Description9"),
            new TestSearchResult("Header10", "Description10")
    ));

    return new SearchOverlayButtonBuilder<TestSearchResult, SerializablePredicate<TestSearchResult>>()
            // add the data provider
            .withDataProvider(listDataProvider)
            // Set the query that is executed to filter the Entities above
            .withQueryProvider(s -> new Query<>(testEntity -> !s.equals("") && testEntity.getHeader().startsWith(s)))
            // Set the producer that produces Components to be shown as search result
            .withDataViewProvider(queryResult -> {
                RippleClickableCard card = new RippleClickableCard(new Item(queryResult.getHeader(), queryResult.getDescription()));
                card.setWidthFull();
                card.setBackground("var(--lumo-base-color)");
                return card;
            })
            // A Listener to react if a search result was clicked
            .withQueryResultListener(testSearchResult -> Notification.show(testSearchResult.getHeader()))
            .build();
}
 
Example #11
Source File: SearchView.java    From vaadin-app-layout with Apache License 2.0 5 votes vote down vote up
@Override
public void furtherConfiguration(AppLayoutBuilder builder) {
    ListDataProvider<TestSearchResult> dataProvider = new ListDataProvider<>(Arrays.asList(
            new TestSearchResult("Header1", "Description1"),
            new TestSearchResult("Header2", "Description2"),
            new TestSearchResult("Header3", "Description3"),
            new TestSearchResult("Header4", "Description4"),
            new TestSearchResult("Header5", "Description5"),
            new TestSearchResult("Header6", "Description6"),
            new TestSearchResult("Header7", "Description7"),
            new TestSearchResult("Header8", "Description8"),
            new TestSearchResult("Header9", "Description9"),
            new TestSearchResult("Header10", "Description10")
    ));

    SearchOverlayButton<TestSearchResult, SerializablePredicate<TestSearchResult>> button = new SearchOverlayButtonBuilder<TestSearchResult, SerializablePredicate<TestSearchResult>>()
            .withDataProvider(dataProvider)
            .withQueryProvider(s -> new Query<TestSearchResult, SerializablePredicate<TestSearchResult>>(testEntity -> !s.equals("") && testEntity.getHeader().startsWith(s)))
            .withDataViewProvider(result -> {
                RippleClickableCard card = new RippleClickableCard(new Item(result.getHeader(), result.getDescription()));
                card.setWidthFull();
                card.setBackground("var(--lumo-base-color)");
                return card;
            })
            .withQueryResultListener(testSearchResult -> Notification.show(testSearchResult.header + " clicked"))
            .build();

    button.setId("it-test-search-button");
    button.getSearchView().getSearchField().setId("it-test-search-field");
    button.getSearchView().getCloseButton().setId("it-test-search-close");
    builder.withAppBar(button);
}
 
Example #12
Source File: PageableDataProvider.java    From spring-data-provider with Apache License 2.0 5 votes vote down vote up
private <T> Stream<T> fromPageable(Page<T> result, Pageable pageable,
        Query<T, ?> query) {
    List<T> items = result.getContent();

    int firstRequested = query.getOffset();
    int nrRequested = query.getLimit();
    int firstReturned = (int) pageable.getOffset();
    int firstReal = firstRequested - firstReturned;
    int afterLastReal = firstReal + nrRequested;
    if (afterLastReal > items.size()) {
        afterLastReal = items.size();
    }
    return items.subList(firstReal, afterLastReal).stream();
}
 
Example #13
Source File: SearchOverlayButtonBuilder.java    From vaadin-app-layout with Apache License 2.0 4 votes vote down vote up
public SearchOverlayButtonBuilder<T, F> withQueryProvider(Function<String, Query<T, F>> queryFunction) {
    this.queryFunction = queryFunction;
    return this;
}
 
Example #14
Source File: MultiselectComboBox.java    From multiselect-combo-box-flow with Apache License 2.0 4 votes vote down vote up
private void refreshAllData(boolean forceServerSideFiltering) {
    setClientSideFilter(!forceServerSideFiltering && getDataProvider()
            .size(new Query<>()) <= getPageSizeDouble());

    reset();
}
 
Example #15
Source File: MultiselectComboBoxTest.java    From multiselect-combo-box-flow with Apache License 2.0 4 votes vote down vote up
@Override
public void setDataProvider(ListDataProvider<T> listDataProvider) {
    super.setDataProvider(listDataProvider);
    items = listDataProvider.fetch(new Query<>())
            .collect(Collectors.toList());
}
 
Example #16
Source File: FilterablePageableDataProvider.java    From spring-data-provider with Apache License 2.0 4 votes vote down vote up
@Override
public int size(Query<T, F> query) {
    return super.size(getFilterQuery(query));
}
 
Example #17
Source File: ConfigurableFilterDataProviderWrapperTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test(expected = Exception.class)
@SuppressWarnings({ "unchecked", "rawtypes" })
public void void_nonNullQueryFilter_throws() {
    configurableVoid
            .size((Query) new Query<StrBean, String>("invalid filter"));
}
 
Example #18
Source File: FilterablePageableDataProvider.java    From spring-data-provider with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<T> fetch(Query<T, F> query) {
    return super.fetch(getFilterQuery(query));
}
 
Example #19
Source File: AbstractDataProviderTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public int size(Query<Object, Object> t) {
    return 0;
}
 
Example #20
Source File: AbstractDataProviderTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<Object> fetch(Query<Object, Object> t) {
    return null;
}
 
Example #21
Source File: FilterablePageableDataProvider.java    From spring-data-provider with Apache License 2.0 4 votes vote down vote up
private Query<T, F> getFilterQuery(Query<T, F> t) {
    return new Query(t.getOffset(), t.getLimit(), t.getSortOrders(),
            t.getInMemorySorting(), filter);
}
 
Example #22
Source File: PageableDataProvider.java    From spring-data-provider with Apache License 2.0 4 votes vote down vote up
@Override
protected Stream<T> fetchFromBackEnd(Query<T, F> query) {
    Pageable pageable = getPageable(query);
    Page<T> result = fetchFromBackEnd(query, pageable);
    return fromPageable(result, pageable, query);
}
 
Example #23
Source File: ReplaceListDataProvider.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<StrBean> fetch(Query<StrBean, Void> query) {
    return backend.stream().skip(query.getOffset()).limit(query.getLimit());
}
 
Example #24
Source File: ReplaceListDataProvider.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public int size(Query<StrBean, Void> t) {
    return backend.size();
}
 
Example #25
Source File: PageableDataProvider.java    From spring-data-provider with Apache License 2.0 4 votes vote down vote up
protected abstract Page<T> fetchFromBackEnd(Query<T, F> query,
Pageable pageable);
 
Example #26
Source File: HierarchicalFilterUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<T> fetch(Query<T, F> query) {
    return HierarchicalDataProvider.super.fetch(query);
}
 
Example #27
Source File: HierarchicalFilterUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public int size(Query<T, F> query) {
    return HierarchicalDataProvider.super.size(query);
}
 
Example #28
Source File: HierarchicalFilterUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<T> fetch(Query<T, Q> query) {
    return HierarchicalConfigurableFilterDataProvider.super.fetch(
            query);
}
 
Example #29
Source File: HierarchicalFilterUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public int size(Query<T, Q> t) {
    return HierarchicalConfigurableFilterDataProvider.super.size(t);
}
 
Example #30
Source File: GridCrud.java    From crudui with Apache License 2.0 4 votes vote down vote up
protected void findAllButtonClicked() {
    grid.asSingleSelect().clear();
    refreshGrid();
    showNotification(String.format(rowCountCaption, grid.getDataProvider().size(new Query())));
}