public Document getIndexKeys() { Document document = new Document(); for (Entry<String, Direction> entry : fieldSpec.entrySet()) { document.put(entry.getKey(), Direction.ASC.equals(entry.getValue()) ? 1 : -1); } return document; }
private static String toJpaDirection(Order order) { return order.getDirection().name().toLowerCase(Locale.US); }
/** * Creates a new {@link SortModifier} instance given {@link Direction}. * * @param direction must not be {@literal null}. */ SortModifier(Direction direction) { Assert.notNull(direction, "Direction must not be null!"); this.sort = direction.isAscending() ? 1 : -1; }
for (Order order : pageable.getSort()) { SortBuilder sortBuilder = SortBuilders.fieldSort("dto." + order.getProperty()) .order(SortOrder.valueOf(order.getDirection().name())); searchRequest.addSort(sortBuilder);
&& ormProperty.indexOf('(') == -1) { if (ormProperty.indexOf('.') == -1) { sort = Sort.by(new Sort.Order(Direction.valueOf(direction.toUpperCase(Locale.ENGLISH)), ormProperty) .ignoreCase()); } else { sort = JpaSort.unsafe(Direction.valueOf(direction.toUpperCase(Locale.ENGLISH)), "UPPER(" + ormProperty + ")"); sort = JpaSort.unsafe(Direction.valueOf(direction.toUpperCase(Locale.ENGLISH)), ormProperty);
final SortDirection direction = order.getDirection().equals(Sort.Direction.ASC) ? SortDirection.ASCENDING : SortDirection.DESCENDING; final NullHandling nullHandling; switch (order.getNullHandling()) {
final SortDirection direction = order.getDirection().equals(Sort.Direction.ASC) ? SortDirection.ASCENDING : SortDirection.DESCENDING; final NullHandling nullHandling; switch (order.getNullHandling()) {
/** * Creates and returns a map of attributes to be used by pagination functionality in displaying the results<br> * <p> * Pagination functionality supports only a single sort order. If a {@code Sort} instance containing multiple {@code Order} * elements is passed as argument, the last {@code Order} element will be applicable * </p> * @param pageIndex index of page number (page index is start with 0). * @param size size of page. * @param sort Sort option for queries. * @return Map<String, Object> instance of attributes */ public static Map<String, Object> createAttributeMap(int pageIndex, int size, Sort sort) { Map<String, Object> attr = new HashMap<String, Object>(3); attr.put(PAGE_ATTR, pageIndex); attr.put(SIZE_ATTR, size); if (sort != null) { Iterator<Order> orders = sort.iterator(); if (orders.hasNext()) { // support only one order Order order = orders.next(); attr.put(SORT_ORDER_PROPERTY_ATTR, order.getProperty()); attr.put(SORT_ORDER_DIRECTION_ATTR, order.getDirection() .toString()); } } return attr; }
if (Direction.DESC.equals(order.getDirection())) {
/** * Creates an url to sort data by fieldName * * @param context execution context * @param fieldName field name to sort * @param forcedDir optional, if specified then only this sort direction will be allowed * @return sort URL */ public static String createSortUrl(final ITemplateContext context, final String fieldName, final Direction forcedDir) { // Params can be prefixed to manage multiple pagination on the same page final String prefix = getParamPrefix(context); final Collection<String> excludedParams = Arrays .asList(new String[] { prefix.concat(SORT), prefix.concat(PAGE) }); final String baseUrl = buildBaseUrl(context, excludedParams); final StringBuilder sortParam = new StringBuilder(); final Page<?> page = findPage(context); final Sort sort = page.getSort(); final boolean hasPreviousOrder = sort != null && sort.getOrderFor(fieldName) != null; if (forcedDir != null) { sortParam.append(fieldName).append(COMMA).append(forcedDir.toString().toLowerCase()); } else if (hasPreviousOrder) { // Sort parameters exists for this field, modify direction Order previousOrder = sort.getOrderFor(fieldName); Direction dir = previousOrder.isAscending() ? Direction.DESC : Direction.ASC; sortParam.append(fieldName).append(COMMA).append(dir.toString().toLowerCase()); } else { sortParam.append(fieldName); } return buildUrl(baseUrl, context).append(SORT).append(EQ).append(sortParam).toString(); }
public static Pageable initPage(PageVo page){ Pageable pageable = null; int pageNumber = page.getPageNumber(); int pageSize = page.getPageSize(); String sort = page.getSort(); String order = page.getOrder(); if(pageNumber<1){ pageNumber = 1; } if(pageSize<1){ pageSize = 10; } if(StrUtil.isNotBlank(sort)) { Sort.Direction d; if(StrUtil.isBlank(order)) { d = Sort.Direction.DESC; }else { d = Sort.Direction.valueOf(order.toUpperCase()); } Sort s = new Sort(d,sort); pageable = PageRequest.of(pageNumber-1, pageSize,s); }else { pageable = PageRequest.of(pageNumber-1, pageSize); } return pageable; } }
if (Direction.DESC.equals(order.getDirection())) {
if (Direction.DESC.equals(order.getDirection())) {
public Page<T> find(StringQuery stringQuery, Pageable pageable) { Assert.notNull(stringQuery, "StringQuery must not be null"); Sort sort = pageable.getSort(); if (sort != null) { int count = 0; Iterator<Sort.Order> it = sort.iterator(); //自动拼接order by 属性 while (it.hasNext()) { Sort.Order s = it.next(); String name = s.getProperty(); String dir = s.getDirection().name(); if (count == 0) { stringQuery.predicate(true) .query(" order by " + name + " " + dir); } else { stringQuery.predicate(true) .query(", " + name + " " + dir); } count++; } } String query = stringQuery.getQuery(); NamedParams params = stringQuery.getParams(); return find(query, params, pageable); }
/** * Creates an url to sort data by fieldName * * @param context execution context * @param fieldName field name to sort * @return sort URL */ public static String createSortUrl(final ITemplateContext context, final String fieldName) { // Params can be prefixed to manage multiple pagination on the same page final String prefix = getParamPrefix(context); final Collection<String> excludedParams = Arrays .asList(new String[] { prefix.concat(SORT), prefix.concat(PAGE) }); final String baseUrl = buildBaseUrl(context, excludedParams); final StringBuilder sortParam = new StringBuilder(); final Page<?> page = findPage(context); final Sort sort = page.getSort(); final boolean hasPreviousOrder = sort != null && sort.getOrderFor(fieldName) != null; if (hasPreviousOrder) { // Sort parameters exists for this field, modify direction Order previousOrder = sort.getOrderFor(fieldName); Direction dir = previousOrder.isAscending() ? Direction.DESC : Direction.ASC; sortParam.append(fieldName).append(COMMA).append(dir.toString().toLowerCase()); } else { sortParam.append(fieldName); } return buildUrl(baseUrl, context).append(SORT).append(EQ).append(sortParam).toString(); }
protected void applySortIfSpecified(QueryRequest queryRequest, List<String> permittedPropertyNames) { if (permittedPropertyNames.size() > 2) { throw new UnsupportedOperationException("Can only sort by at most a single global hash and range key"); } if (sort != null) { boolean sortAlreadySet = false; for (Order order : sort) { if (permittedPropertyNames.contains(order.getProperty())) { if (sortAlreadySet) { throw new UnsupportedOperationException("Sorting by multiple attributes not possible"); } if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) { throw new UnsupportedOperationException( "Sorting for global index queries with criteria on both hash and range not possible"); } queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC)); sortAlreadySet = true; } else { throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames + " for the criteria specified"); } } } }
/** * Creates the {@code GET} sortable query based upon configuration. With this method it is necessary to use * URLDecoder#decode(String, String) to decode URL or use {@link #toUrl(URIBuilder)}. * * <p/> * <pre>{@code * createSortPair(new Sort(new Sort.Order(Sort.Direction.DESC, "dateFrom"))) → sort=name&name,dir=desc * }</pre> * * @param sort the sort instrument * @return the list of {@link NameValuePair} * @see URLDecoder#decode(String, String) * @see #toUrl(URIBuilder) */ public static List<NameValuePair> createSortPair(Sort sort) { List<NameValuePair> sortableQuery = new LinkedList<>(); for (Sort.Order order : sort) { sortableQuery.add(new BasicNameValuePair("sort", order.getProperty().concat(",") .concat(order.getDirection().toString().toLowerCase()))); } return sortableQuery; }
@Override public void serialize(Pageable pageable, JsonGenerator gen, SerializerProvider serializers) throws IOException { if (pageable == null) { return; } gen.writeStartObject(); gen.writeNumberField(PageableJsonDeserializer.PROPERTY_PAGE_NUMBER, pageable.getPageNumber()); gen.writeNumberField(PageableJsonDeserializer.PROPERTY_PAGE_SIZE, pageable.getPageSize()); Sort sort = pageable.getSort(); if (sort != null) { gen.writeFieldName(PageableJsonDeserializer.PROPERTY_SORT); gen.writeStartArray(); for (Order order : sort) { gen.writeStartObject(); gen.writeStringField(PageableJsonDeserializer.PROPERTY_PROPERTY, order.getProperty()); gen.writeStringField(PageableJsonDeserializer.PROPERTY_DIRECTION, order.getDirection().toString()); gen.writeEndObject(); } gen.writeEndArray(); } gen.writeEndObject(); }
protected void applySortIfSpecified(QueryRequest queryRequest, List<String> permittedPropertyNames) { if (permittedPropertyNames.size() > 2) { throw new UnsupportedOperationException("Can only sort by at most a single global hash and range key"); } boolean sortAlreadySet = false; for (Order order : sort) { if (permittedPropertyNames.contains(order.getProperty())) { if (sortAlreadySet) { throw new UnsupportedOperationException("Sorting by multiple attributes not possible"); } if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) { throw new UnsupportedOperationException( "Sorting for global index queries with criteria on both hash and range not possible"); } queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC)); sortAlreadySet = true; } else { throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames + " for the criteria specified and not for " + order.getProperty()); } } }
/** * Retorna a listagem de lançamentos de um funcionário. * * @param funcionarioId * @return ResponseEntity<Response<LancamentoDto>> */ @GetMapping(value = "/funcionario/{funcionarioId}") public ResponseEntity<Response<Page<LancamentoDto>>> listarPorFuncionarioId( @PathVariable("funcionarioId") Long funcionarioId, @RequestParam(value = "pag", defaultValue = "0") int pag, @RequestParam(value = "ord", defaultValue = "id") String ord, @RequestParam(value = "dir", defaultValue = "DESC") String dir) { log.info("Buscando lançamentos por ID do funcionário: {}, página: {}", funcionarioId, pag); Response<Page<LancamentoDto>> response = new Response<Page<LancamentoDto>>(); PageRequest pageRequest = new PageRequest(pag, this.qtdPorPagina, Direction.valueOf(dir), ord); Page<Lancamento> lancamentos = this.lancamentoService.buscarPorFuncionarioId(funcionarioId, pageRequest); Page<LancamentoDto> lancamentosDto = lancamentos.map(lancamento -> this.converterLancamentoDto(lancamento)); response.setData(lancamentosDto); return ResponseEntity.ok(response); }