collectionstoragehelper.java

来自「Java的面向对象数据库系统的源代码」· Java 代码 · 共 84 行

JAVA
84
字号
/* 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 + =
减小字号Ctrl + -
显示快捷键?