Package com.globant.google.mendoza.command

Source Code of com.globant.google.mendoza.command.MendozaRefundOrderCommand

/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;
import com.globant.google.mendoza.malbec.Order;

/** Represents the mendoza refund order command.
*/
public final class MendozaRefundOrderCommand extends MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaRefundOrderCommand.class);

  /** The amount label. */
  private final String refundAmountLabel = "Amount";

  /** The order amount to refund. */
  private float amountToRefund = -1;

  /** The reason label. */
  private final String reasonLabel = "Reason";

  /** The comment label. */
  private final String commentLabel = "Comment";

  /** The reason to refund the order. */
  private String reason = "";

  /** The comment to refund the order. */
  private String comment = "";

  /** Creates an instance of MendozaRefundOrderCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaRefundOrderCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
    super(mendozaRequest, mendoza, mendozaState);
    setName("Refund order");
  }

  /** Executes the mendoza server refund order command.
   */
  public void execute() {
    log.trace("Entering execute");
    MendozaCommandResult result = new MendozaCommandResult();
    String commandResultMsg =
        "OK - refund order command sent to the mendoza server.";
    try {
      MendozaServerState state = this.getMendozaState();
      if (!state.isNewOrderNotificationReceived()
          || !state.isRiskInformationNotificationReceived()) {
        throw new RuntimeException(
            "The order can not be refunded in its"
            + " current financial order state.");
      }
      setRequestParameters();
      Order order = this.getMendozaState().getOrder();
      if (amountToRefund < 0) {
        order.refund(reason, comment);
      } else {
        order.refund(new BigDecimal(amountToRefund), reason, comment);
      }
      result.setSuccess(commandResultMsg);
    } catch (RuntimeException e) {
      commandResultMsg = e.getMessage();
      result.setError(commandResultMsg);
    }
    setResult(result);
    logCommand(this);
    log.trace("Leaving execute");
  }

  /** Sets the refund order command parameters.
   */
  private void setRequestParameters() {
    log.trace("Entering setRequestParameters");
    reason = getMendozaRequest().getRequestParameters().getProperty("reason");
    comment = getMendozaRequest().getRequestParameters().getProperty("comment");
    if (reason == null) {
      log.trace("Leaving setRequestParameters");
      throw new RuntimeException("The reason cannot be null");
    }
    if (comment == null) {
      comment = reason;
    }
    logRequestParameter(reasonLabel, reason);
    logRequestParameter(commentLabel, comment);
    String amountStr =
      getMendozaRequest().getRequestParameters().getProperty("amount");
    if (amountStr == null) {
      logRequestParameter(refundAmountLabel, "Fully");
    } else {
      try {
        amountToRefund = Float.parseFloat(amountStr);
      } catch (Exception e) {
        log.trace("Leaving setRequestParameters");
        throw new RuntimeException("The amount must be a positive number");
      }
      if (amountToRefund < 0) {
        log.trace("Leaving setRequestParameters");
        throw new RuntimeException("The amount cannot be a negative number");
      }
      logRequestParameter(refundAmountLabel, amountStr);
    }
    log.trace("Leaving setRequestParameters");
  }
}
TOP

Related Classes of com.globant.google.mendoza.command.MendozaRefundOrderCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.