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.PageException;
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.PageHelper;
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 com.github.aidensuen.mongo.util.StringUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.ClassUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
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;
    private String default_processor_class = PageHelper.class.getName();

    @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 page-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;
    }

    public void setProperties(Properties properties) {
        String processorClass = properties.getProperty("processor");
        if (StringUtil.isEmpty(processorClass)) {
            processorClass = default_processor_class;
        }
        try {
            Class pageProcessor = ClassUtils.forName(processorClass, ClassUtils.getDefaultClassLoader());
            this.processor = (PageProcessor) pageProcessor.newInstance();
        } catch (Exception e) {
            throw new PageException(e);
        }
        this.processor.setProperties(properties);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy