private static Direction toDirection(Sort sort, String property) { if (sort.isUnsorted()) { return Direction.DESC; } Order order = sort.getOrderFor(property); return order == null ? Direction.DESC : order.isAscending() ? Direction.ASC : Direction.DESC; } }
private static Direction toDirection(Sort sort, String property) { if (sort.isUnsorted()) { return Direction.DESC; } Order order = sort.getOrderFor(property); return order == null ? Direction.DESC : order.isAscending() ? Direction.ASC : Direction.DESC; } }
/** * Returns in which direction to sort revisions for the given {@link Sort} instance. Defaults to * {@link Direction#ASC}. * * @param sort must not be {@literal null}. * @return */ public static Direction getRevisionDirection(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); Order order = sort.getOrderFor(PROPERTY); return order == null ? Direction.ASC : order.getDirection(); } }
@Override protected Sort processSort(Sort sort) { if(sort.getOrderFor(DEFAULT_PERIOD_SORT_FIELD) != null){ return sort; } return sort.and(new Sort(Direction.ASC, DEFAULT_PERIOD_SORT_FIELD)); }
/** * 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(); }
@SuppressWarnings("unchecked") private void saveLastSearchParams(final HttpServletRequest request, final String listName, final Pageable pageable, final String search) { final List<Column> listColumns = SearchFilterUtils.getListColumns(request); boolean isAsc = true; int sortColumnPosition = 0; for (int i = 0; i < listColumns.size(); i++) { final Order order = pageable.getSort().getOrderFor(listColumns.get(i).getName()); if (order != null) { isAsc = order.isAscending(); sortColumnPosition = i + 1; break; } } final String sortDirection = isAsc ? Constants.ASC : Constants.DESC; final LastSearchParamsDTO backSearch = new LastSearchParamsDTO(pageable.getPageNumber(), pageable.getPageSize(), search, listName, sortColumnPosition, sortDirection); Map<String, LastSearchParamsDTO> map = (Map<String, LastSearchParamsDTO>) request.getSession().getAttribute(LAST_SEARCH_PARAMS_MAP); if (map == null) { map = new HashMap<String, LastSearchParamsDTO>(); } map.put(listName, backSearch); request.getSession().setAttribute(LAST_SEARCH_PARAMS_MAP, map); }
@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)); }
protected TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) { Sort sort = pageable == null ? null : pageable.getSort(); if (sort != null) { Field[] fields = getDomainClass().getDeclaredFields(); for (Field field : fields) { Id id = field.getDeclaredAnnotation(Id.class); if (id != null) { Order order = sort.getOrderFor(field.getName()); if (order == null) { sort = sort.and(new Sort(field.getName())); } break; } } } return getQuery(spec, sort); }
@Override public Page<StreamDefinition> findAll(Pageable pageable) { List<StreamDefinition> results; List<String> allKeys = new ArrayList<>(hashOperations.keys()); int total = allKeys.size(); if (total == 0) { results = Collections.emptyList(); } else { Collections.sort(allKeys, comparatorFor(pageable.getSort().getOrderFor("name").getDirection())); int start = pageable.getOffset(); int end = Math.min(pageable.getOffset() + pageable.getPageSize(), total); List<String> pageKeys = allKeys.subList(start, end); List<String> definitions = hashOperations.multiGet(pageKeys); results = zipToStreamDefinitions(pageKeys, definitions); } return new PageImpl<>(results, pageable, total); }