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

com.github.ydespreaux.spring.data.jpa.criteria.SpecificationCriteria Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 Yoann Despréaux
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING . If not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Please send bugreports with examples or suggestions to [email protected]
 */

package com.github.ydespreaux.spring.data.jpa.criteria;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.NonNull;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author Yoann Despréaux
 * @since 0.0.3
 */
public class SpecificationCriteria implements Specification {

    private static final long serialVersionUID = 3295157927853086841L;

    private List criteres;

    public SpecificationCriteria(List criteres) {
        this.criteres = criteres;
    }

    @Override
    public Predicate toPredicate(@NonNull Root root, @NonNull CriteriaQuery query, @NonNull CriteriaBuilder cb) {
        if (this.criteres == null) {
            return null;
        }
        List restrictions = new ArrayList<>();
        for (ICriteria criteria : this.criteres) {
            Predicate predicate = toPredicate(criteria, root, cb);
            if (predicate != null) {
                restrictions.add(predicate);
            }
        }
        if (!restrictions.isEmpty()) {
            return cb.and(restrictions.toArray(new Predicate[restrictions.size()]));
        }
        return null;
    }

    /**
     * @param criteria
     * @param root
     * @param cb
     * @return
     */
    protected Predicate toPredicate(ICriteria criteria, Root root, CriteriaBuilder cb) {
        return criteria.isGrouped() ? toPredicate((GroupCriteria) criteria, root, cb) : toPredicate((SimpleCriteria) criteria, root, cb);
    }

    /**
     * @param criteria
     * @param root
     * @param cb
     * @return
     */
    protected Predicate toPredicate(GroupCriteria criteria, Root root, CriteriaBuilder cb) {
        List restrictions = new ArrayList<>();
        for (ICriteria child : criteria.getCriteriaList()) {
            Predicate predicate = child.isGrouped() ? toPredicate((GroupCriteria) child, root, cb) : toPredicate((SimpleCriteria) child, root, cb);
            if (predicate != null) {
                restrictions.add(predicate);
            }
        }
        if (!restrictions.isEmpty()) {
            return criteria.getOperator() == Predicate.BooleanOperator.AND ?
                    cb.and(restrictions.toArray(new Predicate[restrictions.size()])) :
                    cb.or(restrictions.toArray(new Predicate[restrictions.size()]));
        }
        return null;

    }

    /**
     * @param criteria
     * @param root
     * @param cb
     * @return
     */
    protected Predicate toPredicate(SimpleCriteria criteria, Root root, CriteriaBuilder cb) {
        Path path = getPath(root, splitProperties(criteria.getProperty().getPropertyPath()));
        return toPredicate(cb, path, criteria.getValue(), criteria.getOperator());
    }

    /**
     * @param root
     * @param properties
     * @param 
     * @return
     */
    protected  Path getPath(From root, String... properties) {
        if (properties.length == 1) {
            return root.get(properties[0]);
        }
        Join join = root.join(properties[0], JoinType.LEFT);
        String[] newProperties = new String[properties.length - 1];
        System.arraycopy(properties, 1, newProperties, 0, newProperties.length);
        return getPath(join, newProperties);
    }

    /**
     * @param builder
     * @param path
     * @param value
     * @param operator
     * @param 
     * @return
     */
    private  Predicate toPredicate(CriteriaBuilder builder, Path path, V value, SimpleCriteria.EnumOperator operator) {
        assertSearchCriteriaValue(operator, value);
        switch (operator) {
            case EQ:
                return builder.equal(path, value);
            case NOT_EQ:
                return builder.notEqual(path, value);
            case NOT_NULL:
                return builder.isNotNull(path);
            case NULL:
                return builder.isNull(path);
            case BETWEEN:
                return builder.between(castPath(path, Comparable.class), ((Comparable[]) value)[0], ((Comparable[]) value)[1]);
            case GREATER_OR_EQUALS:
                return builder.greaterThanOrEqualTo(castPath(path, Comparable.class), (Comparable) value);
            case GREATER_THAN:
                return builder.greaterThan(castPath(path, Comparable.class), (Comparable) value);
            case LESS_OR_EQUALS:
                return builder.lessThanOrEqualTo(castPath(path, Comparable.class), (Comparable) value);
            case LESS_THAN:
                return builder.lessThan(castPath(path, Comparable.class), (Comparable) value);
            case IN:
                if (value instanceof Object[]) {
                    return path.in((Object[]) value);
                } else {
                    return path.in((Collection) value);
                }
            case NOT_IN:
                return builder.not(toPredicate(builder, path, value, SimpleCriteria.EnumOperator.IN));
            case CONTAINS:
            case LIKE:
                return builder.like(builder.upper(castPath(path, String.class)), "%" + ((String) value).toUpperCase() + "%");
            case END_WITH:
                return builder.like(builder.upper(castPath(path, String.class)), "%" + ((String) value).toUpperCase());
            case START_WITH:
                return builder.like(builder.upper(castPath(path, String.class)), ((String) value).toUpperCase() + "%");
        }
        return null;
    }

    /**
     * @param operator
     * @param value
     * @param 
     */
//    @SuppressWarnings("unchecked")
    private  void assertSearchCriteriaValue(SimpleCriteria.EnumOperator operator, V value) {
        if (operator != SimpleCriteria.EnumOperator.NOT_NULL && operator != SimpleCriteria.EnumOperator.NULL) {
            Objects.requireNonNull(value, "value must not be null!!");
        }
    }

    /**
     * @param path
     * @param valueType
     * @param 
     * @param 
     * @return
     */
    private  Expression castPath(Path path, Class valueType) {
        if (valueType.isAssignableFrom(path.getJavaType())) {
            return (Path) path;
        }
        throw new IllegalArgumentException(String.format("Could not convert java type [%s] to [%s]", valueType.getName(), path.getJavaType().getName()));
    }

    /**
     * @param propertyPath
     * @return
     */
    private String[] splitProperties(String propertyPath) {
        return propertyPath.split("\\.");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy