/**
   * Applies the selected errata
   *
   * @param mapping ActionMapping
   * @param formIn ActionForm
   * @param request ServletRequest
   * @param response ServletResponse
   * @return The ActionForward to go to next.
   */
  public ActionForward applyErrata(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {

    Map<String, Object> params = new HashMap<String, Object>();

    RequestContext requestContext = new RequestContext(request);
    StrutsDelegate strutsDelegate = getStrutsDelegate();
    // if they chose errata, send them to the confirmation page
    Long sid = requestContext.getParamAsLong("sid");

    User user = requestContext.getCurrentUser();
    RhnSet set = getSetDecl(sid).get(user);

    // if they chose no errata, return to the same page with a message
    if (set.isEmpty()) {
      ActionMessages msg = new ActionMessages();
      msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errata.applynone"));
      params = makeParamMap(formIn, request);
      strutsDelegate.saveMessages(request, msg);
      return strutsDelegate.forwardParams(mapping.findForward(RhnHelper.DEFAULT_FORWARD), params);
    }

    if (sid != null) {
      params.put("sid", sid);
    }

    return strutsDelegate.forwardParams(mapping.findForward(RhnHelper.CONFIRM_FORWARD), params);
  }
  /** {@inheritDoc} */
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response) {

    StrutsDelegate strutsDelegate = getStrutsDelegate();

    ActionForward forward = null;
    DynaActionForm f = (DynaActionForm) form;
    RequestContext requestContext = new RequestContext(request);
    Long sid = requestContext.getRequiredParam("sid");
    User user = requestContext.getLoggedInUser();
    Server server = SystemManager.lookupByIdAndUser(sid, user);
    request.setAttribute("system", server);

    DynaActionForm dynaForm = (DynaActionForm) form;
    DatePicker picker =
        getStrutsDelegate()
            .prepopulateDatePicker(request, dynaForm, "date", DatePicker.YEAR_RANGE_POSITIVE);

    request.setAttribute("date", picker);

    if (!isSubmitted(f)) {
      setup(request, f);
      forward =
          strutsDelegate.forwardParams(mapping.findForward("default"), request.getParameterMap());
    } else {
      ActionMessages msgs = processForm(user, server, f, request);
      strutsDelegate.saveMessages(request, msgs);

      String mode = (String) f.get("mode");
      forward =
          strutsDelegate.forwardParams(
              mapping.findForward(getForward(mode)), request.getParameterMap());
    }

    return forward;
  }
  /**
   * Executes the appropriate PackageAction
   *
   * @param mapping ActionMapping
   * @param formIn ActionForm
   * @param request ServletRequest
   * @param response ServletResponse
   * @return The ActionForward to go to next.
   */
  public ActionForward executePackageAction(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {

    RequestContext requestContext = new RequestContext(request);
    StrutsDelegate strutsDelegate = getStrutsDelegate();

    Long sid = requestContext.getRequiredParam("sid");
    User user = requestContext.getCurrentUser();
    // updateList(newactions, user.getId());

    List<Map<String, Long>> data = PackageListItem.toKeyMaps(getDataResult(request));
    int numPackages = data.size();

    // Archive the actions
    Server server = SystemManager.lookupByIdAndUser(sid, user);

    // The earliest time to perform the action.
    DynaActionForm dynaActionForm = (DynaActionForm) formIn;
    Date earliest =
        getStrutsDelegate().readDatePicker(dynaActionForm, "date", DatePicker.YEAR_RANGE_POSITIVE);

    // The action chain to append this action to, if any
    ActionChain actionChain = ActionChainHelper.readActionChain(dynaActionForm, user);

    PackageAction pa = schedulePackageAction(formIn, requestContext, data, earliest, actionChain);

    // Remove the actions from the users set
    SessionSetHelper.obliterate(request, getDecl(sid));

    ActionMessages msgs = new ActionMessages();

    if (actionChain == null) {
      /**
       * If there was only one action archived, display the "action" archived message, else display
       * the "actions" archived message.
       */
      if (numPackages == 1) {
        msgs.add(
            ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage(
                getMessageKeyForOne(),
                LocalizationService.getInstance().formatNumber(numPackages),
                pa.getId().toString(),
                sid.toString(),
                StringUtil.htmlifyText(server.getName())));
      } else {
        msgs.add(
            ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage(
                getMessageKeyForMany(),
                LocalizationService.getInstance().formatNumber(numPackages),
                pa.getId().toString(),
                sid.toString(),
                StringUtil.htmlifyText(server.getName())));
      }
    } else {
      msgs.add(
          ActionMessages.GLOBAL_MESSAGE,
          new ActionMessage(
              "message.addedtoactionchain",
              actionChain.getId(),
              StringUtil.htmlifyText(actionChain.getLabel())));
    }

    strutsDelegate.saveMessages(request, msgs);
    Map params = new HashMap();
    processParamMap(formIn, request, params);
    return strutsDelegate.forwardParams(mapping.findForward(RhnHelper.CONFIRM_FORWARD), params);
  }