Example usage for javax.persistence.criteria CriteriaBuilder equal

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

Introduction

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

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

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

/**
 * Get all the clusters given the specified parameters.
 *
 * @param commandId The id of the command that is registered with this cluster
 * @param statuses The status of the cluster
 * @return The specification//from   www. j  a  v  a2  s.  co  m
 */
public static Specification<Cluster> findClustersForCommand(final String commandId,
        final Set<ClusterStatus> statuses) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            final Join<Cluster, Command> commands = root.join(Cluster_.commands);

            predicates.add(cb.equal(commands.get(Command_.id), commandId));

            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()]));
        }
    };
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<PayOrder> root,
        PayOrderQueryCommand command) {//from  w ww .ja v  a 2  s.  c  om
    List<Predicate> ps = Lists.newArrayListWithCapacity(3);
    if (command.getCustomer() != null) {
        ps.add(cb.equal(root.get(PayOrder_.customer), command.getCustomer()));
    }
    if (command.getPayDateStart() != null) {
        ps.add(cb.greaterThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    if (command.getPayDateEnd() != null) {
        ps.add(cb.lessThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

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

private static Predicate targetstarget(Root<Annotation> root, CriteriaBuilder cb, Long target) {
    return cb.equal(root.get(TARGET).get(ID), target);
}

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

/**
 * Get all the clusters given the specified parameters.
 *
 * @param clusterCriteria The cluster criteria
 * @param commandCriteria The command Criteria
 * @return The specification//from  w ww. j  a  v  a2s .  c  o m
 */
public static Specification<ClusterEntity> findByClusterAndCommandCriteria(
        final ClusterCriteria clusterCriteria, final Set<String> commandCriteria) {
    return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<ClusterEntity, CommandEntity> commands = root.join(ClusterEntity_.commands);

        cq.distinct(true);

        predicates.add(cb.equal(root.get(ClusterEntity_.status), ClusterStatus.UP));

        if (clusterCriteria != null && clusterCriteria.getTags() != null
                && !clusterCriteria.getTags().isEmpty()) {
            predicates.add(cb.like(root.get(ClusterEntity_.tags),
                    JpaSpecificationUtils.getTagLikeString(clusterCriteria.getTags())));
        }

        predicates.add(cb.equal(commands.get(CommandEntity_.status), CommandStatus.ACTIVE));

        if (commandCriteria != null && !commandCriteria.isEmpty()) {
            predicates.add(cb.like(commands.get(CommandEntity_.tags),
                    JpaSpecificationUtils.getTagLikeString(commandCriteria)));
        }

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

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

/**
 * Get all the clusters given the specified parameters.
 *
 * @param clusterCriteria The cluster criteria
 * @param commandCriteria The command Criteria
 * @return The specification/*from   ww w .  ja va2 s .  c o m*/
 */
public static Specification<Cluster> findByClusterAndCommandCriteria(final ClusterCriteria clusterCriteria,
        final Set<String> commandCriteria) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            final Join<Cluster, Command> commands = root.join(Cluster_.commands);

            cq.distinct(true);

            predicates.add(cb.equal(commands.get(Command_.status), CommandStatus.ACTIVE));
            predicates.add(cb.equal(root.get(Cluster_.status), ClusterStatus.UP));

            if (commandCriteria != null) {
                for (final String tag : commandCriteria) {
                    predicates.add(cb.isMember(tag, commands.get(Command_.tags)));

                }
            }

            if (clusterCriteria != null) {
                for (final String tag : clusterCriteria.getTags()) {
                    predicates.add(cb.isMember(tag, root.get(Cluster_.tags)));
                }
            }

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

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from w  w  w  .  j a  v  a  2  s.co m
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

public static <E> Predicate stringPredicate(Expression<String> path, Object attrValue,
        final SearchParameters sp, final CriteriaBuilder builder) {
    if (sp.isCaseInsensitive()) {
        path = builder.lower(path);/*  ww w . ja  v a  2 s .  co m*/
        attrValue = ((String) attrValue).toLowerCase(Locale.ENGLISH);
    }

    switch (sp.getSearchMode()) {
    case EQUALS:
        return builder.equal(path, attrValue);
    case ENDING_LIKE:
        return builder.like(path, "%" + attrValue);
    case STARTING_LIKE:
        return builder.like(path, attrValue + "%");
    case ANYWHERE:
        return builder.like(path, "%" + attrValue + "%");
    case LIKE:
        return builder.like(path, (String) attrValue); // assume user
                                                       // provide the wild
                                                       // cards
    default:
        throw new IllegalStateException("expecting a search mode!");
    }
}

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

private static Predicate buildIdSpec(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester,
        Long id) {//from w  ww.j  a v  a 2s  . co m
    final Predicate authPredicate = authFilter(root, cb, requester);
    final Predicate idPredicate = cb.equal(root.get(ID), id);
    final Predicate[] predicates = new Predicate[] { authPredicate, idPredicate };
    return cb.and(predicates);
}

From source file:br.nom.abdon.gastoso.dal.FatosDao.java

protected static void buildQuery(final CriteriaBuilder cb, final FiltroFatos filtroFatos,
        final Path<Fato> fatoPath, final List<Predicate> where, final Map<String, Object> params) {

    final Fato fato = filtroFatos.getFato();
    if (fato != null) {
        final ParameterExpression<Fato> fatoParameter = cb.parameter(Fato.class, "fato");

        where.add(cb.equal(fatoParameter, fatoPath));
        params.put("fato", fato);

    }/*ww w . j av  a 2s  .  c om*/

    final LocalDate dataMaxima = filtroFatos.getDataMaxima();
    if (dataMaxima != null) {
        final Path<LocalDate> diaPath = fatoPath.get("dia");

        final ParameterExpression<LocalDate> dataMaximaParameter = cb.parameter(LocalDate.class, "dataMaxima");

        final Predicate menorOuIgualDataMaxima = cb.lessThanOrEqualTo(diaPath, dataMaximaParameter);

        where.add(menorOuIgualDataMaxima);
        params.put("dataMaxima", dataMaxima);
    }

    final LocalDate dataMinima = filtroFatos.getDataMinima();
    if (dataMinima != null) {
        final Path<LocalDate> diaPath = fatoPath.get("dia");

        final ParameterExpression<LocalDate> dataMinimaParameter = cb.parameter(LocalDate.class, "dataMinima");

        final Predicate maiorOuIgualQueDataMinima = cb.greaterThanOrEqualTo(diaPath, dataMinimaParameter);

        where.add(maiorOuIgualQueDataMinima);
        params.put("dataMinima", dataMinima);
    }
}

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

private static Predicate buildParentSpec(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester,
        Long id, Boolean showRedacted) {
    final Predicate authPredicate = authFilter(root, cb, requester);
    final Predicate idPredicate = cb.equal(root.get(PARENT).get(ID), id);
    Predicate[] predicates;/*from  ww w .j  a  v  a 2 s  .  co  m*/
    if (showRedacted) {
        predicates = new Predicate[] { authPredicate, idPredicate };
    } else {
        predicates = new Predicate[] { authPredicate, idPredicate, notRedacted(root, cb) };
    }
    return cb.and(predicates);
}