Example usage for javax.persistence.criteria CriteriaBuilder or

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

Introduction

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

Prototype

Predicate or(Expression<Boolean> x, Expression<Boolean> y);

Source Link

Document

Create a disjunction of the given boolean expressions.

Usage

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

private Predicate addPredicateDateFromRange(CriteriaBuilder theBuilder,
        From<ResourceIndexedSearchParamDate, ResourceIndexedSearchParamDate> theFrom, DateRangeParam theRange) {
    Date lowerBound = theRange.getLowerBoundAsInstant();
    Date upperBound = theRange.getUpperBoundAsInstant();

    Predicate lb = null;/*from www .ja  v a 2  s  .  com*/
    if (lowerBound != null) {
        Predicate gt = theBuilder.greaterThanOrEqualTo(theFrom.<Date>get("myValueLow"), lowerBound);
        Predicate lt = theBuilder.greaterThanOrEqualTo(theFrom.<Date>get("myValueHigh"), lowerBound);
        lb = theBuilder.or(gt, lt);

        // Predicate gin = builder.isNull(from.get("myValueLow"));
        // Predicate lbo = builder.or(gt, gin);
        // Predicate lin = builder.isNull(from.get("myValueHigh"));
        // Predicate hbo = builder.or(lt, lin);
        // lb = builder.and(lbo, hbo);
    }

    Predicate ub = null;
    if (upperBound != null) {
        Predicate gt = theBuilder.lessThanOrEqualTo(theFrom.<Date>get("myValueLow"), upperBound);
        Predicate lt = theBuilder.lessThanOrEqualTo(theFrom.<Date>get("myValueHigh"), upperBound);
        ub = theBuilder.or(gt, lt);

        // Predicate gin = builder.isNull(from.get("myValueLow"));
        // Predicate lbo = builder.or(gt, gin);
        // Predicate lin = builder.isNull(from.get("myValueHigh"));
        // Predicate ubo = builder.or(lt, lin);
        // ub = builder.and(ubo, lbo);

    }

    if (lb != null && ub != null) {
        return (theBuilder.and(lb, ub));
    } else if (lb != null) {
        return (lb);
    } else {
        return (ub);
    }
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TRsDriverinfoEntity> listAllCandidateDrivers(String[] applicationIds, String carId, String keyword,
        Integer offset, Integer limit, PrincipalExt principalExt) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TRsDriverinfoEntity> cq = cb.createQuery(TRsDriverinfoEntity.class);
    Root<TRsDriverinfoEntity> driverRoot = cq.from(TRsDriverinfoEntity.class);
    Root<TAzCarinfoEntity> carRoot = cq.from(TAzCarinfoEntity.class);
    cq.select(driverRoot);//from   w  w w .j  ava  2 s  .c  o  m

    Subquery<TBusScheduleCarEntity> overlapScheduleCarSubQuery = cq.subquery(TBusScheduleCarEntity.class);

    applyOverlapScheduleCarSubquery(overlapScheduleCarSubQuery, applicationIds, carRoot, driverRoot, cb,
            principalExt);

    Subquery<TRsDriverinfoEntity> subqueryLicense = cq.subquery(TRsDriverinfoEntity.class);
    Root<TAzJzRelaEntity> licenseMapping = subqueryLicense.from(TAzJzRelaEntity.class);
    subqueryLicense.select(driverRoot);
    subqueryLicense.where(

            cb.equal(carRoot.get(TAzCarinfoEntity_.xszcx), licenseMapping.get(TAzJzRelaEntity_.xszcx)),
            cb.equal(licenseMapping.get(TAzJzRelaEntity_.jzyq),
                    driverRoot.get(TRsDriverinfoEntity_.drivecartype)));

    Predicate predicate = cb.and(cb.equal(carRoot.get(TAzCarinfoEntity_.id), carId),
            cb.equal(driverRoot.get(TRsDriverinfoEntity_.department), carRoot.get(TAzCarinfoEntity_.sysOrg)),
            cb.equal(driverRoot.get(TRsDriverinfoEntity_.enabled), "1"),
            cb.or(cb.exists(subqueryLicense), cb.isNull(driverRoot.get(TRsDriverinfoEntity_.drivecartype))

            ),

            cb.not(cb.exists(overlapScheduleCarSubQuery))

    );

    if (keyword != null) {
        predicate = cb.and(predicate,
                cb.or(cb.like(driverRoot.get(TRsDriverinfoEntity_.drivername), "%" + keyword + "%")));
    }

    cq.where(predicate);
    cq.orderBy(cb
            .asc(cb.function("casttogbk", String.class, cb.trim(driverRoot.get(TRsDriverinfoEntity_.drivername))

    )));
    TypedQuery<TRsDriverinfoEntity> query = em.createQuery(cq);
    if (offset != null) {
        query.setFirstResult(offset);
    }

    if (limit != null) {
        query.setMaxResults(limit);
    }

    return query.getResultList();

}

