An open API service indexing awesome lists of open source software.

https://github.com/overworkedcriminal/entity-filters

Spring Boot Specification API filters generator
https://github.com/overworkedcriminal/entity-filters

code-generation java library spring-boot-specification

Last synced: 7 months ago
JSON representation

Spring Boot Specification API filters generator

Awesome Lists containing this project

README

          

# Entity-filters

Spring Boot library that allows to automatically generate filter classes for given entities.

For any class T annotated with @Entity and @GenerateFilters class TFilters will be generated.
TFilters will have intoSpecification() method that allows easy integration with Spring Boot
Specification API.

## Project organization
| module | description |
| --- | --- |
| filters | classes that allow filtering by some type |
| generator-filters | classes that allow automatic TFilters generation |
| tests | library tests |

## Example
For given entity:
```Java
@Entity
@GenerateFilters
public class CodeGenerationTestEntity {
@Id
private Long id;
private String name;
private BigDecimal price;
private LocalDate createdAt;
private LocalDateTime updatedAt;
private Integer version;
}
```
This filters class will be generated
```Java
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CodeGenerationTestEntityFilters {
@Valid private NumericValueFilter id;
@Valid private StringValueFilter name;
@Valid private NumericValueFilter price;
@Valid private NumericValueFilter createdAt;
@Valid private NumericValueFilter updatedAt;
@Valid private NumericValueFilter version;

public Specification intoSpecification() {
return Specification.allOf(
mapFilterToSpecification(CodeGenerationTestEntity_.id, id),
mapFilterToSpecification(CodeGenerationTestEntity_.name, name),
mapFilterToSpecification(CodeGenerationTestEntity_.price, price),
mapFilterToSpecification(CodeGenerationTestEntity_.createdAt, createdAt),
mapFilterToSpecification(CodeGenerationTestEntity_.updatedAt, updatedAt),
mapFilterToSpecification(CodeGenerationTestEntity_.version, version)
);
}

private static > Specification mapFilterToSpecification(
SingularAttribute attribute,
NumericValueFilter extends T> filter
) {
if (filter == null) {
return (root, query, cb) -> cb.conjunction();
}

return switch (filter.getType()) {
case LESS -> (root, query, cb) -> cb.lessThan(root.get(attribute), filter.getV1());
case LESS_EQUAL -> (root, query, cb) -> cb.lessThanOrEqualTo(root.get(attribute), filter.getV1());
case EQUAL -> (root, query, cb) -> cb.equal(root.get(attribute), filter.getV1());
case GREATER -> (root, query, cb) -> cb.greaterThan(root.get(attribute), filter.getV1());
case GREATER_EQUAL -> (root, query, cb) -> cb.greaterThanOrEqualTo(root.get(attribute), filter.getV1());
case BETWEEN -> (root, query, cb) -> cb.between(root.get(attribute), filter.getV1(), filter.getV2());
case IS_NULL -> (root, query, cb) -> cb.isNull(root.get(attribute));
case IS_NOT_NULL -> (root, query, cb) -> cb.isNotNull(root.get(attribute));
};
}

private static > Specification mapFilterToSpecification(
SingularAttribute attribute,
StringValueFilter filter
) {
if (filter == null) {
return (root, query, cb) -> cb.conjunction();
}

return switch (filter.getType()) {
case EQUAL -> (root, query, cb) -> cb.equal(root.get(attribute), filter.getV());
case EQUAL_IGNORE_CASE -> (root, query, cb) -> cb.equal(
cb.upper(
root
.get(attribute)
.as(String.class)
),
filter.getV().toUpperCase()
);
case IS_NULL -> (root, query, cb) -> cb.isNull(root.get(attribute));
case IS_NOT_NULL -> (root, query, cb) -> cb.isNotNull(root.get(attribute));
};
}

}
```

## How to run tests
```bash
mvn test
```

If for some reason there's problem with running test using onlny ```mvn test``` try this:

```bash
mvn clean
mvn test-compile
mvn test
```

## How to use it (Maven)
First compile tests (without it there may be errors related to code not being generated at correct time )
```bash
mvn test-compile
```
Then install library
```bash
mvn install
```

Add dependencies to pom.xml
```xml

com.example.filter
filters
0.0.2

com.example.filter
generator-filters
0.0.2

org.projectlombok
lombok
1.18.36
provided

```

Add plugins to pom.xml
```xml



org.springframework.boot
spring-boot-maven-plugin


org.apache.maven.plugins
maven-compiler-plugin
3.13.0




org.hibernate
hibernate-jpamodelgen
6.6.6.Final



org.projectlombok
lombok
1.18.36



com.example.filter
generator-filters
0.0.2




```