Skip to content

Commit

Permalink
3.2.2: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
biagio committed Nov 9, 2024
1 parent 79ee031 commit 3638e07
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 165 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ curl -X POST -H "Content-type: application/json" -d '{
<dependency>
<groupId>app.tozzi</groupId>
<artifactId>jpa-search-helper</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
</dependency>
```

#### Gradle
```
implementation 'app.tozzi:jpa-search-helper:3.2.1
implementation 'app.tozzi:jpa-search-helper:3.2.2
```

## Queries - Usage
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'app.tozzi'
version = '3.2.1'
version = '3.2.2'

java {
sourceCompatibility = '17'
Expand Down Expand Up @@ -58,7 +58,7 @@ publishing {
maven(MavenPublication) {
group = 'app.tozzi'
artifactId = 'jpa-search-helper'
version = "3.2.1"
version = "3.2.2"
from components.java
pom {
name = 'JPA Search Helper'
Expand Down
37 changes: 5 additions & 32 deletions src/main/java/app/tozzi/core/JPAProjectionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,6 @@ public static <E> ProjectionDescriptor getQuery(@NonNull JPASearchInput input, @
return new ProjectionDescriptor(criteriaQuery, selections, input, root);
}

public static <E> ProjectionDescriptor getQuery(@NonNull JPASearchInput input, @NonNull Class<?> type, @NonNull Class<E> entityClass,
@NonNull CriteriaBuilder criteriaBuilder, @NonNull Map<Class<?>, Map<String, Field>> idFields, boolean processSortOptions, Map<String, JoinType> fetchMap,
Map<String, String> entityFieldMap, Map<String, Pair<Searchable, Field>> searchableFields) {

return getQuery(input, type, entityClass, criteriaBuilder, idFields, processSortOptions, fetchMap, entityFieldMap, searchableFields, false, null);
}

public static <E> ProjectionDescriptor getQuery(@NonNull Map<String, String> filters, @NonNull Class<?> type,
@NonNull Class<E> entityClass, @NonNull CriteriaBuilder criteriaBuilder, @NonNull Map<Class<?>, Map<String, Field>> idFields,
boolean processSortOptions, Map<String, JoinType> fetchMap,
Map<String, String> entityFieldMap, Map<String, Pair<Searchable, Field>> searchableFields) {

var input = JPASearchUtils.toObject(filters, false, processSortOptions, true);
return getQuery(input, type, entityClass, criteriaBuilder, idFields, processSortOptions, fetchMap, entityFieldMap, searchableFields);
}

public static <E> ProjectionDescriptor getQuery(@NonNull Map<String, String> filters, @NonNull Class<?> type,
@NonNull Class<E> entityClass, @NonNull CriteriaBuilder criteriaBuilder, @NonNull Map<Class<?>, Map<String, Field>> idFields,
boolean processSortOptions, Map<String, JoinType> fetchMap,
Map<String, String> entityFieldMap, Map<String, Pair<Searchable, Field>> searchableFields, boolean overrideJoins, Map<String, JoinType> overrideJoinTypes) {

var input = JPASearchUtils.toObject(filters, false, processSortOptions, true);
return getQuery(input, type, entityClass, criteriaBuilder, idFields, processSortOptions, fetchMap, entityFieldMap, searchableFields, overrideJoins, overrideJoinTypes);
}

public static List<Selection<?>> loadSelection(List<String> fields, Root<?> root, Class<?> entityClass,
Map<String, Pair<Projectable, Field>> projectableFields, Map<Class<?>, Map<String, Field>> idFields, boolean throwsIfNotExists, boolean overrideJoins, Map<String, JoinType> overrideJoinTypes) {

Expand Down Expand Up @@ -277,11 +252,7 @@ private static void toMap(Tuple tuple, Map<ClassID, Map<String, Object>> ids, Cl

public static List<Map<String, Object>> toMap(List<Tuple> tuple, Class<?> entityClass, List<Selection<?>> selections, Map<Class<?>, Map<String, Field>> idFields) {
Map<ClassID, Map<String, Object>> map = new LinkedHashMap<>();

for (var t : tuple) {
toMap(t, map, entityClass, selections, idFields);
}

tuple.forEach(t -> toMap(t, map, entityClass, selections, idFields));
return map.entrySet().stream().filter(e -> e.getKey().getClazz().equals(entityClass)).map(Map.Entry::getValue).toList();
}

Expand All @@ -296,11 +267,13 @@ private static Collection<Object> createCollection(Field field) {

if (List.class.isAssignableFrom(field.getType())) {
return new ArrayList<>();
}

} else if (Set.class.isAssignableFrom(field.getType())) {
if (Set.class.isAssignableFrom(field.getType())) {
return new HashSet<>();
}

} else if (Queue.class.isAssignableFrom(field.getType())) {
if (Queue.class.isAssignableFrom(field.getType())) {
return new LinkedList<>();
}

Expand Down
59 changes: 29 additions & 30 deletions src/main/java/app/tozzi/core/JPASearchCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

@AllArgsConstructor
public class JPASearchCore {
Expand All @@ -38,7 +39,7 @@ public static <R> Specification<R> specification(JPASearchInput.RootFilter filte
Map<String, String> entityFieldMap) {

if (filter == null) {
return null;
return Specification.where(null);
}

return (root, query, criteriaBuilder) -> {
Expand All @@ -49,13 +50,16 @@ public static <R> Specification<R> specification(JPASearchInput.RootFilter filte
searchableFields,
entityFieldMap
);

if (expr == null) {
return null;
} else if (expr instanceof Predicate predicate) {
return criteriaBuilder.conjunction();
}

if (expr instanceof Predicate predicate) {
return predicate;
}

throw new JPASearchException("Not resulting a predicate " + expr);
throw new JPASearchException("Not resulting a predicate: " + expr);
};
}

Expand Down Expand Up @@ -89,17 +93,12 @@ public static PageRequest loadSortAndPagination(JPASearchInput.JPASearchOptions
throw new JPASearchException("Invalid or not present page size value");
}

var result = PageRequest.ofSize(options.getPageSize());
if (options.getPageOffset() != null && options.getPageOffset() >= 0) {
result = result.withPage(options.getPageOffset());
}

var result = PageRequest.ofSize(options.getPageSize()).withPage(options.getPageOffset() != null && options.getPageOffset() >= 0 ? options.getPageOffset() : 0);
var sort = loadSort(options, searchableFields, entityFieldMap, true);
if (sort != null) {
result = result.withSort(sort);
}

return result;
return sort != null
? result.withSort(sort)
: result;
}

private static Sort loadSort(JPASearchInput.JPASearchOptions options,
Expand All @@ -124,8 +123,7 @@ private static Sort loadSort(JPASearchInput.JPASearchOptions options,
}

var sort = Sort.by(des.getEntityKey());
sort = Boolean.TRUE.equals(options.getSortDesc()) ? sort.descending() : sort.ascending();
return sort;
return Boolean.TRUE.equals(options.getSortDesc()) ? sort.descending() : sort.ascending();
}

private static Expression<?> processExpression(
Expand All @@ -143,13 +141,10 @@ private static Expression<?> processExpression(
}

var operator = JPASearchOperatorGroup.load(rootFilter.getOperator());
var arguments = new ArrayList<>();
for (JPASearchInput.Filter f : rootFilter.getFilters()) {
var ex = process(f, cb, root, entityFieldMap, searchableFields);
if (ex != null) {
arguments.add(ex);
}
}
var arguments = rootFilter.getFilters().stream()
.map(f -> process(f, cb, root, entityFieldMap, searchableFields))
.filter(Objects::nonNull)
.toList();

if (arguments.isEmpty()) {
throw new JPASearchException("Invalid expression");
Expand All @@ -172,8 +167,9 @@ private static Expression<?> process(

if (filter instanceof JPASearchInput.RootFilter) {
return processExpression(filter, cb, root, searchableFields, entityFieldMap);
}

} else if (filter instanceof JPASearchInput.FieldFilter fieldFilter) {
if (filter instanceof JPASearchInput.FieldFilter fieldFilter) {
var exps = new ArrayList<>();
var searchFilter = JPASearchOperatorFilter.load(fieldFilter.getOperator());
var descriptor = JPASearchCoreFieldProcessor.processField(fieldFilter.getKey(), entityFieldMap, searchableFields, true, true, false);
Expand All @@ -189,18 +185,21 @@ private static Expression<?> process(
var trim = false;
var ignoreCase = false;

if (descriptor.getSearchable().trim()) { // TODO && SearchType.STRING.equals(descriptor.getSearchType())
if (descriptor.getSearchable().trim()) {
exp = cb.trim(path.as(String.class));
trim = true;
}

if (!trim && fieldFilter.getOptions() != null && fieldFilter.getOptions().isTrim()) { // TODO && SearchType.STRING.equals(descriptor.getSearchType())
exp = cb.trim(path.as(String.class));
}
if (fieldFilter.getOptions() != null) {

if (fieldFilter.getOptions() != null && fieldFilter.getOptions().isIgnoreCase()) { // TODO && SearchType.STRING.equals(descriptor.getSearchType())
ignoreCase = true;
exp = exp != null ? cb.lower(exp.as(String.class)) : cb.lower(path.as(String.class));
if (!trim && fieldFilter.getOptions().isTrim()) {
exp = cb.trim(path.as(String.class));
}

if (fieldFilter.getOptions().isIgnoreCase()) {
ignoreCase = true;
exp = exp != null ? cb.lower(exp.as(String.class)) : cb.lower(path.as(String.class));
}
}

exps.add(exp != null ? exp : path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ private static Object getValue(JPASearchOperatorFilter jpaSearchOperatorFilter,
case OFFSETDATETIME -> GenericUtils.parseOffsetDateTime(field, value, searchable.datePattern());
case OFFSETTIME -> GenericUtils.parseOffsetTime(field, value, searchable.datePattern());
case BOOLEAN -> GenericUtils.parseBoolean(field, value);
case INTEGER, LONG, FLOAT, DOUBLE, BIGDECIMAL ->
formatNumber(field, value, searchable, searchType, jpaSearchOperatorFilter);
case INTEGER, LONG, FLOAT, DOUBLE, BIGDECIMAL -> formatNumber(field, value, searchable, searchType, jpaSearchOperatorFilter);
case ZONEDDATETIME -> GenericUtils.parseZonedDateTime(field, value, searchable.datePattern());
case UNTYPED -> throw new IllegalArgumentException();
};
Expand Down
Loading

0 comments on commit 3638e07

Please sign in to comment.