-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Spring Data helper class (#644)
* 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
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/VaadinSpringDataHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
|
||
} |