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

com.github.aidensuen.mongo.core.MongoDaoStatement Maven / Gradle / Ivy

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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.aidensuen.mongo.command.CommandSource;
import com.github.aidensuen.mongo.command.DynamicCommandBuilder;
import com.github.aidensuen.mongo.command.DynamicCommandSource;
import com.github.aidensuen.mongo.command.OperationType;
import com.github.aidensuen.mongo.exception.BindingException;
import com.github.aidensuen.mongo.exception.InvalidParamException;
import com.github.aidensuen.mongo.mapping.AggregateInfo;
import com.github.aidensuen.mongo.mapping.BoundCommand;
import com.github.aidensuen.mongo.mapping.ParameterHolder;
import com.github.aidensuen.mongo.mapping.UpdateInfo;
import com.github.aidensuen.mongo.session.Configuration;
import com.github.aidensuen.mongo.session.impl.DefaultMongoSession;
import com.github.aidensuen.util.ReflectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Update;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

public final class MongoDaoStatement {

    private static Logger logger = LoggerFactory.getLogger(MongoDaoStatement.class);

    private String id;

    private Configuration configuration;

    private String[] paramNames;

    private OperationType operationType;

    private String methodName;

    private CommandSource commandSource;

    private String returnFields;

    private String sortFields;

    private UpdateInfo updateInfo;

    private AggregateInfo aggregateInfo;

    private boolean hasCommandAnnotation;

    private Class implementationClass;

    private Class entityClass;

    MongoDaoStatement() {

    }

    public String getId() {
        return id;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public String getMethodName() {
        return methodName;
    }

    public OperationType getOperationType() {
        return operationType;
    }

    public CommandSource getCommandSource() {
        return commandSource;
    }

    public String[] getParamNames() {
        return paramNames;
    }

    public String getReturnFields() {
        return returnFields;
    }

    public String getSortFields() {
        return sortFields;
    }

    public UpdateInfo getUpdateInfo() {
        return updateInfo;
    }

    public boolean hasCommandAnnotation() {
        return hasCommandAnnotation;
    }

    public AggregateInfo getAggregateInfo() {
        return aggregateInfo;
    }

    public Class getEntityClass() {
        if (entityClass != null) {
            return entityClass;
        }
        if (entityClass == null) {
            entityClass = configuration.getEntityClass(id);
        }
        return entityClass != null ? entityClass : null;
    }

    public Class getImplementationClass() {
        return implementationClass;
    }

    public BoundCommand getBoundCommand(Object parameterObject) {
        //solve one param issue
        if (getParamNames().length == 1) {
            if (!(parameterObject instanceof DefaultMongoSession.StrictMap) && this.commandSource instanceof DynamicCommandSource) {
                ParameterHolder parameterHolder = new ParameterHolder(configuration, parameterObject);
                parameterHolder.setAdditionalParameter(getParamNames()[0], parameterObject);
                parameterHolder.setContextVariable(getParamNames()[0], parameterObject);
                DynamicCommandBuilder builder = new DynamicCommandBuilder(configuration);
                CommandSource commandSource = builder.parse(((DynamicCommandSource) this.commandSource).getDynamicCommand().getDynamicCommand(), parameterHolder);
                BoundCommand boundCommand = commandSource.getBoundCommand(parameterObject);
                try {
                    ReflectionUtil.setProperty(boundCommand, "parameterHolder", parameterHolder);
                } catch (IllegalAccessException e) {
                    logger.warn("can't set property parameterHolder, {}", e);
                }
                return boundCommand;
            }
        }
        return this.commandSource.getBoundCommand(parameterObject);

    }

    public Update getUpdate(Object param) {
        if (isEntityClass(param)) {
            Map updateMap;
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                if (this.getUpdateInfo().isNullable()) {
                    objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
                    String json = objectMapper.writeValueAsString(param);
                    updateMap = objectMapper.readValue(json, Map.class);
                } else {
                    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                    String json = objectMapper.writeValueAsString(param);
                    updateMap = objectMapper.readValue(json, Map.class);
                }

            } catch (IOException e) {
                throw new BindingException("build update failed ! " + e.getMessage());
            }

            if (this.getUpdateInfo().getUpdateFields().length > 0) {
                Map updateMap1 = new HashMap<>();
                for (int i = 0; i < this.getUpdateInfo().getUpdateFields().length; i++) {
                    String name = this.getUpdateInfo().getUpdateFields()[i];
                    if (!this.getUpdateInfo().isNullable() && updateMap.get(name) != null) {
                        updateMap1.put(name, updateMap.get(name));
                    }
                }
                return buildUpdate(updateMap1);
            } else {
                return buildUpdate(updateMap);
            }
        } else {
            throw new InvalidParamException("param type must be " + getEntityClass().getName());
        }
    }

    private boolean isEntityClass(Object parameterObject) {
        Class parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
        if (Objects.equals(parameterType, getEntityClass())) {
            return true;
        }
        return false;
    }

    private Update buildUpdate(Map o) {
        Update localUpdate = new Update();
        Iterator> iterator = o.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            localUpdate.set(entry.getKey(), entry.getValue());
        }
        return localUpdate;
    }

    public static class Builder {

        private MongoDaoStatement mongoDaoStatement = new MongoDaoStatement();

        public Builder(Configuration configuration) {
            mongoDaoStatement.configuration = configuration;
        }

        public Builder id(String id) {
            mongoDaoStatement.id = id;
            return this;
        }

        public Builder methodName(String methodName) {
            mongoDaoStatement.methodName = methodName;
            return this;
        }

        public Builder operationType(OperationType operationType) {
            mongoDaoStatement.operationType = operationType;
            return this;
        }

        public Builder mongoCommand(CommandSource commandSource) {
            mongoDaoStatement.commandSource = commandSource;
            return this;
        }

        public Builder implementationClass(Class implementationClass) {
            mongoDaoStatement.implementationClass = implementationClass;
            return this;
        }

        public Builder hasCommandAnnotation(boolean hasCommandAnnotation) {
            mongoDaoStatement.hasCommandAnnotation = hasCommandAnnotation;
            return this;
        }

        public Builder paramNames(String[] paramNames) {
            mongoDaoStatement.paramNames = paramNames;
            return this;
        }

        public Builder returnFields(String returnFields) {
            mongoDaoStatement.returnFields = returnFields;
            return this;
        }

        public Builder sortFields(String sortFields) {
            mongoDaoStatement.sortFields = sortFields;
            return this;
        }

        public Builder updateInfo(UpdateInfo updateInfo) {
            mongoDaoStatement.updateInfo = updateInfo;
            return this;
        }

        public Builder aggregeteInfo(AggregateInfo aggregateInfo) {
            mongoDaoStatement.aggregateInfo = aggregateInfo;
            return this;
        }

        public Builder entityClass(Class entityClass) {
            mongoDaoStatement.entityClass = entityClass;
            return this;
        }

        public MongoDaoStatement build() {
            return mongoDaoStatement;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy