/*
   * (non-Javadoc)
   *
   * @see uk.ac.cam.caret.sakai.rwiki.service.api.HttpCommand#execute(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  public void execute(
      Dispatcher dispatcher, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    RequestScopeSuperBean rssb = RequestScopeSuperBean.getFromRequest(request);

    ViewParamsHelperBean vphb = rssb.getNameHelperBean();
    AuthZGroupEditBean realmEditBean = rssb.getRealmEditBean();
    String requestedRealmId = realmEditBean.getLocalSpace();

    String saveType = vphb.getSaveType();

    AuthzGroup realmEdit;
    try {
      if (realmEditBean.getRealmEdit() == null) {
        realmEdit = realmService.getAuthzGroup(requestedRealmId);
        realmEditBean.setRealmEdit(realmEdit);

      } else {
        // Check the current realm edit is the same as the requested
        // one:
        realmEdit = realmEditBean.getRealmEdit();
        if (!requestedRealmId.equals(realmEdit.getId())) {
          // TODO: set errorBean and ignore current request i.e. go
          // back to start
          ErrorBean errorBean = rssb.getErrorBean();
          ResourceLoaderBean rlb = rssb.getResourceLoaderBean();

          errorBean.addError(
              rlb.getString("editauthz.currently_editing1", "You are currently editing: ")
                  + realmEdit.getId()
                  + rlb.getString(
                      "editauthz.currently_editing2",
                      ". You must finish editing this realm first"));
          realmEditBean.setLocalSpace(realmEdit.getId());
          saveType = null;
        }
      }

      if (saveType == null || "".equals(saveType)) {
        // Begin a realmEdit...
        editDispatch(dispatcher, request, response);
        return;
      } else if (saveType.equals(AuthZGroupEditBean.CANCEL_VALUE)) {
        // cancel a realmEdit...
        // TODO: CHECK We beleive that this is no longer needed since
        // locking in authz group
        // is optimistic
        // realmService.cancelEdit(realmEdit);
        realmEditBean.setRealmEdit(null);
        cancelDispatch(dispatcher, request, response);

        String pageName = vphb.getGlobalName();
        String realm = vphb.getLocalSpace();
        ViewBean vb = new ViewBean(pageName, realm);
        String requestURL = request.getRequestURL().toString();
        SessionManager.getCurrentToolSession()
            .setAttribute(RWikiServlet.SAVED_REQUEST_URL, requestURL + vb.getInfoUrl());

        return;
      } else if (saveType.equals(AuthZGroupEditBean.SAVE_VALUE)) {
        // complete a realmEdit...
        Map requestMap = request.getParameterMap();

        for (Iterator it = realmEdit.getRoles().iterator(); it.hasNext(); ) {
          Role roleEdit = (Role) it.next();
          updateRoleEdit(roleEdit, requestMap);
        }

        realmService.save(realmEdit);
        realmEditBean.setRealmEdit(null);
        successfulDispatch(dispatcher, request, response);

        String pageName = vphb.getGlobalName();
        String realm = vphb.getLocalSpace();
        ViewBean vb = new ViewBean(pageName, realm);
        String requestURL = request.getRequestURL().toString();
        SessionManager.getCurrentToolSession()
            .setAttribute(RWikiServlet.SAVED_REQUEST_URL, requestURL + vb.getInfoUrl());
      }
    } catch (GroupNotDefinedException e) {
      realmEditBean.setRealmEdit(null);
      unknownRealmDispatch(dispatcher, request, response);
      return;
    } catch (AuthzPermissionException e) {
      // redirect to permission denied page
      realmEditBean.setRealmEdit(null);
      permissionDeniedDispatch(dispatcher, request, response);
      return;
    }
  }
 public ViewBean newViewBean(RequestScopeSuperBean rssb) {
   ViewBean vb = new ViewBean();
   vb.setLocalSpace(rssb.getCurrentLocalSpace());
   return vb;
 }