1 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 ; 17 18 21 public class CollectionStorageHelper { 22 23 private Logger logger = Logger.getLogger(CollectionStorageHelper.class); 24 25 private ExternalDatabase db = null; 26 27 31 private String dbURI = "ozonedb:remote://localhost:3333"; 32 33 34 35 public CollectionStorageHelper(final String dbURI) { 36 this.dbURI = dbURI; 37 } 38 39 public void createCollection(String collectionName) throws Exception { 40 connect(); 41 XMLCollection test = (XMLCollection)db.objectForName(collectionName); 44 if (test != null) { 45 deleteCollection(collectionName); 46 connect(); 47 } 48 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 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 it = collection.getResources().iterator(); 61 String id; 62 while (it.hasNext()) { 63 id = (String )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 e) { 72 e.printStackTrace(); 73 } 74 } 75 76 private void connect() throws Exception { 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 |