package org.dbxml.client.xmldb.services;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: CollectionManager.java,v 1.14 2001/08/11 09:26:40 kstaken Exp $
 */

import org.dbxml.xml.*;
import org.dbxml.xml.dom.*;
import org.dbxml.client.xmldb.*;
import org.dbxml.core.FaultCodes;
import org.dbxml.core.corba.db.*;
import org.dbxml.core.corba.EncodedBufferConverter;

import org.w3c.dom.*;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.base.Collection;

/**
 * CollectionManager provides management facilities for a Collection instance.
 * Administrative access is required for retrieval of the CollectionManager
 * interface. User level access is provided through the underlying Collection
 * itself.
 */
public class CollectionManager extends CommonConfigurable
      implements ManagedObject, Service, CollectionManagementService {
   protected org.dbxml.core.corba.db.CollectionManager manager = null;
   protected org.dbxml.core.corba.db.Database db = null;
   protected Collection collection = null;
   protected DatabaseImpl database = null;

   /**
    * Creates a new CollectionManager service
    *
    * @param manager the server side CollectionManager to use.
    * @param db the root Database instance that the
    *        CollectionManager is associated with.
    * @param database a reference to the XML:DB Database instance for the API.
    */
   public CollectionManager(org.dbxml.core.corba.db.CollectionManager manager,
         org.dbxml.core.corba.db.Database db, DatabaseImpl database) {
      this.manager = manager;
      this.db = db;
      this.database = database;
   }

   /**
    * Returns the name of the Service
    *
    * @return the name of the Service
    */
   public String getName() {
      return "CollectionManager";
   }

   /**
    * Returns the version of the Service
    *
    * @return the version of the Service
    */
   public String getVersion() {
      return "1.0";
   }

   /**
    * Provides a reference to the XML:DB collection instance that this service
    * is associated with.
    *
    * @param col the XML:DB collection instance associated with this Service
    */
   public void setCollection(org.xmldb.api.base.Collection col) {
      this.collection = col;
   }

   /**
    * Returns the name of the collection that this manager is associated with.
    *
    * @return the name of the collection
    * @exception XMLDBException
    */
   public String getCollectionName() throws XMLDBException {
      try {
         return manager.getName();
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Returns the fully qualified name of the collection that this manager is
    * associated with. This name includes all parent collections.
    *
    * @return the fully qualified name for this collection.
    * @exception XMLDBException
    */
   public String getCanonicalName() throws XMLDBException {
      try {
         return manager.getCanonicalName();
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Creates a new collection in the database identified by name and using
    * the provided configuration.
    *
    * @param path the path of the new collection
    * @param configuration the XML collection configuration to use for
    *        creating this collection.
    * @return The newly created collection
    * @exception XMLDBException
    */
   public Collection createCollection(String path, Document configuration)
         throws XMLDBException {
      try {

         EncodedBuffer buffer =
           EncodedBufferConverter.convertFromDocument(configuration);
         Collection col = new CollectionImpl(manager.createCollection(path, buffer),
            db, database);

         // db.addManagedObject(col);

         return col;
      }
      catch (Exception e) {
e.printStackTrace();         
         throw FaultCodes.createXMLDBException(e);
      }
   }   
   
   /**
    * Drops a child collection from this collection.
    *
    * @param collection The child collection to drop.
    * @exception XMLDBException
    */
   public void dropCollection(String name) throws XMLDBException {
      try {
         this.manager.dropCollection(name);
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Returns a list of all indexers for this collection.
    *
    * @return the list of indexers
    * @exception XMLDBException
    */
   public String[] listIndexers() throws XMLDBException {
      try {
         return manager.listIndexers();
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Creates a new Indexer for this collection.
    *
    * @param name The name for this indexer
    * @param configuration The configuration to use for this indexer.
    * @exception XMLDBException
    */
   public void createIndexer(Document configuration)
         throws XMLDBException {
      EncodedBuffer config =
        EncodedBufferConverter.convertFromDocument(configuration);

      try {
         manager.createIndexer(config);
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Drops the indexer from the collection
    *
    * @param indexer The indexer to drop.
    * @exception XMLDBException
    */
   public void dropIndexer(String name)
         throws XMLDBException {
      try {
         manager.dropIndexer(name);
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Returns a list of all collection level XMLObjects for this collection.
    *
    * @return the list of XMLObjects.
    * @exception XMLDBException
    */
   public String[] listXMLObjects() throws XMLDBException {
      try {
         return manager.listXMLObjects();
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Creates a new collection level XMLObject using the provided configuration.
    * The XMLObject will be added to the collection using the provided name as
    * a key.
    *
    * @param name The name of this XMLObject
    * @param configuration The XML configuration to use
    * @return The created XMLObject
    * @exception XMLDBException
    */
   public void createXMLObject(Document configuration) throws XMLDBException {
      EncodedBuffer config =
        EncodedBufferConverter.convertFromDocument(configuration);
      try {
         manager.createXMLObject(config);
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   /**
    * Drops a collection level XMLObject from the collection.
    *
    * @param object The XMLObject to drop.
    * @exception XMLDBException
    */
   public void dropXMLObject(String name)
         throws XMLDBException {
      try {
         manager.dropXMLObject(name);
      }
      catch (Exception e) {
         throw FaultCodes.createXMLDBException(e);
      }
   }

   // Implementation of the CollectionManagementService interface
   
   /**
    * Creates a simple collection with a basic default configuration. More
    * complex configuration requires using a proprietary interface
    */
   public Collection createCollection(String name) throws XMLDBException {
      Document doc = new DocumentImpl();

      Element colEle = doc.createElement("collection");
      colEle.setAttribute("compressed", "true");
      colEle.setAttribute("name", name);

      doc.appendChild(colEle);

      Element filEle = doc.createElement("filer");
      filEle.setAttribute("class", "org.dbxml.core.filer.BTreeFiler");
      filEle.setAttribute("gzip", "true");

      colEle.appendChild(filEle);
System.out.println(((CollectionImpl) collection).getCanonicalName());
      return createCollection(name, doc);
   }
   
   /**
    * Removes the named collection from the system.
    */
   public void removeCollection(String name) throws XMLDBException {
      dropCollection(name);      
   }

   /**
    * Returns the underlying CORBA implementation object for this collection
    *
    * @return the server side object
    */
   protected org.dbxml.core.corba.db.CollectionManager getServerObject() {      
      return manager;
   }

   /**
    * Removes the server side Servant for this object. Should be called when the
    * the object is no longer needed.
    */
   public void remove() {
      manager.remove();
   }
}

