List of usage examples for javax.persistence.criteria CriteriaBuilder or
Predicate or(Predicate... restrictions);
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;/* www . jav a 2 s .c o m*/ } 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 addPredicateToken(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsToken", theParamName, ResourceIndexedSearchParamToken.class); return;//from ww w.j ava 2 s.c om } 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:aode.lx.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//from w w w . j a va 2s. co m public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); List<Predicate> ORpredicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } Object value = filter.value; if (expression.getJavaType().isEnum()) { value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString()); predicates.add(builder.equal(expression, value)); } else { // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case NEQ: predicates.add(builder.notEqual(expression, filter.value)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case OREQ: ORpredicates.add(builder.equal(expression, filter.value)); break; case ORLIKE: ORpredicates.add(builder.like(expression, "%" + filter.value + "%")); break; case ORGT: ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case ORLT: ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case ORGTE: ORpredicates .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLTE: ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORNEQ: ORpredicates.add(builder.notEqual(expression, filter.value)); break; case ORNOTNULL: ORpredicates.add(builder.isNotNull(expression)); break; case ORISNULL: ORpredicates.add(builder.isNull(expression)); break; } } } // ? and ??? if (!predicates.isEmpty() && !ORpredicates.isEmpty()) { return builder.and(builder.and(predicates.toArray(new Predicate[predicates.size()])), builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()]))); } if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } if (!ORpredicates.isEmpty()) { return builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()])); } } return builder.conjunction(); } }; }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateNumber(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsNumber", theParamName, ResourceIndexedSearchParamNumber.class); return;/* w w w . jav a 2s . c o m*/ } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamNumber> from = cq.from(ResourceIndexedSearchParamNumber.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } if (params instanceof NumberParam) { NumberParam param = (NumberParam) params; BigDecimal value = param.getValue(); if (value == null) { continue; } final Expression<BigDecimal> fromObj = from.get("myValue"); ParamPrefixEnum prefix = ObjectUtils.defaultIfNull(param.getPrefix(), ParamPrefixEnum.EQUAL); String invalidMessageName = "invalidNumberPrefix"; String valueAsString = param.getValue().toPlainString(); Predicate num = createPredicateNumeric(builder, params, prefix, value, fromObj, invalidMessageName, valueAsString); codePredicates.add(num); } else { throw new IllegalArgumentException("Invalid token type: " + params.getClass()); } } 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(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateNumber(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }/*w w w . j ava2 s. co m*/ CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamNumber> from = cq.from(ResourceIndexedSearchParamNumber.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (params instanceof NumberParam) { NumberParam param = (NumberParam) params; BigDecimal value = param.getValue(); if (value == null) { return thePids; } Path<Object> fromObj = from.get("myValue"); if (param.getComparator() == null) { double mul = value.doubleValue() * 1.01; double low = value.doubleValue() - mul; double high = value.doubleValue() + mul; Predicate lowPred = builder.ge(fromObj.as(Long.class), low); Predicate highPred = builder.le(fromObj.as(Long.class), high); codePredicates.add(builder.and(lowPred, highPred)); } else { switch (param.getComparator()) { case GREATERTHAN: codePredicates.add(builder.greaterThan(fromObj.as(BigDecimal.class), value)); break; case GREATERTHAN_OR_EQUALS: codePredicates.add(builder.ge(fromObj.as(BigDecimal.class), value)); break; case LESSTHAN: codePredicates.add(builder.lessThan(fromObj.as(BigDecimal.class), value)); break; case LESSTHAN_OR_EQUALS: codePredicates.add(builder.le(fromObj.as(BigDecimal.class), value)); break; } } } else { throw new IllegalArgumentException("Invalid token type: " + params.getClass()); } } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:org.pushio.webapp.support.persistence.DynamicSpecifications.java
/** * @see ??JPA//from w ww.j a v a2 s .com * @param filters * @param entityClazz * @param isDistinct trueSQLdistinctfalse? * @return */ public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz, final boolean isDistinct, final List<OrderParam> orderParams) { return new Specification<T>() { @Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (orderParams != null && orderParams.size() > 0) { /* CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); Root<Foo> from = criteriaQuery.from(Foo.class); CriteriaQuery<Foo> select = criteriaQuery.select(from); criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); */ List<Order> orders = new ArrayList<Order>(orderParams.size()); for (OrderParam orderParam : orderParams) { if (orderParam != null && orderParam.getField() != null) { String fields[] = StringUtil.split(orderParam.getField(), '.'); Path expression = (fields.length > 1) ? root.join(fields[0]) : root.get(fields[0]); for (int i = 1, len = fields.length; i < len; ++i) { expression = expression.get(fields[i]); } if (expression != null) { Order order = (orderParam.getType() == null || orderParam.getType().equalsIgnoreCase("asc")) ? builder.asc(expression) : builder.desc(expression); orders.add(order); // query.orderBy(order); } } } query.orderBy(orders); } query.distinct(isDistinct); if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, '.'); Path expression = null; ////// ? boolean hasJoin = names[0].startsWith("[join]"); String fn = hasJoin ? names[0].substring(6) : names[0]; boolean isNotDateType = !(filter.value instanceof Date); try { expression = hasJoin ? root.join(fn) : root.get(fn); if (isNotDateType && isDateType(expression.getJavaType())) { // filter.value?? filter.value = parseDate(filter.value.toString(), filter.operator); } } catch (Exception e) { // logger.error(e.getMessage(), e); continue; // ?? } boolean isPropertyNotValid = false; for (int i = 1; i < names.length; i++) { try { expression = expression.get(names[i]); if (isNotDateType && isDateType(expression.getJavaType())) { filter.value = parseDate(filter.value.toString(), filter.operator); } } catch (Exception e) { // logger.error(e.getMessage(), e); isPropertyNotValid = true; // break; // ?? } } if (expression == null || isPropertyNotValid) { continue; } /////// // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.like(expression, "%" + values[i] + "%"); } predicates.add(builder.or(like)); } break; case ANDLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.like(expression, "%" + values[i] + "%"); } predicates.add(builder.and(like)); } break; case ANDNOTLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.notLike(expression, "%" + values[i] + "%"); } predicates.add(builder.and(like)); } break; case OREQ: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.equal(expression, values[i]); } predicates.add(builder.or(like)); } break; case ANDNOTEQ: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.notEqual(expression, values[i]); } predicates.add(builder.and(like)); } break; case NOTEQ: predicates.add(builder.notEqual(expression, (Comparable) filter.value)); break; case NOTLIKE: predicates.add(builder.notLike(expression, "%" + filter.value + "%")); break; case NULL: predicates.add(builder.isNull(expression)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; // case IN: // predicates.add(builder.in(expression).in(values)); // break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private Predicate createPredicateToken(IQueryParameterType theParameter, String theParamName, CriteriaBuilder theBuilder, From<ResourceIndexedSearchParamToken, ResourceIndexedSearchParamToken> theFrom) { String code;// w w w . j a v a 2 s . c om String system; TokenParamModifier modifier = null; if (theParameter instanceof TokenParam) { TokenParam id = (TokenParam) theParameter; system = id.getSystem(); code = (id.getValue()); modifier = id.getModifier(); } 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); } /* * Process token modifiers (:in, :below, :above) */ List<VersionIndependentConcept> codes = null; if (modifier == TokenParamModifier.IN) { codes = myTerminologySvc.expandValueSet(code); } else if (modifier == TokenParamModifier.ABOVE) { system = determineSystemIfMissing(theParamName, code, system); codes = myTerminologySvc.findCodesAbove(system, code); } else if (modifier == TokenParamModifier.BELOW) { system = determineSystemIfMissing(theParamName, code, system); codes = myTerminologySvc.findCodesBelow(system, code); } if (codes != null) { if (codes.isEmpty()) { return null; } List<Predicate> orPredicates = new ArrayList<Predicate>(); for (VersionIndependentConcept nextCode : codes) { Predicate systemPredicate = theBuilder.equal(theFrom.get("mySystem"), nextCode.getSystem()); Predicate codePredicate = theBuilder.equal(theFrom.get("myValue"), nextCode.getCode()); orPredicates.add(theBuilder.and(systemPredicate, codePredicate)); } return theBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])); } /* * Ok, this is a normal query */ 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 { /* * As of HAPI FHIR 1.5, if the client searched for a token with a system but no specified value this means to * match all tokens with the given value. * * I'm not sure I agree with this, but hey.. FHIR-I voted and this was the result :) */ // singleCodePredicates.add(theBuilder.isNull(theFrom.get("myValue"))); } Predicate singleCode = theBuilder.and(toArray(singleCodePredicates)); return singleCode; }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateReference(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { assert theParamName.contains(".") == false; Set<Long> pidsToRetain = thePids; if (theList == null || theList.isEmpty()) { return pidsToRetain; }/*w ww .j a v a 2 s . c o m*/ CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceLink> from = cq.from(ResourceLink.class); cq.select(from.get("mySourceResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (params instanceof ReferenceParam) { ReferenceParam ref = (ReferenceParam) params; String resourceId = ref.getValueAsQueryToken(); if (resourceId.contains("/")) { IdDt dt = new IdDt(resourceId); resourceId = dt.getIdPart(); } if (isBlank(ref.getChain())) { Long targetPid = translateForcedIdToPid(new IdDt(resourceId)); ourLog.info("Searching for resource link with target PID: {}", targetPid); Predicate eq = builder.equal(from.get("myTargetResourcePid"), targetPid); codePredicates.add(eq); } else { String chain = getContext().getResourceDefinition(myResourceType).getSearchParam(theParamName) .getPath(); BaseRuntimeChildDefinition def = getContext().newTerser().getDefinition(myResourceType, chain); if (!(def instanceof RuntimeChildResourceDefinition)) { throw new ConfigurationException("Property " + chain + " of type " + myResourceName + " is not a resource: " + def.getClass()); } List<Class<? extends IBaseResource>> resourceTypes; if (isBlank(ref.getResourceType())) { RuntimeChildResourceDefinition resDef = (RuntimeChildResourceDefinition) def; resourceTypes = resDef.getResourceTypes(); } else { resourceTypes = new ArrayList<Class<? extends IBaseResource>>(); RuntimeResourceDefinition resDef = getContext() .getResourceDefinition(ref.getResourceType()); resourceTypes.add(resDef.getImplementingClass()); } for (Class<? extends IBaseResource> nextType : resourceTypes) { RuntimeResourceDefinition typeDef = getContext().getResourceDefinition(nextType); RuntimeSearchParam param = typeDef.getSearchParam(ref.getChain()); if (param == null) { ourLog.debug("Type {} doesn't have search param {}", nextType.getSimpleName(), param); continue; } IFhirResourceDao<?> dao = getDao(nextType); if (dao == null) { ourLog.debug("Don't have a DAO for type {}", nextType.getSimpleName(), param); continue; } IQueryParameterType chainValue = toParameterType(param, resourceId); Set<Long> pids = dao.searchForIds(ref.getChain(), chainValue); if (pids.isEmpty()) { continue; } Predicate eq = from.get("myTargetResourcePid").in(pids); codePredicates.add(eq); } } } else { throw new IllegalArgumentException("Invalid token type: " + params.getClass()); } } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); RuntimeSearchParam param = getContext().getResourceDefinition(getResourceType()) .getSearchParam(theParamName); String path = param.getPath(); Predicate type = builder.equal(from.get("mySourcePath"), path); if (pidsToRetain.size() > 0) { Predicate inPids = (from.get("mySourceResourcePid").in(pidsToRetain)); cq.where(builder.and(type, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateTag(List<List<? extends IQueryParameterType>> theList, String theParamName, DateRangeParam theLastUpdated) { TagTypeEnum tagType;/*from ww w .java2 s. c om*/ if (Constants.PARAM_TAG.equals(theParamName)) { tagType = TagTypeEnum.TAG; } else if (Constants.PARAM_PROFILE.equals(theParamName)) { tagType = TagTypeEnum.PROFILE; } else if (Constants.PARAM_SECURITY.equals(theParamName)) { tagType = TagTypeEnum.SECURITY_LABEL; } else { throw new IllegalArgumentException("Param name: " + theParamName); // shouldn't happen } /* * 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)); */ List<Pair<String, String>> notTags = Lists.newArrayList(); for (List<? extends IQueryParameterType> nextAndParams : theList) { for (IQueryParameterType nextOrParams : nextAndParams) { if (nextOrParams instanceof TokenParam) { TokenParam param = (TokenParam) nextOrParams; if (param.getModifier() == TokenParamModifier.NOT) { if (isNotBlank(param.getSystem()) || isNotBlank(param.getValue())) { notTags.add(Pair.of(param.getSystem(), param.getValue())); } } } } } /* * We have a parameter of ResourceType?_tag:not=foo This means match resources that don't have the given tag(s) */ if (notTags.isEmpty() == false) { // 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<ResourceTag> subQfrom = subQ.from(ResourceTag.class); // subQ.select(subQfrom.get("myResourceId").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)); } for (List<? extends IQueryParameterType> nextAndParams : theList) { boolean haveTags = false; for (IQueryParameterType nextParamUncasted : nextAndParams) { if (nextParamUncasted instanceof TokenParam) { TokenParam nextParam = (TokenParam) nextParamUncasted; if (isNotBlank(nextParam.getValue())) { haveTags = true; } else if (isNotBlank(nextParam.getSystem())) { throw new InvalidRequestException("Invalid " + theParamName + " parameter (must supply a value/code and not just a system): " + nextParam.getValueAsQueryToken(myContext)); } } else { UriParam nextParam = (UriParam) nextParamUncasted; if (isNotBlank(nextParam.getValue())) { haveTags = true; } } } if (!haveTags) { continue; } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); boolean paramInverted = false; List<Pair<String, String>> tokens = Lists.newArrayList(); for (IQueryParameterType nextOrParams : nextAndParams) { String code; String system; if (nextOrParams instanceof TokenParam) { TokenParam nextParam = (TokenParam) nextOrParams; code = nextParam.getValue(); system = nextParam.getSystem(); if (nextParam.getModifier() == TokenParamModifier.NOT) { paramInverted = true; } } else { UriParam nextParam = (UriParam) nextOrParams; code = nextParam.getValue(); system = null; } if (isNotBlank(code)) { tokens.add(Pair.of(system, code)); } } if (tokens.isEmpty()) { continue; } if (paramInverted) { ourLog.debug("Searching for _tag:not"); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> newFrom = cq.from(ResourceTable.class); Subquery<Long> subQ = cq.subquery(Long.class); Root<ResourceTag> subQfrom = subQ.from(ResourceTag.class); subQ.select(subQfrom.get("myResourceId").as(Long.class)); cq.select(newFrom.get("myId").as(Long.class)); List<Predicate> andPredicates = new ArrayList<Predicate>(); andPredicates = new ArrayList<Predicate>(); andPredicates.add(builder.equal(newFrom.get("myResourceType"), myResourceName)); andPredicates.add(builder.not(builder.in(newFrom.get("myId")).value(subQ))); Subquery<Long> defJoin = subQ.subquery(Long.class); Root<TagDefinition> defJoinFrom = defJoin.from(TagDefinition.class); defJoin.select(defJoinFrom.get("myId").as(Long.class)); subQ.where(subQfrom.get("myTagId").as(Long.class).in(defJoin)); List<Predicate> orPredicates = createPredicateTagList(defJoinFrom, builder, tagType, tokens); defJoin.where(toArray(orPredicates)); cq.where(toArray(andPredicates)); TypedQuery<Long> q = myEntityManager.createQuery(cq); Set<Long> pids = new HashSet<Long>(q.getResultList()); doSetPids(pids); continue; } CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTag> from = cq.from(ResourceTag.class); List<Predicate> andPredicates = new ArrayList<Predicate>(); andPredicates.add(builder.equal(from.get("myResourceType"), myResourceName)); From<ResourceTag, TagDefinition> defJoin = from.join("myTag"); Join<?, ResourceTable> defJoin2 = from.join("myResource"); Predicate notDeletedPredicatePrediate = builder.isNull(defJoin2.get("myDeleted")); andPredicates.add(notDeletedPredicatePrediate); List<Predicate> orPredicates = createPredicateTagList(defJoin, builder, tagType, tokens); andPredicates.add(builder.or(toArray(orPredicates))); if (theLastUpdated != null) { andPredicates.addAll(createLastUpdatedPredicates(theLastUpdated, builder, defJoin2)); } createPredicateResourceId(builder, cq, andPredicates, from.get("myResourceId").as(Long.class)); Predicate masterCodePredicate = builder.and(toArray(andPredicates)); cq.select(from.get("myResourceId").as(Long.class)); cq.where(masterCodePredicate); TypedQuery<Long> q = myEntityManager.createQuery(cq); Set<Long> pids = new HashSet<Long>(q.getResultList()); doSetPids(pids); } }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateUri(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsUri", theParamName, ResourceIndexedSearchParamUri.class); return;//from ww w. j a v a 2 s. com } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamUri> from = cq.from(ResourceIndexedSearchParamUri.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } if (params instanceof UriParam) { UriParam param = (UriParam) params; String value = param.getValue(); if (value == null) { continue; } Path<Object> fromObj = from.get("myUri"); Predicate predicate; if (param.getQualifier() == UriParamQualifierEnum.ABOVE) { /* * :above is an inefficient query- It means that the user is supplying a more specific URL (say * http://example.com/foo/bar/baz) and that we should match on any URLs that are less * specific but otherwise the same. For example http://example.com and http://example.com/foo would both * match. * * We do this by querying the DB for all candidate URIs and then manually checking each one. This isn't * very efficient, but this is also probably not a very common type of query to do. * * If we ever need to make this more efficient, lucene could certainly be used as an optimization. */ ourLog.info("Searching for candidate URI:above parameters for Resource[{}] param[{}]", myResourceName, theParamName); Collection<String> candidates = myResourceIndexedSearchParamUriDao .findAllByResourceTypeAndParamName(myResourceName, theParamName); List<String> toFind = new ArrayList<String>(); for (String next : candidates) { if (value.length() >= next.length()) { if (value.substring(0, next.length()).equals(next)) { toFind.add(next); } } } if (toFind.isEmpty()) { continue; } predicate = fromObj.as(String.class).in(toFind); } else if (param.getQualifier() == UriParamQualifierEnum.BELOW) { predicate = builder.like(fromObj.as(String.class), createLeftMatchLikeExpression(value)); } else { predicate = builder.equal(fromObj.as(String.class), value); } codePredicates.add(predicate); } else { throw new IllegalArgumentException("Invalid URI type: " + params.getClass()); } } if (codePredicates.isEmpty()) { doSetPids(new HashSet<Long>()); 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())); }