/**
   * Handles deletion of announcements
   *
   * @param topicId
   * @param annId
   * @param response
   * @throws PortletException
   */
  @RequestMapping(params = "action=deleteAnnouncement")
  public void actionDeleteAnnouncement(
      @RequestParam("topicId") Long topicId,
      @RequestParam("annId") Long annId,
      ActionRequest request,
      ActionResponse response)
      throws PortletException {

    Topic topic = announcementService.getTopic(topicId);
    Announcement ann = announcementService.getAnnouncement(annId);

    UserPermissionChecker upChecker =
        userPermissionCheckerFactory.createUserPermissionChecker(request, topic);
    if (upChecker.isAdmin()
        || upChecker.isModerator()
        || (upChecker.isAuthor()
            && ann.getAuthor() != null
            && ann.getAuthor().equals(request.getRemoteUser()))) {
      // the person deleting the announcement must be the author, a moderator or an admin
      announcementService.deleteAnnouncement(ann);
    } else {
      throw new UnauthorizedException("You do not have permission to delete this announcement");
    }

    response.setRenderParameter("topicId", topicId.toString());
    response.setRenderParameter("action", "showTopic");
  }
  @Override
  public void processAction(ActionRequest portletReq, ActionResponse portletResp)
      throws PortletException, IOException {
    LOGGER.entering(LOG_CLASS, "main portlet processAction entry");

    portletResp.setRenderParameters(portletReq.getParameterMap());
    long tid = Thread.currentThread().getId();
    portletReq.setAttribute(THREADID_ATTR, tid);

    StringWriter writer = new StringWriter();
  }
  /**
   * Saves the announcement
   *
   * @param req
   * @param res
   * @throws PortletException
   */
  @RequestMapping(params = "action=addAnnouncement")
  public void actionAddAnnouncementForm(
      @ModelAttribute("announcement") Announcement announcement,
      BindingResult result,
      SessionStatus status,
      ActionRequest req,
      ActionResponse res)
      throws PortletException {

    // First verify the user has AUTHOR permission for this topic
    UserPermissionChecker upChecker =
        userPermissionCheckerFactory.createUserPermissionChecker(req, announcement.getParent());
    if (!(upChecker.isAdmin() || upChecker.isModerator() || upChecker.isAuthor())) {
      throw new UnauthorizedException(
          "You do not have permission to create an announcement in this topic");
    }

    // Next validate the announcement
    new AnnouncementValidator(getAllowOpenEndDate(req), getAllowEmptyMessage(req))
        .validate(announcement, result);
    if (result.hasErrors()) {
      res.setRenderParameter("action", "addAnnouncement");
      return;
    }

    if (!result.hasErrors()) {
      if (!announcement.hasId()) {
        // add the automatic data
        announcement.setAuthor(req.getRemoteUser());
        announcement.setCreated(new Date());
        announcementService.addOrSaveAnnouncement(announcement);
      } else {
        announcementService.mergeAnnouncement(announcement);
      }

      status.setComplete();
      res.setRenderParameter("topicId", announcement.getParent().getId().toString());
      res.setRenderParameter("action", "showTopic");
    }
  }
  /* ACTION call from Portlet's <form> of EDIT JSP */
  public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws IOException, PortletException {

    String saveSettingsGoogleMapsUtility =
        actionRequest.getParameter("saveSettingsGoogleMapsUtility");
    /*You can add other getParameter of EDIT JSP*/

    if (saveSettingsGoogleMapsUtility != null) {
      PortletPreferences prefs = actionRequest.getPreferences();

      if (actionRequest.getParameter("inTypeUtility") != null)
        prefs.setValue("typeUtility", actionRequest.getParameter("inTypeUtility"));

      if (actionRequest.getParameter("inStreetView") != null)
        prefs.setValue("chkStreetView", actionRequest.getParameter("inStreetView"));
      if (actionRequest.getParameter("inZoom") != null)
        prefs.setValue("chkZoom", actionRequest.getParameter("inZoom"));
      if (actionRequest.getParameter("inDraggable") != null)
        prefs.setValue("chkDraggable", actionRequest.getParameter("inDraggable"));
      if (actionRequest.getParameter("inPanControl") != null)
        prefs.setValue("chkPanControl", actionRequest.getParameter("inPanControl"));
      if (actionRequest.getParameter("inRotateControl") != null)
        prefs.setValue("chkRotateControl", actionRequest.getParameter("inRotateControl"));
      if (actionRequest.getParameter("inScaleControl") != null)
        prefs.setValue("chkScaleControl", actionRequest.getParameter("inScaleControl"));

      if (actionRequest.getParameter("inTypeMap") != null)
        prefs.setValue("typeMap", actionRequest.getParameter("inTypeMap"));

      if (actionRequest.getParameter("inAddress") != null)
        prefs.setValue("address", actionRequest.getParameter("inAddress"));
      if (actionRequest.getParameter("inDescriptionMarker") != null)
        prefs.setValue("descriptionMarker", actionRequest.getParameter("inDescriptionMarker"));
      if (actionRequest.getParameter("inTypeIcon") != null)
        prefs.setValue("typeIcon", actionRequest.getParameter("inTypeIcon"));

      if (actionRequest.getParameter("inFromAddress") != null)
        prefs.setValue("fromAddress", actionRequest.getParameter("inFromAddress"));
      if (actionRequest.getParameter("inToAddress") != null)
        prefs.setValue("toAddress", actionRequest.getParameter("inToAddress"));

      prefs.store();
      actionResponse.setPortletMode(PortletMode.VIEW);
    }
  }
  @Override
  public void processAction(ActionRequest portletReq, ActionResponse portletResp)
      throws PortletException, IOException {
    LOGGER.entering(LOG_CLASS, "main portlet processAction entry");

    portletResp.setRenderParameters(portletReq.getParameterMap());
    long tid = Thread.currentThread().getId();
    portletReq.setAttribute(THREADID_ATTR, tid);

    StringWriter writer = new StringWriter();

    // Now do the actual dispatch
    String target =
        JSP_PREFIX
            + "DispatcherReqRespTests3_SPEC2_19_IncludeJSPActionResponse"
            + JSP_SUFFIX
            + "?"
            + QUERY_STRING;
    PortletRequestDispatcher rd = portletConfig.getPortletContext().getRequestDispatcher(target);
    rd.include(portletReq, portletResp);
  }
  @Override
  public void processAction(ActionRequest request, ActionResponse response)
      throws PortletException, IOException {
    String ctx = request.getContextPath();
    boolean isClose = "true".equals(request.getParameter("close"));

    if (isClose) {
      // Clean up the session
      LOG.debug("Closing the form");
      request.getPortletSession().removeAttribute(VIEW_TYPE);
      response.sendRedirect(ctx + CLOSE_PAGE);
      return;
    }

    int projectIdToBookmark = Integer.valueOf(request.getParameter(PROJECT_ID_TO_BOOKMARK));
    int projectIdOfLists = Integer.valueOf(request.getParameter(PROJECT_ID_OF_LISTS));
    String newListName = request.getParameter(NEW_LIST_NAME);

    // If the user selected an item in the drop-down, then that means they are
    // changing lists
    int pidToCompare = Integer.valueOf(request.getParameter("pidToCompare"));
    boolean isChangePidOfLists = pidToCompare != projectIdOfLists;
    if (isChangePidOfLists) {
      LOG.debug("A new project has been selected: " + projectIdOfLists);
      response.setRenderParameter(PROJECT_ID_OF_LISTS, String.valueOf(projectIdOfLists));
      return;
    }

    try {
      LOG.debug("Saving the form...");

      boolean isSuccess;

      User user = PortalUtils.getUser(request);
      int userId = user.getId();
      Connection db = PortalUtils.useConnection(request);
      Project projectOfLists = new Project(db, projectIdOfLists);
      Project projectToBookmark = new Project(db, projectIdToBookmark);
      Collection<Integer> listIds = getListIds(request.getParameterValues(LIST));
      // verify user can modify lists for project
      boolean isAddNewList = false;
      if (ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-modify")) {

        if (!StringUtils.hasText(newListName) && (listIds.size() == 0)) {
          System.out.println("Error need to show From");
          request.getPortletSession().setAttribute(ACTION_ERROR, "Choose a list or create one");
          request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
          return;
        }

        if (StringUtils.hasText(newListName)) {
          if (!ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-add")) {
            request
                .getPortletSession()
                .setAttribute(ACTION_ERROR, "Not authorized to create new list");
            request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
            return;
          }
          int newListId = saveNewList(db, projectIdOfLists, newListName);
          if (newListId == -1) {
            request.getPortletSession().setAttribute(ACTION_ERROR, "Unable to create new list.");
            request.getPortletSession().setAttribute(VIEW_TYPE, SAVE_FAILURE);
            return;
          } else {
            listIds.add(newListId);
            isAddNewList = true;
          }
        }
        TaskList existingTasks =
            findExistingTasksForProjects(db, projectIdOfLists, projectIdToBookmark);
        // check to see if the user is deleting tasks (listItems)
        if ((isAddNewList && existingTasks.size() > listIds.size() - 1)
            || !isAddNewList && existingTasks.size() > listIds.size()) {
          if (!ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-delete")) {
            request
                .getPortletSession()
                .setAttribute(ACTION_ERROR, "Not authorized to delete items");
            request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
            return;
          } else {
            deleteFromLists(db, existingTasks, listIds);
          }
        }
        isSuccess =
            saveToLists(
                db,
                existingTasks,
                listIds,
                userId,
                projectIdToBookmark,
                projectToBookmark.getTitle(),
                projectIdOfLists,
                request);
      } else {
        isSuccess = false;
        request.getPortletSession().setAttribute(ACTION_ERROR, "Not authorized to bookmark");
      }

      if (isSuccess) {
        // Close the panel, everything went well
        response.sendRedirect(ctx + "/close_panel_refresh.jsp");
      } else {
        request.getPortletSession().setAttribute(VIEW_TYPE, SAVE_FAILURE);
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }