⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collectionstoragehelper.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
/* 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 org.apache.log4j.Logger;

import java.util.Iterator;

/**
 * @author  Per Nyfelt
 */
public class CollectionStorageHelper {

    private Logger logger = Logger.getLogger(CollectionStorageHelper.class);

    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);
        logger.debug("CollectionStorageHelper.createCollection() - created XMLCollectionImpl as " + collectionName);
        root.setName(collectionName);
    } 
    
    public void deleteCollection(String collectionName) {
        try {
            connect();
            XMLCollection collection = (XMLCollection)db.objectForName(collectionName);
            logger.debug("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();
                logger.debug("CollectionStorageHelper.deleteCollection() - removed " + id);
            }
            db.deleteObject(collection);
            logger.debug("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);
            logger.debug("CollectionStorageHelper.connect() - connected");
            db.reloadClasses();            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -