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

cn.hiboot.mcn.autoconfigure.jpa.ExampleSpecification Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
package cn.hiboot.mcn.autoconfigure.jpa;

import cn.hiboot.mcn.core.util.McnUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.query.EscapeCharacter;
import org.springframework.lang.NonNull;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * ExampleSpecification
 *
 * @author DingHao
 * @since 2022/1/22 11:25
 */
public class ExampleSpecification implements Specification {

    private final PredicateProvider[] predicateProviders;
    private Example example;
    private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
    private boolean isOr;

    @SafeVarargs
    ExampleSpecification(PredicateProvider... predicateProviders) {
        this.predicateProviders = predicateProviders;
    }

    @SafeVarargs
    ExampleSpecification(T bean, PredicateProvider... predicateProviders) {
        this(predicateProviders);
        if(!McnUtils.isFieldAllNull(bean)){
            this.example = Example.of(bean);
        }
    }

    ExampleSpecification or() {
        this.isOr = true;
        return this;
    }

    public ExampleSpecification escapeCharacter(EscapeCharacter escapeCharacter) {
        this.escapeCharacter = escapeCharacter;
        return this;
    }

    @Override
    public Predicate toPredicate(@NonNull Root root, @NonNull CriteriaQuery query, @NonNull CriteriaBuilder criteriaBuilder) {
        if(example == null && McnUtils.isNullOrEmpty(predicateProviders)){
            return null;
        }
        Predicate beanPredicate = null;
        if(example != null){
            beanPredicate = criteriaBuilder.and(QueryByExamplePredicateBuilder.getPredicate(root, criteriaBuilder, example, escapeCharacter));
        }
        Predicate[] predicates =  predicates(predicateProviders,root,criteriaBuilder);
        Predicate logicPredicate = null;
        if(McnUtils.isNotNullAndEmpty(predicates)){
            logicPredicate = isOr ? criteriaBuilder.or(predicates) : criteriaBuilder.and(predicates);
        }
        if(beanPredicate == null && logicPredicate == null){
            return null;
        }
        if(beanPredicate == null){
            return logicPredicate;
        }
        if(logicPredicate == null){
            return beanPredicate;
        }
        return isOr ? criteriaBuilder.or(beanPredicate,logicPredicate) : criteriaBuilder.and(beanPredicate,logicPredicate);
    }

    private Predicate[] predicates(PredicateProvider[] predicateProviders,Root root, CriteriaBuilder criteriaBuilder){
        if(McnUtils.isNullOrEmpty(predicateProviders)){
            return null;
        }
        List predicates =  new ArrayList<>(predicateProviders.length);
        for (PredicateProvider predicateProvider : predicateProviders) {
            Predicate predicate = predicateProvider.getPredicate(root, criteriaBuilder);
            if(predicate == null){
                continue;
            }
            predicates.add(predicate);
        }
        return predicates.toArray(new Predicate[0]);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy