01: /* You can redistribute this software and/or modify it under the terms of
02: * the Ozone Library License version 1 published by ozone-db.org.
03: *
04: * The original code and portions created by SMB are
05: * Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
06: *
07: */
08: package test.xmldb;
09:
10: import org.ozoneDB.ExternalDatabase;
11: import org.ozoneDB.OzoneInterface;
12: import org.ozoneDB.xml.core.XMLCollectionImpl;
13: import org.ozoneDB.xml.core.XMLCollection;
14: import java.util.Iterator;
15:
16: /**
17: * @author Per Nyfelt
18: */
19: public class CollectionStorageHelper {
20: private ExternalDatabase db = null;
21:
22: /**
23: * the URI used to get the connection to our database,
24: * a default value provided as example
25: */
26: private String dbURI = "ozonedb:remote://localhost:3333";
27:
28: /** Creates new Class */
29: public CollectionStorageHelper(final String dbURI) {
30: this .dbURI = dbURI;
31: }
32:
33: public void createCollection(String collectionName)
34: throws Exception {
35: connect();
36: // check if there is an object there already in that case delete it
37: // we assume we are cleaning up after an usuccessful testrun
38: XMLCollection test = (XMLCollection) db
39: .objectForName(collectionName);
40: if (test != null) {
41: deleteCollection(collectionName);
42: connect();
43: }
44: // create a new Collection
45: XMLCollection root = (XMLCollection) db.createObject(
46: XMLCollectionImpl.class.getName(),
47: OzoneInterface.Public, collectionName);
48: System.out
49: .println("CollectionStorageHelper.createCollection() - created XMLCollectionImpl as "
50: + collectionName);
51: root.setName(collectionName);
52: }
53:
54: public void deleteCollection(String collectionName) {
55: try {
56: connect();
57: XMLCollection collection = (XMLCollection) db
58: .objectForName(collectionName);
59: System.out
60: .println("CollectionStorageHelper.deleteCollection() - "
61: + collection.getResourceCount()
62: + " XML documents in this collection");
63: Iterator it = collection.getResources().iterator();
64: String id;
65: while (it.hasNext()) {
66: id = (String) it.next();
67: db.deleteObject(db.objectForName(id));
68: it.remove();
69: System.out
70: .println("CollectionStorageHelper.deleteCollection() - removed "
71: + id);
72: }
73: db.deleteObject(collection);
74: System.out
75: .println("CollectionStorageHelper.deleteCollection() - deleted "
76: + collectionName);
77: db.close();
78: } catch (Exception e) {
79: e.printStackTrace();
80: }
81: }
82:
83: private void connect() throws Exception {
84: if (db == null || !db.isOpen()) {
85: db = ExternalDatabase.openDatabase(dbURI);
86: System.out
87: .println("CollectionStorageHelper.connect() - connected");
88: db.reloadClasses();
89: }
90: }
91: }
|