com.kasinf.framework.rest.repository.callback.DefaultSearchCallback Maven / Gradle / Ivy
package com.kasinf.framework.rest.repository.callback;
import com.kasinf.framework.rest.support.filter.AndCondition;
import com.kasinf.framework.rest.support.filter.Condition;
import com.kasinf.framework.rest.support.filter.OrCondition;
import com.kasinf.framework.rest.support.filter.SearchFilter;
import com.kasinf.framework.rest.support.SearchOperator;
import com.kasinf.framework.rest.support.Searchable;
import lombok.Getter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import javax.persistence.Query;
import java.util.List;
/**
* @author lkhsh
* 默认过滤条件组装,将{@link com.kasinf.framework.rest.support.Searchable} 转换为对应的 HQL
*/
@Getter
public class DefaultSearchCallback implements SearchCallback {
private static final String PARAM_PREFIX = "param_";
private String alias;
private String aliasWithDot;
public DefaultSearchCallback() {
this("");
}
public DefaultSearchCallback(String alias) {
this.alias = alias;
if (!StringUtils.isEmpty(alias)) {
this.aliasWithDot = alias + ".";
} else {
this.aliasWithDot = "";
}
}
@Override
public void prepareQl(StringBuilder ql, Searchable search) {
if (!search.hasSearchFilter()) {
return;
}
int paramIndex = 1;
for (SearchFilter searchFilter : search.getSearchFilters()) {
if (searchFilter instanceof Condition) {
Condition condition = (Condition) searchFilter;
if (condition.getOperator() == SearchOperator.custom) {
continue;
}
}
ql.append(" and ");
paramIndex = genCondition(ql, paramIndex, searchFilter);
}
}
/**
* 组装HQL语句
*
* @param ql hql
* @param paramIndex 参数索引
* @param searchFilter 过滤条件
* @return 参数索引
*/
private int genCondition(StringBuilder ql, int paramIndex, SearchFilter searchFilter) {
boolean needAppendBracket = searchFilter instanceof OrCondition || searchFilter instanceof AndCondition;
if (needAppendBracket) {
ql.append("(");
}
if (searchFilter instanceof Condition) {
Condition condition = (Condition) searchFilter;
//自定义条件
String entityProperty = condition.getEntityProperty();
// 实体名称
ql.append(getAliasWithDot());
ql.append(entityProperty);
//操作符
//1、如果是自定义查询符号,则使用SearchPropertyMappings中定义的默认的操作符
ql.append(" ");
ql.append(condition.getOperatorStr());
if (!condition.isUnaryFilter()) {
ql.append(" :");
ql.append(PARAM_PREFIX);
ql.append(paramIndex++);
return paramIndex;
}
} else if (searchFilter instanceof OrCondition) {
boolean isFirst = true;
List orFilters = ((OrCondition) searchFilter).getOrFilters();
for (SearchFilter orSearchFilter : orFilters) {
if (!isFirst) {
ql.append(" or ");
}
paramIndex = genCondition(ql, paramIndex, orSearchFilter);
isFirst = false;
}
} else if (searchFilter instanceof AndCondition) {
boolean isFirst = true;
List andFilters = ((AndCondition) searchFilter).getAndFilters();
for (SearchFilter andSearchFilter : andFilters) {
if (!isFirst) {
ql.append(" and ");
}
paramIndex = genCondition(ql, paramIndex, andSearchFilter);
isFirst = false;
}
}
if (needAppendBracket) {
ql.append(")");
}
return paramIndex;
}
@Override
public void prepareOrder(StringBuilder ql, Searchable search) {
if (search.hashSort()) {
ql.append(" order by ");
Sort sort = search.getSort();
for (Sort.Order order : sort) {
ql.append(String.format("%s%s %s, ", getAliasWithDot(), order.getProperty(), order.getDirection().name().toLowerCase()));
}
ql.delete(ql.length() - 2, ql.length());
}
}
@Override
public void setValues(Query query, Searchable search) {
int paramIndex = 1;
for (SearchFilter searchFilter : search.getSearchFilters()) {
paramIndex = setValues(query, searchFilter, paramIndex);
}
}
/**
* 设置查询语句的值
*
* @param query 查询
* @param searchFilter 过滤条件
* @param paramIndex 参数索引
* @return 索引值
*/
private int setValues(Query query, SearchFilter searchFilter, int paramIndex) {
if (searchFilter instanceof Condition) {
Condition condition = (Condition) searchFilter;
if (condition.getOperator() == SearchOperator.custom) {
return paramIndex;
}
if (condition.isUnaryFilter()) {
return paramIndex;
}
query.setParameter(PARAM_PREFIX + paramIndex++, formatValue(condition, condition.getValue()));
} else if (searchFilter instanceof OrCondition) {
for (SearchFilter orSearchFilter : ((OrCondition) searchFilter).getOrFilters()) {
paramIndex = setValues(query, orSearchFilter, paramIndex);
}
} else if (searchFilter instanceof AndCondition) {
for (SearchFilter andSearchFilter : ((AndCondition) searchFilter).getAndFilters()) {
paramIndex = setValues(query, andSearchFilter, paramIndex);
}
}
return paramIndex;
}
/**
* 格式化value,比如like查询,需要加%
*
* @param condition 条件
* @param value 值
* @return 处理后的值
*/
private Object formatValue(Condition condition, Object value) {
SearchOperator operator = condition.getOperator();
if (operator == SearchOperator.like || operator == SearchOperator.notLike) {
return "%" + value + "%";
}else if (operator == SearchOperator.start || operator == SearchOperator.notStart) {
return value + "%";
}else if (operator == SearchOperator.end || operator == SearchOperator.notEnd) {
return "%" + value;
}
return value;
}
@Override
public void setPageable(Query query, Searchable search) {
if (search.hasPageable()) {
Pageable pageable = search.getPage();
query.setFirstResult(new Long(pageable.getOffset()).intValue());
query.setMaxResults(pageable.getPageSize());
}
}
}