com.flexoodb.FlexManager.java Source code

Java tutorial

Introduction

Here is the source code for com.flexoodb.FlexManager.java

Source

/*
 * FlexManager.java
 *
 * Created on Mar 17, 2009 10:28:03 AM
 *
 * Copyright (C) 2009 Jayson Yu
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package com.flexoodb;

import com.flexoodb.engines.FlexDataInterface;
import com.flexoodb.common.FlexUtils;
import com.flexoodb.pool.RecordSet;
import java.io.ByteArrayInputStream;
import java.util.Collection;
import org.apache.commons.configuration.XMLConfiguration;

/**
 The FlexOODB API is designed to provide java persistence without the tight coupling and/or manual mapping required by mainstream persistence APIs.
 Using the RBMS of your choice, java objects are saved in XML which also allows the embedding of Serializable objects as well as byte arrays (e.g.
 if you need to store binary files in XML).
 <br/><br/>
 FlexOODB requires only that you have a RBDMS up and running and that your persisted Classes have a <code>getId()</code> -- which returns a String -- and <code>setId(String id)</code> methods
 used by FlexOODB to register a unique ID. Your Class' methods must also have one or more '<code>getXXX()</code>' and '<code>setXXX(Object obj)</code>' methods where <code>XXX</code> is
 any label describing the data being handled. Object '<code>obj</code>' must be a non-primitive Java type (e.g. <code>String, Date,Long, Boolean</code> etc), <code>Serializable</code> (in case you want
 to embed your own objects) or <code>byte[]</code> (for binary content) for FlexOODB to consider saving.
<br/><br/>
<b>Step 1:Setup Your Database Connection Configuration</b><br/><br/>
 FlexOODB requires a running database to save the XML objects to, please make sure one is running and create a database (say 'flexdb') for your FlexOODB
 database. <u><b>Extract</b></u> the file <i>flexoodb.xml</i> and <b><u>edit it</b></u> to reflect your database' configuration. Note that the username you specify in
 the config file must also have table creation and deletion rights within the designated database as the API automatically creates tables for your classes.
 Additional documentation can be found in the <i>flexoodb.xml</i> file.
<br/><br/>
    
  <b>Step 2:Use the API!</b><br/>(Note:all example classes below -- e.g. <i>Employee, Manager</i> etc.-- can be found in <i>com/flexoodb/demo</i>):
<br/><br/>
  To Instantiate the FlexOODB Manager:<br/>
 <code><br/>
  FlexManager fm = new FlexManager("c:/flexoodb.xml");<br/>
</code>
<br/>
  To Add/Persist an Object:<br/>
 <code><br/>
  Employee e = new Employee();<br/>
  e.setFirsName("Joe");<br/>
  e.setLastName("Programmer");<br/>
  e.setEmploymentDate(new Date());<br/>
  fm.persist(e); <b> <-- <i>API will create the table and index table if the 'employee' table is not found.</i></b><br/>
 </code>
<br/>
  To Find:<br/>
<br/>
    
  <code>
Employee e2 = (Employee)fm.find(e.getId(),Employee.class);<br/>
    
 </code>
<br/>
  To Query:<br/>
 <code>
<br/>
   Collection<Object> v= fm.runQuery("select from employee where departmentid = 'someid'", Employee.class);<br/>
   Iterator i = v.iterator();<br/>
   while (i.hasNext())<br/>
   {<br/>
       Employee e = (Employee)i.next();<br/>
       System.out.println(e.getId()+")"+e.getFirstName()+" "+e.getLastName()+" "+e.getEmploymentDate());<br/>
   }<br/>
 </code>
<br/>
  To Embed a Serializable Object and Save:<br/>
 <code><br/>
   byte[] i = FlexUtils.getFileInBytes("c:/whateverfile.doc");<br/>
   DocumentContainer fc = new DocumentContainer();<br/>
   fc.setFileName("whateverfile.doc");<br/>
   fc.setDescription("My Image");<br/>
   fc.setBytes(i);<br/>
<br/>
   Manager m = new Manager();<br/>
   m.setFirstName("Peter");<br/>
   m.setLastName("Manager);<br/>
   m.setReportingRequirement(fc);<br/>
<br/>
   fm.persist(m);
<br/> </code><br/>
  Please see <i>com/flexoodb/demo/Demo.java</i> for more examples.
    
 @author      Jayson Yu
 @version     %I%, %G%
 @since       1.0
 */
