forked from plusone/plusone-commons
修改 API,也使 PagingAndSortingQueryParams 支持 Gson。
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
|
||||
/**
|
||||
* 分页排序查询参数
|
||||
*
|
||||
* <p>
|
||||
* 根据传入的 {@code size} 和 {@code pageNum},
|
||||
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @see PageResult
|
||||
*/
|
||||
public class PagingAndSortingQueryParams {
|
||||
|
||||
private static final int DEFAULT_PAGE_SIZE = 15;
|
||||
|
||||
private Integer size;
|
||||
private Long pageNum;
|
||||
private List<String> orderBy;
|
||||
|
||||
private static final Pattern sortStrPattern = Pattern.compile("^[a-zA-Z]\\w+-(desc|asc|DESC|ASC)$");
|
||||
|
||||
private final Map<String, String> sortableProperties;
|
||||
|
||||
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
|
||||
Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
|
||||
"Sortable properties can not be empty.");
|
||||
sortableProperties.forEach((k, v) ->
|
||||
Preconditions.checkArgument(StringUtils.isNotBlank(k) && StringUtils.isNotBlank(v),
|
||||
"Property name must not be blank."));
|
||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
public final void setOrderBy(@Nullable List<String> orderBy) {
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
public final void setSize(@Nullable Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public final void setPageNum(@Nullable Long pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
// Setters end
|
||||
|
||||
public final PagingParams buildPagingParams() {
|
||||
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
|
||||
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
|
||||
final List<SortableProperty> propertiesToSort = this.orderBy.stream().map(this::generateSortableProperty)
|
||||
.collect(Collectors.toList());
|
||||
return new PagingParams(sizeValue, pageNumValue, propertiesToSort);
|
||||
}
|
||||
|
||||
@Virtual
|
||||
protected int defaultSizeInternal() {
|
||||
return DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PagingAndSortingQueryParams ["
|
||||
+ "size=" + size
|
||||
+ ", pageNum=" + pageNum
|
||||
+ ", orderBy=" + orderBy
|
||||
+ ", sortableProperties=" + sortableProperties
|
||||
+ "]";
|
||||
}
|
||||
|
||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||
Preconditions.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
|
||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||
Preconditions.checkArgument(propertyNameAndOrderType.length == 2);
|
||||
|
||||
String propertyName = propertyNameAndOrderType[0];
|
||||
Preconditions.checkArgument(sortableProperties.containsKey(propertyName),
|
||||
"The property name must be in the set of sortable properties.");
|
||||
String columnName = sortableProperties.get(propertyName);
|
||||
String orderType = propertyNameAndOrderType[1];
|
||||
return new SortableProperty(propertyName, columnName, orderType);
|
||||
}
|
||||
|
||||
public static final class SortableProperty {
|
||||
private final String propertyName;
|
||||
private final String columnName;
|
||||
private final String orderType;
|
||||
|
||||
private final String sqlSnippet;
|
||||
|
||||
SortableProperty(String propertyName, String columnName, String orderType) {
|
||||
this.propertyName = propertyName;
|
||||
this.columnName = columnName;
|
||||
Preconditions.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||
this.orderType = orderType.toUpperCase();
|
||||
|
||||
this.sqlSnippet = this.propertyName + " " + this.orderType;
|
||||
}
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public String getOrderType() {
|
||||
return orderType;
|
||||
}
|
||||
|
||||
public String getSqlSnippet() {
|
||||
return sqlSnippet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SortableProperty ["
|
||||
+ "propertyName=" + propertyName
|
||||
+ ", columnName=" + columnName
|
||||
+ ", orderType=" + orderType
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams.SortableProperty;
|
||||
|
||||
public class PagingParams {
|
||||
|
||||
private final int size;
|
||||
private final long pageNum;
|
||||
private final long offset;
|
||||
private final List<SortableProperty> orderBy;
|
||||
|
||||
PagingParams(int size, long pageNum, List<SortableProperty> orderBy) {
|
||||
this.size = size;
|
||||
this.pageNum = pageNum;
|
||||
this.offset = (pageNum - 1) * size;
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
public final List<SortableProperty> getOrderBy() {
|
||||
return Collections.unmodifiableList(this.orderBy);
|
||||
}
|
||||
|
||||
public final int getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
public final long getPageNum() {
|
||||
return this.pageNum;
|
||||
}
|
||||
|
||||
public final long getOffset() {
|
||||
return this.offset;
|
||||
}
|
||||
|
||||
// Getters end
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageInfo [size=" + size + ", pageNum=" + pageNum + ", orderBy=" + orderBy + ", offset="
|
||||
+ getOffset() + "]";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user