private List<Map<String, Long>> getPackages(
     User user, HttpServletRequest request, String sessionSetLabel) {
   List<Map<String, Long>> packs;
   Set<String> set = SessionSetHelper.lookupAndBind(request, sessionSetLabel);
   packs = toList(set);
   return packs;
 }
  private ActionMessages processForm(
      User user, Server server, DynaActionForm f, HttpServletRequest request) {
    if (log.isDebugEnabled()) {
      log.debug("Processing form.");
    }

    // scheduleScriptRun takes a list of servers
    List servers = new ArrayList();
    servers.add(server);

    ActionMessages msgs = new ActionMessages();

    Boolean submitted = (Boolean) f.get("submitted");
    String runBefore = (String) f.get("run_script");
    String username = (String) f.get("username");
    String group = (String) f.get("group");
    Long timeout = (Long) f.get("timeout");
    String script = (String) f.get("script");
    String sessionSetLabel = (String) f.get("session_set_label");
    String mode = (String) f.get("mode");

    if (log.isDebugEnabled()) {
      log.debug("submitted [" + submitted + "]");
      log.debug("runBefore [" + runBefore + "]");
      log.debug("username [" + username + "]");
      log.debug("group [" + group + "]");
      log.debug("timeout [" + timeout + "]");
      log.debug("script [" + script + "]");
      log.debug("mode [" + mode + "]");
    }

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

    if (BEFORE.equals(runBefore)) {
      ScriptActionDetails sad = ActionManager.createScript(username, group, timeout, script);
      ScriptRunAction sra = ActionManager.scheduleScriptRun(user, servers, "", sad, earliest);
      List<Map<String, Long>> packs = getPackages(user, request, sessionSetLabel);
      int numPackages = packs.size();
      PackageAction pa = schedulePackageAction(user, server, packs, mode, earliest);
      pa.setPrerequisite(sra);
      ActionManager.storeAction(pa);
      showMessages(msgs, pa, server, numPackages, mode);
      showRemoteCommandMsg(msgs, true, mode);
    } else {
      List<Map<String, Long>> packs = getPackages(user, request, sessionSetLabel);
      int numPackages = packs.size();
      PackageAction pa = schedulePackageAction(user, server, packs, mode, earliest);
      ScriptActionDetails sad = ActionManager.createScript(username, group, timeout, script);
      ScriptRunAction sra = ActionManager.scheduleScriptRun(user, servers, "", sad, earliest);
      sra.setPrerequisite(pa);
      ActionManager.storeAction(sra);
      showMessages(msgs, sra, server, numPackages, mode);
      showRemoteCommandMsg(msgs, false, mode);
    }
    SessionSetHelper.obliterate(request, sessionSetLabel);
    return msgs;
  }
  private List<PackageListItem> getDataResult(HttpServletRequest request) {
    RequestContext requestContext = new RequestContext(request);

    Long sid = requestContext.getRequiredParam("sid");

    Set<String> data = SessionSetHelper.lookupAndBind(request, getDecl(sid));
    List<PackageListItem> items = new LinkedList<PackageListItem>();
    for (String key : data) {
      items.add(PackageListItem.parse(key));
    }
    return items;
  }
  /**
   * Callback for the Subscribe to channels button, it attempts to subscribe to the channels
   * containing the Packages.
   *
   * @param mapping ActionMapping
   * @param formIn ActionForm
   * @param request HttpServletRequest
   * @param response HttpServletResponse
   * @return ActionForward
   */
  public ActionForward subscribeToChannels(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {
    RequestContext requestContext = new RequestContext(request);
    Long sid = requestContext.getRequiredParam("sid");
    Set<String> pkgIdCombos = SessionSetHelper.lookupAndBind(request, getDecl(requestContext, sid));
    Map params = new HashMap();
    params.put("sid", sid);

    syncToVictim(requestContext, sid, pkgIdCombos, ProfileManager.OPTION_SUBSCRIBE);
    return getStrutsDelegate().forwardParams(mapping.findForward("showprofile"), params);
  }
  /** {@inheritDoc} */
  protected DataResult getDataResult(User user, ActionForm formIn, HttpServletRequest request) {
    RequestContext requestContext = new RequestContext(request);
    Long sid = requestContext.getRequiredParam("sid");
    Set<String> pkgIdCombos = SessionSetHelper.lookupAndBind(request, getDecl(requestContext, sid));

    if (isProfileSync(requestContext)) {
      Long prid = requestContext.getRequiredParam("prid");

      return ProfileManager.getMissingProfilePackages(
          requestContext.getCurrentUser(), sid, prid, pkgIdCombos, null);
    } else if (isSystemSync(requestContext)) {
      Long sid1 = requestContext.getRequiredParam("sid_1");

      return ProfileManager.getMissingSystemPackages(
          requestContext.getCurrentUser(), sid, sid1, pkgIdCombos, null);
    }

    return null;
  }
  /**
   * 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);
  }