package data.ooimpl;

import data.*;

import log.*;

/**
  * A {@link DataBasketEntry} representing operations on {@link CatalogImpl}s
  * and {@link CatalogItemImpl}s. The fields of the <code>DataBasketEntry</code>
  * are set as follows:
  *
  * <table border=1>
  * <tr><td><strong>Field</strong></td><td><strong>Value</strong></td></tr>
  * <tr><td>{@link DataBasketEntry#getMainKey main key}</td>
  *     <td>{@link DataBasketKeys#CATALOG_ITEM_MAIN_KEY CATALOG_ITEM_MAIN_KEY}
  *     </td>
  * </tr>
  * <tr><td>{@link DataBasketEntry#getSecondaryKey secondary key}</td>
  *     <td>{@link CatalogItem#getName name} of the CatalogItem in question</td>
  * </tr>
  * <tr><td>{@link DataBasketEntry#getSource source}</td>
  *     <td>{@link Catalog source catalog}<td>
  * </tr>
  * <tr><td>{@link DataBasketEntry#getDestination destination}</td>
  *     <td>{@link Catalog destination catalog}<td>
  * </tr>
  * <tr><td>{@link DataBasketEntry#getValue value}</td>
  *     <td>{@link CatalogItem} in question<td>
  * </tr>
  * </table>
  *
  * @author Steffen Zschaler
  * @version 2.0 19/08/1999
  * @since v2.0
  */
public class CatalogItemDataBasketEntry extends DataBasketEntryImpl {

  /**
    * Create a new CatalogItemDataBasketEntry.
    *
    * @param cSource the source Catalog.
    * @param cDest the destination Catalog.
    * @param ci the CatalogItem that was operated on.
    */
  public CatalogItemDataBasketEntry (CatalogImpl cSource,
                                     CatalogImpl cDest,
                                     CatalogItemImpl ci) {
    super (CATALOG_ITEM_MAIN_KEY,
           ci.getName(),
           (SelfManagingDBESource) cSource,
           (SelfManagingDBEDestination) cDest,
           ci);
  }

  /**
    * Set the source of the DataBasketEntry.
    *
    * <p>This method is public as an implementation detail and must not be called directly!</p>
    *
    * @override Never
    */
  public void setSource (CatalogImpl cSource) {
    m_dbesSource = (SelfManagingDBESource) cSource;
  }

  /**
    * Set the destination of the DataBasketEntry.
    *
    * <p>This method is public as an implementation detail and must not be called directly!</p>
    *
    * @override Never
    */
  public void setDestination (CatalogImpl cDest) {
    m_dbedDest = (SelfManagingDBEDestination) cDest;
  }

  /**
    * A LogEntry that describes {@link CatalogItemDataBasketEntry CatalogItemDataBasketEntries}.
    *
    * @author Steffen Zschaler
    * @version 2.0 19/08/1999
    * @since v2.0
    */
  public static class CIDBELogEntry extends LogEntry {

    /**
      * The name of the source Catalog,if any.
      *
      * @serial
      */
    private String m_sSourceName;

    /**
      * The name of the destination Catalog, if any.
      *
      * @serial
      */
    private String m_sDestName;

    /**
      * The key of the CatalogItem.
      *
      * @serial
      */
    private String m_sKey;

    /**
      * The result of the CatalogItem's toString() method.
      *
      * @serial
      */
    private String m_sCIDescription;

    /**
      * Create a new CIDBELogEntry.
      *
      * @param cidbe the DataBasketEntry to be logged.
      */
    public CIDBELogEntry (CatalogItemDataBasketEntry cidbe) {
      super();

      m_sSourceName = ((cidbe.getSource() != null)?
                       (((Nameable) cidbe.getSource()).getName()):
                       (null));
      m_sDestName = ((cidbe.getDestination() != null)?
                     (((Nameable) cidbe.getDestination()).getName()):
                     (null));
      m_sKey = cidbe.getSecondaryKey();
      m_sCIDescription = ((cidbe.getValue() != null)?
                          (cidbe.getValue().toString()):
                          (null));
    }

    /**
      * Get the source Catalog's name.
      *
      * @override Never
      */
    public String getSource() {
      return m_sSourceName;
    }

    /**
      * Get the destination Catalog's name.
      *
      * @override Never
      */
    public String getDestination() {
      return m_sDestName;
    }

    /**
      * Get the CatalogItem's key.
      *
      * @override Never
      */
    public String getKey() {
      return m_sKey;
    }

    /**
      * Get the CatalogItem's description. I.e. the result of its toString() method.
      *
      * @override Never
      */
    public String getCIDescription() {
      return m_sCIDescription;
    }

    /**
      * Return a String representation of this LogEntry.
      *
      * @override Never
      */
    public String toString() {
      return "CatalogItem transfer: \"" + getKey() + "\" (" + getCIDescription() + ") was transferred" +
             ((getSource() != null)?(" from Catalog \"" + getSource() + "\""):("")) +
             ((getDestination() != null)?(" to Catalog \"" + getDestination() + "\""):("")) +
             " on " + getLogDate() + ".";
    }
  }

  /**
    * Return a LogEntry describing this DataBasketEntry.
    *
    * @override Never
    */
  public LogEntry getLogData() {
    return new CIDBELogEntry (this);
  }
}