/* vim: set ts=2 et sw=2 cindent fo=qroca: */
package com.globant.google.mendoza.command;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.globant.google.mendoza.malbec.SchemaValidator;
import com.globant.google.mendoza.malbec.SchemaValidator.
SchemaValidationResult;
import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;
/** Represents the command that validates the shopping cart.
*/
public final class MendozaAssertCartCommand extends
MendozaBaseCommand {
/** The class logger.
*/
private static Log log = LogFactory.getLog(MendozaAssertCartCommand.class);
/** Creates an instance of MendozaAssertCartCommand.
*
* @param mendozaRequest The mendoza server request.
*
* @param mendoza The mendoza server.
*
* @param mendozaState The mendoza server state.
*/
public MendozaAssertCartCommand(
final MendozaRequest mendozaRequest, final MendozaServer mendoza,
final MendozaServerState mendozaState) {
super(mendozaRequest, mendoza, mendozaState);
setName("Assert shopping cart");
}
/** Executes the schema validation of the shopping cart.
*/
public void execute() {
log.trace("Entering execute");
MendozaCommandResult result = new MendozaCommandResult();
String commandResultMsg =
"The shopping cart is OK.";
try {
if (getMendozaState().getShoppingCart() == null) {
log.trace("Leaving execute");
throw new RuntimeException(
"No shopping cart found."
+ " Please checkout one first.");
}
SchemaValidator validator = new SchemaValidator();
SchemaValidationResult validationResult =
validator.validate(getMendozaState().getShoppingCart());
if (!validationResult.isXmlValid()) {
log.trace("Leaving execute");
throw new RuntimeException(
"The shopping cart XML is not valid. "
+ validationResult.getMessage());
}
result.setSuccess(commandResultMsg);
} catch (RuntimeException e) {
commandResultMsg = e.getMessage();
result.setError(commandResultMsg);
}
setResult(result);
logCommand(this);
log.trace("Leaving execute");
}
}