/**
   * 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");
  }
  /** Does all the prep work before showing the form */
  @RequestMapping(params = "action=addAnnouncement")
  public String showAddAnnouncementForm(
      @RequestParam(value = "editId", required = false) Long editId,
      @RequestParam(value = "topicId", required = false) Long topicId,
      RenderRequest request,
      Model model)
      throws PortletException {

    PortletPreferences prefs = request.getPreferences();

    if (!model.containsAttribute("announcement")) {
      Announcement ann = new Announcement();
      Topic topic = null;

      if (editId != null) {
        try {
          log.debug(
              "editId found. This is an edit request for announcement Id " + editId.toString());
          ann = announcementService.getAnnouncement(editId);
          // return immediately when we have our announcement

        } catch (NumberFormatException e) {
          log.debug("No editId found. This is not an edit request");
        }
      }

      if (ann != null && ann.getParent() == null) {
        try {
          topic = announcementService.getTopic(topicId);
          ann.setParent(topic);
        } catch (NumberFormatException e) {
          log.error("Unable to get topicId from request");
        }
      }

      model.addAttribute("announcement", ann);
    }

    model.addAttribute("datePickerFormat", datePickerFormat);
    model.addAttribute(
        "abstractMaxLength",
        prefs.getValue(PREFERENCE_ABSTRACT_MAX_LENGTH, DEFAULT_ABSTRACT_MAX_LENGTH));
    model.addAttribute(
        "tinyMceInitializationOptions",
        prefs.getValue(
            PREFERENCE_TINY_MCE_INITIALIZATION_OPTIONS, DEFAULT_TINY_MCE_INITIALIZATION_OPTIONS));
    return "addAnnouncement";
  }
  @RequestMapping(value = "VIEW", params = "action=displayFullAnnouncement")
  public String displayFullAnnouncement(
      Model model, RenderRequest request, @RequestParam("announcementId") String announcementId)
      throws Exception {

    Long annId = Long.valueOf(announcementId);

    Announcement announcement = announcementService.getAnnouncement(annId);

    if (!UserPermissionChecker.inRoleForTopic(request, "audience", announcement.getParent())) {
      throw new UnauthorizedException();
    }

    model.addAttribute("announcement", announcement);

    return viewNameSelector.select(request, "displayFullAnnouncement");
  }
  /**
   * 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");
    }
  }