KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > xmldb > CollectionStorageHelper


1 /* You can redistribute this software and/or modify it under the terms of
2  * the Ozone Library License version 1 published by ozone-db.org.
3  *
4  * The original code and portions created by SMB are
5  * Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6  *
7  */

8 package test.xmldb;
9
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 org.apache.log4j.Logger;
15
16 import java.util.Iterator JavaDoc;
17
18 /**
19  * @author Per Nyfelt
20  */

21 public class CollectionStorageHelper {
22
23     private Logger logger = Logger.getLogger(CollectionStorageHelper.class);
24
25     private ExternalDatabase db = null;
26     
27     /**
28      * the URI used to get the connection to our database,
29      * a default value provided as example
30      */

31     private String JavaDoc dbURI = "ozonedb:remote://localhost:3333";
32
33     
34     /** Creates new Class */
35     public CollectionStorageHelper(final String JavaDoc dbURI) {
36         this.dbURI = dbURI;
37     }
38     
39     public void createCollection(String JavaDoc collectionName) throws Exception JavaDoc {
40         connect();
41         // check if there is an object there already in that case delete it
42
// we assume we are cleaning up after an usuccessful testrun
43
XMLCollection test = (XMLCollection)db.objectForName(collectionName);
44         if (test != null) {
45             deleteCollection(collectionName);
46             connect();
47         }
48         // create a new Collection
49
XMLCollection root = (XMLCollection)db.createObject( XMLCollectionImpl.class.getName(), OzoneInterface.Public, collectionName);
50         logger.debug("CollectionStorageHelper.createCollection() - created XMLCollectionImpl as " + collectionName);
51         root.setName(collectionName);
52     }
53     
54     public void deleteCollection(String JavaDoc collectionName) {
55         try {
56             connect();
57             XMLCollection collection = (XMLCollection)db.objectForName(collectionName);
58             logger.debug("CollectionStorageHelper.deleteCollection() - " +
59                 collection.getResourceCount() + " XML documents in this collection");
60             Iterator JavaDoc it = collection.getResources().iterator();
61             String JavaDoc id;
62             while (it.hasNext()) {
63                 id = (String JavaDoc)it.next();
64                 db.deleteObject(db.objectForName(id));
65                 it.remove();
66                 logger.debug("CollectionStorageHelper.deleteCollection() - removed " + id);
67             }
68             db.deleteObject(collection);
69             logger.debug("CollectionStorageHelper.deleteCollection() - deleted " + collectionName);
70             db.close();
71         } catch (Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74     }
75     
76     private void connect() throws Exception JavaDoc {
77         if (db == null || !db.isOpen()) {
78             db = ExternalDatabase.openDatabase(dbURI);
79             logger.debug("CollectionStorageHelper.connect() - connected");
80             db.reloadClasses();
81         }
82     }
83 }
84
Popular Tags