Example usage for javax.persistence.criteria CriteriaBuilder parameter

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

Introduction

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

Prototype

<T> ParameterExpression<T> parameter(Class<T> paramClass, String name);

Source Link

Document

Create a parameter expression with the given name.

Usage

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);

    }//from w  w  w  . j  a  v  a2  s.c o m

    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:sf.net.experimaestro.scheduler.Resource.java

/**
 * Get a resource by locator//from  ww  w .  ja  v a 2  s.  c o  m
 *
 * @param em   The current entity manager
 * @param path The path of the resource
 * @return The resource or null if there is no such resource
 */
public static Resource getByLocator(EntityManager em, String path) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Resource> cq = cb.createQuery(Resource.class);
    Root<Resource> root = cq.from(Resource.class);
    cq.where(root.get("locator").in(cb.parameter(String.class, "locator")));

    TypedQuery<Resource> query = em.createQuery(cq);
    query.setParameter("locator", path);
    List<Resource> result = query.getResultList();
    assert result.size() <= 1;

    if (result.isEmpty())
        return null;

    return result.get(0);
}

From source file:org.jasig.jpa.BaseJpaDao.java

protected final <T> ParameterExpression<T> createParameterExpression(Class<T> paramClass, String name) {
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();

    return criteriaBuilder.parameter(paramClass, name);
}

From source file:com.expressui.sample.view.role.RoleQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Role> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(name)) {
        ParameterExpression<String> p = builder.parameter(String.class, "name");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p));
    }//from w  w  w .  j  a  va 2 s . co  m

    if (!isEmpty(doesNotBelongToUser)) {
        ParameterExpression<User> p = builder.parameter(User.class, "doesNotBelongToUser");
        Join join = rootEntity.join("userRoles", JoinType.LEFT);
        criteria.add(builder.or(builder.notEqual(join.get("user"), p), builder.isNull(join.get("user"))));
    }

    return criteria;
}

From source file:com.expressui.sample.view.user.UserQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<User> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(loginName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "loginName");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("loginName")), p));
    }/*from   w  ww . j av  a2  s.  c  o m*/

    if (!isEmpty(doesNotBelongToRole)) {
        ParameterExpression<Role> p = builder.parameter(Role.class, "doesNotBelongToRole");
        Join join = rootEntity.join("userRoles", JoinType.LEFT);
        criteria.add(builder.or(builder.notEqual(join.get("role"), p), builder.isNull(join.get("role"))));
    }

    return criteria;
}

From source file:com.expressui.core.dao.security.query.RelatedRolesQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Role> query, Root<Role> role) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(user)) {
        ParameterExpression<User> userExp = builder.parameter(User.class, "user");
        predicates.add(builder.equal(role.join("userRoles").get("user"), userExp));
    }//from w  w  w.  j  a v  a2s . c o m

    return predicates;
}

From source file:com.expressui.sample.view.opportunity.OpportunityQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Opportunity> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(accountName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "accountName");
        criteria.add(builder.like(builder.upper(rootEntity.get("account").<String>get("name")), p));
    }//from  ww  w.  j a  v a 2  s  .  co  m
    if (!isEmpty(salesStages)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "salesStages");
        criteria.add(builder.in(rootEntity.get("salesStage")).value(p));
    }

    return criteria;
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.PersistentDocumentStoreController.java

/**
 * Gets all UUIDs for {@code T} type document stores owned by the given owner.
 * @param em the {@link EntityManager} to use.
 * @param ownerId the ID of the owner.//from   www.ja va2s. com
 * @param entityClass the specific type of document stores to be retrieved; must be annotated with the {@link Entity} annotation.
 * @return a {@link List} containing {@link String} representations of the UUIDs.
 */
public <T extends PersistentDocumentStore> List<String> getAllUuids(EntityManager em, String ownerId,
        Class<T> entityClass) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(ownerId, CannedMessages.NULL_ARGUMENT, "ownerId");
    Validate.notNull(entityClass, CannedMessages.NULL_ARGUMENT, "entityClass");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class);
    Root<T> store = cq.from(entityClass);
    cq.multiselect(store.get("id"))
            .where(cb.equal(store.get("ownerId"), cb.parameter(String.class, "ownerId")));
    TypedQuery<byte[]> tq = em.createQuery(cq);
    tq.setParameter("ownerId", ownerId);
    return Lists.newArrayList(Iterables.transform(tq.getResultList(), UuidUtils.uuidBytesToStringFunction()));
}

From source file:com.expressui.sample.view.account.AccountQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Account> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(name)) {
        ParameterExpression<String> p = builder.parameter(String.class, "name");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p));
    }/*from ww w  .  j  av a  2s.c  om*/
    if (!isEmpty(states)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "states");
        criteria.add(builder.in(rootEntity.get("billingAddress").get("state")).value(p));
    }
    if (!isEmpty(country)) {
        ParameterExpression<Country> p = builder.parameter(Country.class, "country");
        criteria.add(builder.equal(rootEntity.get("billingAddress").get("country"), p));
    }

    return criteria;
}

From source file:com.expressui.sample.view.contact.ContactQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Contact> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(lastName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "lastName");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("lastName")), p));
    }/*from w ww.  ja  v  a 2 s .c o  m*/
    if (!isEmpty(states)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "states");
        criteria.add(builder.in(rootEntity.get("mailingAddress").get("state")).value(p));
    }
    if (!isEmpty(country)) {
        ParameterExpression<Country> p = builder.parameter(Country.class, "country");
        criteria.add(builder.equal(rootEntity.get("mailingAddress").get("country"), p));
    }

    return criteria;
}