/* You can redistribute this software and/or modify it under the terms of
* the Ozone Library License version 1 published by ozone-db.org.
*
* The original code and portions created by SMB are
* Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
*
*/
package test.xmldb;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.OzoneInterface;
import org.ozoneDB.xml.core.XMLCollectionImpl;
import org.ozoneDB.xml.core.XMLCollection;
import java.util.Iterator;
/**
* @author Per Nyfelt
*/
public class CollectionStorageHelper {
private ExternalDatabase db = null;
/**
* the URI used to get the connection to our database,
* a default value provided as example
*/
private String dbURI = "ozonedb:remote://localhost:3333";
/** Creates new Class */
public CollectionStorageHelper(final String dbURI) {
this.dbURI = dbURI;
}
public void createCollection(String collectionName) throws Exception {
connect();
// check if there is an object there already in that case delete it
// we assume we are cleaning up after an usuccessful testrun
XMLCollection test = (XMLCollection)db.objectForName(collectionName);
if (test != null) {
deleteCollection(collectionName);
connect();
}
// create a new Collection
XMLCollection root = (XMLCollection)db.createObject( XMLCollectionImpl.class.getName(), OzoneInterface.Public, collectionName);
System.out.println("CollectionStorageHelper.createCollection() - created XMLCollectionImpl as " + collectionName);
root.setName(collectionName);
}
public void deleteCollection(String collectionName) {
try {
connect();
XMLCollection collection = (XMLCollection)db.objectForName(collectionName);
System.out.println("CollectionStorageHelper.deleteCollection() - " +
collection.getResourceCount() + " XML documents in this collection");
Iterator it = collection.getResources().iterator();
String id;
while (it.hasNext()) {
id = (String)it.next();
db.deleteObject(db.objectForName(id));
it.remove();
System.out.println("CollectionStorageHelper.deleteCollection() - removed " + id);
}
db.deleteObject(collection);
System.out.println("CollectionStorageHelper.deleteCollection() - deleted " + collectionName);
db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void connect() throws Exception {
if (db == null || !db.isOpen()) {
db = ExternalDatabase.openDatabase(dbURI);
System.out.println("CollectionStorageHelper.connect() - connected");
db.reloadClasses();
}
}
}