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

com.jpattern.core.command.ICommand Maven / Gradle / Ivy

package com.jpattern.core.command;

import java.io.Serializable;

import com.jpattern.core.IProvider;
import com.jpattern.core.exception.NullProviderException;
import com.jpattern.logger.ILogger;
import com.jpattern.logger.SystemOutLogger;

/**
 * 
 * @author Francesco Cina'
 *
 * 11/set/2011
 */
public abstract class ICommand implements Serializable  {
    
	private static final long serialVersionUID = 1L;
	private ICommandExecutor commandExecutor;
	private T provider;
	private ILogger logger = null;

	ICommand() {}
	
    /**
     * This method launch the execution of the command (or chain of commands) using the default
     * default Executor.
     * This command is the same of:
     * exec(new DefaultCommandExecutor());
     * @return the result of the execution
     */
	public final ICommandResult exec() {
		return exec(new DefaultCommandExecutor());
	}
	
    /**
     * This method launch the execution of the command (or chain of commands).
     * Every command in the chain will be managed by an ICommandExecutor object
     * @param aCommandExecutor the pool in which the command will runs
     * @return the result of the execution
     */
	public final ICommandResult exec(ICommandExecutor commandExecutor) {
		return exec( commandExecutor, new CommandResult());
	}

    /**
     * This method launch the rollback of the command execution (or chain of commands) using the default
     * default Executor.
     * The rollback is effectively performed only if the command has been executed with a positive result, otherwise
     * the command is intended as "not executed" then no rollback will be performed. 
     * This command is the same of:
     * rollback(new DefaultCommandExecutor());
     * @return the result of the rollback
     */
	public final ICommandResult rollback() {
		return rollback(new DefaultCommandExecutor());
	}
	
    /**
     * This method launch the rollback of the command execution (or chain of commands) using a custom command executor.
     * The rollback is effectively performed only if the command has been executed with a positive result, otherwise
     * the command is intended as "not executed" then no rollback will be performed. 
     * @return the result of the rollback
     */
	public final ICommandResult rollback(ICommandExecutor commandExecutor) {
		return rollback(commandExecutor, new CommandResult());
	}
	
	protected final ICommandResult exec(ICommandExecutor commandExecutor, ACommandResult commandResult) {
		this.commandExecutor = commandExecutor;
		getCommandExecutor().execute(this, commandResult);
		return commandResult;
	}
	
	protected final ICommandResult rollback(ICommandExecutor commandExecutor, ACommandResult commandResult) {
		this.commandExecutor = commandExecutor;
		getCommandExecutor().rollback(this, commandResult);
		return commandResult;
	}
	
	protected final ICommandExecutor getCommandExecutor() {
		if (commandExecutor==null) {
			commandExecutor = new DefaultCommandExecutor();
		}
		return commandExecutor;
	}
	
	protected final T getProvider() {
		if (provider==null) {
			throw new NullProviderException();
		}
		return provider;
	}
	
	public final void visit(T provider) {
		this.provider = provider;
	}
	
	protected final ILogger getLogger() {
		if (logger == null) {
			if (provider!=null) {
				logger = provider.getLoggerService().logger(this.getClass());
			} else {
				logger = new SystemOutLogger(this.getClass());
			}
		}
		return logger;
	}
	
	protected abstract void doExec(ACommandResult commandResult);

	protected abstract void doRollback(ACommandResult commandResult);
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy