Java Code Examples for org.springframework.data.domain.Sort#isSorted()

The following examples show how to use org.springframework.data.domain.Sort#isSorted() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-data   File: AqlUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static StringBuilder buildPageableClause(
	final Pageable pageable,
	@Nullable final String varName,
	final StringBuilder clause) {

	if (pageable.isUnpaged()) {
		return clause;
	}

	final Sort sort = pageable.getSort();
	buildSortClause(sort, varName, clause);

	if (sort.isSorted()) {
		clause.append(' ');
	}

	clause.append("LIMIT ").append(pageable.getOffset()).append(", ").append(pageable.getPageSize());
	return clause;
}
 
Example 2
protected <T> TypedQuery<T> getQuery(Class<T> clazz, @Nullable Specification<T> spec, Sort sort) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(clazz);

    Root<T> root = applySpecificationToCriteria(clazz, spec, query);
    query.select(root);

    if (sort.isSorted()) {
        query.orderBy(toOrders(sort, root, builder));
    }

    return entityManager.createQuery(query);
}
 
Example 3
Source Project: spring-data-cosmosdb   File: DocumentQuery.java    License: MIT License 5 votes vote down vote up
public DocumentQuery with(@NonNull Sort sort) {
    if (sort.isSorted()) {
        this.sort = sort.and(this.sort);
    }

    return this;
}
 
Example 4
private List executeReadSql(Pageable pageable, Sort sort, QueryTagValue queryTagValue) {
	SpannerPageableQueryOptions spannerQueryOptions = new SpannerPageableQueryOptions()
			.setAllowPartialRead(true);

	if (sort != null && sort.isSorted()) {
		spannerQueryOptions.setSort(sort);
	}

	if (pageable != null && pageable.isPaged()) {
		spannerQueryOptions.setOffset(pageable.getOffset()).setLimit(pageable.getPageSize());
	}

	final Class<?> returnedType = getReturnedType();
	final SpannerPersistentEntity<?> entity = returnedType == null ? null : this.spannerMappingContext.getPersistentEntity(returnedType);

	queryTagValue.sql = SpannerStatementQueryExecutor
			.applySortingPagingQueryOptions(this.entityType, spannerQueryOptions,
					queryTagValue.sql, this.spannerMappingContext, entity != null && entity.hasEagerlyLoadedProperties());

	Statement statement = buildStatementFromQueryAndTags(queryTagValue);

	return (getReturnedSimpleConvertableItemType() != null)
			? this.spannerTemplate.query(
					(struct) -> new StructAccessor(struct).getSingleValue(0), statement,
					spannerQueryOptions)
			: this.spannerTemplate.query(this.entityType,
					statement,
			spannerQueryOptions);
}
 
Example 5
Source Project: spring-vault   File: VaultQueryCreator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected KeyValueQuery<VaultQuery> complete(VaultQuery vaultQuery, Sort sort) {

	KeyValueQuery<VaultQuery> query = new KeyValueQuery<>(vaultQuery);

	if (sort.isSorted()) {
		query.orderBy(sort);
	}

	return query;
}
 
Example 6
protected TypedQuery<Tuple> getTupleQuery(@Nullable Specification spec, Sort sort, ReturnedType returnedType) {
    if (!returnedType.needsCustomConstruction()){
        return getQuery(spec,sort);
    }
    CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> query = builder.createQuery(Tuple.class);
    Root<T> root = this.applySpecificationToCriteria(spec, getDomainClass(), query);
    Predicate predicate = spec.toPredicate(root, query, builder);

    if (predicate != null) {
        query.where(predicate);
    }
    if (returnedType.needsCustomConstruction()) {
        List<Selection<?>> selections = new ArrayList<>();

        for (String property : returnedType.getInputProperties()) {
            PropertyPath path = PropertyPath.from(property, returnedType.getReturnedType());
            selections.add(toExpressionRecursively(root, path, true).alias(property));
        }

        query.multiselect(selections);
    } else {
        throw new IllegalArgumentException("only except projection");
    }
    if (sort.isSorted()) {
        query.orderBy(QueryUtils.toOrders(sort, root, builder));
    }

    return this.applyRepositoryMethodMetadata(this.entityManager.createQuery(query));
}
 