public class FlexManager implements FlexDataInterface {

    private FlexDataInterface _flexdb = null;

    public FlexManager() throws Exception {
        this(null);
    }

    public FlexManager(String conf) throws Exception {

        XMLConfiguration config = new XMLConfiguration();
        config.setDelimiterParsingDisabled(true);
        String dbimplementation = null;
        if (conf == null) {

            byte[] b = FlexUtils
                    .getBytesFromInputStream(this.getClass().getClassLoader().getResourceAsStream("flexoodb.xml"));
            if (b != null) {
                config.load(new ByteArrayInputStream(b));
                dbimplementation = config.getString("flexoodb[@engine]");
            }

        } else {
            config.load(conf);
            dbimplementation = config.getString("flexoodb[@engine]");
        }

        // use the default impl
        if (dbimplementation == null) {
            dbimplementation = "com.flexoodb.engines.FlexJAXBDBDataEngine";
        }

        //_flexdb = (FlexDataInterface) Class.forName(dbimplementation).newInstance();
        _flexdb = (FlexDataInterface) ClassLoader.getSystemClassLoader().loadClass((dbimplementation))
                .newInstance();

        _flexdb.initialize(config);

    }

    public void initialize(Object config) throws Exception {
        _flexdb.initialize(config);
    }

    public void stop() throws Exception {
        _flexdb.stop();
    }

    public Object find(String id, Class c) throws Exception {
        return _flexdb.find(id, c);
    }

    public Object find(String id, Class c, boolean revivchildren) throws Exception {
        return _flexdb.find(id, c, revivchildren);
    }

    public Object find(String id, Class c, String targettable) throws Exception {
        return _flexdb.find(id, c, targettable);
    }

    public Object find(String id, Class c, String targettable, boolean revivechildren) throws Exception {
        return _flexdb.find(id, c, targettable, revivechildren);
    }

    public Collection<Object> runQuery(String query, Class c, boolean usedefaultimplementation) throws Exception {
        //System.out.println(">>>"+usedefaultimplementation);
        return _flexdb.runQuery(query, c, usedefaultimplementation);
    }

    public Collection<Object> runQuery(String query, Class c) throws Exception {
        return _flexdb.runQuery(query, c);
    }

    public Object persist(Object obj) throws Exception {
        return _flexdb.persist(obj);
    }

    public Object persist(Object obj, String targettable) throws Exception {
        return _flexdb.persist(obj, targettable);
    }

    public Object remove(Object obj) throws Exception {
        return _flexdb.remove(obj);
    }

    public Object remove(Object obj, String targettable) throws Exception {
        return _flexdb.remove(obj, targettable);
    }

    public Object refresh(Object obj) throws Exception {
        return _flexdb.refresh(obj);
    }

    public Object refresh(Object obj, String targettable) throws Exception {
        return _flexdb.refresh(obj, targettable);
    }

    public Object merge(Object obj) throws Exception {
        return _flexdb.merge(obj);
    }

    public Object merge(Object obj, String targettable) throws Exception {
        return _flexdb.merge(obj, targettable);
    }

    @Override
    public boolean drop(Object obj) throws Exception {
        return _flexdb.drop(obj);
    }

    @Override
    public boolean reindex(Object obj) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public RecordSet rawQuery(String query) throws Exception {
        return _flexdb.rawQuery(query);
    }

    @Override
    public int rawUpdate(String update) throws Exception {
        return _flexdb.rawUpdate(update);
    }

}