/*
 * 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@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.unistore;

import java.io.*;
import java.util.*;
import com.silverwrist.dynamo.db.OpsBase;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

abstract class MessageOps extends OpsBase
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  static final String PARAM_PART = "part";
  static final String PARAM_IDENTITY = "identity";
  static final String PARAM_MIMETYPE = "mimetype";
  static final String PARAM_SIZE = "size";
  static final String PARAM_LINECOUNT = "linecount";
  static final String PARAM_READS = "reads";
  static final String PARAM_LASTREAD = "lastread";
  static final String PARAM_FILENAME = "filename";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private TextPartOps m_textops = null;
  private BinaryPartOps m_binaryops = null;

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

  protected MessageOps(DBConnectionPool pool)
  {
    super(pool);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class OpsBase
   *--------------------------------------------------------------------------------
   */

  public void dispose()
  {
    if (m_textops!=null)
      m_textops.dispose();
    m_textops = null;
    if (m_binaryops!=null)
      m_binaryops.dispose();
    m_binaryops = null;
    super.dispose();

  } // end dispose

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

  protected abstract TextPartOps createTextPartOps(DBConnectionPool pool);

  protected abstract BinaryPartOps createBinaryPartOps(DBConnectionPool pool);

  abstract Object getProperty(long msgid, PropertyKey key) throws DatabaseException;

  abstract Object setProperty(long msgid, PropertyKey key, Object value) throws DatabaseException;

  abstract Object removeProperty(long msgid, PropertyKey key) throws DatabaseException;

  abstract int[] getPropertyNamespaceIDs(long msgid) throws DatabaseException;

  abstract Map getAllProperties(long msgid, int namespace) throws DatabaseException;

  abstract void setParentMessageID(long msgid, long parent) throws DatabaseException;

  abstract void setSequence(long msgid, int seq) throws DatabaseException;

  abstract void setAclID(long msgid, int aclid) throws DatabaseException;

  abstract int getNumTextParts(long msgid) throws DatabaseException;

  abstract int getNumBinaryParts(long msgid) throws DatabaseException;

  abstract Map loadTextPart(long msgid, int part) throws DatabaseException;

  abstract Map loadTextPart(long msgid, PropertyKey identity) throws DatabaseException;

  abstract Map loadBinaryPart(long msgid, int part) throws DatabaseException;

  abstract Map loadBinaryPart(long msgid, PropertyKey identity) throws DatabaseException;

  abstract int createTextPart(long msgid, int nsid, String name, String mimetype, int charcount, int linecount,
			      String data) throws DatabaseException;

  abstract int createBinaryPart(long msgid, int nsid, String name, String mimetype, String filename, int length,
				InputStream data) throws DatabaseException;

  abstract void delete(long msgid) throws DatabaseException;

  abstract Map listTextParts(long msgid) throws DatabaseException;

  abstract Map listBinaryParts(long msgid) throws DatabaseException;

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

  synchronized TextPartOps getTextPartOps()
  {
    if (m_textops==null)
      m_textops = createTextPartOps(getPool());
    return m_textops;

  } // end getTextPartOps

  synchronized BinaryPartOps getBinaryPartOps()
  {
    if (m_binaryops==null)
      m_binaryops = createBinaryPartOps(getPool());
    return m_binaryops;

  } // end getBinaryPartOps

} // end class MessageOps
