Skip to content

Commit

Permalink
Added Spring Data helper class (#644)
Browse files Browse the repository at this point in the history
* Added Spring Data helper class

The class contains just one method to make it easier to convert Vaadin sort hints from Query object to Sprind Data Sort, which is used by many backend implementations.

* Allow filters in the query & docs for parameters

* Changed to interface

Constantly learning Java 8 in 2020 😊

* Update vaadin-spring/src/main/java/com/vaadin/flow/spring/data/VaadinSpringDataHelpers.java

Co-authored-by: Mikhail Shabarov <[email protected]>

* serializable, to pass the tests

Co-authored-by: Mikhail Shabarov <[email protected]>
  • Loading branch information
mstahv and mshabarov authored Jul 17, 2020
1 parent 3e0810c commit abce0d7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>flow-html-components</artifactId>
<version>${vaadin.flow.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-data</artifactId>
<version>${vaadin.flow.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
12 changes: 11 additions & 1 deletion vaadin-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-data</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2020 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.spring.data;

import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.data.provider.SortDirection;
import java.io.Serializable;
import java.util.stream.Collectors;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

/**
* Contains helper methods to work with Spring Data based back-ends
* and Vaadin components.
*/
public interface VaadinSpringDataHelpers extends Serializable {

/**
* Translates given Query object from a Vaadin component to Spring Data Sort
* object.
* <p>
* Can be used as a helper when making a lazy data binding from a Vaadin
* component to a Spring Data based back-end. The method expects Vaadin sort
* data to include the property name.
*
* @param vaadinQuery the Vaadin Query object passed by the component
* @return the Sort object that can be passed for Spring Data based back-end
*/
static Sort toSpringDataSort(Query<?, ?> vaadinQuery) {
return Sort.by(vaadinQuery.getSortOrders().stream()
.map(
so -> so.getDirection() == SortDirection.ASCENDING
? Order.asc(so.getSorted())
: Order.desc(so.getSorted()))
.collect(Collectors.toList())
);
}

}

0 comments on commit abce0d7

Please sign in to comment.