@SuppressWarnings("unchecked") public static <T> Expression defaultGetEntityField(CriteriaQuery<T> query, String listId, Attribute attr) { Expression entityField = null; if( attr != null ) { Class attrType = attr.getDeclaringType().getJavaType(); for( From from : query.getRoots() ) { if( from.getJavaType().equals(attrType) ) { if( attr != null ) { if( attr instanceof SingularAttribute ) { entityField = from.get((SingularAttribute) attr); } else if( attr instanceof PluralAttribute ) { entityField = from.get((PluralAttribute) attr); } else { throw new IllegalStateException("Unexpected attribute type when processing criteria with list id " + listId + ": " + attr.getClass().getName() ); } break; } } } } // if entityField == null, this is because this QueryCriteria is a implementation specific criteria, such as "LAST_VARIABLE_LIST" return entityField; }
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Character> explicitPath, List<Character> directValues) { CriteriaQuery<Serializable> criteria = fieldPathBuilder.getCriteria(); Root<CountryImpl> blcCountry = criteria.from(CountryImpl.class); Predicate join = builder.equal( root.get("alpha2").as(String.class), blcCountry.get("abbreviation").as(String.class) ); return join; } })
@SuppressWarnings("unchecked") static <T> Expression<T> toExpressionRecursively(From<?, ?> from, PropertyPath property, boolean isForSelection) { Bindable<?> propertyPathModel; Bindable<?> model = from.getModel(); String segment = property.getSegment(); if (model instanceof ManagedType) { /* * Required to keep support for EclipseLink 2.4.x. TODO: Remove once we drop that (probably Dijkstra M1) * See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=413892 */ propertyPathModel = (Bindable<?>) ((ManagedType<?>) model).getAttribute(segment); } else { propertyPathModel = from.get(segment).getModel(); } if (requiresJoin(propertyPathModel, model instanceof PluralAttribute, !property.hasNext(), isForSelection) && !isAlreadyFetched(from, segment)) { Join<?, ?> join = getOrCreateJoin(from, segment); return (Expression<T>) (property.hasNext() ? toExpressionRecursively(join, property.next(), isForSelection) : join); } else { Path<Object> path = from.get(segment); return (Expression<T>) (property.hasNext() ? toExpressionRecursively(path, property.next()) : path); } }
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { return root.get("productOption").get("id").in(directValues); } }));
if( attr != null ) { if( attr instanceof SingularAttribute ) { entityField = from.get((SingularAttribute) attr); } else if( attr instanceof PluralAttribute ) { entityField = from.get((PluralAttribute) attr); } else { throw new IllegalStateException("Unexpected attribute type when processing criteria with list id " + listId + ": " + attr.getClass().getName() );
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { return root.get("productOption").get("id").in(directValues); } }));
public FieldPath getFieldPath(From root, String fullPropertyName) { String[] pieces = fullPropertyName.split("\\."); List<String> associationPath = new ArrayList<String>(); List<String> basicProperties = new ArrayList<String>(); int j = 0; for (String piece : pieces) { checkPiece: { if (j == 0) { Path path = root.get(piece); if (path instanceof PluralAttributePath) { associationPath.add(piece); break checkPiece; } } basicProperties.add(piece); } j++; } FieldPath fieldPath = new FieldPath() .withAssociationPath(associationPath) .withTargetPropertyPieces(basicProperties); return fieldPath; }
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { Date currentTime = SystemTime.asDate(true); if (isActive) { return builder.and( builder.isNotNull(root.get("startDate")), builder.lessThan(root.get("startDate"), currentTime), builder.or( builder.isNull(root.get("endDate")), builder.greaterThan(root.get("endDate"), currentTime) ) ); } else { return builder.or( builder.isNull(root.get("startDate")), builder.greaterThan(root.get("startDate"), currentTime), builder.and( builder.isNotNull(root.get("endDate")), builder.lessThan(root.get("endDate"), currentTime) ) ); } } }
private Predicate combineParamIndexPredicateWithParamNamePredicate(String theResourceName, String theParamName, From<?, ? extends BaseResourceIndexedSearchParam> theFrom, Predicate thePredicate) { if (myDontUseHashesForSearch) { Predicate resourceTypePredicate = myBuilder.equal(theFrom.get("myResourceType"), theResourceName); Predicate paramNamePredicate = myBuilder.equal(theFrom.get("myParamName"), theParamName); Predicate outerPredicate = myBuilder.and(resourceTypePredicate, paramNamePredicate, thePredicate); return outerPredicate; } long hashIdentity = BaseResourceIndexedSearchParam.calculateHashIdentity(theResourceName, theParamName); Predicate hashIdentityPredicate = myBuilder.equal(theFrom.get("myHashIdentity"), hashIdentity); return myBuilder.and(hashIdentityPredicate, thePredicate); }
private Predicate createResourceLinkPathPredicate(FhirContext theContext, String theParamName, From<?, ? extends ResourceLink> theFrom, String theResourceType) { RuntimeResourceDefinition resourceDef = theContext.getResourceDefinition(theResourceType); RuntimeSearchParam param = mySearchParamRegistry.getSearchParamByName(resourceDef, theParamName); List<String> path = param.getPathsSplit(); /* * SearchParameters can declare paths on multiple resources * types. Here we only want the ones that actually apply. */ path = new ArrayList<>(path); for (int i = 0; i < path.size(); i++) { String nextPath = trim(path.get(i)); if (!nextPath.contains(theResourceType + ".")) { path.remove(i); i--; } } return theFrom.get("mySourcePath").in(path); }
private static List<Predicate> createLastUpdatedPredicates(final DateRangeParam theLastUpdated, CriteriaBuilder builder, From<?, ResourceTable> from) { List<Predicate> lastUpdatedPredicates = new ArrayList<>(); if (theLastUpdated != null) { if (theLastUpdated.getLowerBoundAsInstant() != null) { ourLog.debug("LastUpdated lower bound: {}", new InstantDt(theLastUpdated.getLowerBoundAsInstant())); Predicate predicateLower = builder.greaterThanOrEqualTo(from.get("myUpdated"), theLastUpdated.getLowerBoundAsInstant()); lastUpdatedPredicates.add(predicateLower); } if (theLastUpdated.getUpperBoundAsInstant() != null) { Predicate predicateUpper = builder.lessThanOrEqualTo(from.get("myUpdated"), theLastUpdated.getUpperBoundAsInstant()); lastUpdatedPredicates.add(predicateUpper); } } return lastUpdatedPredicates; }
Predicate system = null; if (!isBlank(systemValue)) { system = theBuilder.equal(theFrom.get("mySystem"), systemValue); code = theBuilder.equal(theFrom.get("myUnits"), unitsValue); final Expression<BigDecimal> path = theFrom.get("myValue"); String invalidMessageName = "invalidQuantityPrefix"; if (!isBlank(systemValue) && !isBlank(unitsValue)) { long hash = ResourceIndexedSearchParamQuantity.calculateHashSystemAndUnits(theResourceName, theParamName, systemValue, unitsValue); hashPredicate = myBuilder.equal(theFrom.get("myHashIdentitySystemAndUnits"), hash); } else if (!isBlank(unitsValue)) { long hash = ResourceIndexedSearchParamQuantity.calculateHashUnits(theResourceName, theParamName, unitsValue); hashPredicate = myBuilder.equal(theFrom.get("myHashIdentityAndUnits"), hash); } else { long hash = BaseResourceIndexedSearchParam.calculateHashIdentity(theResourceName, theParamName); hashPredicate = myBuilder.equal(theFrom.get("myHashIdentity"), hash); final Expression<BigDecimal> path = theFrom.get("myValue"); String invalidMessageName = "invalidQuantityPrefix";
Predicate gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLow"), lowerBound); Predicate lt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHigh"), lowerBound); if (theRange.getLowerBound().getPrefix() == ParamPrefixEnum.STARTS_AFTER || theRange.getLowerBound().getPrefix() == ParamPrefixEnum.EQUAL) { lb = gt; Predicate gt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLow"), upperBound); Predicate lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHigh"), upperBound); if (theRange.getUpperBound().getPrefix() == ParamPrefixEnum.ENDS_BEFORE || theRange.getUpperBound().getPrefix() == ParamPrefixEnum.EQUAL) { ub = lt;
final Path<String> systemExpression = theFrom.get("mySystem"); final Path<String> valueExpression = theFrom.get("myValue"); for (Map.Entry<String, List<VersionIndependentConcept>> entry : map.entrySet()) { CriteriaBuilder.In<String> codePredicate = theBuilder.in(valueExpression); singleCodePredicates.add(theBuilder.notEqual(theFrom.get("mySystem"), system)); } else { singleCodePredicates.add(theBuilder.equal(theFrom.get("mySystem"), system)); singleCodePredicates.add(theBuilder.isNull(theFrom.get("mySystem"))); singleCodePredicates.add(theBuilder.notEqual(theFrom.get("myValue"), code)); } else { singleCodePredicates.add(theBuilder.equal(theFrom.get("myValue"), code)); hashField = theFrom.get("myHashSystemAndValue").as(Long.class); } else if (haveSystem) { hashField = theFrom.get("myHashSystem").as(Long.class); } else { hashField = theFrom.get("myHashValue").as(Long.class); Predicate identityPredicate = theBuilder.equal(theFrom.get("myHashIdentity").as(Long.class), BaseResourceIndexedSearchParam.calculateHashIdentity(theResourceName, theParamName)); Predicate disjunctionPredicate = theBuilder.not(predicate); predicate = theBuilder.and(identityPredicate, disjunctionPredicate);
Predicate singleCode = theBuilder.like(theFrom.get("myValueNormalized").as(String.class), likeExpression); if (theParameter instanceof StringParam && ((StringParam) theParameter).isExact()) { Predicate exactCode = theBuilder.equal(theFrom.get("myValueExact"), rawSearchTerm); singleCode = theBuilder.and(singleCode, exactCode); return theBuilder.equal(theFrom.get("myHashExact").as(Long.class), hash); Predicate hashCode = theBuilder.equal(theFrom.get("myHashNormalizedPrefix").as(Long.class), hash); Predicate singleCode = theBuilder.like(theFrom.get("myValueNormalized").as(String.class), likeExpression); return theBuilder.and(hashCode, singleCode);
From<?, ?> forcedIdJoin = theFrom.join("myForcedId", JoinType.LEFT); if (theSort.getOrder() == null || theSort.getOrder() == SortOrderEnum.ASC) { theOrders.add(theBuilder.asc(forcedIdJoin.get("myForcedId"))); theOrders.add(theBuilder.asc(theFrom.get("myId"))); } else { theOrders.add(theBuilder.desc(forcedIdJoin.get("myForcedId"))); theOrders.add(theBuilder.desc(theFrom.get("myId")));
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { From from = getRoot(property, root); String field = getProperty(property); return cb.greaterThan(from.get(field), compare); } }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { From from = getRoot(property, root); String field = getProperty(property); return from.get(field).in(values); } }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { From from = getRoot(property, root); String field = getProperty(property); return cb.between(from.get(field), lower, upper); } }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { From from = getRoot(property, root); String field = getProperty(property); return cb.lessThanOrEqualTo(from.get(field), compare); } }