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

com.github.aidensuen.mongo.interceptor.PageInterceptor Maven / Gradle / Ivy

package com.github.aidensuen.mongo.interceptor;

import com.github.aidensuen.mongo.annotation.Intercepts;
import com.github.aidensuen.mongo.annotation.Signature;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.executor.Executor;
import com.github.aidensuen.mongo.pagehelper.ExecutorUtil;
import com.github.aidensuen.mongo.pagehelper.PageProcessor;
import com.github.aidensuen.mongo.plugin.Interceptor;
import com.github.aidensuen.mongo.plugin.Invocation;
import com.github.aidensuen.mongo.plugin.Plugin;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

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


@Intercepts(
        {
                @Signature(type = Executor.class, method = "find", args = {MongoDaoStatement.class, Object.class, Pageable.class, Function.class}),
        }
)
public class PageInterceptor implements Interceptor {

    private PageProcessor processor;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            Object[] args = invocation.getArgs();
            MongoDaoStatement ms = (MongoDaoStatement) args[0];
            Object parameter = args[1];
            Pageable pageable = (Pageable) args[2];
            Function function = (Function) args[3];
            Executor executor = (Executor) invocation.getTarget();

            List result;
            if (!processor.skip(ms, parameter, pageable)) {
                if (processor.beforeCount(ms, parameter, pageable)) {
                    Long count = executor.count(ms, parameter);
                    if (!processor.afterCount(count, ms, parameter, pageable)) {
                        return processor.afterPage(new ArrayList(), parameter, pageable);
                    }
                }
                if (processor.beforePage(ms, parameter, pageable)) {
                    Sort.Direction direction = processor.getLastIdDirection(ms, parameter, pageable);
                    Object lastId = processor.getLastId(ms, parameter, pageable);
                    result = ExecutorUtil.pageQuery(executor, ms, parameter, pageable, function, lastId, direction).getResult();
                } else {
                    result = executor.find(ms, parameter, pageable, function);
                }
            } else {
                //Do not use pager-helper paging
                return executor.find(ms, parameter, pageable, function);
            }
            return processor.afterPage(result, parameter, pageable);
        } finally {
            processor.afterAll();
        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    public void setProcessor(PageProcessor processor) {
        this.processor = processor;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy