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

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

package top.openyuan.jpa.specification.handler.impl;
import top.openyuan.jpa.specification.annotation.LikeIn;
import top.openyuan.jpa.specification.handler.AbstractPredicateHandler;
import top.openyuan.jpa.common.util.CollectionUtils;

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

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

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

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

    @SuppressWarnings("unchecked")
    @Override
    public Predicate buildPredicate(
            CriteriaBuilder criteriaBuilder, From from, String fieldName, Object value) {
        Predicate p = null;
        List list = (List) value;
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0, len = list.size(); i < len; i++) {
                Object v = list.get(i);
                String pattern = "%" + String.valueOf(v).replace("%", "\\%") + "%";
                if (i == 0) {
                    p = criteriaBuilder.like(from.get(fieldName), pattern);
                } else {
                    p = criteriaBuilder.or(criteriaBuilder.like(from.get(fieldName), pattern), p);
                }
            }
        }
        return criteriaBuilder.and(p);
    }

}