Example usage for javax.persistence.criteria CriteriaBuilder createQuery

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

Introduction

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

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:net.shopxx.dao.impl.OrderDaoImpl.java

public List<Order> findList(Order.Type type, Order.Status status, Member member, Goods goods,
        Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint,
        Boolean isAllocatedStock, Boolean hasExpired, Integer count, List<Filter> filters,
        List<net.shopxx.Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/* w  w w .  j a va2  s .  c om*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (type != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type));
    }
    if (status != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), status));
    }
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (goods != null) {
        Subquery<Product> productSubquery = criteriaQuery.subquery(Product.class);
        Root<Product> productSubqueryRoot = productSubquery.from(Product.class);
        productSubquery.select(productSubqueryRoot);
        productSubquery.where(criteriaBuilder.equal(productSubqueryRoot.get("goods"), goods));

        Subquery<OrderItem> orderItemSubquery = criteriaQuery.subquery(OrderItem.class);
        Root<OrderItem> orderItemSubqueryRoot = orderItemSubquery.from(OrderItem.class);
        orderItemSubquery.select(orderItemSubqueryRoot);
        orderItemSubquery.where(criteriaBuilder.equal(orderItemSubqueryRoot.get("order"), root),
                criteriaBuilder.in(orderItemSubqueryRoot.get("product")).value(productSubquery));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderItemSubquery));
    }
    if (isPendingReceive != null) {
        Predicate predicate = criteriaBuilder.and(
                criteriaBuilder.or(root.get("expire").isNull(),
                        criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date())),
                criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery),
                criteriaBuilder.notEqual(root.get("status"), Order.Status.completed),
                criteriaBuilder.notEqual(root.get("status"), Order.Status.failed),
                criteriaBuilder.notEqual(root.get("status"), Order.Status.canceled),
                criteriaBuilder.notEqual(root.get("status"), Order.Status.denied),
                criteriaBuilder.lessThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount")));
        if (isPendingReceive) {
            restrictions = criteriaBuilder.and(restrictions, predicate);
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate));
        }
    }
    if (isPendingRefunds != null) {
        Predicate predicate = criteriaBuilder.or(
                criteriaBuilder.and(
                        criteriaBuilder.or(
                                criteriaBuilder.and(root.get("expire").isNotNull(),
                                        criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"),
                                                new Date())),
                                criteriaBuilder.equal(root.get("status"), Order.Status.failed),
                                criteriaBuilder.equal(root.get("status"), Order.Status.canceled),
                                criteriaBuilder.equal(root.get("status"), Order.Status.denied)),
                        criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), BigDecimal.ZERO)),
                criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), Order.Status.completed),
                        criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"),
                                root.<BigDecimal>get("amount"))));
        if (isPendingRefunds) {
            restrictions = criteriaBuilder.and(restrictions, predicate);
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate));
        }
    }
    if (isUseCouponCode != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isUseCouponCode"), isUseCouponCode));
    }
    if (isExchangePoint != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isExchangePoint"), isExchangePoint));
    }
    if (isAllocatedStock != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isAllocatedStock"), isAllocatedStock));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * Get count of specification rules//from   w ww.jav a2  s  .  c  o  m
 * 
 * @return count
 */
private Long getSpecificationRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(SpecificationRule.class)));
    return em.createQuery(c).getSingleResult();
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

public Sessiondata getSessionByHash(String SID) {
    try {/*from w w  w . jav  a2 s.c  o m*/
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return null;
        }

        Sessiondata sd = fullList.get(0);

        return sd;
    } catch (Exception ex2) {
        log.error("[updateUser]: ", ex2);
    }
    return null;
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * Load resources/*  ww  w . ja  va 2  s  . c  om*/
 * 
 * @param project
 */
private void loadFeatures(final Project project) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Feature> c = cb.createQuery(Feature.class);
    Root<Feature> obj = c.from(Feature.class);
    c.select(obj);
    c.where(cb.equal(obj.get("project"), project));
    c.orderBy(cb.asc(obj.get("id")));
    this.features = em.createQuery(c).getResultList();
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);/*from ww w . java 2 s .co  m*/
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    addRestrictions(criteriaQuery, filters);

    CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class);

    for (Root<?> root : criteriaQuery.getRoots()) {
        Root<?> dest = countCriteriaQuery.from(root.getJavaType());
        dest.alias(getAlias(root));
        copyJoins(root, dest);
    }

    Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType());
    countCriteriaQuery.select(criteriaBuilder.count(countRoot.get("id").<String>get("stcd")));

    if (criteriaQuery.getGroupList() != null) {
        countCriteriaQuery.groupBy(criteriaQuery.getGroupList());
    }
    if (criteriaQuery.getGroupRestriction() != null) {
        countCriteriaQuery.having(criteriaQuery.getGroupRestriction());
    }
    if (criteriaQuery.getRestriction() != null) {
        countCriteriaQuery.where(criteriaQuery.getRestriction());
    }
    return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

/**
 * update the session every time a user makes a request
 * //w w w  .j av a2 s.  c  o m
 * @param SID
 */
private void updatesession(String SID) {
    try {
        // log.debug("****** updatesession: "+SID);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Found NO session to updateSession: ");

        } else {
            // log.debug("Found session to updateSession: ");
            Sessiondata sd = fullList.iterator().next();
            // log.debug("Found session to updateSession sd "+sd.getUser_id()+" "+sd.getSession_id());
            sd.setRefresh_time(new Date());

            if (sd.getId() == null) {
                em.persist(sd);
            } else {
                if (!em.contains(sd)) {
                    em.merge(sd);
                }
            }
        }

    } catch (Exception ex2) {
        log.error("[updatesession]: ", ex2);
    }
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * load specifications//from   w ww. j  a  v  a  2 s  .  c om
 */
private void loadList() throws UCMException {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Specification> c = cb.createQuery(Specification.class);
    Root<Specification> obj = c.from(Specification.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.specifications = em.createQuery(c).getResultList();

}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *//*from  w w w  .  ja va 2 s.  com*/
private List<SpecificationTest> findSpecificationTests(Specification specification) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationTest> c = cb.createQuery(SpecificationTest.class);
    Root<SpecificationTest> obj = c.from(SpecificationTest.class);
    c.select(obj).where(cb.equal(obj.get("specification"), specification));
    return em.createQuery(c).getResultList();
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {//from   w  w  w.j  a  va 2s. com
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);
        List<Sessiondata> fullList = q.getResultList();

        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return false;
        } else {
            // log.error("Found session to update: "+SID);
        }
        Sessiondata sd = fullList.get(0);
        // log.debug("Found session to update: "+sd.getSession_id()+
        // " userId: "+USER_ID);

        sd.setRefresh_time(new Date());
        sd.setSessionXml(sessionXml);

        if (sd.getId() == null) {
            em.persist(sd);
        } else {
            if (!em.contains(sd)) {
                em.merge(sd);
            }
        }
        // log.debug("session updated User: "+USER_ID);
        return true;
    } catch (Exception ex2) {
        log.error("[updateUserRemoteSession]: ", ex2);
    }
    return null;
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *//*from  w  ww . j a v a  2  s.c  o m*/
private List<SpecificationRuleTest> findSpecificationRuleTests(SpecificationRule specificationRule) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationRuleTest> c = cb.createQuery(SpecificationRuleTest.class);
    Root<SpecificationRuleTest> obj = c.from(SpecificationRuleTest.class);
    c.select(obj).where(cb.equal(obj.get("specificationRule"), specificationRule));
    return em.createQuery(c).getResultList();
}