com.kasinf.framework.rest.util.SearchableConvertUtils Maven / Gradle / Ivy
The newest version!
package com.kasinf.framework.rest.util;
import com.kasinf.framework.rest.exception.InvalidSearchPropertyException;
import com.kasinf.framework.rest.exception.InvalidSearchValueException;
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 org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author 凯鸿
* 查询条件与实体类转换工具类
*/
public final class SearchableConvertUtils {
private static volatile ConversionService conversionService;
public SearchableConvertUtils(ConversionService conversionService) {
SearchableConvertUtils.conversionService = conversionService;
}
/**
* @param search 查询条件
* @param entityClass 实体类型
* @param 实体类型
*/
public static void convertSearchValueToEntityValue(final Searchable search, final Class entityClass) {
if (search.isConverted()) {
return;
}
Collection searchFilters = search.getSearchFilters();
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(entityClass);
beanWrapper.setAutoGrowNestedPaths(true);
beanWrapper.setConversionService(conversionService);
searchFilters.forEach(searchFilter -> convertSearchValueToEntityValue(beanWrapper, searchFilter));
}
private static void convertSearchValueToEntityValue(BeanWrapperImpl beanWrapper, SearchFilter searchFilter) {
if (searchFilter instanceof Condition) {
Condition condition = (Condition) searchFilter;
convert(beanWrapper, condition);
return;
}
if (searchFilter instanceof OrCondition) {
List orFilters = ((OrCondition) searchFilter).getOrFilters();
for (SearchFilter orFilter : orFilters) {
convertSearchValueToEntityValue(beanWrapper, orFilter);
}
return;
}
if (searchFilter instanceof AndCondition) {
for (SearchFilter andFilter : ((AndCondition) searchFilter).getAndFilters()) {
convertSearchValueToEntityValue(beanWrapper, andFilter);
}
}
}
@SuppressWarnings("all")
private static void convert(BeanWrapperImpl beanWrapper, Condition condition) {
String searchProperty = condition.getSearchProperty();
//自定义的也不转换
if (condition.getOperator() == SearchOperator.custom) {
return;
}
//一元运算符不需要计算
if (condition.isUnaryFilter()) {
return;
}
String entityProperty = condition.getEntityProperty();
Object value = condition.getValue();
if (value instanceof String && ((String) value).contains(",")) {
value = ((String) value).split(",");
}
Object newValue;
boolean isCollection = value instanceof Collection;
boolean isArray = value != null && value.getClass().isArray();
if (isCollection || isArray) {
List