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: dbXMLClientImpl.java,v 1.2 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.dbXMLClient;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.CollectionManager;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.Database;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.server.dbXML;
import com.dbxml.util.dbXMLException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * dbXMLClientImpl
 */

public class dbXMLClientImpl implements dbXMLClient {
   private static final String[] EmptyStrings = new String[0];

   private Map props = new TreeMap();
   private boolean connected;

   public void setProperty(String name, String value) {
      props.put(name, value);
   }

   public String getProperty(String name) {
      return (String)props.get(name);
   }

   public String[] listProperties() {
      return (String[])props.keySet().toArray(EmptyStrings);
   }

   public Map getProperties() {
      return props;
   }

   public void connect() throws dbXMLException {
      if ( connected )
         throw new dbXMLException("Already connected");
      connected = true;
   }

   public void disconnect() throws dbXMLException {
      if ( !connected )
         throw new dbXMLException("Not connected");
      connected = false;
   }

   public String getServerVersion() throws dbXMLException {
      return dbXML.VersionString;
   }
   
   public void shutdown(int exitCode) throws dbXMLException {
      /** @todo Should this method do anything? */
   }

   public CollectionClient getDatabase() throws dbXMLException {
      Database db = Database.getInstance();
      if ( db != null )
         return new CollectionClientImpl(this, db);
      else
         throw new dbXMLException("Database not found");
   }

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

   public ContentClient getContent(String path) throws dbXMLException {
      Transaction tx = new Transaction();
      try {
         int idx = path.lastIndexOf('/');
         if ( idx != -1 ) {
            String docName = path.substring(idx + 1);
            path = path.substring(0, idx);
            Collection col = getAbsoluteCollection(path);
            if ( col != null ) {
               Container con = col.getContainer(tx, docName);
               if ( con != null )
                  return new ContentClientImpl(new CollectionClientImpl(this, col), con);
               else
                  throw new dbXMLException("Document '" + docName + "' not found");
            }
            else
               throw new dbXMLException("Collection '" + path + "' not found");
         }
         else
            throw new dbXMLException("Invalid Document Path '" + path + "'");
      }
      catch ( dbXMLException e ) {
         tx.cancel();
         throw e;
      }
		finally {
			if ( tx.getStatus() == Transaction.ACTIVE )
				tx.commit();
		}
   }

   private Collection getAbsoluteCollection(String path) throws dbXMLException {
      if ( path.startsWith("/") ) {
         StringTokenizer st = new StringTokenizer(path, "/");
         CollectionManager cm = Database.getInstance();
         if ( !st.hasMoreTokens() )
            return (Collection)cm;
         while ( cm != null && st.hasMoreTokens() ) {
            path = st.nextToken();
            cm = cm.getCollection(path);
         }
         return (Collection)cm;
      }
      else
         throw new dbXMLException("Invalid Collection Path '"+path+"'");
   }
}
