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

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

There is a newer version: 3.6.2
Show newest version
package com.jpattern.core.command;

import com.jpattern.core.IProvider;
import com.jpattern.shared.result.ErrorMessage;

/**
 * 
 * @author Francesco Cina'
 *
 * 11/set/2011
 */
public abstract class ACommand extends ICommand {

	private boolean executed = false;
	private boolean rolledback = false;

	@Override
	protected final void doExec(boolean catchRuntimeException, ACommandResult commandResult) {
		try {
			int errorSize = commandResult.getErrorMessages().size();
			executed = false;
			rolledback = false;
			execute(commandResult);
			executed = commandResult.getErrorMessages().size() == errorSize;
		} catch (RuntimeException e) {
			if (!catchRuntimeException) throw e;
			getLogger().error("doExec", "RuntimeException thrown", e);
			commandResult.addErrorMessage(new ErrorMessage(ICommandErrorsCostants.RUNTIME_EXEC_ERROR_NAME, e.getMessage()));			
		} finally {
			commandResult.setExecutionEnd(this);
		}
	}
	
	@Override
	protected final void doRollback(boolean catchRuntimeException, ACommandResult commandResult) {
		if (executed && !rolledback) {
			try {
				rollback(commandResult);
				rolledback = true;
			} catch (RuntimeException e) {
				if (!catchRuntimeException) throw e;
				getLogger().error("doRollback", "RuntimeException thrown while rollbacking", e);
				commandResult.addErrorMessage(new ErrorMessage(ICommandErrorsCostants.RUNTIME_ROLLBACK_ERROR_NAME, e.getMessage()));
			} 
		} 
		commandResult.setExecutionEnd(this);
	}
	
	protected abstract void execute(ICommandResult commandResult);

	protected abstract void rollback(ICommandResult commandResult);
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy