Example usage for javax.persistence.criteria CriteriaBuilder in

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

Introduction

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

Prototype

<T> In<T> in(Expression<? extends T> expression);

Source Link

Document

Create predicate to test whether given expression is contained in a list of values.

Usage

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);/*w  w w  .j av  a2 s.c  o  m*/
    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);
}

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

public Page<Article> findPage(ArticleCategory articleCategory, Tag tag, Boolean isPublication,
        Pageable pageable) {//from www .ja v a 2s. com
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);
    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 (pageable == null
            || ((StringUtils.isEmpty(pageable.getOrderProperty()) || pageable.getOrderDirection() == null)
                    && CollectionUtils.isEmpty(pageable.getOrders()))) {
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")),
                criteriaBuilder.desc(root.get("createDate")));
    }
    return super.findPage(criteriaQuery, pageable);
}

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

public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication,
        Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);//from  w  w  w  .jav  a2s  .  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 (isPublication != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isPublication"), isPublication));
    }
    if (generateMethod != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("generateMethod"), generateMethod));
    }
    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.findList(criteriaQuery, first, count, null, null);
}

From source file:fi.vm.sade.organisaatio.dao.impl.OrganisaatioDAOImpl.java

@Override
public List<Organisaatio> findByOidList(List<String> oidList, int maxResults) {
    LOG.debug("findByOidList({}, {})", oidList, maxResults);

    // first drop nulls from oidList
    List<String> oidListFiltered = new ArrayList<>();
    for (String oid : oidList) {
        if (oid != null) {
            oidListFiltered.add(oid);//from w w  w  .ja va  2 s. c o m
        }
    }

    // perform query
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Organisaatio> query = cb.createQuery(Organisaatio.class);
    Root<Organisaatio> organisaatio = query.from(Organisaatio.class);
    query.orderBy(cb.asc(organisaatio.get("nimihaku")));
    Predicate where = cb.in(organisaatio.get("oid")).value(oidListFiltered);
    query.where(where);
    return getEntityManager().createQuery(query).setMaxResults(maxResults).getResultList();

}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forFirmwareModuleVersion(final FirmwareModuleFilterType firmwareModuleFilterType,
        final String firmwareModuleVersion) throws ArgumentNullOrEmptyException {
    if (StringUtils.isEmpty(firmwareModuleVersion)) {
        throw new ArgumentNullOrEmptyException("firmwareModuleVersion");
    }//w w w.  j  a va  2  s.  c  o  m
    if (firmwareModuleFilterType == null) {
        throw new ArgumentNullOrEmptyException("firmwareModuleType");
    }

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {
            String moduleFieldName = "";
            switch (firmwareModuleFilterType) {
            case COMMUNICATION:
                moduleFieldName = "moduleVersionComm";
                break;
            case FUNCTIONAL:
                moduleFieldName = "moduleVersionFunc";
                break;
            case SECURITY:
                moduleFieldName = "moduleVersionSec";
                break;
            case M_BUS:
                moduleFieldName = "moduleVersionMbus";
                break;
            case MODULE_ACTIVE:
                moduleFieldName = "moduleVersionMa";
                break;
            case ACTIVE_FIRMWARE:
                break;

            default:
                break;
            }

            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceFirmware> deviceFirmwareRoot = subquery.from(DeviceFirmware.class);
            subquery.select(deviceFirmwareRoot.get("device").get("id").as(Long.class));
            subquery.where(cb.and(
                    cb.like(cb.upper(deviceFirmwareRoot.get("firmware").<String>get(moduleFieldName)),
                            firmwareModuleVersion.toUpperCase()),
                    cb.equal(deviceFirmwareRoot.<Boolean>get("active"), true)));
            return cb.in(deviceRoot.get("id").as(Long.class)).value(subquery);
        }
    };
}

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

public Page<Order> findPage(Order.Type type, Order.Status status, Member member, Goods goods,
        Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint,
        Boolean isAllocatedStock, Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/*from w  w w . j  av a2 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.findPage(criteriaQuery, pageable);
}

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

public Long count(Order.Type type, Order.Status status, Member member, Goods goods, Boolean isPendingReceive,
        Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint, Boolean isAllocatedStock,
        Boolean hasExpired) {//w ww  . ja  v  a 2 s  . c o  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    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.count(criteriaQuery, null);
}

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);/*from  www  .ja v a2 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.goodhuddle.huddle.repository.MemberSpecification.java

public static Specification<Member> search(final Huddle huddle, final SearchMembersRequest request) {
    return new Specification<Member>() {
        @Override//from w w w  .ja  v  a 2  s . c om
        public Predicate toPredicate(Root<Member> member, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Predicate conjunction = builder.conjunction();

            // huddle
            conjunction.getExpressions().add(builder.equal(member.get("huddle"), huddle));

            // keywords

            if (StringUtils.isNotBlank(request.getKeywords())) {
                String[] terms = StringUtils.split(request.getKeywords());
                for (String keyword : terms) {
                    if (keyword != null && keyword.length() > 0) {

                        String matchTerm = "%" + keyword.toLowerCase() + "%";
                        conjunction.getExpressions().add(builder.or(
                                builder.like(builder.lower(member.<String>get("username")), matchTerm),
                                builder.like(builder.lower(member.<String>get("email")), matchTerm),
                                builder.like(builder.lower(member.<String>get("firstName")), matchTerm),
                                builder.like(builder.lower(member.<String>get("lastName")), matchTerm)));
                    }
                }
            }

            // security groups

            if (CollectionUtils.isNotEmpty(request.getSecurityGroupIds())) {

                Join<Object, Object> securityGroup = member.join("securityGroup",
                        request.isIncludeNoAccess() ? JoinType.LEFT : JoinType.INNER);

                Predicate disjunction = builder.disjunction();
                for (Long id : request.getSecurityGroupIds()) {
                    disjunction.getExpressions().add(builder.equal(securityGroup.get("id"), id));
                }
                if (request.isIncludeNoAccess()) {
                    disjunction.getExpressions().add(builder.isNull(securityGroup.get("id")));
                }
                conjunction.getExpressions().add(disjunction);

            } else if (request.isIncludeNoAccess()) {

                conjunction.getExpressions().add(builder.isNull(member.get("securityGroup")));

            }

            // tags

            MemberTagFilter tagFilter = request.getTags();
            if (tagFilter != null) {

                if (tagFilter.getIncluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getIncluded().getTagIds())) {

                        MemberTagFilter.TagSet included = request.getTags().getIncluded();
                        MatchType matchType = included.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.conjunction()
                                : builder.disjunction();

                        for (Long tagId : included.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq));
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

                if (tagFilter.getExcluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getExcluded().getTagIds())) {

                        MemberTagFilter.TagSet excluded = request.getTags().getExcluded();
                        MatchType matchType = excluded.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.disjunction()
                                : builder.conjunction();

                        for (Long tagId : excluded.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq).not());
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

            }

            return conjunction;
        }
    };
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateParamMissing(String joinName, String theParamName,
        Class<? extends BaseResourceIndexedSearchParam> theParamTable) {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<ResourceTable> from = cq.from(ResourceTable.class);
    cq.select(from.get("myId").as(Long.class));

    Subquery<Long> subQ = cq.subquery(Long.class);
    Root<? extends BaseResourceIndexedSearchParam> subQfrom = subQ.from(theParamTable);
    subQ.select(subQfrom.get("myResourcePid").as(Long.class));
    Predicate subQname = builder.equal(subQfrom.get("myParamName"), theParamName);
    Predicate subQtype = builder.equal(subQfrom.get("myResourceType"), myResourceName);
    subQ.where(builder.and(subQtype, subQname));

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(builder.not(builder.in(from.get("myId")).value(subQ)));
    predicates.add(builder.equal(from.get("myResourceType"), myResourceName));
    predicates.add(builder.isNull(from.get("myDeleted")));
    createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class));

    cq.where(builder.and(toArray(predicates)));

    ourLog.info("Adding :missing qualifier for parameter '{}'", theParamName);

    TypedQuery<Long> q = myEntityManager.createQuery(cq);
    doSetPids(q.getResultList());//from   w w w  . j  a v  a  2s.c om
}