From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java

public List<ServerTuple> getNodeServers(String hostFilter, String appServerFilter, String runtimeFilter,
        String nodeFilter, String contextFilter) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class);

    //get Node ResType
    Root<ResourceEntity> node = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT);

    //get Props on node
    Join<ResourceEntity, ResourceContextEntity> resCtx = node.join("contexts", JoinType.LEFT);
    Join<ResourceContextEntity, ContextEntity> nodeCtx = resCtx.join("context", JoinType.LEFT);
    Join<ContextEntity, ContextEntity> nodeDomain = nodeCtx.join("parent", JoinType.LEFT);
    Join<ResourceContextEntity, PropertyEntity> nodeProp = resCtx.join("properties", JoinType.LEFT);
    Join<PropertyEntity, PropertyDescriptorEntity> nodePropType = nodeProp.join("descriptor", JoinType.LEFT);

    //get AppServer
    Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations",
            JoinType.LEFT);//from w  ww . ja v a2s . co  m
    Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource",
            JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT);

    //get Runtime of as
    Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations",
            JoinType.LEFT);
    Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource",
            JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT);

    q.select(cb.construct(ServerTuple.class, nodeProp.get("value"), appServer.get("name"), appServer.get("id"),
            asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"),
            nodeRelease.get("name"), nodeDomain.get("name"), nodeDomain.get("id"), nodeCtx.get("name"),
            nodeCtx.get("id"), cb.literal(1) // true
    ));

    Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()),
            cb.or(cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()),
                    cb.isNull(asType.get("name")) //nodes without appServer
            ),
            cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME),
                    cb.isNull(runtimeType.get("name"))),
            cb.isNotNull(nodeProp.get("value")), cb.equal(nodePropType.get("propertyName"), "hostName")

    );

    p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter,
            nodeProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"),
            node.<String>get("name"), nodeCtx.<String>get("name"));

    q.where(p);

    TypedQuery<ServerTuple> query = entityManager.createQuery(q);
    List<ServerTuple> servers = query.getResultList();

    return servers;
}

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

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

    List<HotelInfo> hotelInfos = null;

    try {/*  ww  w .ja  va  2  s .  co m*/
        //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;
}

From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java

public List<ServerTuple> getAppServers(String hostFilter, String appServerFilter, String runtimeFilter,
        String nodeFilter, String contextFilter) {

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class);

    //get Node ResType
    Root<ResourceEntity> node = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT);

    //get AppServer
    Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations",
            JoinType.LEFT);/*  w  w  w . ja va  2 s. c  o m*/
    Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource",
            JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT);

    //get Runtime of as
    Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations",
            JoinType.LEFT);
    Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource",
            JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT);

    //get Props between as and node
    Join<ConsumedResourceRelationEntity, ResourceRelationContextEntity> resRelCtx = nodeResRel.join("contexts",
            JoinType.LEFT);
    Join<ResourceRelationContextEntity, ContextEntity> asCtx = resRelCtx.join("context", JoinType.LEFT);
    Join<ContextEntity, ContextEntity> asDomain = asCtx.join("parent", JoinType.LEFT);

    Join<ResourceRelationContextEntity, PropertyEntity> asProp = resRelCtx.join("properties", JoinType.LEFT);
    //here an on clause should be added, so we don't get hostnames that are divined directly on the node multiple times (and descriptor.propertyName = 'hostName')
    //on support was added in jpa 2.1 which is part of JEE 7
    Join<PropertyEntity, PropertyDescriptorEntity> asPropType = asProp.join("descriptor", JoinType.LEFT);

    q.select(cb.construct(ServerTuple.class, asProp.get("value"), appServer.get("name"), appServer.get("id"),
            asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"),
            nodeRelease.get("name"), asDomain.get("name"), asDomain.get("id"), asCtx.get("name"),
            asCtx.get("id"), cb.literal(0) //false
    ));

    Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()),
            cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()),
            cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME),
                    cb.isNull(runtimeType.get("name"))),
            cb.isNotNull(asProp.get("value")), cb.equal(asPropType.get("propertyName"), "hostName"));

    p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter,
            asProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"),
            node.<String>get("name"), asCtx.<String>get("name"));

    q.where(p);

    TypedQuery<ServerTuple> query = entityManager.createQuery(q);
    List<ServerTuple> servers = query.getResultList();

    return servers;
}

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

