/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.std;

import java.net.URL;
import java.util.*;
import org.apache.commons.httpclient.NameValuePair;
import com.silverwrist.venice.except.TrackbackException;

/**
 * A class representing a trackback item found on an external Web page via RDF auto-discovery.  This
 * conforms to the Moveable Type Trackback Protocol, Version 1.1.
 *
 * @author Eric J. Bowersox &lt;erbo@ricochet.com&gt;
 * @version $Id$
 */
public abstract class TrackbackItem
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  /**
   * The URL of this particular trackback item.
   */
  private final URL m_item;

  /**
   * The trackback "ping URL" which is used to register trackbacks for this item.
   */
  private final URL m_trackback;

  /**
   * The title associated with this item.
   */
  private final String m_title;

  /*--------------------------------------------------------------------------------
   * Abstract operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Send a trackback ping to this trackback URL.
   *
   * @param url The URL of the trackback item to send to.
   * @param parms The parameters to pass to the trackback operation.
   * @exception com.silverwrist.venice.except.TrackbackException If the trackback could not be sent.
   */
  protected abstract void sendPing(String url, NameValuePair[] parms) throws TrackbackException;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a new <code>TrackbackItem</code>.
   *
   * @param item The URL of the trackback item.
   * @param trackback The "trackback ping URL" for this particular trackback item.
   * @param title The title associated with this trackback item.
   */
  protected TrackbackItem(URL item, URL trackback, String title)
  {
    m_item = item;
    m_trackback = trackback;
    m_title = title;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns a string representation of the object. In general, the <code>toString</code> method returns a
   * string that "textually represents" this object.
   *
   * @return A string representation of the object.
   */
  public String toString()
  {
    StringBuffer buf = new StringBuffer("{TrackbackItem: item = ");
    buf.append(m_item).append(", trackback ping = ").append(m_trackback).append(", title = \"");
    buf.append(m_title).append("\"}");
    return buf.toString();

  } // end toString

  /*--------------------------------------------------------------------------------
   * Public getters
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the {@link java.net.URL URL} of this trackback item.
   *
   * @return The URL of this trackback item.
   */
  public URL getItem()
  {
    return m_item;

  } // end getItem

  /**
   * Returns the {@link java.net.URL URL} of the "trackback ping" URL for this trackback.
   *
   * @return The URL of the "trackback ping" URL for this trackback.
   */
  public URL getTrackbackURL()
  {
    return m_trackback;

  } // end getTrackbackURL

  /**
   * Returns the title of this trackback item.
   *
   * @return The title of this trackback item.
   */
  public String getTitle()
  {
    return m_title;

  } // end getTitle

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Send a trackback ping to this trackback URL.
   *
   * @param permalink URL that is the "permalink" to the entity tracking back to this entry.
   * @param blog_name Name of the blog that is linking to this entry.
   * @param title Entry title of the entry that is linking to this entry.
   * @param excerpt Excerpt of the entry that is linked to this entry.  (Moveable Type limits this to 255 characters.)
   * @exception com.silverwrist.venice.except.TrackbackException If the trackback could not be sent.
   */
  public void sendPing(String permalink, String blog_name, String title, String excerpt) throws TrackbackException
  {
    // Construct the NameValuePairs that make up the fields.
    ArrayList tmp = new ArrayList(4);
    tmp.add(new NameValuePair("url",permalink));
    if (blog_name!=null)
      tmp.add(new NameValuePair("blog_name",blog_name));
    if (title!=null)
      tmp.add(new NameValuePair("title",title));
    if (excerpt!=null)
      tmp.add(new NameValuePair("excerpt",excerpt));
    NameValuePair[] parms = new NameValuePair[tmp.size()];
    tmp.toArray(parms);

    // Pass to subfunction.
    sendPing(m_trackback.toString(),parms);
    
  } // end sendPing

} // end class TrackbackItem
