-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : introduce criteriagroup for searching (#1605)
- Loading branch information
1 parent
314c1b9
commit 40f6b30
Showing
9 changed files
with
324 additions
and
142 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
53 changes: 53 additions & 0 deletions
53
...ndow-pagination/src/main/java/com/example/keysetpagination/model/query/CriteriaGroup.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,53 @@ | ||
package com.example.keysetpagination.model.query; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public class CriteriaGroup implements ISearchCriteria { | ||
|
||
private QueryOperator operator; // AND or OR | ||
private List<ISearchCriteria> criteriaList; | ||
|
||
public CriteriaGroup() {} | ||
|
||
public CriteriaGroup(QueryOperator operator, List<ISearchCriteria> criteriaList) { | ||
this.operator = operator; | ||
this.criteriaList = criteriaList; | ||
} | ||
|
||
public QueryOperator getOperator() { | ||
return operator; | ||
} | ||
|
||
public CriteriaGroup setOperator(QueryOperator operator) { | ||
this.operator = operator; | ||
return this; | ||
} | ||
|
||
public List<ISearchCriteria> getCriteriaList() { | ||
return criteriaList; | ||
} | ||
|
||
public CriteriaGroup setCriteriaList(List<ISearchCriteria> criteriaList) { | ||
this.criteriaList = criteriaList; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Specification<?> toSpecification() { | ||
List<Specification> specs = | ||
criteriaList.stream().map(ISearchCriteria::toSpecification).toList(); | ||
|
||
Specification result = specs.getFirst(); | ||
for (int i = 1; i < specs.size(); i++) { | ||
if (getOperator() == QueryOperator.AND) { | ||
result = result.and(specs.get(i)); | ||
} else if (getOperator() == QueryOperator.OR) { | ||
result = result.or(specs.get(i)); | ||
} else { | ||
throw new UnsupportedOperationException("Operator not supported in group: " + operator); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ow-pagination/src/main/java/com/example/keysetpagination/model/query/ISearchCriteria.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,20 @@ | ||
package com.example.keysetpagination.model.query; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.PROPERTY, | ||
property = "type", | ||
defaultImpl = SearchCriteria.class // Set default implementation | ||
) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = SearchCriteria.class, name = "criteria"), | ||
@JsonSubTypes.Type(value = CriteriaGroup.class, name = "group") | ||
}) | ||
public interface ISearchCriteria<T> { | ||
|
||
Specification<T> toSpecification(); | ||
} |
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
Oops, something went wrong.