private Predicate createPredicateDateFromRange(CriteriaBuilder theBuilder,
        From<ResourceIndexedSearchParamDate, ResourceIndexedSearchParamDate> theFrom, DateRangeParam theRange) {
    Date lowerBound = theRange.getLowerBoundAsInstant();
    Date upperBound = theRange.getUpperBoundAsInstant();

    Predicate lb = null;/* w  ww .  j  a va 2s .c  o m*/
    if (lowerBound != null) {
        Predicate gt = theBuilder.greaterThanOrEqualTo(theFrom.<Date>get("myValueLow"), lowerBound);
        Predicate lt = theBuilder.greaterThanOrEqualTo(theFrom.<Date>get("myValueHigh"), lowerBound);
        if (theRange.getLowerBound().getPrefix() == ParamPrefixEnum.STARTS_AFTER
                || theRange.getLowerBound().getPrefix() == ParamPrefixEnum.EQUAL) {
            lb = gt;
        } else {
            lb = theBuilder.or(gt, lt);
        }
    }

    Predicate ub = null;
    if (upperBound != null) {
        Predicate gt = theBuilder.lessThanOrEqualTo(theFrom.<Date>get("myValueLow"), upperBound);
        Predicate lt = theBuilder.lessThanOrEqualTo(theFrom.<Date>get("myValueHigh"), upperBound);
        if (theRange.getUpperBound().getPrefix() == ParamPrefixEnum.ENDS_BEFORE
                || theRange.getUpperBound().getPrefix() == ParamPrefixEnum.EQUAL) {
            ub = lt;
        } else {
            ub = theBuilder.or(gt, lt);
        }
    }

    if (lb != null && ub != null) {
        return (theBuilder.and(lb, ub));
    } else if (lb != null) {
        return (lb);
    } else {
        return (ub);
    }
}

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

private Predicate createPredicateNumeric(CriteriaBuilder builder, IQueryParameterType params,
        ParamPrefixEnum cmpValue, BigDecimal valueValue, final Expression<BigDecimal> path,
        String invalidMessageName, String theValueString) {
    Predicate num;//from w w w  .  ja v a  2s .  c o m
    switch (cmpValue) {
    case GREATERTHAN:
        num = builder.gt(path, valueValue);
        break;
    case GREATERTHAN_OR_EQUALS:
        num = builder.ge(path, valueValue);
        break;
    case LESSTHAN:
        num = builder.lt(path, valueValue);
        break;
    case LESSTHAN_OR_EQUALS:
        num = builder.le(path, valueValue);
        break;
    case APPROXIMATE:
    case EQUAL:
    case NOT_EQUAL:
        BigDecimal mul = calculateFuzzAmount(cmpValue, valueValue);
        BigDecimal low = valueValue.subtract(mul, MathContext.DECIMAL64);
        BigDecimal high = valueValue.add(mul, MathContext.DECIMAL64);
        Predicate lowPred;
        Predicate highPred;
        if (cmpValue != ParamPrefixEnum.NOT_EQUAL) {
            lowPred = builder.ge(path.as(BigDecimal.class), low);
            highPred = builder.le(path.as(BigDecimal.class), high);
            num = builder.and(lowPred, highPred);
            ourLog.trace("Searching for {} <= val <= {}", low, high);
        } else {
            // Prefix was "ne", so reverse it!
            lowPred = builder.lt(path.as(BigDecimal.class), low);
            highPred = builder.gt(path.as(BigDecimal.class), high);
            num = builder.or(lowPred, highPred);
        }
        break;
    default:
        String msg = myContext.getLocalizer().getMessage(SearchBuilder.class, invalidMessageName,
                cmpValue.getValue(), params.getValueAsQueryToken(myContext));
        throw new InvalidRequestException(msg);
    }
    return num;
}

From source file:utils.jpa.EntityResource.java

