package org.dbxml.core;

/*
 * 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.32 2001/09/04 11:13:32 kstaken Exp $
 */

import org.dbxml.core.objects.*;
import org.dbxml.core.Collection;
import org.dbxml.server.*;

import java.util.*;
import org.w3c.dom.*;

/**
 * CollectionManager is the base class for both Database and Collection.
 */

public class CollectionManager implements Configurable, Disposable {
   private static final String COLLECTIONS = "collections";
   private static final String COLLECTION = "collection";
   private static final String NAME = "name";
   
   private static final String[] EmptyStrings = new String[0];
   
   protected Map collections = new HashMap(); // Collection

   protected Configuration config = null;

   protected CollectionManager() {
   }
   
   public void setConfig(Configuration config) throws dbXMLException {
      this.config = config;

      Configuration colConfig = config.getChild(COLLECTIONS);
      if ( colConfig != null ) {
         colConfig.processChildren(COLLECTION,
            new ConfigurationCallback() {
               public void process(Configuration cfg) throws dbXMLException {
                  Collection col = new Collection((Collection)CollectionManager.this);
                  col.setConfig(cfg);
                  collections.put(col.getName(), col);
               }
         });
      }
   }

   public Configuration getConfig() {
      return config;
   }

   /**
    * getCollection retrieves a Collection by name.
    *
    * @param path The Collection path
    * @return The Collection (or null)
    */
   public Collection getCollection(String path) throws DBException {
      if ( path.indexOf("/") != -1 ) {
         CollectionManager cm = this;
         StringTokenizer st = new StringTokenizer(path, "/");
         while ( cm != null && st.hasMoreTokens()) {
            path = st.nextToken();
            cm = (CollectionManager)cm.collections.get(path);
         }
         return (Collection)cm;
      }
      else
         return (Collection)collections.get(path);
   }

   /**
    * listCollections retrieves a list of Collections as an array of
    * Strings.
    *
    * @return The Collection list
    */
   public final String[] listCollections() throws DBException {
      return (String[])collections.keySet().toArray(EmptyStrings);
   }

   /**
    * dropCollection physically removes the specified Collection and any
    * associated system resources that the Collection uses.
    *
    * @param collection The Collection to drop
    * @return Whether or not the Collection was dropped
    */
   public final boolean dropCollection(Collection collection) throws DBException  {
      if ( collection == null )
         throw new DBException(FaultCodes.COL_COLLECTION_NOT_FOUND, "Collection Value Null");
            
      Collection cm = collection.getParentCollection();

      if ( cm == null )
         throw new DBException(FaultCodes.DBE_CANNOT_DROP, "You Cannot Drop The Database");
      
      if ( cm != this )
         return cm.dropCollection(collection);
      else {
         String path = collection.getCanonicalName();
         final String name = collection.getName();
         boolean dropped = collection.drop();
         if ( dropped ) {
            collections.remove(name);
            Configuration colConfig = config.getChild(COLLECTIONS);
            colConfig.processChildren(COLLECTION,
               new ConfigurationCallback() {
                  public void process(Configuration cfg) {
                     try {
                        if ( cfg.getAttribute(NAME).equals(name) )
                           cfg.delete();
                     }
                     catch ( Exception e ) {
                        org.dbxml.Debug.printStackTrace(e);
                     }
                  }
               });
         }
         return dropped;
      }
   }

   /**
    * createCollection creates a new Collection object and any associated
    * system resources that the Collection will need.
    *
    * @param path The relative path of the Collection
    * @param config The Collection's configuration
    * @return The newly created Collection
    */
   public final Collection createCollection(String path, Configuration config)
         throws DBException {
      if ( path.indexOf("/") != -1 ) {
         CollectionManager cm = this;
         StringTokenizer st = new StringTokenizer(path, "/");
         while ( cm != null && st.hasMoreTokens()) {
            path = st.nextToken().trim();
            if ( path.length() == 0 )
               continue;
            if ( st.hasMoreTokens() )
               cm = (CollectionManager)cm.collections.get(path);
            else
               return cm.createCollection(path, config);
         }
         throw new DBException(FaultCodes.COL_COLLECTION_NOT_FOUND, "Parent Collection '"+path+"' doesn't exist");
      }

      String url = "";
      Collection collection = null;

      if ( CollectionManager.this instanceof Database )
         collection = new Collection((Database)CollectionManager.this);
      else
         collection = new Collection((Collection)CollectionManager.this);
      
      try {
         collection.setConfig(config);
         
         // Do a name check to see if all is well
         String n = collection.getName();
            
         if ( n == null || n.trim().equals("") )
            throw new DBException(FaultCodes.COL_CANNOT_CREATE, "No name specified");
         
         if ( getCollection(n) != null )
            throw new DBException(FaultCodes.COL_DUPLICATE_COLLECTION, "Duplicate Collection '"+n+"'");
         
         collection.create();
         collections.put(n, collection);
         
         Configuration colConfig = this.config.getChild(COLLECTIONS, true);
         colConfig.add(config);
      }
      catch ( DBException d ) {
         throw d;
      }
      catch ( Exception e ) {
         throw new DBException(FaultCodes.COL_CANNOT_CREATE, "Error Creating Collection '"+path+"'");
      }
      return collection;
   }
   
   public void dispose() {
      if ( this instanceof Collection ) {
         try {
            ((Collection)this).close();
         }
         catch ( Exception e ) {
            org.dbxml.Debug.printStackTrace(e);
         }
      }
   }
}
