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

com.github.aidensuen.mongo.executor.AbstractExecutor Maven / Gradle / Ivy

package com.github.aidensuen.mongo.executor;

import com.github.aidensuen.mongo.bson.CustomAggregationOperation;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.mapping.BoundCommand;
import com.github.aidensuen.mongo.mapping.ExampleStr;
import com.github.aidensuen.mongo.session.Configuration;
import com.github.aidensuen.mongo.util.MongoCommandUtil;
import com.github.aidensuen.util.StringUtil;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class AbstractExecutor implements Executor {

    protected Configuration configuration;

    protected MongoOperations mongoOperations;

    public AbstractExecutor(Configuration configuration, MongoOperations mongoOperations) {
        this.configuration = configuration;
        this.mongoOperations = mongoOperations;
    }

    @Override
    public  T save(MongoDaoStatement ms, T objectToSave) {
        return this.mongoOperations.save(objectToSave);
    }

    @Override
    public  T insert(MongoDaoStatement ms, T objectToSave) {
        return this.mongoOperations.insert(objectToSave);
    }

    @Override
    public  Collection insert(MongoDaoStatement ms, Collection batchToSave) {
        Class entityClass = ms.getEntityClass();
        if (entityClass != null && !Object.class.equals(entityClass)) {
            return this.mongoOperations.insert(batchToSave, ms.getEntityClass());
        } else {
            return this.mongoOperations.insertAll(batchToSave);
        }
    }

    @Override
    public  List find(MongoDaoStatement ms, Object parameter, Pageable pageable, Function converter) {
        Query query = null;
        if (parameter instanceof ExampleStr){
            query = new Query(MongoCommandUtil.buildCriteriaByExampleStr((ExampleStr)parameter));
        } else if (parameter instanceof Query){
            query = (Query) parameter;
        } else {
            BoundCommand boundCommand = ms.getBoundCommand(parameter);
            query = MongoCommandUtil.createQuery(boundCommand, ms);
        }
        List result = (List) doFind(ms, query, pageable);
        if (converter != null) {
            result = (List) result.stream().map(converter).collect(Collectors.toList());
        }
        return result;
    }

    protected Object doFind(MongoDaoStatement ms, Query query, Pageable pageable) {
        if (pageable != null) {
            query = query.with(pageable);
        }
        switch (ms.getOperationType()) {
            case FINDONE: {
                List list = new ArrayList();
                Object result = this.mongoOperations.findOne(query, ms.getEntityClass());
                list.add(result);
                return list;
            }
            default: {
                return this.mongoOperations.find(query, ms.getEntityClass());
            }
        }
    }

    @Override
    public UpdateResult update(MongoDaoStatement ms, Object parameter) {
        Query query = null;
        if (parameter instanceof ExampleStr){
            query = new Query(MongoCommandUtil.buildCriteriaByExampleStr((ExampleStr)parameter));
        } else if (parameter instanceof Query){
            query = (Query) parameter;
        } else {
            BoundCommand boundCommand = ms.getBoundCommand(parameter);
            query = MongoCommandUtil.createQuery(boundCommand, ms);
        }
        Update update = ms.getUpdate(parameter);
        switch (ms.getOperationType()) {
            case UPDATEONE: {
                return this.mongoOperations.updateFirst(query, update, ms.getEntityClass());
            }
            case UPSERT: {
                return this.mongoOperations.upsert(query, update, ms.getEntityClass());
            }
            case UPDATEMULTI: {
                return this.mongoOperations.updateMulti(query, update, ms.getEntityClass());
            }
        }
        return null;
    }

    @Override
    public long count(MongoDaoStatement ms, Object parameter) {
        Query query = null;
        if (parameter instanceof ExampleStr){
            query = new Query(MongoCommandUtil.buildCriteriaByExampleStr((ExampleStr)parameter));
        } else if (parameter instanceof Query){
            query = (Query) parameter;
        } else {
            BoundCommand boundCommand = ms.getBoundCommand(parameter);
            query = MongoCommandUtil.createQuery(boundCommand, ms);
        }
        return this.mongoOperations.count(query, ms.getEntityClass());
    }

    @Override
    public boolean exists(MongoDaoStatement ms, Object parameter) {
        Query query = null;
        if (parameter instanceof ExampleStr){
            query = new Query(MongoCommandUtil.buildCriteriaByExampleStr((ExampleStr)parameter));
        } else if (parameter instanceof Query){
            query = (Query) parameter;
        } else {
            BoundCommand boundCommand = ms.getBoundCommand(parameter);
            query = MongoCommandUtil.createQuery(boundCommand, ms);
        }
        return this.mongoOperations.exists(query, ms.getEntityClass());
    }

    @Override
    public  AggregationResults aggregate(MongoDaoStatement ms, Object parameter) {
        BoundCommand boundCommand = ms.getBoundCommand(parameter);
        List list = MongoCommandUtil.createPipeline(boundCommand);
        List operations = new ArrayList<>();
        list.forEach(doc -> operations.add(new CustomAggregationOperation(doc)));
        Class inputType = ms.getAggregateInfo().getInputType();
        Class outpuType = ms.getAggregateInfo().getOutputType();
        Aggregation aggregation = Aggregation.newAggregation(operations);
        String options = ms.getAggregateInfo().getOptions();
        if (!StringUtil.isEmpty(options)) {
            aggregation = aggregation.withOptions(AggregationOptions.fromDocument(Document.parse(options)));
        }
        return this.mongoOperations.aggregate(aggregation, inputType, outpuType);
    }

    @Override
    public Document executeCommand(MongoDaoStatement ms, Object parameter) {
        BoundCommand boundCommand = ms.getBoundCommand(parameter);
        Document command = MongoCommandUtil.createCommand(boundCommand);
        return this.mongoOperations.executeCommand(command);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy