Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: add missing module-info #10

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ public class SpringQueryFilter implements ISpringQueryFilter {
private String sort;

@Override
public int getPage() {
public int getComputedPage() {
return Math.max(Optional.ofNullable(page).orElse(0), 0);
}

@Override
public int getPageSize() {
public int getComputedSize() {
return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
}

Expand Down
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [Unreleased]

### Fixed

- Add missing module-info.java.

### Removed

- Lombok dependency.

## [1.1.1] 2024/11/18

### Fixed
Expand Down
10 changes: 1 addition & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.zorin95670</groupId>
<artifactId>spring-query-filter</artifactId>
<version>1.1.1</version>
<version>1.1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-query-filter</name>
Expand Down Expand Up @@ -73,14 +73,6 @@
<scope>test</scope>
</dependency>

<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

<!-- JUnit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.github.zorin95670.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* Exception thrown to indicate an error occurred while processing query filters in the Spring Query Filter library.
* Provides additional details about the filter type, query parameter name, and the associated value,
Expand All @@ -11,8 +8,6 @@
* Extends {@link RuntimeException}, allowing users to handle or propagate this exception as needed.
* </p>
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class SpringQueryFilterException extends RuntimeException {

/**
Expand Down Expand Up @@ -98,4 +93,31 @@ public SpringQueryFilterException(final String message,
this.queryParameterName = queryParameterName;
this.queryParameterValue = queryParameterValue;
}

/**
* Gets the query filter type that caused the exception.
*
* @return the query filter type
*/
public String getQueryFilterType() {
return queryFilterType;
}

/**
* Gets the query parameter name that caused the exception.
*
* @return the query parameter name
*/
public String getQueryParameterName() {
return queryParameterName;
}

/**
* Gets the query parameter value that caused the exception.
*
* @return the query parameter value
*/
public String getQueryParameterValue() {
return queryParameterValue;
}
}
22 changes: 18 additions & 4 deletions src/main/java/io/github/zorin95670/predicate/PredicateFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -34,8 +32,6 @@ public abstract class PredicateFilter<T, Y> implements IPredicateFilter<T, Y> {
/**
* The name of the query parameter, representing the field to filter.
*/
@Getter
@Setter
private String name;

/**
Expand Down Expand Up @@ -76,6 +72,24 @@ public void setIsNotOperator(final int index, final boolean state) {
this.isNotOperators[index] = state;
}

/**
* Gets the name of the query parameter.
*
* @return the name of the query parameter
*/
public String getName() {
return name;
}

/**
* Sets the name of the query parameter.
*
* @param name the name of the query parameter
*/
public void setName(final String name) {
this.name = name;
}

/**
* Retrieves a specific value from the parameter list.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.zorin95670.predicate;

import lombok.Getter;

/**
* Enum representing different predicate operators used for query filtering.
* <p>
Expand Down Expand Up @@ -79,9 +77,17 @@ public enum PredicateOperator {
/**
* Operator value.
*/
@Getter
private final String value;

/**
* Gets the operator value.
*
* @return the operator value.
*/
public String getValue() {
return value;
}

/**
* Constructor to assign the string value to each operator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public interface ISpringQueryFilter {
*
* @return the page number (0-based index).
*/
int getPage();
int getComputedPage();

/**
* Retrieves the page size for pagination.
*
* @return the number of records per page.
*/
int getPageSize();
int getComputedPageSize();

/**
* Retrieves the field by which results should be ordered.
Expand Down Expand Up @@ -81,9 +81,9 @@ default Pageable getPageable(String defaultOrder) {
Sort sort = this.getOrderBy(defaultOrder);

if (sort != null) {
return PageRequest.of(this.getPage(), this.getPageSize(), sort);
return PageRequest.of(this.getComputedPage(), this.getComputedPageSize(), sort);
}

return PageRequest.of(this.getPage(), this.getPageSize());
return PageRequest.of(this.getComputedPage(), this.getComputedPageSize());
}
}
90 changes: 68 additions & 22 deletions src/main/java/io/github/zorin95670/query/SpringQueryFilter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package io.github.zorin95670.query;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.Optional;

/**
* Class implementing {@link ISpringQueryFilter} for managing pagination and sorting parameters in a Spring application.
* This class provides default values for pagination and handles sorting direction based on the `sort` field.
*/
@NoArgsConstructor
@AllArgsConstructor
public class SpringQueryFilter implements ISpringQueryFilter {

/**
Expand Down Expand Up @@ -41,42 +36,93 @@ public class SpringQueryFilter implements ISpringQueryFilter {
/**
* Field name by which results should be ordered. Can be set to null.
*/
private String order;
private String order = null;

/**
* Sorting direction. Should be "asc" for ascending order; any other value is treated as descending.
*/
private String sort;
private String sort = null;

/**
* Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0.
* Default no-argument constructor.
*/
public SpringQueryFilter() {
}

/**
* Constructor to initialize all properties.
*
* @return the page number, constrained to be non-negative.
* @param page the page number (0-based index).
* @param pageSize the number of records per page.
* @param order the field name by which results should be ordered.
* @param sort the sorting direction (ascending or descending).
*/
@Override
public int getPage() {
return Math.max(Optional.ofNullable(page).orElse(0), 0);
public SpringQueryFilter(final Integer page, final Integer pageSize, final String order, final String sort) {
this.page = page;
this.pageSize = pageSize;
this.order = order;
this.sort = sort;
}

/**
* Retrieves the page size for pagination, constrained between 1 and 10.
* Defaults to 1 if page size is null or less than 1.
* Returns the page number for pagination (0-based index).
* Defaults to 0 if not specified.
*
* @return the page size, bounded by a minimum of 1 and a maximum of 10.
* @return the page number (0-based index)
*/
public Integer getPage() {
return page;
}

/**
* Returns the number of records per page.
* Defaults to 1 if not specified.
*
* @return the number of records per page
*/
public Integer getPageSize() {
return pageSize;
}

/**
* Returns the field name by which results should be ordered.
* Can be set to null.
*
* @return the field name for ordering, or null if not specified
*/
public String getOrder() {
return order;
}

/**
* Returns the sorting direction.
* Should be "asc" for ascending order; any other value is treated as descending.
*
* @return the sorting direction, either "asc" for ascending or any other value for descending
*/
public String getSort() {
return sort;
}

/**
* Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0.
*
* @return the page number, constrained to be non-negative.
*/
@Override
public int getPageSize() {
return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
public int getComputedPage() {
return Math.max(Optional.ofNullable(getPage()).orElse(0), 0);
}

/**
* Retrieves the field name by which results should be ordered.
* Retrieves the page size for pagination, constrained between 1 and 10.
* Defaults to 1 if page size is null or less than 1.
*
* @return the order field, or null if not set.
* @return the page size, bounded by a minimum of 1 and a maximum of 10.
*/
@Override
public String getOrder() {
return this.order;
public int getComputedPageSize() {
return Math.clamp(Optional.ofNullable(getPageSize()).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
}

/**
Expand All @@ -86,6 +132,6 @@ public String getOrder() {
*/
@Override
public boolean isAscendantSort() {
return "asc".equalsIgnoreCase(sort);
return "asc".equalsIgnoreCase(getSort());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.domain.Specification;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -49,18 +47,15 @@ public class SpringQueryFilterSpecification<T> implements Specification<T> {
* This map is used to dynamically build the filtering predicates based on the provided values for each field.
* </p>
*/
@Setter
private Map<String, List<String>> filters;
private final Map<String, List<String>> filters;

/**
* The class of the entity to filter.
* <p>
* This class is used to reflect on the fields of the entity and apply filters accordingly.
* </p>
*/
@Getter
@Setter
private Class<T> entityClass;
private final Class<T> entityClass;

/**
* Constructs a new specification with the provided entity class and filters.
Expand All @@ -69,8 +64,26 @@ public class SpringQueryFilterSpecification<T> implements Specification<T> {
* @param filters The map of filters for field names and values.
*/
public SpringQueryFilterSpecification(final Class<T> entityClass, final Map<String, List<String>> filters) {
setEntityClass(entityClass);
setFilters(filters);
this.entityClass = entityClass;
this.filters = filters;
}

/**
* Gets the map of filters.
*
* @return the map of filters
*/
public Map<String, List<String>> getFilters() {
return filters;
}

/**
* Gets the class of the entity to filter.
*
* @return the class of the entity
*/
public Class<T> getEntityClass() {
return entityClass;
}

/**
Expand Down
Loading
Loading