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

top.openyuan.jpa.specification.handler.impl.OrLikeOrLikePredicateHandler Maven / Gradle / Ivy

package top.openyuan.jpa.specification.handler.impl;

import top.openyuan.jpa.exception.BuildSpecificationException;
import top.openyuan.jpa.specification.annotation.OrLikeOrLike;
import top.openyuan.jpa.specification.handler.AbstractPredicateHandler;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;

/**
 * 构建“或者多种模糊条件”({@code OR (field1 LIKE '%xx%' OR field2 LIKE '%yyy%')})场景的 {@link Predicate} 处理器.
 *
 * @author lzy
 * @since v1.0.0
 */
public class OrLikeOrLikePredicateHandler extends AbstractPredicateHandler {

    @Override
    public Class getAnnotation() {
        return OrLikeOrLike.class;
    }

    @Override
    public  Predicate buildPredicate(
            CriteriaBuilder criteriaBuilder, From from, String name, Object value, Annotation annotation) {
        value = value.getClass().isArray() ? Arrays.asList((Object[]) value) : value;
        if (!(value instanceof List)) {
            throw new BuildSpecificationException(
                    "【Jpa-plus 异常】对【" + name + "】使用【@OrLikeOrLike】时,属性类型不是数组或者 List 集合!");
        }

        String[] fields = ((OrLikeOrLike) annotation).fields();
        List values = (List) value;
        if (fields.length != values.size()) {
            throw new BuildSpecificationException(
                    "【Jpa-plus 异常】对【" + name + "】使用【@OrLikeOrLike】时,注解上【fields】长度和字段值的大小不同,fileds长为:【"
                            + fields.length + "】,字段值大小为:【" + values.size() + "】.");
        }

        return criteriaBuilder.or(criteriaBuilder.or(
                super.buildLikeOrLikePredicates(criteriaBuilder, from, fields, values).toArray(new Predicate[0])));
    }

    @Override
    public Predicate buildPredicate(
            CriteriaBuilder criteriaBuilder, From from, String fieldName, Object value) {
        throw new BuildSpecificationException("不支持本方法.");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy