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

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

package com.github.aidensuen.mongo.pagehelper;


import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.executor.Executor;
import com.github.aidensuen.mongo.util.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.function.Function;

public class ExecutorUtil {

    public static final int FIRST_PAGE_NUM = 0;
    public static final String ID = "_id";

    private ExecutorUtil() {

    }

    public static  Page pageQuery(Executor executor, MongoDaoStatement ms, Object parameter,
                                        Pageable pageable) {
        return pageQuery(executor, ms, parameter, pageable, Executor.DEFAULT_CONVERTER);
    }

    public static  Page pageQuery(Executor executor, MongoDaoStatement ms, Object parameter,
                                           Pageable pageable, Function converter) {
        return pageQuery(executor, ms, parameter, pageable, converter, null, Sort.Direction.ASC);
    }

    public static  Page pageQuery(Executor executor, MongoDaoStatement ms, Object parameter,
                                           Pageable pageable, Function converter, Sort.Direction direction) {
        return pageQuery(executor, ms, parameter, pageable, converter, null, direction);
    }

    public static  Page pageQuery(Executor executor, MongoDaoStatement ms, Object parameter,
                                           Pageable pageable, Function converter, String lastId) {
        return pageQuery(executor, ms, parameter, pageable, converter, (Object) lastId, Sort.Direction.ASC);
    }

    public static  Page pageQuery(Executor executor, MongoDaoStatement ms, Object parameter, Pageable pageable, Function converter, Object lastId, Sort.Direction direction) {

        Page page = PageHelper.getLocalPage();

        List result;
        if (page != null) {
            Query query = ms.getQuery(parameter);
            Sort sort = page.getSort();
            if ((!StringUtil.isEmpty(page.getLastId()) || !StringUtil.isEmpty(lastId))) {
                if (page.getPageNumber() != FIRST_PAGE_NUM) {
                    final Criteria criteria = new Criteria();
                    if (!StringUtil.isEmpty(page.getID_NAME()) && !StringUtil.isEmpty(page.getLastId())) {
                        String _id;
                        Object objectId;
                        if ("_id".equalsIgnoreCase(page.getID_NAME())) {
                            _id = ID;
                            objectId = new ObjectId((String) page.getLastId());
                        } else {
                            _id = page.getID_NAME();
                            objectId = page.getLastId();
                        }
                        switch (direction) {
                            case ASC: {
                                criteria.and(_id).gt(objectId);
                                break;
                            }
                            case DESC: {
                                criteria.and(_id).lt(objectId);
                                break;
                            }
                        }
                        sort = sort.and(Sort.by(direction, page.getID_NAME()));
                        query.addCriteria(criteria);
                    } else if (!StringUtil.isEmpty(lastId)) {
                        switch (direction) {
                            case ASC: {
                                criteria.and(ID).gt(new ObjectId((String) lastId));
                                break;
                            }
                            case DESC: {
                                criteria.and(ID).lt(new ObjectId((String) lastId));
                                break;
                            }
                        }
                        sort = sort.and(Sort.by(direction, ID));
                        query.addCriteria(criteria);
                    }
                }
                query.with(page);
                query.with(sort);
                query.skip(0);
                query.limit(page.getPageSize());
            } else {
                int skip = page.getPageSize() * page.getPageNumber();
                query.skip(skip).limit(page.getPageSize());
            }
            result = executor.find(ms, query, page, converter);
        } else {
            result = executor.find(ms, parameter, pageable, converter);
        }
        return page.setResult(result);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy