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

com.github.aidensuen.mongo.session.Configuration Maven / Gradle / Ivy

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

import com.github.aidensuen.mongo.builder.annotation.MethodResolver;
import com.github.aidensuen.mongo.builder.annotation.MongoDaoAnnotationBuilder;
import com.github.aidensuen.mongo.builder.MongoDaoBuilder;
import com.github.aidensuen.mongo.command.DynamicCommandBuilder;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.core.proxy.MongoDaoRegistry;
import com.github.aidensuen.mongo.exception.MongoDaoException;
import com.github.aidensuen.mongo.executor.BatchExecutor;
import com.github.aidensuen.mongo.executor.Executor;
import com.github.aidensuen.mongo.executor.SimpleExecutor;
import com.github.aidensuen.mongo.plugin.Interceptor;
import com.github.aidensuen.mongo.plugin.InterceptorChain;
import com.github.aidensuen.mongo.reflection.ParamNameResolver;
import com.github.aidensuen.mongo.support.MapToObjectConverter;
import com.github.aidensuen.mongo.util.MsUtil;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Configuration {

    public static final String PREFIX = "mongodao";

    protected boolean useActualParamName = true;

    protected boolean safeDelete = true;

    protected boolean safeUpdate = true;

    protected MongoDaoRegistry registy = new MongoDaoRegistry(this);

    protected Map> entityClassMap = new ConcurrentHashMap>();

    protected Map resolvers = new ConcurrentHashMap<>();

    protected Map mongoDaoStatements = new ConcurrentHashMap<>();

    protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;

    protected final InterceptorChain interceptorChain = new InterceptorChain();

    protected ExpressionParser parser = new SpelExpressionParser();

    protected ParserContext parserContext = new TemplateParserContext();

    protected DynamicCommandBuilder builder = new DynamicCommandBuilder(this);

    protected Environment environment;

    protected ConversionService conversionService = new DefaultConversionService();

    protected Collection incompleteMethods;

    public Configuration(Environment environment) {
        this();
        this.environment = environment;
    }

    public Configuration() {
        this.incompleteMethods = new LinkedList<>();
        ((ConverterRegistry) this.conversionService).addConverter(new MapToObjectConverter());
    }

    public MongoDaoRegistry getRegisty() {
        return registy;
    }

    public void setRegisty(MongoDaoRegistry registy) {
        this.registy = registy;
    }

    public DynamicCommandBuilder getBuilder() {
        return builder;
    }

    public  void addMongoDao(Class type) {
        registy.addMongoDao(type);
    }

    public  T getMongoDao(Class type, MongoSession mongoSession) {
        return registy.getMongoDao(type, mongoSession);
    }

    public ConversionService getConversionService() {
        return conversionService;
    }

    public boolean hasMongoDao(Class type) {
        return registy.hasMongoDao(type);
    }

    public void addMongoDaoStatement(MongoDaoStatement ms) {
        mongoDaoStatements.put(ms.getId(), ms);
    }

    public Collection getMongoDaoStatementNames() {
        this.buildAllStatements();
        return mongoDaoStatements.keySet();
    }

    public Collection getMongoDaoStatements() {
        this.buildAllStatements();
        return mongoDaoStatements.values();
    }

    public MongoDaoStatement getMongoDaoStatement(String id) {
        return this.getMongoDaoStatement(id, true);
    }

    public MongoDaoStatement getMongoDaoStatement(String id, boolean validateIncompleteStatements) {
        if (validateIncompleteStatements) {
            this.buildAllStatements();
        }
        return mongoDaoStatements.get(id);
    }

    public Environment getEnvironment() {
        return environment;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public ParamNameResolver getResolver(String id) {
        return resolvers.get(id);
    }

    public ParamNameResolver addResolver(String id, ParamNameResolver resolver) {
        return resolvers.put(id, resolver);
    }

    public void addIncompleteMethod(MethodResolver builder) {
        incompleteMethods.add(builder);
    }

    public Collection getIncompleteMethods() {
        return incompleteMethods;
    }

    public boolean hasStatement(String statementName) {
        return this.hasStatement(statementName, true);
    }

    public boolean hasStatement(String statementName, boolean validateIncompleteStatements) {
        if (validateIncompleteStatements) {
            this.buildAllStatements();
        }
        return mongoDaoStatements.containsKey(statementName);
    }

    public void buildMongoDaoStatement(Class mongoDaoInterface) {
        try {
            MongoDaoBuilder builder = new MongoDaoAnnotationBuilder(this, mongoDaoInterface);
            builder.parse();
        } catch (Exception e) {
            throw new MongoDaoException("build MongoDaoStatement failed ! " + e.toString());
        }
    }

    protected void buildAllStatements() {
        if (!this.incompleteMethods.isEmpty()) {
            synchronized (this.incompleteMethods) {
                this.incompleteMethods.iterator().next().resolve();
            }
        }
    }

    public Class getEntityClass(String statement) {
        try {
            String msId = statement;
            if (entityClassMap.containsKey(msId)) {
                return entityClassMap.get(msId);
            } else {
                Class mongoDaoClass = MsUtil.getMongoDaoClass(msId);
                ResolvableType resolvableType = ResolvableType.forClass(mongoDaoClass);
                if (resolvableType.getInterfaces().length > 0) {
                    return resolvableType.getInterfaces()[0].getGeneric(0).resolve();
                }
            }
        } catch (Exception e) {

        }
        return null;
    }

    public Executor newExecutor(MongoOperations mongoOperations, ExecutorType executorType) {
        executorType = executorType == null ? defaultExecutorType : executorType;
        Executor executor = null;
        if (ExecutorType.BATCH == executorType) {
            executor = new BatchExecutor(this, mongoOperations);
        } else {
            executor = new SimpleExecutor(this, mongoOperations);
        }
        executor = (Executor) interceptorChain.pluginAll(executor);
        return executor;
    }

    public ExecutorType getDefaultExecutorType() {
        return defaultExecutorType;
    }

    public boolean isUseActualParamName() {
        return useActualParamName;
    }

    public void setUseActualParamName(boolean useActualParamName) {
        this.useActualParamName = useActualParamName;
    }

    public boolean isSafeDelete() {
        return safeDelete;
    }

    public void setSafeDelete(boolean safeDelete) {
        this.safeDelete = safeDelete;
    }

    public boolean isSafeUpdate() {
        return safeUpdate;
    }

    public void setSafeUpdate(boolean safeUpdate) {
        this.safeUpdate = safeUpdate;
    }

    public ExpressionParser getParser() {
        return parser;
    }

    public void setParser(ExpressionParser parser) {
        this.parser = parser;
    }


    public ParserContext getParserContext() {
        return parserContext;
    }

    public void setParserContext(ParserContext parserContext) {
        this.parserContext = parserContext;
    }

    public void setPlugins(Class... plugins) {
        if (plugins != null) {
            for (Class classz : plugins) {
                try {
                    addInterceptor((Interceptor) classz.newInstance());
                } catch (InstantiationException | IllegalAccessException e) {

                }
            }
        }
    }

    public void setPlugins(List plugins) {
        if (plugins != null) {
            for (Interceptor interceptor : plugins) {
                addInterceptor(interceptor);
            }
        }
    }

    public void addInterceptor(Interceptor interceptor) {
        interceptorChain.addInterceptor(interceptor);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy