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

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

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

import top.openyuan.jpa.specification.annotation.NotIn;
import top.openyuan.jpa.specification.handler.AbstractPredicateHandler;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import top.openyuan.jpa.specification.predicate.AbstractSimplePredicateExt;

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.Collection;

/**
 * 构建“范围不匹配条件”({@code field NOT IN ('xxx', 'yyy')})场景的 {@link Predicate} 处理器.
 *
 * @author lzy
 * @since v1.0.0
 */
public class NotInPredicateHandler extends AbstractPredicateHandler {

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

    @Override
    public  Predicate buildPredicate(
            CriteriaBuilder criteriaBuilder, From from, String fieldName, Object value, Annotation annotation) {
        return this.buildPredicate(criteriaBuilder, from, fieldName, value);
    }

    @Override
    public Predicate buildPredicate(
            CriteriaBuilder criteriaBuilder, From from, String fieldName, Object value) {
        CriteriaBuilder.In in = criteriaBuilder.in(from.get(fieldName));
        value = value.getClass().isArray() ? Arrays.asList((Object[]) value) : value;

        if (value instanceof Collection) {
            Collection list = (Collection) value;
            if (list.isEmpty()) {
                return new AbstractSimplePredicateExt(
                        (CriteriaBuilderImpl) criteriaBuilder, true, Predicate.BooleanOperator.AND);
            } else {
                list.forEach(in::value);
            }
        } else {
            in.value(value);
        }
        return criteriaBuilder.and(criteriaBuilder.not(in));
    }

}