All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.edurt.datacap.service.adapter.PageRequestAdapter Maven / Gradle / Ivy

The newest version!
package io.edurt.datacap.service.adapter;

import io.edurt.datacap.common.utils.CollectorUtils;
import io.edurt.datacap.service.body.FilterBody;
import io.edurt.datacap.service.body.OrderBody;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

public class PageRequestAdapter
{
    private PageRequestAdapter()
    {
    }

    public static PageRequest of(int start, int end)
    {
        return PageRequest.of(start - 1, end);
    }

    public static PageRequest of(int start, int end, Sort sort)
    {
        return PageRequest.of(start - 1, end, sort);
    }

    public static PageRequest of(FilterBody filter)
    {
        PageRequest pageable = PageRequestAdapter.of(filter.getPage(), filter.getSize());
        if (ObjectUtils.isNotEmpty(filter.getOrders())) {
            // Remove duplicate data passed
            List remoted = new ArrayList<>();
            filter.getOrders()
                    .stream()
                    .filter(CollectorUtils.distinctByKey(OrderBody::getColumn))  //filter保留true的值
                    .forEach(remoted::add);

            List orders = new ArrayList<>();
            remoted.stream()
                    .filter(value -> value.getOrder().equalsIgnoreCase("asc") || value.getOrder().equalsIgnoreCase("desc")) // Filter other
                    .forEach(value -> {
                        if (value.getOrder().equalsIgnoreCase("desc")) {
                            orders.add(Sort.Order.desc(value.getColumn()));
                        }
                        else {
                            orders.add(Sort.Order.asc(value.getColumn()));
                        }
                    });
            Sort sort = Sort.by(orders);
            pageable = PageRequestAdapter.of(filter.getPage(), filter.getSize(), sort);
        }
        return pageable;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy