Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually create Pageable object from a string

Tags:

java

spring

I didn't find anywhere whether it's possible to create pageable manually from string, let assume that we have the following service method

public <T> List<T> findAnything(final int page, final int size, final String sort) { // e.g. id,desc&username,asc

    final Pageable pageable = new PageRequest(page, size, null);
    return null;
}

my question is how can i instantiate an object of

org.springframework.data.domain.Sort

from a given string of format, important note that these parameters are chagned dynamically, so more likely i need a path to the spring parser, in my example im passing null instead the object

id,desc&username,asc

EDIT

A little bit more details I'm looking for a mechanism of how spring converts the 'sort' string(with the rest of default parameters) that's coming to the rest endpoint as a query param to Pageable object

like image 410
hdmiimdh Avatar asked Jan 01 '23 15:01

hdmiimdh


2 Answers

You can do :

private Sort orderBy() {
    return new Sort(Sort.Direction.DESC, "ID")
                .and(new Sort(Sort.Direction.ASC, "username"));
}

I think this is helpful

Sort class has static nested class Order :

public static class Order{
    private final Direction direction;
    private final String property;
    private final boolean ignoreCase;
    private final NullHandling nullHandling;
}

and then you can use :

public static Sort by(List<Order> orders)

where you create your Order from String like simply splitting.

like image 148
Mykhailo Moskura Avatar answered Jan 04 '23 04:01

Mykhailo Moskura


For that purpose I've written something similar to what spring has, i'd be happy if spring exposes SortHandlerMethodArgumentResolver.parseParameterIntoSort for usage outside the package but so far it's not

private Sort parseMultipleSortQueries(final String query) {

    final String[] queries = query.split("&");
    return parseSortQuery(queries, ",");
}

private Sort parseSortQuery(final String[] query, String delimiter) {

    final List<Sort.Order> orders = new ArrayList<>();
    for (String q : query) {

        if (q == null) {
            continue;
        }

        final String[] parts = q.split(delimiter);
        final Sort.Direction direction = parts.length == 0 ? null : Sort.Direction.fromStringOrNull(parts[parts.length - 1]);
        for (int i = 0; i < parts.length; i++) {

            if (i == parts.length - 1 && direction != null) {
                continue;
            }

            final String property = parts[i];
            if (!StringUtils.hasText(property)) {
                continue;
            }
            orders.add(new Sort.Order(direction, property));
        }
    }
    return orders.isEmpty() ? null : new Sort(orders);
}

and here is the test

@Test
public void testParseQuery() {
    System.out.println(parseMultipleSortQueries("firstName,asc&lastName,desc")); //firstName: ASC,lastName: DESC
}
like image 37
hdmiimdh Avatar answered Jan 04 '23 05:01

hdmiimdh