Example 7
@Override
protected KeyValueQuery<SpelExpression> complete(String criteria, Sort sort) {

	KeyValueQuery<SpelExpression> query = new KeyValueQuery<>(this.expression);

	if (sort.isSorted()) {
		query.orderBy(sort);
	}

	return query;
}
 
Example 8
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
    final Object[] parameterValues = context.getParameterValues();
    if (parameterValues.length != 2) {
        throw new IllegalStateException("Expected exactly 2 arguments to method");
    }
    final Object parameterValue = parameterValues[0];
    final Object pageableObject = parameterValues[1];
    if (parameterValue instanceof Specification) {
        Specification specification = (Specification) parameterValue;
        final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
        final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context));
        final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context));
        final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
        query.where(predicate);
        query.select(root);

        if (pageableObject instanceof Pageable) {
            Pageable pageable = (Pageable) pageableObject;
            final Sort sort = pageable.getSort();
            if (sort.isSorted()) {
                final List<Order> orders = QueryUtils.toOrders(sort, root, criteriaBuilder);
                query.orderBy(orders);
            }
            final TypedQuery<Object> typedQuery = entityManager
                    .createQuery(query);
            if (pageable.isUnpaged()) {
                return new PageImpl<>(
                    typedQuery
                            .getResultList()
                );
            } else {
                typedQuery.setFirstResult((int) pageable.getOffset());
                typedQuery.setMaxResults(pageable.getPageSize());
                final List<Object> results = typedQuery.getResultList();
                final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
                final Root<?> countRoot = countQuery.from(getRequiredRootEntity(context));
                final Predicate countPredicate = specification.toPredicate(root, query, criteriaBuilder);
                countQuery.where(countPredicate);
                countQuery.select(criteriaBuilder.count(countRoot));

                return new PageImpl<>(
                        results,
                        pageable,
                        entityManager.createQuery(countQuery).getSingleResult()
                );
            }

        } else {
            return new PageImpl<>(
                    entityManager
                            .createQuery(query)
                            .getResultList()
            );
        }
    } else {
        throw new IllegalArgumentException("Argument must be an instance of: " + Specification.class);
    }
}
 
Example 9
/**
 * Add sorting to the given query on the given alias
 * 
 * @param query to add sorting to
 * @param alias to retrieve columns from for sorting
 * @param sort  specification of sorting
 */
public default void addOrderBy(JPAQuery<VisitorEntity> query, VisitorEntity alias, Sort sort) {
	if (sort != null && sort.isSorted()) {
		Iterator<Order> it = sort.iterator();
		while (it.hasNext()) {
			Order next = it.next();
			switch (next.getProperty()) {
			case "username":
				if (next.isAscending()) {
					query.orderBy($(alias.getUsername()).asc());
				} else {
					query.orderBy($(alias.getUsername()).desc());
				}
				break;
			case "name":
				if (next.isAscending()) {
					query.orderBy($(alias.getName()).asc());
				} else {
					query.orderBy($(alias.getName()).desc());
				}
				break;
			case "phoneNumber":
				if (next.isAscending()) {
					query.orderBy($(alias.getPhoneNumber()).asc());
				} else {
					query.orderBy($(alias.getPhoneNumber()).desc());
				}
				break;
			case "password":
				if (next.isAscending()) {
					query.orderBy($(alias.getPassword()).asc());
				} else {
					query.orderBy($(alias.getPassword()).desc());
				}
				break;
			case "acceptedCommercial":
				if (next.isAscending()) {
					query.orderBy($(alias.getAcceptedCommercial()).asc());
				} else {
					query.orderBy($(alias.getAcceptedCommercial()).desc());
				}
				break;
			case "acceptedTerms":
				if (next.isAscending()) {
					query.orderBy($(alias.getAcceptedTerms()).asc());
				} else {
					query.orderBy($(alias.getAcceptedTerms()).desc());
				}
				break;
			case "userType":
				if (next.isAscending()) {
					query.orderBy($(alias.getUserType()).asc());
				} else {
					query.orderBy($(alias.getUserType()).desc());
				}
				break;
			default:
				throw new IllegalArgumentException("Sorted by the unknown property '" + next.getProperty() + "'");
			}
		}
	}
}
 
