package com.dbxml.db.client.local;

/*
 * 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: CollectionClientImpl.java,v 1.3 2004/02/12 00:17:55 bradford Exp $
 */

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ContentClient;
import com.dbxml.db.client.ResultSetClient;
import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.common.adapters.DOMAdapter;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.data.Value;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.NamespaceMap;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import java.util.Map;
import org.w3c.dom.Document;

/**
 * CollectionClientImpl
 */

public class CollectionClientImpl implements CollectionClient {
   private dbXMLClient client;
   private Collection col;
   private DOMAdapter domAdapter;

   public CollectionClientImpl(dbXMLClient client, Collection col) {
      this.client = client;
      this.col = col;
      domAdapter = new DOMAdapter(col);
   }

   public dbXMLClient getClient() {
      return client;
   }

   public Collection getInternalCollection() {
      return col;
   }

   public String getCanonicalName() throws dbXMLException {
      return col.getCanonicalName();
   }

   public String getName() throws dbXMLException {
      return col.getName();
   }

   public int getCollectionType() throws dbXMLException {
      return col.getCollectionType();
   }

   public CollectionClient getParentCollection() throws dbXMLException {
      Collection pc = col.getParentCollection();
      if ( pc != null )
         return new CollectionClientImpl(client, pc);
      else
         throw new dbXMLException("No parent Collection");
   }

   public CollectionClient getDatabase() throws dbXMLException {
      return new CollectionClientImpl(client, col.getDatabase());
   }

   public CollectionClient getSystemCollection() throws dbXMLException {
      return new CollectionClientImpl(client, col.getSystemCollection());
   }

   public CollectionClient getCollection(String name) throws dbXMLException {
      Collection c = col.getCollection(name);
      if ( c != null )
         return new CollectionClientImpl(client, c);
      else
         throw new dbXMLException("Collection '" + name + "' not found");
   }

   public CollectionClient createCollection(String path, Document configuration) throws dbXMLException {
      Configuration cfg = new Configuration(configuration);
      Collection c = col.createCollection(path, cfg);
      if ( c != null )
         return new CollectionClientImpl(client, c);
      else
         throw new dbXMLException("Couldn't create Collection '" + path + "'");
   }

   public String[] listCollections() throws dbXMLException {
      return col.listCollections();
   }

   public boolean dropCollection(String name) throws dbXMLException {
      Collection c = col.getCollection(name);
      if ( c != null )
         return col.dropCollection(c);
      else
         return false;
   }

   public String createTrigger(Document configuration) throws dbXMLException {
      Configuration cfg = new Configuration(configuration);
      return col.getTriggerManager().create(cfg).getName();
   }

   public boolean dropTrigger(String name) throws dbXMLException {
      return col.getTriggerManager().drop(name);
   }

   public String[] listTriggers() throws dbXMLException {
      return col.getTriggerManager().list();
   }

   public String createIndexer(Document configuration) throws dbXMLException {
      Configuration cfg = new Configuration(configuration);
      return col.getIndexManager().create(cfg).getName();
   }

   public boolean dropIndexer(String name) throws dbXMLException {
      return col.getIndexManager().drop(name);
   }

   public String[] listIndexers() throws dbXMLException {
      return col.getIndexManager().list();
   }

   public String getExtension(String name) throws dbXMLException {
      return col.getExtensionManager().getCanonicalName(name);
   }

   public String createExtension(Document configuration) throws dbXMLException {
      Configuration cfg = new Configuration(configuration);
      String name = col.getExtensionManager().create(cfg).getName();
      return col.getExtensionManager().getCanonicalName(name);
   }

   public String[] listExtensions() throws dbXMLException {
      return col.getExtensionManager().list();
   }

   public boolean dropExtension(String name) throws dbXMLException {
      return col.getExtensionManager().drop(name);
   }

   public String createKey() throws dbXMLException {
      return col.createNewOID().toString();
   }

   public Document getDocument(String docKey) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return domAdapter.getDocument(tx, docKey);
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public String getDocumentAsText(String docKey) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return DTSMHelper.tableToText(col.getDocument(tx, docKey));
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public ContentClient getContent(String key) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         Container con = col.getContainer(tx, key);
         if ( con != null )
            return new ContentClientImpl(this, con);
         else
            throw new dbXMLException("Content '" + key + "' not found");
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public String insertDocument(Document document) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         DocumentTable dt = DTSMHelper.documentToTable(document, col.getSymbols());
         return col.insertDocument(tx, dt).toString();
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public String insertDocumentAsText(String document) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         DocumentTable dt = DTSMHelper.textToTable(document, col.getSymbols());
         return col.insertDocument(tx, dt).toString();
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public void setDocument(String docKey, Document document) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         DocumentTable dt = DTSMHelper.documentToTable(document, col.getSymbols());
         col.setDocument(tx, docKey, dt);
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }


   public void setDocumentAsText(String docKey, String document) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         DocumentTable dt = DTSMHelper.textToTable(document, col.getSymbols());
         col.setDocument(tx, docKey, dt);
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public void remove(String docKey) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         col.remove(tx, docKey);
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public String[] listKeys() throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         Key[] keys = col.listKeys(tx);
         String[] result = new String[keys.length];
         for ( int i = 0; i < keys.length; i++ )
            result[i] = keys[i].toString();
         return result;
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public long getKeyCount() throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return col.getKeyCount(tx);
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public String insertValue(byte[] value) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return col.insertRecord(tx, new Value(value)).toString();
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public void setValue(String key, byte[] value) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         col.setRecord(tx, key, new Value(value));
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public byte[] getValue(String key) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return col.getRecord(tx, key).getValue().getData();
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public ResultSetClient queryCollection(String style, String query, Map nsMap) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return new ResultSetClientImpl(this, col.queryCollection(tx, style, query, new NamespaceMap(nsMap)));
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }

   public ResultSetClient queryDocument(String style, String query, Map nsMap, String key) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         return new ResultSetClientImpl(this, col.queryDocument(tx, style, query, new NamespaceMap(nsMap), key));
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE )
            tx.commit();
      }
   }
}
