List of usage examples for javax.persistence.criteria CriteriaBuilder and
Predicate and(Predicate... restrictions);
From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java
private HmFolder findRootFolder(final Long ownerId) { Specification<HmFolder> spec = new Specification<HmFolder>() { @Override//from w ww .j a va 2 s .co m public Predicate toPredicate(Root<HmFolder> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Predicate[] predicates = new Predicate[2]; predicates[0] = cb.isNull(root.get("parent")); predicates[1] = cb.equal(root.get("ownerId"), ownerId); return cb.and(predicates); } }; List<HmFolder> all = rep.findAll(spec); return all.isEmpty() ? null : all.get(0); }
From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java
private List<HmFolder> findFolderBuildingNode(final String name, final Long ownerId) { Specification<HmFolder> spec = new Specification<HmFolder>() { @Override/* ww w . j a v a2s . co m*/ public Predicate toPredicate(Root<HmFolder> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Predicate[] predicates = new Predicate[2]; predicates[0] = cb.equal(root.get("name"), name); predicates[1] = cb.equal(root.get("ownerId"), ownerId); return cb.and(predicates); } }; return rep.findAll(spec); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateQuantity(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsQuantity", theParamName, ResourceIndexedSearchParamQuantity.class); return;/*from w w w.ja v a 2 s .c o m*/ } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamQuantity> from = cq.from(ResourceIndexedSearchParamQuantity.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } Predicate singleCode = createPredicateQuantity(builder, from, nextOr); codePredicates.add(singleCode); } List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); predicates.add(builder.or(toArray(codePredicates))); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); createPredicateLastUpdatedForIndexedSearchParam(builder, from, predicates); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(new HashSet<Long>(q.getResultList())); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateString(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsString", theParamName, ResourceIndexedSearchParamString.class); return;//from w ww . ja v a 2s . com } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamString> from = cq.from(ResourceIndexedSearchParamString.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType theParameter = nextOr; if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } Predicate singleCode = createPredicateString(theParameter, theParamName, builder, from); codePredicates.add(singleCode); } List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); predicates.add(builder.or(toArray(codePredicates))); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); createPredicateLastUpdatedForIndexedSearchParam(builder, from, predicates); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(new HashSet<Long>(q.getResultList())); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateDate(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsDate", theParamName, ResourceIndexedSearchParamDate.class); return;//from ww w . j a v a2 s . com } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamDate> from = cq.from(ResourceIndexedSearchParamDate.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } IQueryParameterType params = nextOr; Predicate p = createPredicateDate(builder, from, params); codePredicates.add(p); } Predicate masterCodePredicate = builder.or(toArray(codePredicates)); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); createPredicateLastUpdatedForIndexedSearchParam(builder, from, predicates); predicates.add(masterCodePredicate); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateComposite(RuntimeSearchParam theParamDef, List<? extends IQueryParameterType> theNextAnd) { // TODO: fail if missing is set for a composite query 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)); IQueryParameterType or = theNextAnd.get(0); if (!(or instanceof CompositeParam<?, ?>)) { throw new InvalidRequestException("Invalid type for composite param (must be " + CompositeParam.class.getSimpleName() + ": " + or.getClass()); }//from ww w .j ava 2s. co m CompositeParam<?, ?> cp = (CompositeParam<?, ?>) or; List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); RuntimeSearchParam left = theParamDef.getCompositeOf().get(0); IQueryParameterType leftValue = cp.getLeftValue(); predicates.add(createCompositeParamPart(builder, from, left, leftValue)); RuntimeSearchParam right = theParamDef.getCompositeOf().get(1); IQueryParameterType rightValue = cp.getRightValue(); predicates.add(createCompositeParamPart(builder, from, right, rightValue)); createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class)); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(q.getResultList()); }
From source file:com.hiperium.dao.common.generic.GenericDAO.java
/** * // w w w .j a v a 2 s . c o m * @param criteriaBuilder * @param criteriaQuery * @param root * @param entity */ private <E> void constructQuery(CriteriaBuilder criteriaBuilder, final CriteriaQuery<E> criteriaQuery, Root<E> root, E entity) { Map<String, Object> properties = this.getEntityProperties(entity); CriteriaFieldConditions criteria = new CriteriaFieldConditions(properties); List<Predicate> predicateList = new ArrayList<Predicate>(); // Add equality conditions conditions Set<String> equalityKeys = criteria.equality.keySet(); for (String key : equalityKeys) { predicateList.add(criteriaBuilder.equal(root.get(key), criteria.equality.get(key))); } // Add like conditions Set<String> likeKeys = criteria.like.keySet(); for (String key : likeKeys) { predicateList.add(criteriaBuilder.like(root.<String>get(key), criteria.like.get(key).toString())); } // Add composite conditions, only with equality conditions Set<String> compositeKeys = criteria.composite.keySet(); for (String key : compositeKeys) { Object value = criteria.composite.get(key); try { if (value.toString().startsWith("class java.util")) { continue; } else if (StringUtils.containsIgnoreCase(key, PK_OBJECT_NAME)) { Map<String, Object> propsComposites = this.getEntityProperties(value, key); CriteriaFieldConditions compositeCriteria = new CriteriaFieldConditions(propsComposites); if (!compositeCriteria.equality.isEmpty()) { Set<String> equalityKeysPk = compositeCriteria.equality.keySet(); for (String keyPk : equalityKeysPk) { String pkValueName = keyPk.replace(PK_OBJECT_NAME.concat("."), ""); predicateList.add(criteriaBuilder.equal(root.get(PK_OBJECT_NAME).get(pkValueName), compositeCriteria.equality.get(keyPk))); } } if (!compositeCriteria.like.isEmpty()) { Set<String> likeKeysPK = compositeCriteria.like.keySet(); for (String keyPk : likeKeysPK) { String pkValueName = keyPk.replace(PK_OBJECT_NAME.concat("."), ""); Expression<String> expression = root.<String>get(PK_OBJECT_NAME).get(pkValueName); predicateList.add( criteriaBuilder.like(expression, compositeCriteria.like.get(keyPk).toString())); } } } } catch (RuntimeException e) { continue; } } // Adding where stuff is necessary if (predicateList.size() == 1) { criteriaQuery.where(predicateList.get(0)); } else if (predicateList.size() > 1) { Predicate[] predicateCriteria = new Predicate[predicateList.size()]; predicateCriteria = predicateList.toArray(predicateCriteria); criteriaQuery.where(criteriaBuilder.and(predicateCriteria)); } }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateToken(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsToken", theParamName, ResourceIndexedSearchParamToken.class); return;/*from w w w . j a va 2 s . c o m*/ } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamToken> from = cq.from(ResourceIndexedSearchParamToken.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } if (nextOr instanceof TokenParam) { TokenParam id = (TokenParam) nextOr; if (id.isText()) { addPredicateString(theParamName, theList); continue; } } Predicate singleCode = createPredicateToken(nextOr, theParamName, builder, from); if (singleCode == null) { doSetPids(new ArrayList<Long>()); return; } codePredicates.add(singleCode); } if (codePredicates.isEmpty()) { return; } List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); predicates.add(builder.or(toArray(codePredicates))); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(new HashSet<Long>(q.getResultList())); }
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() + "%")); }//from w w w . java 2 s . co m } } 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:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Predicate createPredicateToken(IQueryParameterType theParameter, String theParamName, CriteriaBuilder theBuilder, From<ResourceIndexedSearchParamToken, ResourceIndexedSearchParamToken> theFrom) { String code;/* ww w . j a v a 2 s . c o m*/ String system; if (theParameter instanceof TokenParam) { TokenParam id = (TokenParam) theParameter; system = id.getSystem(); code = id.getValue(); } else if (theParameter instanceof BaseIdentifierDt) { BaseIdentifierDt id = (BaseIdentifierDt) theParameter; system = id.getSystemElement().getValueAsString(); code = id.getValueElement().getValue(); } else if (theParameter instanceof BaseCodingDt) { BaseCodingDt id = (BaseCodingDt) theParameter; system = id.getSystemElement().getValueAsString(); code = id.getCodeElement().getValue(); } else { throw new IllegalArgumentException("Invalid token type: " + theParameter.getClass()); } if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) { throw new InvalidRequestException("Parameter[" + theParamName + "] has system (" + system.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + system); } if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) { throw new InvalidRequestException("Parameter[" + theParamName + "] has code (" + code.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + code); } ArrayList<Predicate> singleCodePredicates = (new ArrayList<Predicate>()); if (StringUtils.isNotBlank(system)) { singleCodePredicates.add(theBuilder.equal(theFrom.get("mySystem"), system)); } else if (system == null) { // don't check the system } else { // If the system is "", we only match on null systems singleCodePredicates.add(theBuilder.isNull(theFrom.get("mySystem"))); } if (StringUtils.isNotBlank(code)) { singleCodePredicates.add(theBuilder.equal(theFrom.get("myValue"), code)); } else { singleCodePredicates.add(theBuilder.isNull(theFrom.get("myValue"))); } Predicate singleCode = theBuilder.and(singleCodePredicates.toArray(new Predicate[0])); return singleCode; }