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

com.github.aidensuen.mongo.pagehelper.PageConfig Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.github.aidensuen.mongo.pagehelper;

import com.github.aidensuen.util.StringUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Stream;

public class PageConfig {

    /**
     * Default sort direction used by lastId
     */
    protected Direction direction = Direction.ASC;

    protected boolean pageableWithCount = true;

    public Page getPage(Object parameterObject, Pageable pageable) {
        Page page = PageHelper.getLocalPage();
        if (page == null) {
            if (pageable != null) {
                page = Page.of(pageable.getPageNumber(), pageable.getPageSize(), pageableWithCount, pageable.getSort());
                if (pageable instanceof Page) {
                    Page page1 = (Page) pageable;
                    page.setCount(Boolean.valueOf(page1.isCount()) == null || page1.isCount());
                    page.setLastId(page1.getLastId());
                    page.setID_NAME(page1.getID_NAME());
                }
            }

            if (page == null) {
                return null;
            }
            PageHelper.setLocalPage(page);
        }

        return page;
    }

    private Sort rebuildSort(Page page) {
        Sort sort = page.getSort();
        List orders = new ArrayList<>();
        Stream orderStream = sort.get();
        orderStream.forEach(order -> {
            if (Objects.equals(order.getProperty(), page.getID_NAME()) && !StringUtil.isEmpty(page.getLastId())) {

            } else {
                orders.add(order);
            }
        });
        return Sort.by(orders);
    }

    public void setProperties(Properties properties) {

        String direction = properties.getProperty("direction");
        if ("asc".equalsIgnoreCase(direction)) {
            this.direction = Direction.ASC;
        } else if ("desc".equalsIgnoreCase(direction)) {
            this.direction = Direction.DESC;
        }

        String pageableWithCount = properties.getProperty("pageableWithCount");
        this.pageableWithCount = Boolean.parseBoolean(pageableWithCount);

    }

    public boolean isPageableWithCount() {
        return pageableWithCount;
    }

    public void setPageableWithCount(boolean pageableWithCount) {
        this.pageableWithCount = pageableWithCount;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy