/**
  * Configure the {@link BlojsomFetcher} that will be used to fetch categories and entries
  *
  * @param servletConfig Servlet configuration information
  * @param blojsomConfiguration blojsom properties
  * @throws javax.servlet.ServletException If the {@link BlojsomFetcher} class could not be loaded
  *     and/or initialized
  */
 protected void configureFetcher(
     ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration)
     throws ServletException {
   String fetcherClassName = blojsomConfiguration.getFetcherClass();
   try {
     Class fetcherClass = Class.forName(fetcherClassName);
     _fetcher = (BlojsomFetcher) fetcherClass.newInstance();
     _fetcher.init(servletConfig, blojsomConfiguration);
     _logger.info("Added blojsom fetcher: " + fetcherClassName);
   } catch (ClassNotFoundException e) {
     _logger.error(e);
     throw new ServletException(e);
   } catch (InstantiationException e) {
     _logger.error(e);
     throw new ServletException(e);
   } catch (IllegalAccessException e) {
     _logger.error(e);
     throw new ServletException(e);
   } catch (BlojsomFetcherException e) {
     _logger.error(e);
     throw new ServletException(e);
   }
 }
  /**
   * Process the blog entries
   *
   * @param httpServletRequest Request
   * @param httpServletResponse Response
   * @param user {@link org.blojsom.blog.BlogUser} instance
   * @param context Context
   * @param entries Blog entries retrieved for the particular request
   * @return Modified set of blog entries
   * @throws BlojsomPluginException If there is an error processing the blog entries
   */
  public BlogEntry[] process(
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      BlogUser user,
      Map context,
      BlogEntry[] entries)
      throws BlojsomPluginException {
    if (!authenticateUser(httpServletRequest, httpServletResponse, context, user)) {
      httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_LOGIN_PAGE);

      return entries;
    }

    String username = getUsernameFromSession(httpServletRequest, user.getBlog());
    if (!checkPermission(user, null, username, EDIT_BLOG_ENTRIES_PERMISSION)) {
      httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
      addOperationResultMessage(
          context,
          getAdminResource(
              FAILED_PERMISSION_EDIT_KEY,
              FAILED_PERMISSION_EDIT_KEY,
              user.getBlog().getBlogAdministrationLocale()));

      return entries;
    }

    String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
    if (BlojsomUtils.checkNullOrBlank(action)) {
      _logger.debug("User did not request edit action");
      httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
    } else if (PAGE_ACTION.equals(action)) {
      _logger.debug("User requested edit blog entries page");

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRIES_PAGE);
    } else if (EDIT_BLOG_ENTRIES_ACTION.equals(action)) {
      _logger.debug("User requested edit blog entries list page");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      Map fetchMap = new HashMap();
      fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
      fetchMap.put(BlojsomFetcher.FETCHER_NUM_POSTS_INTEGER, new Integer(-1));
      try {
        entries = _fetcher.fetchEntries(fetchMap, user);
        if (entries != null) {
          _logger.debug(
              "Retrieved " + entries.length + " entries from category: " + blogCategoryName);
          Arrays.sort(entries, BlojsomUtils.FILE_TIME_COMPARATOR);
        } else {
          _logger.debug("No entries found in category: " + blogCategoryName);
        }
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
        entries = new BlogEntry[0];
      }

      context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
      context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_LIST, entries);
      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRIES_LIST_PAGE);
    } else if (EDIT_BLOG_ENTRY_ACTION.equals(action)) {
      _logger.debug("User requested edit blog entry action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      _logger.debug("Blog entry id: " + blogEntryId);

      try {
        BlogEntry entry = BlojsomUtils.fetchEntry(_fetcher, user, blogCategoryName, blogEntryId);
        context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entry);

        _blojsomConfiguration
            .getEventBroadcaster()
            .processEvent(
                new ProcessBlogEntryEvent(
                    this,
                    new Date(),
                    entry,
                    user,
                    httpServletRequest,
                    httpServletResponse,
                    context));
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogEntryId}));
        entries = new BlogEntry[0];
      }

      context.put(
          BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, BlojsomUtils.addSlashes(blogCategoryName));
      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);
    } else if (UPDATE_BLOG_ENTRY_ACTION.equals(action)) {
      _logger.debug("User requested update blog entry action");

      Blog blog = user.getBlog();
      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String updatedBlogCategoryName =
          BlojsomUtils.getRequestValue(UPDATED_BLOG_CATEGORY_NAME, httpServletRequest);
      updatedBlogCategoryName = BlojsomUtils.normalize(updatedBlogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      String blogEntryDescription =
          BlojsomUtils.getRequestValue(BLOG_ENTRY_DESCRIPTION, httpServletRequest);
      String blogEntryTitle = BlojsomUtils.getRequestValue(BLOG_ENTRY_TITLE, httpServletRequest);
      if (BlojsomUtils.checkNullOrBlank(blogEntryTitle)) {
        blogEntryDescription = BlojsomUtils.LINE_SEPARATOR + blogEntryDescription;
      }
      String allowComments =
          BlojsomUtils.getRequestValue(BLOG_METADATA_COMMENTS_DISABLED, httpServletRequest);
      String allowTrackbacks =
          BlojsomUtils.getRequestValue(BLOG_METADATA_TRACKBACKS_DISABLED, httpServletRequest);
      String blogTrackbackURLs =
          BlojsomUtils.getRequestValue(BLOG_TRACKBACK_URLS, httpServletRequest);
      String pingBlogURLS = BlojsomUtils.getRequestValue(PING_BLOG_URLS, httpServletRequest);
      String sendPingbacks =
          BlojsomUtils.getRequestValue(
              PingbackPlugin.PINGBACK_PLUGIN_METADATA_SEND_PINGBACKS, httpServletRequest);

      _logger.debug("Blog entry id: " + blogEntryId);

      try {
        BlogEntry entryToUpdate =
            BlojsomUtils.fetchEntry(_fetcher, user, blogCategoryName, blogEntryId);
        entryToUpdate.setTitle(blogEntryTitle);
        entryToUpdate.setDescription(blogEntryDescription);

        boolean movingCategory = !blogCategoryName.equals(updatedBlogCategoryName);

        Map entryMetaData = entryToUpdate.getMetaData();
        if (entryMetaData == null) {
          entryMetaData = new HashMap();
        }

        if (!BlojsomUtils.checkNullOrBlank(allowComments)) {
          entryMetaData.put(BLOG_METADATA_COMMENTS_DISABLED, "y");
        } else {
          entryMetaData.remove(BLOG_METADATA_COMMENTS_DISABLED);
        }

        if (!BlojsomUtils.checkNullOrBlank(allowTrackbacks)) {
          entryMetaData.put(BLOG_METADATA_TRACKBACKS_DISABLED, "y");
        } else {
          entryMetaData.remove(BLOG_METADATA_TRACKBACKS_DISABLED);
        }

        if (BlojsomUtils.checkNullOrBlank(pingBlogURLS)) {
          entryMetaData.put(WeblogsPingPlugin.NO_PING_WEBLOGS_METADATA, "true");
        } else {
          entryMetaData.remove(WeblogsPingPlugin.NO_PING_WEBLOGS_METADATA);
        }

        if (!BlojsomUtils.checkNullOrBlank(sendPingbacks)) {
          entryMetaData.put(PingbackPlugin.PINGBACK_PLUGIN_METADATA_SEND_PINGBACKS, "true");
        } else {
          entryMetaData.remove(PingbackPlugin.PINGBACK_PLUGIN_METADATA_SEND_PINGBACKS);
        }

        String entryPublishDateTime = httpServletRequest.getParameter(BLOG_ENTRY_PUBLISH_DATETIME);
        if (!BlojsomUtils.checkNullOrBlank(entryPublishDateTime)) {
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
          try {
            Date publishDateTime = simpleDateFormat.parse(entryPublishDateTime);
            _logger.debug("Publishing blog entry at: " + publishDateTime.toString());
            entryMetaData.put(
                BlojsomMetaDataConstants.BLOG_ENTRY_METADATA_TIMESTAMP,
                Long.toString(publishDateTime.getTime()));
          } catch (ParseException e) {
            _logger.error(e);
          }
        }

        entryToUpdate.setMetaData(entryMetaData);

        _blojsomConfiguration
            .getEventBroadcaster()
            .processEvent(
                new ProcessBlogEntryEvent(
                    this,
                    new Date(),
                    entryToUpdate,
                    user,
                    httpServletRequest,
                    httpServletResponse,
                    context));

        if (movingCategory) {
          _logger.debug("Moving entry from " + blogCategoryName + " to " + updatedBlogCategoryName);
          BlogEntry movedEntry = _fetcher.newBlogEntry();

          BlogCategory category;
          category = _fetcher.newBlogCategory();
          category.setCategory(updatedBlogCategoryName);
          category.setCategoryURL(
              user.getBlog().getBlogURL()
                  + BlojsomUtils.removeInitialSlash(updatedBlogCategoryName));

          movedEntry.setTitle(blogEntryTitle);
          movedEntry.setCategory(updatedBlogCategoryName);
          movedEntry.setDescription(blogEntryDescription);
          movedEntry.setBlogCategory(category);
          movedEntry.setMetaData(entryMetaData);

          movedEntry.setComments(entryToUpdate.getComments());
          movedEntry.setTrackbacks(entryToUpdate.getTrackbacks());
          movedEntry.setPingbacks(entryToUpdate.getPingbacks());

          movedEntry.save(user);

          _logger.debug("Moving " + entryToUpdate.getNumComments() + " comments");
          List comments = entryToUpdate.getComments();
          for (int i = 0; i < comments.size(); i++) {
            BlogComment blogComment = (BlogComment) comments.get(i);
            try {
              BlogComment movedComment = _fetcher.newBlogComment();
              movedComment.setAuthor(blogComment.getAuthor());
              movedComment.setAuthorEmail(blogComment.getAuthorEmail());
              movedComment.setAuthorURL(blogComment.getAuthorURL());
              movedComment.setBlogEntry(movedEntry);
              movedComment.setComment(blogComment.getComment());
              movedComment.setMetaData(blogComment.getMetaData());
              movedComment.setId(blogComment.getId());
              movedComment.setCommentDateLong(blogComment.getCommentDateLong());

              movedComment.save(user);
              blogComment.delete(user);
            } catch (BlojsomException e) {
              _logger.error(e);
            }
          }

          _logger.debug("Moving " + entryToUpdate.getNumTrackbacks() + " trackbacks");
          List trackbacks = entryToUpdate.getTrackbacks();
          for (int i = 0; i < trackbacks.size(); i++) {
            Trackback trackback = (Trackback) trackbacks.get(i);
            try {
              Trackback movedTrackback = _fetcher.newTrackback();
              movedTrackback.setBlogEntry(movedEntry);
              movedTrackback.setBlogName(trackback.getBlogName());
              movedTrackback.setExcerpt(trackback.getExcerpt());
              movedTrackback.setId(trackback.getId());
              movedTrackback.setMetaData(trackback.getMetaData());
              movedTrackback.setTitle(trackback.getTitle());
              movedTrackback.setUrl(trackback.getUrl());
              movedTrackback.setTrackbackDateLong(trackback.getTrackbackDateLong());

              movedTrackback.save(user);
              trackback.delete(user);
            } catch (BlojsomException e) {
              _logger.error(e);
            }
          }

          _logger.debug("Moving " + entryToUpdate.getNumPingbacks() + " pingbacks");
          List pingbacks = entryToUpdate.getPingbacks();
          for (int i = 0; i < pingbacks.size(); i++) {
            Pingback pingback = (Pingback) pingbacks.get(i);
            try {
              Pingback movedPingback = _fetcher.newPingback();
              movedPingback.setBlogEntry(movedEntry);
              movedPingback.setBlogName(pingback.getBlogName());
              movedPingback.setExcerpt(pingback.getExcerpt());
              movedPingback.setId(pingback.getId());
              movedPingback.setMetaData(pingback.getMetaData());
              movedPingback.setTitle(pingback.getTitle());
              movedPingback.setUrl(pingback.getUrl());
              movedPingback.setTrackbackDateLong(pingback.getTrackbackDateLong());

              movedPingback.save(user);
              pingback.delete(user);
            } catch (BlojsomException e) {
              _logger.error(e);
            }
          }

          movedEntry.load(user);
          entryToUpdate.delete(user);
          entryToUpdate = movedEntry;
        } else {
          entryToUpdate.save(user);
        }

        entryToUpdate.load(user);

        _logger.debug("Updated blog entry: " + entryToUpdate.getLink());

        StringBuffer entryLink = new StringBuffer();
        entryLink
            .append("<a href=\"")
            .append(user.getBlog().getBlogURL())
            .append(BlojsomUtils.removeInitialSlash(entryToUpdate.getCategory()))
            .append("?")
            .append(PERMALINK_PARAM)
            .append("=")
            .append(entryToUpdate.getPermalink())
            .append("\">")
            .append(entryToUpdate.getTitle())
            .append("</a>");
        addOperationResultMessage(
            context,
            formatAdminResource(
                UPDATED_BLOG_ENTRY_KEY,
                UPDATED_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {entryLink.toString()}));

        UpdatedBlogEntryEvent updateEvent =
            new UpdatedBlogEntryEvent(this, new Date(), entryToUpdate, user);
        _blojsomConfiguration.getEventBroadcaster().broadcastEvent(updateEvent);

        // Send trackback pings
        if (!BlojsomUtils.checkNullOrBlank(blogTrackbackURLs)) {
          sendTrackbackPings(blog, entryToUpdate, blogTrackbackURLs);
        }

        httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);
        context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entryToUpdate);

        if (movingCategory) {
          blogCategoryName = updatedBlogCategoryName;
        }
        blogCategoryName = BlojsomUtils.addSlashes(blogCategoryName);
        context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogEntryId}));
        httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRIES_PAGE);
        entries = new BlogEntry[0];
      } catch (BlojsomException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                FAILED_RETRIEVE_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogEntryId}));
        entries = new BlogEntry[0];
        httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRIES_PAGE);
      }
    } else if (DELETE_BLOG_ENTRY_ACTION.equals(action)) {
      _logger.debug("User requested delete blog entry action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      _logger.debug("Blog entry id: " + blogEntryId);

      try {
        BlogEntry entryToDelete =
            BlojsomUtils.fetchEntry(_fetcher, user, blogCategoryName, blogEntryId);
        String title = entryToDelete.getTitle();
        entryToDelete.delete(user);
        addOperationResultMessage(
            context,
            formatAdminResource(
                DELETED_BLOG_ENTRY_KEY,
                DELETED_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {title}));
        DeletedBlogEntryEvent deleteEvent =
            new DeletedBlogEntryEvent(this, new Date(), entryToDelete, user);
        _blojsomConfiguration.getEventBroadcaster().broadcastEvent(deleteEvent);
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_DELETE_BLOG_ENTRY_KEY,
                FAILED_DELETE_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogEntryId}));
        entries = new BlogEntry[0];
      } catch (BlojsomException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_DELETE_BLOG_ENTRY_KEY,
                FAILED_DELETE_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogEntryId}));
        entries = new BlogEntry[0];
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRIES_PAGE);
    } else if (NEW_BLOG_ENTRY_ACTION.equals(action)) {
      _logger.debug("User requested new blog entry action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);

      _blojsomConfiguration
          .getEventBroadcaster()
          .processEvent(
              new ProcessBlogEntryEvent(
                  this, new Date(), null, user, httpServletRequest, httpServletResponse, context));

      context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
      httpServletRequest.setAttribute(PAGE_PARAM, ADD_BLOG_ENTRY_PAGE);
    } else if (ADD_BLOG_ENTRY_ACTION.equals(action)) {
      _logger.debug("User requested add blog entry action");
      Blog blog = user.getBlog();

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      if (!blogCategoryName.endsWith("/")) {
        blogCategoryName += "/";
      }
      String blogEntryDescription =
          BlojsomUtils.getRequestValue(BLOG_ENTRY_DESCRIPTION, httpServletRequest);
      String blogEntryTitle = BlojsomUtils.getRequestValue(BLOG_ENTRY_TITLE, httpServletRequest);

      if (BlojsomUtils.checkNullOrBlank(blogEntryTitle)
          && BlojsomUtils.checkNullOrBlank(blogEntryDescription)) {
        httpServletRequest.setAttribute(PAGE_PARAM, ADD_BLOG_ENTRY_PAGE);
        _blojsomConfiguration
            .getEventBroadcaster()
            .processEvent(
                new ProcessBlogEntryEvent(
                    this,
                    new Date(),
                    null,
                    user,
                    httpServletRequest,
                    httpServletResponse,
                    context));

        addOperationResultMessage(
            context,
            getAdminResource(
                BLANK_ENTRY_KEY, BLANK_ENTRY_KEY, user.getBlog().getBlogAdministrationLocale()));

        return entries;
      }

      if (BlojsomUtils.checkNullOrBlank(blogEntryTitle)) {
        blogEntryDescription = BlojsomUtils.LINE_SEPARATOR + blogEntryDescription;
      }
      String allowComments =
          BlojsomUtils.getRequestValue(BLOG_METADATA_COMMENTS_DISABLED, httpServletRequest);
      String allowTrackbacks =
          BlojsomUtils.getRequestValue(BLOG_METADATA_TRACKBACKS_DISABLED, httpServletRequest);
      String blogTrackbackURLs =
          BlojsomUtils.getRequestValue(BLOG_TRACKBACK_URLS, httpServletRequest);
      String proposedBlogFilename =
          BlojsomUtils.getRequestValue(BLOG_ENTRY_PROPOSED_NAME, httpServletRequest);
      String pingBlogURLS = BlojsomUtils.getRequestValue(PING_BLOG_URLS, httpServletRequest);
      String sendPingbacks =
          BlojsomUtils.getRequestValue(
              PingbackPlugin.PINGBACK_PLUGIN_METADATA_SEND_PINGBACKS, httpServletRequest);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      BlogEntry entry;
      entry = _fetcher.newBlogEntry();
      entry.setTitle(blogEntryTitle);
      entry.setCategory(blogCategoryName);
      entry.setDescription(blogEntryDescription);
      entry.setBlogCategory(category);

      Map entryMetaData = new HashMap();
      username =
          (String)
              httpServletRequest
                  .getSession()
                  .getAttribute(
                      user.getBlog().getBlogAdminURL() + "_" + BLOJSOM_ADMIN_PLUGIN_USERNAME_KEY);
      entryMetaData.put(BlojsomMetaDataConstants.BLOG_ENTRY_METADATA_AUTHOR, username);

      String entryPublishDateTime = httpServletRequest.getParameter(BLOG_ENTRY_PUBLISH_DATETIME);
      if (!BlojsomUtils.checkNullOrBlank(entryPublishDateTime)) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        try {
          Date publishDateTime = simpleDateFormat.parse(entryPublishDateTime);
          _logger.debug("Publishing blog entry at: " + publishDateTime.toString());
          entryMetaData.put(
              BlojsomMetaDataConstants.BLOG_ENTRY_METADATA_TIMESTAMP,
              Long.toString(publishDateTime.getTime()));
        } catch (ParseException e) {
          _logger.error(e);
          entryMetaData.put(
              BlojsomMetaDataConstants.BLOG_ENTRY_METADATA_TIMESTAMP,
              Long.toString(new Date().getTime()));
        }
      } else {
        entryMetaData.put(
            BlojsomMetaDataConstants.BLOG_ENTRY_METADATA_TIMESTAMP,
            Long.toString(new Date().getTime()));
      }

      if (!BlojsomUtils.checkNullOrBlank(allowComments)) {
        entryMetaData.put(BLOG_METADATA_COMMENTS_DISABLED, "y");
      }

      if (!BlojsomUtils.checkNullOrBlank(allowTrackbacks)) {
        entryMetaData.put(BLOG_METADATA_TRACKBACKS_DISABLED, "y");
      }

      if (BlojsomUtils.checkNullOrBlank(pingBlogURLS)) {
        entryMetaData.put(WeblogsPingPlugin.NO_PING_WEBLOGS_METADATA, "true");
      }

      if (!BlojsomUtils.checkNullOrBlank(sendPingbacks)) {
        entryMetaData.put(PingbackPlugin.PINGBACK_PLUGIN_METADATA_SEND_PINGBACKS, "true");
      }

      entry.setMetaData(entryMetaData);

      try {
        _blojsomConfiguration
            .getEventBroadcaster()
            .processEvent(
                new ProcessBlogEntryEvent(
                    this,
                    new Date(),
                    entry,
                    user,
                    httpServletRequest,
                    httpServletResponse,
                    context));

        entry.save(user);
        entry.load(user);

        StringBuffer entryLink = new StringBuffer();
        entry.setLink(
            user.getBlog().getBlogURL()
                + BlojsomUtils.removeInitialSlash(entry.getCategory())
                + "?"
                + PERMALINK_PARAM
                + "="
                + entry.getPermalink());
        entryLink
            .append("<a href=\"")
            .append(entry.getLink())
            .append("\">")
            .append(entry.getTitle())
            .append("</a>");
        addOperationResultMessage(
            context,
            formatAdminResource(
                ADDED_BLOG_ENTRY_KEY,
                ADDED_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {entryLink.toString()}));

        AddBlogEntryEvent addEvent = new AddBlogEntryEvent(this, new Date(), entry, user);
        _blojsomConfiguration.getEventBroadcaster().broadcastEvent(addEvent);
      } catch (BlojsomException e) {
        _logger.error(e);
        addOperationResultMessage(
            context,
            formatAdminResource(
                FAILED_ADD_BLOG_ENTRY_KEY,
                FAILED_ADD_BLOG_ENTRY_KEY,
                user.getBlog().getBlogAdministrationLocale(),
                new Object[] {blogCategoryName}));
      }

      // Send trackback pings
      if (!BlojsomUtils.checkNullOrBlank(blogTrackbackURLs)) {
        sendTrackbackPings(blog, entry, blogTrackbackURLs);
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_ACTION);
      context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entry);
      context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
    } else if (DELETE_BLOG_COMMENTS.equals(action)) {
      _logger.debug("User requested delete blog comments action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      try {
        blogEntryId = URLDecoder.decode(blogEntryId, UTF8);
      } catch (UnsupportedEncodingException e) {
        _logger.error(e);
      }
      _logger.debug("Blog entry id: " + blogEntryId);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      Map fetchMap = new HashMap();
      fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
      fetchMap.put(BlojsomFetcher.FETCHER_PERMALINK, blogEntryId);
      try {
        entries = _fetcher.fetchEntries(fetchMap, user);
        if (entries != null) {
          _logger.debug(
              "Retrieved " + entries.length + " entries from category: " + blogCategoryName);
          BlogEntry entryToUpdate = entries[0];

          String[] blogCommentIDs = httpServletRequest.getParameterValues(BLOG_COMMENT_ID);
          if (blogCommentIDs != null && blogCommentIDs.length > 0) {
            for (int i = 0; i < blogCommentIDs.length; i++) {
              String blogCommentID = blogCommentIDs[i];
              BlogComment[] blogComments = entryToUpdate.getCommentsAsArray();
              for (int j = 0; j < blogComments.length; j++) {
                BlogComment blogComment = blogComments[j];
                if (blogComment.getId().equals(blogCommentID)) {
                  try {
                    blogComment.delete(user);

                    _blojsomConfiguration
                        .getEventBroadcaster()
                        .broadcastEvent(
                            new CommentDeletedEvent(this, new Date(), blogComment, user));
                  } catch (BlojsomException e) {
                    _logger.error(e);
                  }
                }
              }
            }

            addOperationResultMessage(
                context,
                formatAdminResource(
                    DELETED_COMMENTS_KEY,
                    DELETED_COMMENTS_KEY,
                    user.getBlog().getBlogAdministrationLocale(),
                    new Object[] {new Integer(blogCommentIDs.length)}));

            entries = _fetcher.fetchEntries(fetchMap, user);
            entryToUpdate = entries[0];
          }

          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entryToUpdate);
          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
        }
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);
    } else if (APPROVE_BLOG_COMMENTS.equals(action)) {
      _logger.debug("User requested approve blog comments action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      try {
        blogEntryId = URLDecoder.decode(blogEntryId, UTF8);
      } catch (UnsupportedEncodingException e) {
        _logger.error(e);
      }
      _logger.debug("Blog entry id: " + blogEntryId);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      Map fetchMap = new HashMap();
      fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
      fetchMap.put(BlojsomFetcher.FETCHER_PERMALINK, blogEntryId);
      try {
        entries = _fetcher.fetchEntries(fetchMap, user);
        if (entries != null) {
          _logger.debug(
              "Retrieved " + entries.length + " entries from category: " + blogCategoryName);
          BlogEntry entryToUpdate = entries[0];

          String[] blogCommentIDs = httpServletRequest.getParameterValues(BLOG_COMMENT_ID);
          if (blogCommentIDs != null && blogCommentIDs.length > 0) {
            for (int i = 0; i < blogCommentIDs.length; i++) {
              String blogCommentID = blogCommentIDs[i];
              BlogComment[] blogComments = entryToUpdate.getCommentsAsArray();

              for (int j = 0; j < blogComments.length; j++) {
                BlogComment blogComment = blogComments[j];
                if (blogComment.getId().equals(blogCommentID)) {
                  Map blogCommentMetaData = blogComment.getMetaData();

                  blogCommentMetaData.put(
                      CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, "true");
                  try {
                    blogComment.save(user);

                    _blojsomConfiguration
                        .getEventBroadcaster()
                        .broadcastEvent(
                            new CommentApprovedEvent(this, new Date(), blogComment, user));
                  } catch (BlojsomException e) {
                    _logger.error(e);
                  }
                }
              }
            }

            addOperationResultMessage(
                context,
                formatAdminResource(
                    APPROVED_COMMENTS_KEY,
                    APPROVED_COMMENTS_KEY,
                    user.getBlog().getBlogAdministrationLocale(),
                    new Object[] {new Integer(blogCommentIDs.length)}));

            entries = _fetcher.fetchEntries(fetchMap, user);
            entryToUpdate = entries[0];
          }

          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entryToUpdate);
          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
        }
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);
    } else if (DELETE_BLOG_TRACKBACKS.equals(action)) {
      _logger.debug("User requested delete blog trackbacks action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      try {
        blogEntryId = URLDecoder.decode(blogEntryId, UTF8);
      } catch (UnsupportedEncodingException e) {
        _logger.error(e);
      }
      _logger.debug("Blog entry id: " + blogEntryId);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      Map fetchMap = new HashMap();
      fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
      fetchMap.put(BlojsomFetcher.FETCHER_PERMALINK, blogEntryId);
      try {
        entries = _fetcher.fetchEntries(fetchMap, user);
        if (entries != null) {
          _logger.debug(
              "Retrieved " + entries.length + " entries from category: " + blogCategoryName);
          BlogEntry entryToUpdate = entries[0];

          String[] blogTrackbackIDs = httpServletRequest.getParameterValues(BLOG_TRACKBACK_ID);
          if (blogTrackbackIDs != null && blogTrackbackIDs.length > 0) {
            for (int i = 0; i < blogTrackbackIDs.length; i++) {
              String blogTrackbackID = blogTrackbackIDs[i];
              Trackback[] trackbacks = entryToUpdate.getTrackbacksAsArray();

              for (int j = 0; j < trackbacks.length; j++) {
                Trackback trackback = trackbacks[j];
                if (trackback.getId().equals(blogTrackbackID)) {
                  try {
                    trackback.delete(user);

                    _blojsomConfiguration
                        .getEventBroadcaster()
                        .broadcastEvent(
                            new TrackbackDeletedEvent(this, new Date(), trackback, user));
                  } catch (BlojsomException e) {
                    _logger.error(e);
                  }
                }
              }
            }

            addOperationResultMessage(
                context,
                formatAdminResource(
                    DELETED_TRACKBACKS_KEY,
                    DELETED_TRACKBACKS_KEY,
                    user.getBlog().getBlogAdministrationLocale(),
                    new Object[] {new Integer(blogTrackbackIDs.length)}));

            entries = _fetcher.fetchEntries(fetchMap, user);
            entryToUpdate = entries[0];
          }

          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entryToUpdate);
          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
        }
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);

    } else if (APPROVE_BLOG_TRACKBACKS.equals(action)) {
      _logger.debug("User requested approve blog trackbacks action");

      String blogCategoryName =
          BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest);
      blogCategoryName = BlojsomUtils.normalize(blogCategoryName);
      String blogEntryId = BlojsomUtils.getRequestValue(BLOG_ENTRY_ID, httpServletRequest);
      try {
        blogEntryId = URLDecoder.decode(blogEntryId, UTF8);
      } catch (UnsupportedEncodingException e) {
        _logger.error(e);
      }
      _logger.debug("Blog entry id: " + blogEntryId);

      BlogCategory category;
      category = _fetcher.newBlogCategory();
      category.setCategory(blogCategoryName);
      category.setCategoryURL(
          user.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(blogCategoryName));

      Map fetchMap = new HashMap();
      fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
      fetchMap.put(BlojsomFetcher.FETCHER_PERMALINK, blogEntryId);
      try {
        entries = _fetcher.fetchEntries(fetchMap, user);
        if (entries != null) {
          _logger.debug(
              "Retrieved " + entries.length + " entries from category: " + blogCategoryName);
          BlogEntry entryToUpdate = entries[0];

          String[] blogTrackbackIDs = httpServletRequest.getParameterValues(BLOG_TRACKBACK_ID);
          if (blogTrackbackIDs != null && blogTrackbackIDs.length > 0) {
            for (int i = 0; i < blogTrackbackIDs.length; i++) {
              String blogTrackbackID = blogTrackbackIDs[i];
              Trackback[] trackbacks = entryToUpdate.getTrackbacksAsArray();

              for (int j = 0; j < trackbacks.length; j++) {
                Trackback trackback = trackbacks[j];
                if (trackback.getId().equals(blogTrackbackID)) {
                  Map blogTrackbackMetaData = trackback.getMetaData();

                  blogTrackbackMetaData.put(
                      TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED,
                      "true");
                  try {
                    trackback.save(user);

                    _blojsomConfiguration
                        .getEventBroadcaster()
                        .broadcastEvent(
                            new TrackbackApprovedEvent(this, new Date(), trackback, user));
                  } catch (BlojsomException e) {
                    _logger.error(e);
                  }
                }
              }
            }

            addOperationResultMessage(
                context,
                formatAdminResource(
                    APPROVED_TRACKBACKS_KEY,
                    APPROVED_TRACKBACKS_KEY,
                    user.getBlog().getBlogAdministrationLocale(),
                    new Object[] {new Integer(blogTrackbackIDs.length)}));

            entries = _fetcher.fetchEntries(fetchMap, user);
            entryToUpdate = entries[0];
          }

          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_ENTRY, entryToUpdate);
          context.put(BLOJSOM_PLUGIN_EDIT_BLOG_ENTRIES_CATEGORY, blogCategoryName);
        }
      } catch (BlojsomFetcherException e) {
        _logger.error(e);
      }

      httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_ENTRY_PAGE);
    }

    return entries;
  }