protected String sortingClauseIfRequired(Sort sort) { if (sort == null) { return ""; } StringBuilder orderByClause = new StringBuilder(); orderByClause.append(" ORDER BY "); for(Iterator<Sort.Order> iterator = sort.iterator(); iterator.hasNext();) { final Sort.Order order = iterator.next(); orderByClause. append(order.getProperty()). append(" "). append(order.getDirection().toString()); if (iterator.hasNext()) { orderByClause.append(COMMA); } } return orderByClause.toString(); }
protected String sortingClauseIfRequired(Sort sort) { if (sort == null) { return ""; } StringBuilder orderByClause = new StringBuilder(); orderByClause.append(" ORDER BY "); for(Iterator<Sort.Order> iterator = sort.iterator(); iterator.hasNext();) { final Sort.Order order = iterator.next(); orderByClause. append(order.getProperty()). append(" "). append(order.getDirection().toString()); if (iterator.hasNext()) { orderByClause.append(COMMA); } } return orderByClause.toString(); }
/** * {@inheritDoc} */ @Override public Page<T> findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size, @RequestParam(value = "direction", required = false, defaultValue = "") String direction, @RequestParam(value = "properties", required = false) String properties) { Assert.isTrue(page > 0, "Page index must be greater than 0"); Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC"); if (direction.isEmpty()) { return this.service.findAll(new PageRequest(page - 1, size)); } else { Assert.notNull(properties); return this.service.findAll(new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(",")))); } }
/** * 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; }
/** * 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(); }
/** * 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(); }
/** * 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(); }
/** * Builds query string for sorting parameters. * * @param sort Sort orders. * @return Query string. */ private String buildSort(Sort sort) { StringBuilder builder = new StringBuilder(); // check if there are any sorting directions if (sort != null) { for (Sort.Order order : sort) { builder.append("&sort=") .append(order.getProperty()) .append(",") .append(order.getDirection().toString()); } } return builder.toString(); } }
/** * {@inheritDoc} */ @Override public Page<T> findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size, @RequestParam(value = "direction", required = false, defaultValue = "") String direction, @RequestParam(value = "properties", required = false) String properties) { Assert.isTrue(page > 0, "Page index must be greater than 0"); Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC"); if(direction.isEmpty()) { return this.repository.findAll(new PageRequest(page - 1, size)); } else { Assert.notNull(properties); return this.repository.findAll(new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(",")))); } }
@Override protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) { String attrValue = String.valueOf(Expressions.evaluate(context, attributeValue)).trim(); Page<?> page = PageUtils.findPage(context); String url = PageUtils.createSortUrl(context, attrValue); // Append class to the element if sorted by this field Sort sort = page.getSort(); boolean isSorted = sort != null && sort.getOrderFor(attributeValue) != null; String clas = isSorted ? SORTED_PREFIX.concat(sort.getOrderFor(attributeValue).getDirection().toString().toLowerCase()) : EMPTY; structureHandler.setAttribute(HREF, url); String currentClass = tag.getAttributeValue(CLASS); structureHandler.setAttribute(CLASS, Strings.concat(currentClass, BLANK, clas)); }
@Override protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) { String attrValue = String.valueOf(Expressions.evaluate(context, attributeValue)).trim(); Page<?> page = PageUtils.findPage(context); String url = PageUtils.createSortUrl(context, attrValue, getForcedDirection()); // Append class to the element if sorted by this field Sort sort = page.getSort(); boolean isSorted = sort != null && sort.getOrderFor(attrValue) != null; String clas = isSorted ? SORTED_PREFIX.concat(sort.getOrderFor(attrValue).getDirection().toString().toLowerCase()) : EMPTY; structureHandler.setAttribute(HREF, url); String currentClass = tag.getAttributeValue(CLASS); structureHandler.setAttribute(CLASS, Strings.concat(currentClass, BLANK, clas)); }