/**
   * Send Trackback Email to Blog Author
   *
   * @param entryTitle Blog Entry title for this Trackback
   * @param title title of trackback entry
   * @param category catagory for trackbacked entry
   * @param permalink permalink for trackbacked entry
   * @param url URL of site tracking back
   * @param excerpt excerpt of trackback post
   * @param blogName Title of trackbacking blog
   * @param context Context
   */
  private synchronized void sendTrackbackEmail(
      String entryTitle,
      String title,
      String category,
      String permalink,
      String url,
      String excerpt,
      String blogName,
      Map context) {

    StringBuffer _trackback = new StringBuffer();
    _trackback
        .append("Trackback on: ")
        .append(_blogUrlPrefix)
        .append(BlojsomUtils.removeInitialSlash(category));
    _trackback.append("?permalink=").append(permalink).append("&page=comments").append("\n");

    _trackback
        .append("\n==[ Trackback ]==========================================================")
        .append("\n\n");

    if (title == null && !title.equals("")) {
      _trackback.append("Title    : ").append(title).append("\n");
    }
    if (url != null && !url.equals("")) {
      _trackback.append("Url      : ").append(url).append("\n");
    }
    if (blogName == null && !blogName.equals("")) {
      _trackback.append("Blog Name: ").append(blogName).append("\n");
    }
    if (excerpt != null && !excerpt.equals("")) {
      _trackback.append("Excerpt  : ").append(excerpt).append("\n");
    }

    EmailUtils.notifyBlogAuthor(
        "[blojsom] Trackback on: " + entryTitle, _trackback.toString(), context);
  }
  /**
   * @param category
   * @param permalink
   * @param title
   * @param excerpt
   * @param url
   * @param blogName
   */
  private synchronized Integer addTrackback(
      Map context,
      String category,
      String permalink,
      String title,
      String excerpt,
      String url,
      String blogName) {
    Trackback trackback = new Trackback();
    excerpt = BlojsomUtils.escapeMetaAndLink(excerpt);
    trackback.setTitle(title);
    trackback.setExcerpt(excerpt);
    trackback.setUrl(url);
    trackback.setBlogName(blogName);
    trackback.setTrackbackDateLong(new Date().getTime());

    StringBuffer trackbackDirectory = new StringBuffer();
    String permalinkFilename = BlojsomUtils.getFilenameForPermalink(permalink, _blogFileExtensions);
    permalinkFilename = BlojsomUtils.urlDecode(permalinkFilename);
    if (permalinkFilename == null) {
      _logger.debug("Invalid permalink trackback for: " + permalink);
      context.put(BLOJSOM_TRACKBACK_MESSAGE, "Invalid permalink trackback for: " + permalink);
      return new Integer(1);
    }

    trackbackDirectory.append(_blogHome);
    trackbackDirectory.append(BlojsomUtils.removeInitialSlash(category));
    File blogEntry = new File(trackbackDirectory.toString() + File.separator + permalinkFilename);
    if (!blogEntry.exists()) {
      _logger.error("Trying to create trackback for invalid blog entry: " + permalink);
      context.put(BLOJSOM_TRACKBACK_MESSAGE, "Trying to create trackback for invalid permalink");
      return new Integer(1);
    }
    trackbackDirectory.append(_blogTrackbackDirectory);
    trackbackDirectory.append(File.separator);
    trackbackDirectory.append(permalinkFilename);
    trackbackDirectory.append(File.separator);
    String trackbackFilename =
        trackbackDirectory.toString()
            + trackback.getTrackbackDateLong()
            + BlojsomConstants.TRACKBACK_EXTENSION;
    File trackbackDir = new File(trackbackDirectory.toString());
    if (!trackbackDir.exists()) {
      if (!trackbackDir.mkdirs()) {
        _logger.error("Could not create directory for trackbacks: " + trackbackDirectory);
        context.put(BLOJSOM_TRACKBACK_MESSAGE, "Could not create directory for trackbacks");
        return new Integer(1);
      }
    }

    File trackbackEntry = new File(trackbackFilename);
    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter(trackbackEntry));
      bw.write(trackback.getTitle());
      bw.newLine();
      bw.write(trackback.getExcerpt());
      bw.newLine();
      bw.write(trackback.getUrl());
      bw.newLine();
      bw.write(trackback.getBlogName());
      bw.newLine();
      bw.close();
      _logger.debug("Added trackback: " + trackbackFilename);
    } catch (IOException e) {
      _logger.error(e);
      context.put(BLOJSOM_TRACKBACK_MESSAGE, "I/O error on trackback write.");
      return new Integer(1);
    }

    return new Integer(0);
  }