Example usage for javax.persistence.criteria CriteriaBuilder like

List of usage examples for javax.persistence.criteria CriteriaBuilder like

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder like.

Prototype

Predicate like(Expression<String> x, String pattern);

Source Link

Document

Create a predicate for testing whether the expression satisfies the given pattern.

Usage

From source file:com.gsr.myschool.server.repos.spec.UserSpec.java

public static Specification<User> firstnameLike(final String name) {
    return new Specification<User>() {
        @Override//from  w ww.  j  a  va2s  .  c o m
        public Predicate toPredicate(Root<User> userRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String likePattern = Strings.isNullOrEmpty(name) ? "%" : "%" + name + "%";
            return cb.like(userRoot.<String>get("firstName"), likePattern);
        }
    };
}

From source file:com.gsr.myschool.server.repos.spec.UserSpec.java

public static Specification<User> lastnameLike(final String name) {
    return new Specification<User>() {
        @Override/*from   w  w w.  j av a2 s  .c o m*/
        public Predicate toPredicate(Root<User> userRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String likePattern = Strings.isNullOrEmpty(name) ? "%" : "%" + name + "%";
            return cb.like(userRoot.<String>get("lastName"), likePattern);
        }
    };
}

From source file:com.gsr.myschool.server.repos.spec.UserSpec.java

public static Specification<User> emailLike(final String mail) {
    return new Specification<User>() {
        @Override//www  . ja  v  a2 s  . com
        public Predicate toPredicate(Root<User> userRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String likePattern = Strings.isNullOrEmpty(mail) ? "%" : "%" + mail + "%";
            return cb.like(userRoot.<String>get("email"), likePattern);
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationTargetSpecification.java

private static Predicate belongsToUser(Root<AnnotationTarget> root, CriteriaBuilder cb, String username) {
    return cb.like(cb.lower(root.get(USER).get(USERNAME)), username.toLowerCase());
}

From source file:org.ngrinder.perftest.repository.TagSpecification.java

/**
 * Get the {@link Specification} to get the {@link Tag} whose value starts with given query.
 *
 * @param queryString matching tag value
 * @return {@link Specification}//from   ww  w.  j a  va2  s  .co m
 */
public static Specification<Tag> isStartWith(final String queryString) {
    return new Specification<Tag>() {
        @Override
        public Predicate toPredicate(Root<Tag> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String replacedQueryString = StringUtils.replace(queryString, "%", "\\%");
            return cb.like(cb.lower(root.get("tagValue").as(String.class)),
                    StringUtils.lowerCase(replacedQueryString) + "%");
        }
    };
}

From source file:com.netflix.genie.core.jpa.specifications.JpaSpecificationUtils.java

/**
 * Create either an equals or like predicate based on the presence of the '%' character in the search value.
 *
 * @param cb         The criteria builder to use for predicate creation
 * @param expression The expression of the field the predicate is acting on
 * @param value      The value to compare the field to
 * @return A LIKE predicate if the value contains a '%' otherwise an EQUAL predicate
 *//*from w  ww. j  a  va2 s  . c om*/
public static Predicate getStringLikeOrEqualPredicate(@NotNull final CriteriaBuilder cb,
        @NotNull final Expression<String> expression, @NotNull final String value) {
    if (StringUtils.contains(value, PERCENT)) {
        return cb.like(expression, value);
    } else {
        return cb.equal(expression, value);
    }
}

From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaSpecificationUtils.java

/**
 * Create either an equals or like predicate based on the presence of the '%' character in the search value.
 *
 * @param cb         The criteria builder to use for predicate creation
 * @param expression The expression of the field the predicate is acting on
 * @param value      The value to compare the field to
 * @return A LIKE predicate if the value contains a '%' otherwise an EQUAL predicate
 *///from  www  .  j a  v  a  2 s . co m
static Predicate getStringLikeOrEqualPredicate(@NotNull final CriteriaBuilder cb,
        @NotNull final Expression<String> expression, @NotNull final String value) {
    if (StringUtils.contains(value, PERCENT)) {
        return cb.like(expression, value);
    } else {
        return cb.equal(expression, value);
    }
}

From source file:org.ngrinder.perftest.repository.PerfTestSpecification.java

/**
 * Get the search {@link Specification} for test name and description fields.
 *
 * @param queryString query String/*from  w w w  . ja va2  s.  c o  m*/
 * @return {@link Specification}
 */
public static Specification<PerfTest> likeTestNameOrDescription(final String queryString) {
    return new Specification<PerfTest>() {
        @Override
        public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String queryStr = ("%" + queryString + "%").toLowerCase();
            return cb.or(cb.like(cb.lower(root.get("testName").as(String.class)), queryStr),
                    cb.like(root.get("description").as(String.class), queryStr));
        }
    };
}

From source file:com.netflix.genie.server.repository.jpa.JobSpecs.java

/**
 * Find jobs based on the parameters./*from   w  ww  . j av a 2 s .c o  m*/
 *
 * @param id          The job id
 * @param jobName     The job name
 * @param userName    The user who created the job
 * @param statuses    The job statuses
 * @param tags        The tags for the jobs to find
 * @param clusterName The cluster name
 * @param clusterId   The cluster id
 * @param commandName The command name
 * @param commandId   The command id
 * @return The specification
 */
public static Specification<Job> find(final String id, final String jobName, final String userName,
        final Set<JobStatus> statuses, final Set<String> tags, final String clusterName, final String clusterId,
        final String commandName, final String commandId) {
    return new Specification<Job>() {
        @Override
        public Predicate toPredicate(final Root<Job> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(id)) {
                predicates.add(cb.like(root.get(Job_.id), id));
            }
            if (StringUtils.isNotBlank(jobName)) {
                predicates.add(cb.like(root.get(Job_.name), jobName));
            }
            if (StringUtils.isNotBlank(userName)) {
                predicates.add(cb.equal(root.get(Job_.user), userName));
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final JobStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Job_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Job_.tags)));
                    }
                }
            }
            if (StringUtils.isNotBlank(clusterName)) {
                predicates.add(cb.equal(root.get(Job_.executionClusterName), clusterName));
            }
            if (StringUtils.isNotBlank(clusterId)) {
                predicates.add(cb.equal(root.get(Job_.executionClusterId), clusterId));
            }
            if (StringUtils.isNotBlank(commandName)) {
                predicates.add(cb.equal(root.get(Job_.commandName), commandName));
            }
            if (StringUtils.isNotBlank(commandId)) {
                predicates.add(cb.equal(root.get(Job_.commandId), commandId));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

From source file:com.netflix.genie.server.repository.jpa.ClusterSpecs.java

/**
 * Generate a specification given the parameters.
 *
 * @param name          The name of the cluster to find
 * @param statuses      The statuses of the clusters to find
 * @param tags          The tags of the clusters to find
 * @param minUpdateTime The minimum updated time of the clusters to find
 * @param maxUpdateTime The maximum updated time of the clusters to find
 * @return The specification/* www. j  ava2  s .  c  om*/
 */
public static Specification<Cluster> find(final String name, final Set<ClusterStatus> statuses,
        final Set<String> tags, final Long minUpdateTime, final Long maxUpdateTime) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.like(root.get(Cluster_.name), name));
            }
            if (minUpdateTime != null) {
                predicates.add(cb.greaterThanOrEqualTo(root.get(Cluster_.updated), new Date(minUpdateTime)));
            }
            if (maxUpdateTime != null) {
                predicates.add(cb.lessThan(root.get(Cluster_.updated), new Date(maxUpdateTime)));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Cluster_.tags)));
                    }
                }
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final ClusterStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Cluster_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}