/**
   * Gets the permalink for a blog entry.
   *
   * @return a URI as a String
   */
  public synchronized String getPermalink(BlogEntry blogEntry) {
    if (blogEntry.getTitle() == null || blogEntry.getTitle().length() == 0) {
      return buildPermalink(blogEntry) + ".html";
    } else {
      BlogService service = new BlogService();
      Day day = getBlog().getBlogForDay(blogEntry.getDate());
      List entries = day.getBlogEntries();
      int count = 0;
      for (int i = entries.size() - 1; i > entries.indexOf(blogEntry.getId()); i--) {
        try {
          BlogEntry entry = service.getBlogEntry(getBlog(), (String) entries.get(i));
          if (entry.getTitle().equals(blogEntry.getTitle())) {
            count++;
          }
        } catch (BlogServiceException e) {
          // do nothing
        }
      }

      if (count == 0) {
        return buildPermalink(blogEntry) + ".html";
      } else {
        return buildPermalink(blogEntry) + "_" + blogEntry.getId() + ".html";
      }
    }
  }
  /**
   * Peforms the processing associated with this action.
   *
   * @param request the HttpServletRequest instance
   * @param response the HttpServletResponse instance
   * @return the name of the next view
   */
  public View process(HttpServletRequest request, HttpServletResponse response)
      throws ServletException {
    Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
    BlogEntry blogEntryToClone = null;

    String entryToClone = request.getParameter("entryToClone");
    if (entryToClone != null && entryToClone.length() > 0) {
      BlogService service = new BlogService();
      try {
        blogEntryToClone = service.getBlogEntry(blog, entryToClone);
      } catch (BlogServiceException e) {
        throw new ServletException(e);
      }
    }

    BlogEntry entry = new BlogEntry(blog);
    if (blogEntryToClone != null) {
      entry.setTitle(blogEntryToClone.getTitle());
      entry.setSubtitle(blogEntryToClone.getSubtitle());
      entry.setBody(blogEntryToClone.getBody());
      entry.setExcerpt(blogEntryToClone.getExcerpt());
      entry.setTimeZoneId(blogEntryToClone.getTimeZoneId());
      entry.setCommentsEnabled(blogEntryToClone.isCommentsEnabled());
      entry.setTrackBacksEnabled(blogEntryToClone.isTrackBacksEnabled());

      // copy the categories
      Iterator it = blogEntryToClone.getCategories().iterator();
      while (it.hasNext()) {
        entry.addCategory((Category) it.next());
      }

      entry.setTags(blogEntryToClone.getTags());
    } else {
      entry.setTitle("Title");
      entry.setBody("<p>\n\n</p>");
    }

    entry.setAuthor(SecurityUtils.getUsername());

    getModel().put(Constants.BLOG_ENTRY_KEY, entry);

    return new BlogEntryFormView();
  }
  /**
   * Peforms the processing associated with this action.
   *
   * @param request the HttpServletRequest instance
   * @param response the HttpServletResponse instance
   * @return the name of the next view
   */
  public View process(HttpServletRequest request, HttpServletResponse response)
      throws ServletException {
    Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
    String id = request.getParameter("entry");
    String confirm = request.getParameter("confirm");
    String submit = request.getParameter("submit");

    BlogService service = new BlogService();
    BlogEntry blogEntry = null;
    try {
      blogEntry = service.getBlogEntry(blog, id);
    } catch (BlogServiceException e) {
      throw new ServletException(e);
    }

    if (blogEntry == null) {
      return new NotFoundView();
    }

    if (submit.equals("Edit")) {
      return new ForwardView("/editBlogEntry.secureaction?entry=" + id);
    } else if (submit.equals("Publish") || submit.equals("Unpublish")) {
      getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
      return new PublishBlogEntryView();
    } else if (submit.equals("Clone")) {
      return new ForwardView("/addBlogEntry.secureaction?entryToClone=" + blogEntry.getId());
    } else if (confirm != null && confirm.equals("true")) {
      if (submit.equalsIgnoreCase("Remove")) {
        try {
          service.removeBlogEntry(blogEntry);
          blog.info(
              "Blog entry \"" + StringUtils.transformHTML(blogEntry.getTitle()) + "\" removed.");
        } catch (BlogServiceException be) {
          throw new ServletException(be);
        }

        return new ForwardView("/viewHomePage.action");
      }
    }

    return new RedirectView(blogEntry.getLocalPermalink());
  }
  /**
   * Gets the blog entry referred to by the specified URI.
   *
   * @param uri a relative URI
   * @return a BlogEntry instance, or null if one can't be found
   */
  public BlogEntry getBlogEntry(String uri) {
    BlogService service = new BlogService();
    Day day = getDay(uri);

    Iterator it = day.getBlogEntries().iterator();
    while (it.hasNext()) {
      try {
        BlogEntry blogEntry = service.getBlogEntry(getBlog(), (String) it.next());
        // use the local permalink, just in case the entry has been aggregated
        // and an original permalink assigned
        if (blogEntry.getLocalPermalink().endsWith(uri)) {
          return blogEntry;
        }
      } catch (BlogServiceException e) {
        // do nothing
      }
    }

    return null;
  }