Example 10
Source Project: jump-the-queue   File: QueueRepository.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Add sorting to the given query on the given alias
 *
 * @param query to add sorting to
 * @param alias to retrieve columns from for sorting
 * @param sort  specification of sorting
 */
public default void addOrderBy(JPAQuery<QueueEntity> query, QueueEntity alias, Sort sort) {
	if (sort != null && sort.isSorted()) {
		Iterator<Order> it = sort.iterator();
		while (it.hasNext()) {
			Order next = it.next();
			switch (next.getProperty()) {
			case "name":
				if (next.isAscending()) {
					query.orderBy($(alias.getName()).asc());
				} else {
					query.orderBy($(alias.getName()).desc());
				}
				break;
			case "logo":
				if (next.isAscending()) {
					query.orderBy($(alias.getLogo()).asc());
				} else {
					query.orderBy($(alias.getLogo()).desc());
				}
				break;
			case "currentNumber":
				if (next.isAscending()) {
					query.orderBy($(alias.getCurrentNumber()).asc());
				} else {
					query.orderBy($(alias.getCurrentNumber()).desc());
				}
				break;
			case "attentionTime":
				if (next.isAscending()) {
					query.orderBy($(alias.getAttentionTime()).asc());
				} else {
					query.orderBy($(alias.getAttentionTime()).desc());
				}
				break;
			case "minAttentionTime":
				if (next.isAscending()) {
					query.orderBy($(alias.getMinAttentionTime()).asc());
				} else {
					query.orderBy($(alias.getMinAttentionTime()).desc());
				}
				break;
			case "active":
				if (next.isAscending()) {
					query.orderBy($(alias.getActive()).asc());
				} else {
					query.orderBy($(alias.getActive()).desc());
				}
				break;
			case "customers":
				if (next.isAscending()) {
					query.orderBy($(alias.getCustomers()).asc());
				} else {
					query.orderBy($(alias.getCustomers()).desc());
				}
				break;
			default:
				throw new IllegalArgumentException("Sorted by the unknown property '" + next.getProperty() + "'");
			}
		}
	}
}
 
Example 11
/**
 * Add sorting to the given query on the given alias
 *
 * @param query to add sorting to
 * @param alias to retrieve columns from for sorting
 * @param sort  specification of sorting
 */
public default void addOrderBy(JPAQuery<AccessCodeEntity> query, AccessCodeEntity alias, Sort sort) {
	if (sort != null && sort.isSorted()) {
		Iterator<Order> it = sort.iterator();
		while (it.hasNext()) {
			Order next = it.next();
			switch (next.getProperty()) {
			case "ticketNumber":
				if (next.isAscending()) {
					query.orderBy($(alias.getTicketNumber()).asc());
				} else {
					query.orderBy($(alias.getTicketNumber()).desc());
				}
				break;
			case "creationTime":
				if (next.isAscending()) {
					query.orderBy($(alias.getCreationTime()).asc());
				} else {
					query.orderBy($(alias.getCreationTime()).desc());
				}
				break;
			case "startTime":
				if (next.isAscending()) {
					query.orderBy($(alias.getStartTime()).asc());
				} else {
					query.orderBy($(alias.getStartTime()).desc());
				}
				break;
			case "endTime":
				if (next.isAscending()) {
					query.orderBy($(alias.getEndTime()).asc());
				} else {
					query.orderBy($(alias.getEndTime()).desc());
				}
				break;
			case "visitor":
				if (next.isAscending()) {
					query.orderBy($(alias.getVisitor().getId()).asc());
				} else {
					query.orderBy($(alias.getVisitor().getId()).desc());
				}
				break;
			case "queue":
				if (next.isAscending()) {
					query.orderBy($(alias.getQueue().getId()).asc());
				} else {
					query.orderBy($(alias.getQueue().getId()).desc());
				}
				break;
			default:
				throw new IllegalArgumentException("Sorted by the unknown property '" + next.getProperty() + "'");
			}
		}
	}
}