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

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

package com.github.aidensuen.mongo.pagehelper;

import com.github.aidensuen.mongo.core.MongoDaoStatement;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

public class PageHelper extends PageMethod implements PageProcessor {

    private PageConfig pageConfig = new PageConfig();

    @Override
    public boolean skip(MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = pageConfig.getPage(parameterObject, pageable);
        if (page == null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean beforeCount(MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        return page.isCount();
    }

    @Override
    public boolean afterCount(long count, MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        page.setTotal(count);
        if (page.getPageSize() < 1) {
            return false;
        }
        return count > 0;
    }

    @Override
    public boolean beforePage(MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        if (page.getPageSize() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Object getLastId(MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        if (page != null) {
            return page.getLastId();
        }
        return null;
    }

    @Override
    public Sort.Direction getLastIdDirection(MongoDaoStatement ms, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        Sort.Direction direction = null;
        if (page != null) {
            Sort sort = page.getSort();
            Iterator iterator = sort.iterator();
            while (iterator.hasNext()) {
                Sort.Order order = iterator.next();
                if (Objects.equals(order.getProperty(), page.getID_NAME())) {
                    direction = order.getDirection();
                    break;
                }
            }
        }
        return direction == null ? Sort.Direction.ASC : direction;
    }

    @Override
    public Object afterPage(List pageList, Object parameterObject, Pageable pageable) {
        Page page = getLocalPage();
        if (page == null) {
            return pageList;
        }
        page.setResult(pageList);
        if (!page.isCount()) {
            page.setTotal(-1);
        }
        return pageList;
    }

    @Override
    public void afterAll() {
        clearPage();
    }

    @Override
    public void setProperties(Properties properties) {
        setStaticProperties(properties);
        if (pageConfig == null) {
            pageConfig = new PageConfig();
        }
        pageConfig.setProperties(properties);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy