Example usage for javax.persistence.criteria CriteriaBuilder disjunction

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

Introduction

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

Prototype

Predicate disjunction();

Source Link

Document

Create a disjunction (with zero disjuncts).

Usage

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of {@link InterfaceMappingType}s of the flow with id.
 *
 * @param modelVersion/*from w ww. ja  v a2  s . c  o  m*/
 *            the model version.
 * @param flowId
 *            the flow id.
 * @return the list of {@link InterfaceMappingType}s of the flow with id.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<InterfaceMappingType> getFlowInterfaceMappingTypes(final String modelVersion, final String flowId) {
    final List<InterfaceMappingType> value = new ArrayList<InterfaceMappingType>();
    final List<String> flowProcessTypeIds = this.getFlowProcessTypeIds(modelVersion, flowId);
    final List<String> flowMappingInterfaceMappingTypeIds = this
            .getFlowMappingInterfaceMappingTypeIds(modelVersion, flowProcessTypeIds);
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<InterfaceMappingType> q = cb.createQuery(InterfaceMappingType.class);
    final Root<InterfaceMappingType> f = q.from(InterfaceMappingType.class);

    final Predicate orIds = cb.disjunction();
    flowMappingInterfaceMappingTypeIds.stream()
            .forEach(id -> orIds.getExpressions().add(cb.equal(f.<String>get(InterfaceMappingType_.id), id)));

    q.where(cb.equal(f.<String>get(InterfaceMappingType_.modelVersion), modelVersion), orIds);

    final TypedQuery<InterfaceMappingType> typedQuery = this.em.createQuery(q);
    value.addAll(typedQuery.getResultList());

    final List<String> interfaceMappingIds = value.stream().map(i -> i.getId()).collect(Collectors.toList());
    this.getInterfaceMappingInheritents(modelVersion, interfaceMappingIds, value);
    value.stream().forEach(im -> EagerLoader.load(im));
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

private void getInterfaceMappingInheritents(final String modelVersion, final List<String> interfaceMappingIds,
        final List<InterfaceMappingType> interfaceMappings) {
    if (interfaceMappingIds.size() > 0) {
        List<InterfaceMappingType> value = new ArrayList<InterfaceMappingType>();
        final List<String> fieldDefinitionIds = this.getInterfaceMappingFieldDefinitionIds(modelVersion,
                interfaceMappingIds);//from w  w  w.j a va 2  s  .co m

        final CriteriaBuilder cb = this.em.getCriteriaBuilder();
        final CriteriaQuery<InterfaceMappingType> q = cb.createQuery(InterfaceMappingType.class);
        final Root<InterfaceMappingType> f = q.from(InterfaceMappingType.class);

        final Predicate orIds = cb.disjunction();
        fieldDefinitionIds.stream().forEach(
                id -> orIds.getExpressions().add(cb.equal(f.<String>get(InterfaceMappingType_.id), id)));

        q.where(cb.equal(f.<String>get(InterfaceMappingType_.modelVersion), modelVersion), orIds);

        final TypedQuery<InterfaceMappingType> typedQuery = this.em.createQuery(q);
        value = typedQuery.getResultList();
        interfaceMappings.addAll(value);
        final List<String> foundIds = value.stream().map(i -> i.getId()).collect(Collectors.toList());
        this.getInterfaceMappingInheritents(modelVersion, foundIds, interfaceMappings);
    }
}

From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, OrderSearchParams params,
        Set<UUID> processingPeriodIds, Pageable pageable, boolean count, Set<UUID> availableSupplyingFacilities,
        Set<UUID> availableRequestingFacilities) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<Order> root = query.from(Order.class);

    if (count) {//  ww w  . j a va  2s  .co  m
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }

    Predicate predicate = builder.conjunction();
    predicate = isEqual(SUPPLYING_FACILITY_ID, params.getSupplyingFacilityId(), root, predicate, builder);
    predicate = isEqual(REQUESTING_FACILITY_ID, params.getRequestingFacilityId(), root, predicate, builder);

    if (!(isEmpty(availableSupplyingFacilities) && isEmpty(availableRequestingFacilities))) {
        Predicate orPredicate = builder.disjunction();
        orPredicate = isOneOfOr(SUPPLYING_FACILITY_ID, availableSupplyingFacilities, root, orPredicate,
                builder);
        orPredicate = isOneOfOr(REQUESTING_FACILITY_ID, availableRequestingFacilities, root, orPredicate,
                builder);
        predicate = builder.and(predicate, orPredicate);
    }

    predicate = isEqual(PROGRAM_ID, params.getProgramId(), root, predicate, builder);
    predicate = isOneOf(PROCESSING_PERIOD_ID, processingPeriodIds, root, predicate, builder);
    predicate = isOneOf(ORDER_STATUS, params.getStatusAsEnum(), root, predicate, builder);

    query.where(predicate);

    if (!count && pageable != null && pageable.getSort() != null) {
        query = addSortProperties(query, root, pageable);
    }

    return query;
}

