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

com.github.mikesafonov.specification.builder.starter.predicates.LikePredicateBuilder Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package com.github.mikesafonov.specification.builder.starter.predicates;

import com.github.mikesafonov.specification.builder.starter.annotations.Like;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;

/**
 * Builder for {@code like} predicate
 *
 * @author MikeSafonov
 */
public class LikePredicateBuilder extends SimplePredicateBuilder {

    private static final String PERCENT = "%";

    private final CriteriaBuilder cb;
    private final Like like;
    private final Object fieldValue;

    public LikePredicateBuilder(CriteriaBuilder cb, Like like, Object fieldValue, Expression expression) {
        super(expression);
        this.cb = cb;
        this.like = like;
        this.fieldValue = fieldValue;
    }


    @Override
    public Predicate build() {
        String searchValue = getSearchValue();
        Expression expr = expression.as(String.class);
        if (!like.caseSensitive()) {
            expr = cb.upper(expr);
            searchValue = searchValue.toUpperCase();
        }
        return cb.like(expr, searchValue);
    }

    private String getSearchValue() {
        switch (like.direction()) {
            case LEFT:
                return PERCENT + fieldValue;
            case RIGHT:
                return fieldValue + PERCENT;
            default:
                return PERCENT + fieldValue + PERCENT;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy