All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.util.SearchableConvertUtils Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show 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);
            }
        }
    }

    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 list = new ArrayList<>();
            if (isCollection) {
                list.addAll((Collection) value);
            } else {
                list.addAll(CollectionUtils.arrayToList(value));
            }
            int length = list.size();
            for (int i = 0; i < length; i++) {
                list.set(i, getConvertedValue(beanWrapper, searchProperty, entityProperty, list.get(i)));
            }
            newValue = list;
        } else {
            newValue = getConvertedValue(beanWrapper, searchProperty, entityProperty, value);
        }
        condition.setValue(newValue);
    }

    private static Object getConvertedValue(
            final BeanWrapperImpl beanWrapper,
            final String searchProperty,
            final String entityProperty,
            final Object value) {

        Object newValue;
        try {
            beanWrapper.setPropertyValue(entityProperty, value);
            newValue = beanWrapper.getPropertyValue(entityProperty);
        } catch (InvalidPropertyException e) {
            throw new InvalidSearchPropertyException(searchProperty, entityProperty, e);
        } catch (Exception e) {
            throw new InvalidSearchValueException(searchProperty, entityProperty, value, e);
        }

        return newValue;
    }
}