package com.dbxml.db.common.adapters;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: DOMAdapter.java,v 1.4 2004/02/12 00:17:56 bradford Exp $
 */

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.adapter.SimpleAdapter;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import org.w3c.dom.Document;

/**
 * DOMAdapter adapts a dbXML Collection to support the management of
 * DOM Documents.
 */

public final class DOMAdapter extends SimpleAdapter {
   public DOMAdapter(Collection col) {
      super(col);
   }

   private void putDocument(Transaction tx, Object key, Document doc) throws DBException {
      try {
         DocumentTable dt = DTSMHelper.nodeToTable(doc, col.getSymbols());
         col.setDocument(tx, key, dt);
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.COL_CANNOT_STORE, "Error storing object '" + key + "'", e);
      }
   }

   /**
    * setDocument sets a DOM Document in the Collection based on the provided
    * Key.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @param obj The Document to set
    * @throws DBException If a Database Exception occurs
    */
   public void setDocument(Transaction tx, Object key, Document doc) throws DBException {
      putDocument(tx, key, doc);
   }

   /**
    * insertDocument inserts a DOM Document into the Collection and returns a
    * newly generated Key.
    *
    * @param tx The controlling Transaction
    * @param obj The Document to insert
    * @return The new Object Identifier
    * @throws DBException If a Database Exception occurs
    */
   public Key insertDocument(Transaction tx, Document doc) throws DBException {
      Key key = col.createNewOID();
      putDocument(tx, key, doc);
      return key;
   }

   /**
    * insertDocument inserts a DOM Document into the Collection based
    * on the specified Key.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @param obj The Document to insert
    * @throws DBException If a Database Exception occurs
    */
   public void insertDocument(Transaction tx, Object key, Document doc) throws DBException {
      putDocument(tx, key, doc);
   }

   /**
    * getDocument instantiates and returns a DOM Document based on the
    * provided Key.
    *
    * @param tx The controlling Transaction
    * @param key The Document Key
    * @return a DOM Document
    * @throws DBException If a Database Exception occurs
    */
   public Document getDocument(Transaction tx, Object key) throws DBException {
      try {
         DocumentTable dt = col.getDocument(tx, key);
         if ( dt != null )
            return DTSMHelper.tableToDocument(dt);
         else
            return null;
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Error retrieving object '" + key + "'", e);
      }
   }
}


