Skip to content

Commit c96213d

Browse files
authored
feat: Add interfaces for CRUD services (#20743)
Similar interfaces were previously in Hilla but are equally useful in Flow applications Fixes #20834
1 parent 4688eb1 commit c96213d

28 files changed

Lines changed: 2231 additions & 7 deletions

‎vaadin-spring/pom.xml‎

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<artifactId>jakarta.servlet-api</artifactId>
4444
<scope>provided</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.jspecify</groupId>
48+
<artifactId>jspecify</artifactId>
49+
<version>1.0.0</version>
50+
</dependency>
51+
4652

4753
<dependency>
4854
<groupId>com.vaadin</groupId>
@@ -97,6 +103,26 @@
97103
<artifactId>spring-data-commons</artifactId>
98104
<optional>true</optional>
99105
</dependency>
106+
<dependency>
107+
<groupId>org.springframework.data</groupId>
108+
<artifactId>spring-data-jpa</artifactId>
109+
<scope>provided</scope>
110+
<exclusions>
111+
<exclusion>
112+
<artifactId>aspectjrt</artifactId>
113+
<groupId>org.aspectj</groupId>
114+
</exclusion>
115+
<exclusion>
116+
<artifactId>jcl-over-slf4j</artifactId>
117+
<groupId>org.slf4j</groupId>
118+
</exclusion>
119+
</exclusions>
120+
</dependency>
121+
<dependency>
122+
<groupId>jakarta.persistence</groupId>
123+
<artifactId>jakarta.persistence-api</artifactId>
124+
<scope>provided</scope>
125+
</dependency>
100126
<dependency>
101127
<groupId>com.vaadin</groupId>
102128
<artifactId>flow-data</artifactId>
@@ -120,6 +146,22 @@
120146
<scope>test</scope>
121147
</dependency>
122148

149+
<dependency>
150+
<groupId>com.h2database</groupId>
151+
<artifactId>h2</artifactId>
152+
<scope>test</scope>
153+
</dependency>
154+
<dependency>
155+
<groupId>org.springframework.boot</groupId>
156+
<artifactId>spring-boot-test-autoconfigure</artifactId>
157+
<scope>test</scope>
158+
</dependency>
159+
<dependency>
160+
<groupId>org.springframework.boot</groupId>
161+
<artifactId>spring-boot-starter-data-jpa</artifactId>
162+
<scope>test</scope>
163+
</dependency>
164+
123165
<dependency>
124166
<groupId>org.springframework.boot</groupId>
125167
<artifactId>spring-boot-starter-test</artifactId>
@@ -189,13 +231,13 @@
189231

190232
<dependencyManagement>
191233
<dependencies>
192-
<dependency>
193-
<groupId>org.springframework.boot</groupId>
194-
<artifactId>spring-boot-dependencies</artifactId>
195-
<version>${spring.boot.version}</version>
196-
<type>pom</type>
197-
<scope>import</scope>
198-
</dependency>
234+
<dependency>
235+
<groupId>org.springframework.boot</groupId>
236+
<artifactId>spring-boot-dependencies</artifactId>
237+
<version>${spring.boot.version}</version>
238+
<type>pom</type>
239+
<scope>import</scope>
240+
</dependency>
199241
</dependencies>
200242
</dependencyManagement>
201243
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.vaadin.flow.spring.data;
2+
3+
import org.jspecify.annotations.Nullable;
4+
5+
import com.vaadin.flow.spring.data.filter.Filter;
6+
7+
/**
8+
* A service that can count the number of items with a given filter.
9+
*/
10+
public interface CountService {
11+
12+
/**
13+
* Counts the number of items that match the given filter.
14+
*
15+
* @param filter
16+
* the filter, or {@code null} to use no filter
17+
* @return the number of items in the service that match the filter
18+
*/
19+
public long count(@Nullable Filter filter);
20+
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.vaadin.flow.spring.data;
2+
3+
/**
4+
* A service that can create, read, update, and delete a given type of object.
5+
*
6+
* @param <T>
7+
* the type of object to manage
8+
* @param <ID>
9+
* the type of the object's identifier
10+
*/
11+
public interface CrudService<T, ID> extends ListService<T>, FormService<T, ID> {
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.vaadin.flow.spring.data;
2+
3+
/**
4+
* A service that can update and delete a given type of object.
5+
*
6+
* @param <T>
7+
* the type of object to manage
8+
* @param <ID>
9+
* the type of the object's identifier
10+
*
11+
*/
12+
public interface FormService<T, ID> {
13+
14+
/**
15+
* Saves the given object and returns the (potentially) updated object.
16+
* <p>
17+
* If you store the object in a SQL database, the returned object might have
18+
* a new id or updated consistency version.
19+
*
20+
* @param value
21+
* the object to save
22+
* @return the fresh object; will never be {@literal null}.
23+
*/
24+
T save(T value);
25+
26+
/**
27+
* Deletes the object with the given id.
28+
*
29+
* @param id
30+
* the id of the object to delete
31+
*/
32+
void delete(ID id);
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.vaadin.flow.spring.data;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* A service that can fetch the given type of object.
7+
*/
8+
public interface GetService<T, ID> {
9+
10+
/**
11+
* Gets the object with the given id.
12+
*
13+
* @param id
14+
* the id of the object
15+
* @return the object, or an empty optional if no object with the given id
16+
*/
17+
Optional<T> get(ID id);
18+
19+
/**
20+
* Checks if an object with the given id exists.
21+
*
22+
* @param id
23+
* the id of the object
24+
* @return {@code true} if the object exists, {@code false} otherwise
25+
*/
26+
default boolean exists(ID id) {
27+
return get(id).isPresent();
28+
}
29+
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.vaadin.flow.spring.data;
2+
3+
import java.util.List;
4+
5+
import org.jspecify.annotations.Nullable;
6+
import org.springframework.data.domain.Pageable;
7+
8+
import com.vaadin.flow.spring.data.filter.Filter;
9+
10+
/**
11+
* A service that can list the given type of object.
12+
*
13+
* @param <T>
14+
* the type of object to list
15+
*/
16+
public interface ListService<T> {
17+
/**
18+
* Lists objects of the given type using the paging, sorting and filtering
19+
* options provided in the parameters.
20+
*
21+
* @param pageable
22+
* contains information about paging and sorting
23+
* @param filter
24+
* the filter to apply or {@code null} to not filter
25+
* @return a list of objects or an empty list if no objects were found
26+
*/
27+
List<T> list(Pageable pageable, @Nullable Filter filter);
28+
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.vaadin.flow.spring.data.filter;
2+
3+
import java.util.List;
4+
5+
/**
6+
* A filter that requires all children to pass.
7+
* <p>
8+
* Custom filter implementations need to handle this filter by running all child
9+
* filters and verifying that all of them pass.
10+
*/
11+
public class AndFilter extends Filter {
12+
13+
private List<Filter> children;
14+
15+
/**
16+
* Create an empty filter.
17+
*/
18+
public AndFilter() {
19+
// Empty constructor is needed for serialization
20+
}
21+
22+
/**
23+
* Create a filter with the given children.
24+
*
25+
* @param children
26+
* the children of the filter
27+
*/
28+
public AndFilter(Filter... children) {
29+
setChildren(List.of(children));
30+
}
31+
32+
public List<Filter> getChildren() {
33+
return children;
34+
}
35+
36+
public void setChildren(List<Filter> children) {
37+
this.children = children;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return getClass().getSimpleName() + " [children=" + children + "]";
43+
}
44+
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vaadin.flow.spring.data.filter;
2+
3+
import java.io.Serializable;
4+
5+
import com.fasterxml.jackson.annotation.JsonSubTypes;
6+
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
7+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
9+
/**
10+
* Superclass for all filters to be used with CRUD services. This specific class
11+
* is never used, instead a filter instance will be one of the following types:
12+
* <ul>
13+
* <li>{@link AndFilter} - Contains a list of nested filters, all of which need
14+
* to pass.</li>
15+
* <li>{@link OrFilter} - Contains a list of nested filters, of which at least
16+
* one needs to pass.</li>
17+
* <li>{@link PropertyStringFilter} - Matches a specific property, or nested
18+
* property path, against a filter value, using a specific operator.</li>
19+
* </ul>
20+
*/
21+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
22+
@JsonSubTypes({ @Type(value = OrFilter.class, name = "or"),
23+
@Type(value = AndFilter.class, name = "and"),
24+
@Type(value = PropertyStringFilter.class, name = "propertyString") })
25+
public class Filter implements Serializable {
26+
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.vaadin.flow.spring.data.filter;
2+
3+
import java.util.List;
4+
5+
/**
6+
* A filter that requires at least one of its children to pass.
7+
* <p>
8+
* Custom filter implementations need to handle this filter by running all child
9+
* filters and verifying that at least one of them passes.
10+
*/
11+
public class OrFilter extends Filter {
12+
13+
private List<Filter> children;
14+
15+
/**
16+
* Create an empty filter.
17+
*/
18+
public OrFilter() {
19+
// Empty constructor is needed for serialization
20+
}
21+
22+
/**
23+
* Create a filter with the given children.
24+
*
25+
* @param children
26+
* the children of the filter
27+
*/
28+
public OrFilter(Filter... children) {
29+
setChildren(List.of(children));
30+
}
31+
32+
public List<Filter> getChildren() {
33+
return children;
34+
}
35+
36+
public void setChildren(List<Filter> children) {
37+
this.children = children;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return getClass().getSimpleName() + " [children=" + children + "]";
43+
}
44+
45+
}

0 commit comments

Comments
 (0)