From source file:cn.buk.hotel.dao.HotelDaoImpl.java

@Override
public List<HotelInfo> searchAvailableHotel(HotelSearchCriteria sc) {

    List<HotelInfo> hotelInfos = null;

    try {//from  ww  w . ja v a2 s  .  c  om
        //body
        CriteriaBuilder cb = getEm().getCriteriaBuilder();

        CriteriaQuery<HotelInfo> cq = cb.createQuery(HotelInfo.class);
        Root<HotelInfo> root = cq.from(HotelInfo.class);
        root.alias("h");

        List<Predicate> predicates = new ArrayList<Predicate>();

        Predicate predicate = cb.equal(root.get(HotelInfo_.cityId), sc.getCityId());
        predicates.add(predicate);

        /**
         * ratePlanStatus-1???)  yfddai 2015-1-8
         */
        predicate = cb.notEqual(root.get(HotelInfo_.ratePlanStatus), -1);
        predicates.add(predicate);

        if (sc.getDistrictId() > 0) {
            predicate = cb.equal(root.get(HotelInfo_.areaId), sc.getDistrictId());
            predicates.add(predicate);
        }

        if (sc.getHotelName() != null && sc.getHotelName().trim().length() > 0) {
            predicate = cb.like(root.get(HotelInfo_.hotelName), "%" + sc.getHotelName() + "%");
            predicates.add(predicate);
        }

        if (sc.getStar() != null && sc.getStar().length() > 0) {
            Join<HotelInfo, HotelAward> hotelAward = root.join("hotelAwards", JoinType.LEFT);
            hotelAward.alias("ha");

            predicates.add(cb.equal(hotelAward.get("provider"), "HotelStarRate"));

            String[] stars = sc.getStar().split(",");
            Predicate p0 = cb.disjunction();
            for (String star : stars) {
                if (star.length() == 0)
                    continue;
                int starLevel = Integer.parseInt(star);
                if (starLevel == 2)
                    p0 = cb.or(p0, cb.le(hotelAward.get(HotelAward_.rating), starLevel));
                else
                    p0 = cb.or(p0, cb.equal(hotelAward.get("rating"), starLevel));
            }
            predicates.add(p0);
        }

        if (sc.getZoneId() > 0) {
            Join<HotelInfo, HotelAddressZone> hotelZone = root.join(HotelInfo_.hotelAddressZones,
                    JoinType.LEFT);
            hotelZone.alias("hz");

            predicate = cb.equal(hotelZone.get(HotelAddressZone_.zoneCode), sc.getZoneId());
            predicates.add(predicate);
        }

        // count items
        CriteriaQuery<Long> cq0 = cb.createQuery(Long.class);
        Root<HotelInfo> root0 = cq0.from(HotelInfo.class);
        root0.alias("h");
        if (sc.getStar() != null && sc.getStar().length() > 0) {
            Join<HotelInfo, HotelAward> hotelAward0 = root0.join("hotelAwards", JoinType.LEFT);
            hotelAward0.alias("ha");
        }
        if (sc.getZoneId() > 0) {
            Join<HotelInfo, HotelAddressZone> hotelZone0 = root0.join(HotelInfo_.hotelAddressZones,
                    JoinType.LEFT);
            hotelZone0.alias("hz");
        }
        cq0.select(cb.count(root0)).where(predicates.toArray(new Predicate[0]));
        Long count = getEm().createQuery(cq0).getSingleResult();

        sc.getPage().setRowCount(count.intValue());

        int firstPosition = (sc.getPage().getPageNo() - 1) * sc.getPage().getPageSize();
        cq.select(root).where(predicates.toArray(new Predicate[0]));

        hotelInfos = getEm().createQuery(cq).setFirstResult(firstPosition)
                .setMaxResults(sc.getPage().getPageSize()).getResultList();
    } catch (PersistenceException e) {
        logger.error(e.getMessage());
    }

    return hotelInfos == null ? new ArrayList<HotelInfo>() : hotelInfos;
}