Example usage for javax.persistence.criteria CriteriaBuilder conjunction

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

Introduction

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

Prototype

Predicate conjunction();

Source Link

Document

Create a conjunction (with zero conjuncts).

Usage

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

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {/*www  .  j  ava2 s  .co  m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public Long registerMemberCount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);/* w ww  . jav  a  2s  . co  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java

public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);/*  w  w w .j a v a 2  s.  com*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {//from  ww  w. j ava2s  .co  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.groupbuy.dao.impl.ReviewDaoImpl.java

public Page<Review> findPage(Member member, Product product, Type type, Boolean isShow, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);//ww  w  .  ja v a2 s .c  om
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.groupbuy.dao.impl.ReviewDaoImpl.java

public Long count(Member member, Product product, Type type, Boolean isShow) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);//from   w  w w.j  a v  a  2  s . c om
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the {@link AuthenticationType}s out of the database.
 *
 * @param enabled/*from   w ww .  java  2  s.co m*/
 *            if not <code>null</code> and <code>true</code> only the
 *            enabled {@link AuthenticationType}s are replied.
 * @return the list of {@link AuthenticationType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<AuthenticationType> getAuthenticationTypes(final Boolean enabled) {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    Predicate ands = cb.conjunction();
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), this.getContextName()));
    if (enabled != null) {
        ands.getExpressions().add(cb.equal(c.<Boolean>get(AuthenticationType_.enabled), enabled));
    }
    q.where(ands);
    q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName)));
    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    List<AuthenticationType> list = typedQuery.getResultList();
    for (AuthenticationType auth : list) {
        for (GrantedAuthorityType gr : auth.getGrantedAuthority()) {
            gr.getRoleName();
        }
        for (int i = 0; i < auth.getGrantedAuthority().size(); i++) {
            // eager loading...
            auth.getGrantedAuthority().get(i);
        }
    }
    return list;
}

From source file:net.groupbuy.dao.impl.ReviewDaoImpl.java

public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);/*from  www  .jav  a  2s .  c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:net.groupbuy.dao.impl.MessageDaoImpl.java

public Page<Message> findDraftPage(Member sender, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class);
    Root<Message> root = criteriaQuery.from(Message.class);
    criteriaQuery.select(root);//from  w w w. j a va2 s . co m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forMessage")),
            criteriaBuilder.equal(root.get("isDraft"), true));
    if (sender != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("sender"), sender));
    } else {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("sender")));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public List<Article> findList(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);/*from  www. ja  v  a  2 s  .  c  om*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (articleCategory != null) {
        Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class);
        Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory),
                criteriaBuilder.like(subqueryRoot.<String>get("treePath"),
                        "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.in(root.get("articleCategory")).value(subquery));
    }
    if (tag != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag));
    }
    if (isPublication != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isPublication"), isPublication));
    }
    criteriaQuery.where(restrictions);
    if (orders == null || orders.isEmpty()) {
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")),
                criteriaBuilder.desc(root.get("createDate")));
    }
    return super.findList(criteriaQuery, null, count, filters, orders);
}