private Query createCommonQuery(CriteriaBuilder cb, CriteriaQuery cq, Root root, TableSearchQuery tb,
        List<String> fieldsSearchBy) {

    List<Predicate> orPredicates = new ArrayList<>();
    List<Predicate> andPredicates = new ArrayList<>();

    //setting where conditions      
    if (fieldsSearchBy != null && !fieldsSearchBy.isEmpty()) {
        for (String field : fieldsSearchBy) {
            if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field), "::text")),
                        "%" + tb.getFilter().toLowerCase() + "%"));
            }// ww w  .  j a v a2  s .com
        }
    } else {
        List<Field> filterColumns = SearchFields.getAll(entityClass);
        for (Field field : filterColumns) {
            EntityFilter entityFilter = tb.getFilterParameters().get(field.getName());

            if ((Collection.class.isAssignableFrom(field.getType())
                    || List.class.isAssignableFrom(field.getType()))
                    && (entityFilter == null
                            || !entityFilter.getEntityFilterType().equals(EntityFilterType.EMPTY))) {
                continue;
            }

            if (entityFilter == null) {
                if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                    orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                            "%" + tb.getFilter().toLowerCase() + "%"));
                }
                continue;
            }

            switch (entityFilter.getEntityFilterType()) {
            case WILDCARD:
                andPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                        "%" + entityFilter.getValue().toLowerCase() + "%"));
                break;
            case EMPTY:
                if (Collection.class.isAssignableFrom(field.getType())
                        || List.class.isAssignableFrom(field.getType())) {
                    andPredicates.add(entityFilter.getValue().equals("hide")
                            ? cb.isEmpty(root.<Collection>get(field.getName()))
                            : cb.isNotEmpty(root.<Collection>get(field.getName())));
                } else {
                    andPredicates.add(
                            entityFilter.getValue().equals("hide") ? root.<Object>get(field.getName()).isNull()
                                    : root.<Object>get(field.getName()).isNotNull());
                }

                break;
            case MIN_MAX:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(0)))));
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(1)))));
                break;
            case MIN:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            case MAX:
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            }

        }
    }
    if (orPredicates.size() > 0 && andPredicates.size() > 0) {
        cq.where(cb.or(cb.or(orPredicates.toArray(new Predicate[0])),
                cb.and(andPredicates.toArray(new Predicate[0]))));
    } else if (orPredicates.size() > 0) {
        cq.where(cb.or(orPredicates.toArray(new Predicate[0])));
    } else if (andPredicates.size() > 0) {
        cq.where(cb.and(andPredicates.toArray(new Predicate[0])));
    }

    return em.createQuery(cq);

}

From source file:org.apache.rave.portal.repository.impl.JpaWidgetRepository.java

private Predicate[] getStatusAndTypeAndFreeTextPredicates(CriteriaBuilder cb, Root<JpaWidget> widgetType,
        WidgetStatus widgetStatus, String type, String searchTerm) {
    List<Predicate> predicates = new ArrayList<Predicate>();
    if (StringUtils.isNotBlank(searchTerm)) {
        predicates.add(/*from w ww  .  j ava  2s. co  m*/
                cb.or(cb.like(cb.lower(getTitleField(widgetType)), getLowercaseWildcardSearchTerm(searchTerm)),
                        cb.like(cb.lower(getDescriptionField(widgetType)),
                                getLowercaseWildcardSearchTerm(searchTerm))));
    }
    if (StringUtils.isNotBlank(type)) {
        predicates.add(cb.and(cb.equal(getTypeField(widgetType), type)));
    }
    if (widgetStatus != null) {
        predicates.add(cb.and(cb.equal(getWidgetStatusField(widgetType), widgetStatus)));
    }

    return predicates.toArray(new Predicate[predicates.size()]);
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletDefinitionDao.java

@Override
public void afterPropertiesSet() throws Exception {
    this.nameParameter = this.createParameterExpression(String.class, "name");
    this.titleParameter = this.createParameterExpression(String.class, "title");

    this.findAllPortletDefinitions = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() {
                @Override/* w  w  w.j  av  a  2s .c  o  m*/
                public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb
                            .createQuery(PortletDefinitionImpl.class);
                    final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery
                            .from(PortletDefinitionImpl.class);
                    criteriaQuery.select(definitionRoot);
                    addFetches(definitionRoot);

                    return criteriaQuery;
                }
            });

    this.findDefinitionByNameQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() {
                @Override
                public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb
                            .createQuery(PortletDefinitionImpl.class);
                    final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery
                            .from(PortletDefinitionImpl.class);
                    criteriaQuery.select(definitionRoot);
                    addFetches(definitionRoot);
                    criteriaQuery
                            .where(cb.equal(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter));

                    return criteriaQuery;
                }
            });

    this.findDefinitionByNameOrTitleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() {
                @Override
                public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb
                            .createQuery(PortletDefinitionImpl.class);
                    final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery
                            .from(PortletDefinitionImpl.class);
                    criteriaQuery.select(definitionRoot);
                    addFetches(definitionRoot);
                    criteriaQuery.where(cb.or(
                            cb.equal(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter),
                            cb.equal(definitionRoot.get(PortletDefinitionImpl_.title), titleParameter)));

                    return criteriaQuery;
                }
            });

    this.searchDefinitionByNameOrTitleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() {
                @Override
                public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb
                            .createQuery(PortletDefinitionImpl.class);
                    final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery
                            .from(PortletDefinitionImpl.class);
                    criteriaQuery.select(definitionRoot);
                    addFetches(definitionRoot);
                    criteriaQuery.where(
                            cb.or(cb.like(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter),
                                    cb.like(definitionRoot.get(PortletDefinitionImpl_.title), titleParameter)));

                    return criteriaQuery;
                }
            });
}