-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add interfaces for CRUD services (#20743)
Similar interfaces were previously in Hilla but are equally useful in Flow applications Fixes #20834
- Loading branch information
Showing
28 changed files
with
2,231 additions
and
7 deletions.
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
21 changes: 21 additions & 0 deletions
21
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/CountService.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,21 @@ | ||
package com.vaadin.flow.spring.data; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
import com.vaadin.flow.spring.data.filter.Filter; | ||
|
||
/** | ||
* A service that can count the number of items with a given filter. | ||
*/ | ||
public interface CountService { | ||
|
||
/** | ||
* Counts the number of items that match the given filter. | ||
* | ||
* @param filter | ||
* the filter, or {@code null} to use no filter | ||
* @return the number of items in the service that match the filter | ||
*/ | ||
public long count(@Nullable Filter filter); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/CrudService.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,12 @@ | ||
package com.vaadin.flow.spring.data; | ||
|
||
/** | ||
* A service that can create, read, update, and delete a given type of object. | ||
* | ||
* @param <T> | ||
* the type of object to manage | ||
* @param <ID> | ||
* the type of the object's identifier | ||
*/ | ||
public interface CrudService<T, ID> extends ListService<T>, FormService<T, ID> { | ||
} |
33 changes: 33 additions & 0 deletions
33
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/FormService.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,33 @@ | ||
package com.vaadin.flow.spring.data; | ||
|
||
/** | ||
* A service that can update and delete a given type of object. | ||
* | ||
* @param <T> | ||
* the type of object to manage | ||
* @param <ID> | ||
* the type of the object's identifier | ||
* | ||
*/ | ||
public interface FormService<T, ID> { | ||
|
||
/** | ||
* Saves the given object and returns the (potentially) updated object. | ||
* <p> | ||
* If you store the object in a SQL database, the returned object might have | ||
* a new id or updated consistency version. | ||
* | ||
* @param value | ||
* the object to save | ||
* @return the fresh object; will never be {@literal null}. | ||
*/ | ||
T save(T value); | ||
|
||
/** | ||
* Deletes the object with the given id. | ||
* | ||
* @param id | ||
* the id of the object to delete | ||
*/ | ||
void delete(ID id); | ||
} |
30 changes: 30 additions & 0 deletions
30
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/GetService.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,30 @@ | ||
package com.vaadin.flow.spring.data; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A service that can fetch the given type of object. | ||
*/ | ||
public interface GetService<T, ID> { | ||
|
||
/** | ||
* Gets the object with the given id. | ||
* | ||
* @param id | ||
* the id of the object | ||
* @return the object, or an empty optional if no object with the given id | ||
*/ | ||
Optional<T> get(ID id); | ||
|
||
/** | ||
* Checks if an object with the given id exists. | ||
* | ||
* @param id | ||
* the id of the object | ||
* @return {@code true} if the object exists, {@code false} otherwise | ||
*/ | ||
default boolean exists(ID id) { | ||
return get(id).isPresent(); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/ListService.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,29 @@ | ||
package com.vaadin.flow.spring.data; | ||
|
||
import java.util.List; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import com.vaadin.flow.spring.data.filter.Filter; | ||
|
||
/** | ||
* A service that can list the given type of object. | ||
* | ||
* @param <T> | ||
* the type of object to list | ||
*/ | ||
public interface ListService<T> { | ||
/** | ||
* Lists objects of the given type using the paging, sorting and filtering | ||
* options provided in the parameters. | ||
* | ||
* @param pageable | ||
* contains information about paging and sorting | ||
* @param filter | ||
* the filter to apply or {@code null} to not filter | ||
* @return a list of objects or an empty list if no objects were found | ||
*/ | ||
List<T> list(Pageable pageable, @Nullable Filter filter); | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/filter/AndFilter.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,45 @@ | ||
package com.vaadin.flow.spring.data.filter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A filter that requires all children to pass. | ||
* <p> | ||
* Custom filter implementations need to handle this filter by running all child | ||
* filters and verifying that all of them pass. | ||
*/ | ||
public class AndFilter extends Filter { | ||
|
||
private List<Filter> children; | ||
|
||
/** | ||
* Create an empty filter. | ||
*/ | ||
public AndFilter() { | ||
// Empty constructor is needed for serialization | ||
} | ||
|
||
/** | ||
* Create a filter with the given children. | ||
* | ||
* @param children | ||
* the children of the filter | ||
*/ | ||
public AndFilter(Filter... children) { | ||
setChildren(List.of(children)); | ||
} | ||
|
||
public List<Filter> getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(List<Filter> children) { | ||
this.children = children; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [children=" + children + "]"; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/filter/Filter.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,27 @@ | ||
package com.vaadin.flow.spring.data.filter; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
/** | ||
* Superclass for all filters to be used with CRUD services. This specific class | ||
* is never used, instead a filter instance will be one of the following types: | ||
* <ul> | ||
* <li>{@link AndFilter} - Contains a list of nested filters, all of which need | ||
* to pass.</li> | ||
* <li>{@link OrFilter} - Contains a list of nested filters, of which at least | ||
* one needs to pass.</li> | ||
* <li>{@link PropertyStringFilter} - Matches a specific property, or nested | ||
* property path, against a filter value, using a specific operator.</li> | ||
* </ul> | ||
*/ | ||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) | ||
@JsonSubTypes({ @Type(value = OrFilter.class, name = "or"), | ||
@Type(value = AndFilter.class, name = "and"), | ||
@Type(value = PropertyStringFilter.class, name = "propertyString") }) | ||
public class Filter implements Serializable { | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
vaadin-spring/src/main/java/com/vaadin/flow/spring/data/filter/OrFilter.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,45 @@ | ||
package com.vaadin.flow.spring.data.filter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A filter that requires at least one of its children to pass. | ||
* <p> | ||
* Custom filter implementations need to handle this filter by running all child | ||
* filters and verifying that at least one of them passes. | ||
*/ | ||
public class OrFilter extends Filter { | ||
|
||
private List<Filter> children; | ||
|
||
/** | ||
* Create an empty filter. | ||
*/ | ||
public OrFilter() { | ||
// Empty constructor is needed for serialization | ||
} | ||
|
||
/** | ||
* Create a filter with the given children. | ||
* | ||
* @param children | ||
* the children of the filter | ||
*/ | ||
public OrFilter(Filter... children) { | ||
setChildren(List.of(children)); | ||
} | ||
|
||
public List<Filter> getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(List<Filter> children) { | ||
this.children = children; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [children=" + children + "]"; | ||
} | ||
|
||
} |
Oops, something went wrong.