package com.dbxml.db.client.xmlrpc;

/*
 * 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.7 2004/08/04 12:02:16 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.client.helpers.TextResultSetClient;
import com.dbxml.db.server.labrador.CollectionProxy;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Document;

/**
 * CollectionClientImpl
 */

public final class CollectionClientImpl extends ConnectionManager implements CollectionClient {
   private dbXMLClientImpl dbxmlClient;
   private CollectionClientImpl parent;
   private boolean hasParent = true;

   private String name;
   private String canonicalName;
   private String parentName;
   private int type = -1;

   public CollectionClientImpl(dbXMLClientImpl dbxmlClient, CollectionClientImpl parent, String path) throws dbXMLException {
      super(dbxmlClient);
      this.dbxmlClient = dbxmlClient;
      this.parent = parent;
      setClientPath(path);
      init();
   }

   public CollectionClientImpl(dbXMLClientImpl dbxmlClient, String path, boolean hasParent) throws dbXMLException {
      super(dbxmlClient);
      this.dbxmlClient = dbxmlClient;
      this.hasParent = hasParent;
      setClientPath(path);
      init();
   }

   public CollectionClientImpl(dbXMLClientImpl dbxmlClient, String path) throws dbXMLException {
      this(dbxmlClient, path, true);
   }

   private void init() throws dbXMLException {
      Map info = executeMap("getInformation", null);
      name = (String)info.get(CollectionProxy.NAME);
      canonicalName = (String)info.get(CollectionProxy.CANONICAL_NAME);
      parentName = (String)info.get(CollectionProxy.PARENT_COLLECTION);
      Integer i = (Integer)info.get(CollectionProxy.COLLECTION_TYPE);
      if ( i != null )
         type = i.intValue();
      else
         throw new dbXMLException("This path does not point to a Collection");
   }

   public dbXMLClient getClient() {
      return dbxmlClient;
   }

   public String getCanonicalName() throws dbXMLException {
      return canonicalName;
   }

   public String getName() throws dbXMLException {
      return name;
   }

   public int getCollectionType() throws dbXMLException {
      return type;
   }

   public CollectionClient getParentCollection() throws dbXMLException {
      if ( hasParent && parent == null ) {
         try {
            parent = new CollectionClientImpl(dbxmlClient, parentName);
         }
         catch ( dbXMLException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new dbXMLException(e);
         }
      }
      else if ( !hasParent )
         throw new dbXMLException("Collection '"+getName()+"' has no parent");

      return parent;
   }

   public CollectionClient getDatabase() throws dbXMLException {
      CollectionClient p = getParentCollection();
      if ( p != null )
         return p.getDatabase();
      else
         return this;
   }

   public CollectionClient getSystemCollection() throws dbXMLException {
      return getDatabase().getCollection("system");
   }

   public CollectionClient getCollection(String name) throws dbXMLException {
      String childPath = executeString("getCollection", new Object[]{name});
      return new CollectionClientImpl(dbxmlClient, this, childPath);
   }

   public CollectionClient createCollection(String newPath, Document configuration) throws dbXMLException {
      Object[] params = new Object[]{newPath, TextWriter.toString(configuration)};
      String child = executeString("createCollection", params);
      return new CollectionClientImpl(dbxmlClient, this, child);
   }

   public String[] listCollections() throws dbXMLException {
      return executeList("listCollections", null);
   }

   public boolean dropCollection(String name) throws dbXMLException {
      return executeBoolean("dropCollection", new Object[]{name});
   }

   public String createTrigger(Document configuration) throws dbXMLException {
      return executeString("createTrigger", new Object[]{TextWriter.toString(configuration)});
   }

   public boolean dropTrigger(String name) throws dbXMLException {
      return executeBoolean("dropTrigger", new Object[]{name});
   }

   public String[] listTriggers() throws dbXMLException {
      return executeList("listTriggers", null);
   }

   public String createIndexer(Document configuration) throws dbXMLException {
      return executeString("createIndexer", new Object[]{TextWriter.toString(configuration)});
   }

   public boolean dropIndexer(String name) throws dbXMLException {
      return executeBoolean("dropIndexer", new Object[]{name});
   }

   public String[] listIndexers() throws dbXMLException {
      return executeList("listIndexers", null);
   }

   public String getExtension(String name) throws dbXMLException {
      return executeString("getExtension", new Object[]{name});
   }

   public String createExtension(Document configuration) throws dbXMLException {
      return executeString("createExtension", new Object[]{TextWriter.toString(configuration)});
   }

   public String[] listExtensions() throws dbXMLException {
      return executeList("listExtensions", null);
   }

   public boolean dropExtension(String name) throws dbXMLException {
      return executeBoolean("dropExtension", new Object[]{name});
   }

   public String createKey() throws dbXMLException {
      return executeString("createKey", null);
   }

   public String getDocumentAsText(String docKey) throws dbXMLException {
      return executeString("getDocument", new Object[]{docKey});
   }

   public Document getDocument(String docKey) throws dbXMLException {
      try {
         return DOMHelper.parseText(getDocumentAsText(docKey));
      }
      catch ( dbXMLException e ) {
         throw e;
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }

   public ContentClient getContent(String key) throws dbXMLException {
      try {
         return new ContentClientImpl(dbxmlClient, this, getClientPath() + "/" + key);
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }

   public String insertDocumentAsText(String document) throws dbXMLException {
      return executeString("insertDocument", new Object[]{document});
   }

   public String insertDocument(Document document) throws dbXMLException {
      try {
         return insertDocumentAsText(TextWriter.toString(document));
      }
      catch ( dbXMLException e ) {
         throw e;
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }

   public void setDocumentAsText(String docKey, String document) throws dbXMLException {
      execute("setDocument", new Object[]{docKey, document});
   }

   public void setDocument(String docKey, Document document) throws dbXMLException {
      try {
         setDocumentAsText(docKey, TextWriter.toString(document));
      }
      catch ( dbXMLException e ) {
         throw e;
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }

   public void remove(String docKey) throws dbXMLException {
      execute("remove", new Object[]{docKey});
   }

   public String[] listKeys() throws dbXMLException {
      return executeList("listKeys", null);
   }

   public long getKeyCount() throws dbXMLException {
      return executeInt("getKeyCount", null);
   }

   public String insertValue(byte[] value) throws dbXMLException {
      return executeString("insertValue", new Object[]{value});
   }

   public void setValue(String key, byte[] value) throws dbXMLException {
      execute("setValue", new Object[]{key, value});
   }

   public byte[] getValue(String key) throws dbXMLException {
      return executeBinary("getValue", new Object[]{key});
   }

   public ResultSetClient queryCollection(String style, String query, Map nsMap) throws dbXMLException {
      try {
         Object[] params = new Object[]{style, query, nsMap};
         String content = executeString("queryCollection", params);
         return new TextResultSetClient(this, content, style, query);
      }
      catch ( dbXMLException e ) {
         throw e;
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }

   public ResultSetClient queryDocument(String style, String query, Map nsMap, String key) throws dbXMLException {
      try {
         Object[] params = new Object[]{style, query, nsMap, key};
         String content = executeString("queryDocument", params);
         return new TextResultSetClient(this, content, style, query);
      }
      catch ( Exception e ) {
         throw new dbXMLException(e);
      }
   }
}

