package com.dbxml.db.client.local;
/*
* 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.4 2006/02/02 18:53:46 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.common.adapters.DOMAdapter;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.data.Value;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.NamespaceMap;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import java.util.Map;
import org.w3c.dom.Document;
/**
* CollectionClientImpl
*/
public class CollectionClientImpl implements CollectionClient {
private dbXMLClient client;
private Collection col;
private DOMAdapter domAdapter;
public CollectionClientImpl(dbXMLClient client, Collection col) {
this.client = client;
this.col = col;
domAdapter = new DOMAdapter(col);
}
public dbXMLClient getClient() {
return client;
}
public Collection getInternalCollection() {
return col;
}
public String getCanonicalName() throws dbXMLException {
return col.getCanonicalName();
}
public String getName() throws dbXMLException {
return col.getName();
}
public int getCollectionType() throws dbXMLException {
return col.getCollectionType();
}
public CollectionClient getParentCollection() throws dbXMLException {
Collection pc = col.getParentCollection();
if ( pc != null )
return new CollectionClientImpl(client, pc);
else
throw new dbXMLException("No parent Collection");
}
public CollectionClient getDatabase() throws dbXMLException {
return new CollectionClientImpl(client, col.getDatabase());
}
public CollectionClient getSystemCollection() throws dbXMLException {
return new CollectionClientImpl(client, col.getSystemCollection());
}
public CollectionClient getCollection(String name) throws dbXMLException {
Collection c = col.getCollection(name);
if ( c != null )
return new CollectionClientImpl(client, c);
else
throw new dbXMLException("Collection '" + name + "' not found");
}
public CollectionClient createCollection(String path, Document configuration) throws dbXMLException {
Configuration cfg = new Configuration(configuration);
Collection c = col.createCollection(path, cfg);
if ( c != null )
return new CollectionClientImpl(client, c);
else
throw new dbXMLException("Couldn't create Collection '" + path + "'");
}
public String[] listCollections() throws dbXMLException {
return col.listCollections();
}
public boolean dropCollection(String name) throws dbXMLException {
Collection c = col.getCollection(name);
if ( c != null )
return col.dropCollection(c);
else
return false;
}
public String createTrigger(Document configuration) throws dbXMLException {
Configuration cfg = new Configuration(configuration);
return col.getTriggerManager().create(cfg).getName();
}
public boolean dropTrigger(String name) throws dbXMLException {
return col.getTriggerManager().drop(name);
}
public String[] listTriggers() throws dbXMLException {
return col.getTriggerManager().list();
}
public String createIndexer(Document configuration) throws dbXMLException {
Configuration cfg = new Configuration(configuration);
return col.getIndexManager().create(cfg).getName();
}
public boolean dropIndexer(String name) throws dbXMLException {
return col.getIndexManager().drop(name);
}
public String[] listIndexers() throws dbXMLException {
return col.getIndexManager().list();
}
public String getExtension(String name) throws dbXMLException {
return col.getExtensionManager().getCanonicalName(name);
}
public String createExtension(Document configuration) throws dbXMLException {
Configuration cfg = new Configuration(configuration);
String name = col.getExtensionManager().create(cfg).getName();
return col.getExtensionManager().getCanonicalName(name);
}
public String[] listExtensions() throws dbXMLException {
return col.getExtensionManager().list();
}
public boolean dropExtension(String name) throws dbXMLException {
return col.getExtensionManager().drop(name);
}
public String createKey() throws dbXMLException {
return col.createNewOID().toString();
}
public Document getDocument(String docKey) throws dbXMLException {
Transaction tx = new Transaction();
try {
return domAdapter.getDocument(tx, docKey);
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public String getDocumentAsText(String docKey) throws dbXMLException {
Transaction tx = new Transaction();
try {
return DTSMHelper.tableToText(col.getDocument(tx, docKey));
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public ContentClient getContent(String key) throws dbXMLException {
Transaction tx = new Transaction();
try {
Container con = col.getContainer(tx, key);
if ( con != null )
return new ContentClientImpl(this, con);
else
throw new dbXMLException("Content '" + key + "' not found");
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public String insertDocument(Document document) throws dbXMLException {
Transaction tx = new Transaction();
try {
DocumentTable dt = DTSMHelper.documentToTable(document, col.getSymbols());
return col.insertDocument(tx, dt).toString();
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public String insertDocumentAsText(String document) throws dbXMLException {
Transaction tx = new Transaction();
try {
DocumentTable dt = DTSMHelper.textToTable(document, col.getSymbols());
return col.insertDocument(tx, dt).toString();
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public void setDocument(String docKey, Document document) throws dbXMLException {
Transaction tx = new Transaction();
try {
DocumentTable dt = DTSMHelper.documentToTable(document, col.getSymbols());
col.setDocument(tx, docKey, dt);
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public void setDocumentAsText(String docKey, String document) throws dbXMLException {
Transaction tx = new Transaction();
try {
DocumentTable dt = DTSMHelper.textToTable(document, col.getSymbols());
col.setDocument(tx, docKey, dt);
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public void remove(String docKey) throws dbXMLException {
Transaction tx = new Transaction();
try {
col.remove(tx, docKey);
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public String[] listKeys() throws dbXMLException {
Transaction tx = new Transaction();
try {
Key[] keys = col.listKeys(tx);
String[] result = new String[keys.length];
for ( int i = 0; i < keys.length; i++ )
result[i] = keys[i].toString();
return result;
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public long getKeyCount() throws dbXMLException {
Transaction tx = new Transaction();
try {
return col.getKeyCount(tx);
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public String insertValue(byte[] value) throws dbXMLException {
Transaction tx = new Transaction();
try {
return col.insertRecord(tx, new Value(value)).toString();
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public void setValue(String key, byte[] value) throws dbXMLException {
Transaction tx = new Transaction();
try {
col.setRecord(tx, key, new Value(value));
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public byte[] getValue(String key) throws dbXMLException {
Transaction tx = new Transaction();
try {
return col.getRecord(tx, key).getValue().getData();
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public ResultSetClient queryCollection(String style, String query, Map nsMap) throws dbXMLException {
Transaction tx = new Transaction();
try {
return new ResultSetClientImpl(this, col.queryCollection(tx, style, query, new NamespaceMap(nsMap)));
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
public ResultSetClient queryDocument(String style, String query, Map nsMap, String key) throws dbXMLException {
Transaction tx = new Transaction();
try {
return new ResultSetClientImpl(this, col.queryDocument(tx, style, query, new NamespaceMap(nsMap), key));
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
}