@Override public Predicate predicate(Root<T> root, CriteriaBuilder cb, Map<String, Expression> mappings) throws NotSupportedException { Predicate predicate = null; Expression objectPath = mappings.get(this.fieldName); if (objectPath == null) { objectPath = getExpressionPath(root, cb); } return objectPath.in(fieldValues); }
/** * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}. * * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order} * @param root the {@link Root} the {@link Order} expression is based on * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order} * with * @return */ @SuppressWarnings("unchecked") private static javax.persistence.criteria.Order toJpaOrder( Order order, Root<?> root, CriteriaBuilder cb) { PropertyPath property = PropertyPath.from(order.getProperty(), root.getJavaType()); Expression<?> expression = toExpressionRecursively(root, property); if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) { Expression<String> lower = cb.lower((Expression<String>) expression); return order.isAscending() ? cb.asc(lower) : cb.desc(lower); } else { return order.isAscending() ? cb.asc(expression) : cb.desc(expression); } }
@Override @SuppressWarnings({"unchecked"}) public Expression<Number> quot( Expression<? extends Number> expression1, Expression<? extends Number> expression2) { if (expression1 == null || expression2 == null) { throw new IllegalArgumentException("arguments to quot() cannot be null"); } final Class resultType = BinaryArithmeticOperation.determineResultType( expression1.getJavaType(), expression2.getJavaType(), true); return new BinaryArithmeticOperation<Number>( this, resultType, BinaryArithmeticOperation.Operation.DIVIDE, expression1, expression2); }
@Override public <E, C extends Collection<E>> Predicate isMember(E e, Expression<C> collectionExpression) { if (!PluralAttributePath.class.isInstance(collectionExpression)) { throw new IllegalArgumentException( "unknown collection expression type [" + collectionExpression.getClass().getName() + "]"); } return new MemberOfPredicate<>(this, e, (PluralAttributePath<C>) collectionExpression); }
protected <T> Object[] parseIn(Expression<?> path, JsonNode fieldValue) throws QueryInfoException { Class<?> javaType = path.getJavaType(); TypeFactory typeFactory = objectMapper.getTypeFactory(); ArrayType arrayType = typeFactory.constructArrayType(javaType); return objectMapper.convertValue(fieldValue, arrayType); }
@Override @SuppressWarnings({"unchecked"}) public Coalesce<T> value(Expression<? extends T> value) { expressions.add(value); if (javaType == null) { javaType = (Class<T>) value.getJavaType(); } return this; }
@Override @SuppressWarnings({"unchecked"}) public <M extends Map<?, ?>> Predicate isMapEmpty(Expression<M> mapExpression) { if (PluralAttributePath.class.isInstance(mapExpression)) { return new IsEmptyPredicate(this, (PluralAttributePath<M>) mapExpression); } // TODO : what other specific types? any? throw new IllegalArgumentException( "unknown collection expression type [" + mapExpression.getClass().getName() + "]"); }
@Override public <M extends Map<?, ?>> Expression<Integer> mapSize(Expression<M> mapExpression) { if (LiteralExpression.class.isInstance(mapExpression)) { return mapSize(((LiteralExpression<M>) mapExpression).getLiteral()); } else if (PluralAttributePath.class.isInstance(mapExpression)) { return new SizeOfPluralAttributeExpression(this, (PluralAttributePath) mapExpression); } // TODO : what other specific types? any? throw new IllegalArgumentException( "unknown collection expression type [" + mapExpression.getClass().getName() + "]"); }
@Override public <C extends Collection<?>> Expression<Integer> size(Expression<C> exp) { if (LiteralExpression.class.isInstance(exp)) { return size(((LiteralExpression<C>) exp).getLiteral()); } else if (PluralAttributePath.class.isInstance(exp)) { return new SizeOfPluralAttributeExpression(this, (PluralAttributePath<C>) exp); } // TODO : what other specific types? any? throw new IllegalArgumentException( "unknown collection expression type [" + exp.getClass().getName() + "]"); }
@Override @SuppressWarnings({"unchecked"}) public <N extends Number> Expression<N> diff(N n, Expression<? extends N> expression) { if (n == null || expression == null) { throw new IllegalArgumentException("arguments to diff() cannot be null"); } final Class resultType = BinaryArithmeticOperation.determineResultType(n.getClass(), expression.getJavaType()); return new BinaryArithmeticOperation<N>( this, resultType, BinaryArithmeticOperation.Operation.SUBTRACT, n, expression); }
@Override @SuppressWarnings({"unchecked"}) public <N extends Number> Expression<N> prod(Expression<? extends N> expression, N n) { if (expression == null || n == null) { throw new IllegalArgumentException("arguments to prod() cannot be null"); } final Class resultType = BinaryArithmeticOperation.determineResultType(expression.getJavaType(), n.getClass()); return new BinaryArithmeticOperation<N>( this, resultType, BinaryArithmeticOperation.Operation.MULTIPLY, expression, n); }
public Object parse(Expression<?> path, ConditionOperator conditionOperator, JsonNode fieldValue) throws QueryInfoException { Object result; Class<?> javaType = path.getJavaType(); boolean isEitherInOperator = ConditionOperator.isEitherInOperator(conditionOperator); if (isEitherInOperator) { result = parseIn(path, fieldValue); } else if (Date.class.isAssignableFrom(javaType)) { result = parseDate(fieldValue); } else { result = parseType(fieldValue, javaType); } return result; }
@Override public List<VeteranAssessment> searchVeteranAssessmentForExport( Integer clinicanId, Integer createdByUserId, Integer programId, Date fromAssessmentDate, Date toAssessmentDate, Integer veteranId, List<Integer> programIdList) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class); Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class); Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus"); List<Predicate> criteriaList = new ArrayList<Predicate>(); // only include assessments in the appropriate state Expression<Integer> statusExp = assessmentStatusJoin.get("assessmentStatusId"); Predicate statusPredicate = statusExp.in(getExportAssessmentStates()); criteriaList.add(statusPredicate); if (clinicanId != null) { Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician"); criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId)); } if (createdByUserId != null) { Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser"); criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId)); } if (fromAssessmentDate != null) { criteriaList.add( criteriaBuilder.greaterThan( veteranAssessmentRoot.<Date>get("dateCreated"), fromAssessmentDate)); } if (toAssessmentDate != null) { criteriaList.add( criteriaBuilder.lessThanOrEqualTo( veteranAssessmentRoot.<Date>get("dateCreated"), toAssessmentDate)); } if (veteranId != null) { Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran"); criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId)); } if (programId != null) { // criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId)); if (programIdList == null) { programIdList = new ArrayList<Integer>(); } programIdList.add(programId); } if (programIdList != null && programIdList.size() > 0) { Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program"); Expression<Integer> exp = programJoin.get("programId"); Predicate programIdPredicate = exp.in(programIdList); criteriaList.add(programIdPredicate); } criteriaQuery.select(veteranAssessmentRoot); criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0]))); @SuppressWarnings("rawtypes") Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId"); criteriaQuery.orderBy(criteriaBuilder.desc(orderByPath)); // Generate the query based on the criteria. TypedQuery<VeteranAssessment> query = entityManager.createQuery(criteriaQuery); List<VeteranAssessment> veteranAssessments = query.getResultList(); return veteranAssessments; }
@Override public SearchResult<VeteranAssessment> searchVeteranAssessment( Integer veteranAssessmentId, Integer veteranId, Integer programId, Integer clinicanId, Integer createdByUserId, Date fromAssessmentDate, Date toAssessmentDate, List<Integer> programIdList, SearchAttributes searchAttributes) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class); Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class); Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus"); Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran"); Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program"); Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician"); Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser"); List<Predicate> criteriaList = new ArrayList<Predicate>(); if (veteranAssessmentId != null) { criteriaList.add( criteriaBuilder.equal( veteranAssessmentRoot.get("veteranAssessmentId"), veteranAssessmentId)); } if (veteranId != null) { criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId)); } if (programId != null) { criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId)); } if (clinicanId != null) { criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId)); } if (createdByUserId != null) { criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId)); } if (fromAssessmentDate != null) { criteriaList.add( criteriaBuilder.greaterThanOrEqualTo( veteranAssessmentRoot.<Date>get("dateUpdated"), fromAssessmentDate)); } if (toAssessmentDate != null) { criteriaList.add( criteriaBuilder.lessThanOrEqualTo( veteranAssessmentRoot.<Date>get("dateUpdated"), toAssessmentDate)); } if (programIdList != null && programIdList.size() > 0) { Expression<Integer> exp = programJoin.get("programId"); Predicate programIdPredicate = exp.in(programIdList); criteriaList.add(programIdPredicate); } criteriaQuery.select(veteranAssessmentRoot); criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0]))); // Set default order by field and then check if one was passed to us. @SuppressWarnings("rawtypes") Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId"); if (StringUtils.isNotBlank(searchAttributes.getSortColumn())) { if (searchAttributes.getSortColumn().equalsIgnoreCase("programName")) { orderByPath = programJoin.get("name"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("clinicianName")) { orderByPath = clinicianJoin.get("lastName"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("createdBy")) { orderByPath = createdByUserJoin.get("lastName"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("createDate")) { orderByPath = veteranAssessmentRoot.get("dateCreated"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("assessmentDate")) { orderByPath = veteranAssessmentRoot.get("dateUpdated"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("completeDate")) { orderByPath = veteranAssessmentRoot.get("dateCompleted"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranId")) { orderByPath = veteranJoin.get("veteranId"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranName")) { orderByPath = veteranJoin.get("lastName"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("assessmentStatusName")) { orderByPath = assessmentStatusJoin.get("name"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("ssnLastFour")) { orderByPath = veteranJoin.get("ssnLastFour"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("duration")) { orderByPath = veteranAssessmentRoot.get("duration"); } else if (searchAttributes.getSortColumn().equalsIgnoreCase("percentComplete")) { orderByPath = veteranAssessmentRoot.get("percentComplete"); } } if (searchAttributes.getSortDirection() == SortDirection.SORT_DESCENDING) { criteriaQuery.orderBy(criteriaBuilder.desc(orderByPath)); } else { criteriaQuery.orderBy(criteriaBuilder.asc(orderByPath)); } // Generate the query based on the criteria. TypedQuery<VeteranAssessment> query = entityManager.createQuery(criteriaQuery); SearchResult<VeteranAssessment> searchResult = new SearchResult<VeteranAssessment>(); // Get the total count. Not a very efficient way.... Integer totalCount = query.getResultList().size(); searchResult.setTotalNumRowsFound(totalCount); // Now get only the page. query.setFirstResult(searchAttributes.getRowStartIndex()); query.setMaxResults(searchAttributes.getPageSize()); List<VeteranAssessment> veteranAssessments = query.getResultList(); searchResult.setResultList(veteranAssessments); return searchResult; }
/** * Helper for determining the appropriate operation return type based on one of the operands as an * expression. * * @param defaultType The default return type to use if we cannot determine the java type of * 'expression' operand. * @param expression The operand. * @return The appropriate return type. */ public static Class<? extends Number> determineReturnType( Class<? extends Number> defaultType, Expression<? extends Number> expression) { return expression == null || expression.getJavaType() == null ? defaultType : expression.getJavaType(); }