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

co.com.sofka.infraestructure.handle.CommandExecutor Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package co.com.sofka.infraestructure.handle;

import co.com.sofka.business.asyn.UseCaseExecutor;
import co.com.sofka.business.generic.UseCase;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.logging.Logger;


/**
 * The type Command executor.
 */
public abstract class CommandExecutor implements CommandHandler> {

    private static final Logger logger = Logger.getLogger(CommandExecutor.class.getName());
    /**
     * The Handles.
     */
    protected Map>> handles = new ConcurrentHashMap<>();

    /**
     * Object Returned command
     */
    private UseCase.RequestValues request;


    /**
     * Put.
     *
     * @param type     the type of the command
     * @param consumer the consumer
     */
    protected void put(String type, Consumer> consumer) {
        handles.put(type, consumer);
    }

    @Override
    public final void execute(Map args) {

        if (!args.containsKey("commandType")) {
            throw new IllegalArgumentException("The commandType of the aggregate must be specified");
        }
        var type = args.get("commandType");

        if (!handles.containsKey(type)) {
            throw new ExecutionNoFound(type);
        }

        executeCommand(args, type);
    }

    private void executeCommand(Map args, String type) {
        logger.info("####### Executor Command #######");
        var consumer = handles.get(type);
        var useCaseExecutor = (UseCaseExecutor) consumer;

        if (args.containsKey("aggregateId")) {
            useCaseExecutor.withAggregateId(args.get("aggregateId"));
        }

        useCaseExecutor.accept(args);
        useCaseExecutor.run();
        request = useCaseExecutor.request();
    }

    /**
     * Use case request values
     *
     * @return request requerid for use case
     */
    public UseCase.RequestValues request() {
        return request;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy