package com.dbxml.db.client.xmlrpc;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: CollectionClientImpl.java,v 1.8 2006/02/02 18:53:47 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);
}
}
}