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: ContentClientImpl.java,v 1.4 2004/08/04 12:02:16 bradford Exp $
 */

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ContentClient;
import com.dbxml.db.server.labrador.ContentProxy;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.util.Map;
import org.w3c.dom.Document;

/**
 * ContentClientImpl
 */

public final class ContentClientImpl extends ConnectionManager implements ContentClient {
   private dbXMLClientImpl dbxmlClient;
   private CollectionClientImpl parent;
   private String document;
   private byte[] value;

   private String key;
   private String colName;
   private String canonicalName;
   private int type = -1;

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

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

   private void init() throws dbXMLException {
      Map info = executeMap("getInformation", null);
      key = (String)info.get(ContentProxy.NAME);
      colName = (String)info.get(ContentProxy.COL_NAME);
      canonicalName = (String)info.get(ContentProxy.CANONICAL_NAME);
      Integer i = (Integer)info.get(ContentProxy.CONTENT_TYPE);
      if ( i != null )
         type = i.intValue();
      else
         throw new dbXMLException("This path does not point to a Document");

   }

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

   public String getKey() throws dbXMLException {
      return key;
   }

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

   public CollectionClient getCollection() throws dbXMLException {
      if ( parent == null ) {
         try {
            parent = new CollectionClientImpl(dbxmlClient, colName);
         }
         catch ( Exception e ) {
            throw new dbXMLException(e);
         }
      }
      return parent;
   }

   public String getDocumentAsText() throws dbXMLException {
      if ( document == null )
         document = executeString("getDocument", null);
      return document;
   }

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

   public byte[] getValue() throws dbXMLException {
      if ( value == null )
         value = executeBinary("getValue", null);
      return value;
   }

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

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

   public void setValue(byte[] value) throws dbXMLException {
      execute("setValue", new Object[]{value});
      this.value = value;
   }
}
