FODBBaseDataFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 package org.openmobileis.examples.simpleappli.data.FODB;
00026 
00027 import org.odbms.ObjectSet;
00028 import org.odbms.Query;
00029 import org.openmobileis.common.context.ApplicationContextManager;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 import org.openmobileis.common.util.log.LogManager;
00033 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00034 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00035 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00037 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00038 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00039 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDB;
00040 import org.openmobileis.examples.simpleappli.data.BaseData;
00041 
00051 public final class FODBBaseDataFactory {
00052   private static FODBBaseDataFactory manager;
00053 
00057   private FODBBaseDataFactory() {
00058     super();
00059     this.initDB();
00060   }
00061 
00062   public static FODBBaseDataFactory getManager() {
00063     if (manager == null) {
00064       synchronized (FODBBaseDataFactory.class) {
00065         if (manager == null) {
00066           manager = new FODBBaseDataFactory();
00067           //register the manager to avoid class garbage.
00068           ApplicationContextManager.getManager().addManager(manager);
00069         }
00070       }
00071     }
00072     return manager;
00073   }
00074 
00075   private void initDB() {
00076     try {
00077       FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00078       // test if the collection has already been created.
00079       //if not create it. Created collection are automatiquely 
00080       // loaded when FODB is initialized.
00081       if (!db.isCollectionExist("basedata")) {
00082         //SimpleOpenMISInit intialize the synchronized version of FODB.
00083         // all created collection are synchronized by default.
00084         // Synchro listener are automatiquely registered when the collection is created or loaded.
00085         // use the FODBCollectionDescriptor to define a collection that is not synchronized.
00086         FODBCollectionDescriptor coldes = new FODBCollectionDescriptor("basedata", BaseData.class);
00087         db.createCollection(coldes);
00088         //add index to query the collection
00089         FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getId()", 12, 26);
00090         db.addIndex("basedata", IDDescriptor);
00091       }
00092       //register the log renderer.
00093       //logrenderer are use to show synchronization result for this collection.
00094       ((SynchroFastObjectDB) db).registerJournalLogRenderer("basedata", "Base data synchronization :");
00095 
00096     } catch (Throwable ex) {
00097       LogManager.traceError(0, "BaseData Factory FODB ERROR : Unknown error during creation");
00098       LogManager.traceError(0, ex);
00099     }
00100   }
00101 
00102   public BaseData[] getAllBaseData() throws DatabaseException {
00103     Query q = FastObjectDBManager.getCurrentFODB().query();
00104     q.constrain(BaseData.class);
00105     q.descend("getId()");
00106     ObjectSet set = q.execute();
00107     Array ret = ((FODBSodaObjectSet)set).getArrayFromSet();
00108     BaseData[] datalist = new BaseData[ret.size()];
00109     ret.toArray(datalist);
00110     return datalist;
00111   }
00112 
00113   public BaseData getBaseData(String id) throws DatabaseException {
00114     Query q = FastObjectDBManager.getCurrentFODB().query();
00115     q.constrain(BaseData.class);
00116     Query subq = q.descend("getId()");
00117     subq.constrain(id).equal();
00118     ObjectSet set = q.execute();
00119     if (set.hasNext()) {
00120       return (BaseData) set.next();
00121     } else {
00122       return null;
00123     }
00124   }
00125 
00129   public void storeBaseData(BaseData data) throws DatabaseException {
00130     BaseData dbdata = this.getBaseData(data.getId());
00131     if (dbdata == null) {
00132       FastObjectDBManager.getCurrentFODB().add("basedata", data);
00133     } else {
00134       FastObjectDBManager.getCurrentFODB().replace("basedata", data);
00135     }
00136   }
00137   
00138   // delete a data
00139   public void deleteBaseData (String id) throws DatabaseException {
00140     try {     
00141       FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00142       db.deleteWithId("basedata", id); // can only be call if the first collection index is an UNIQUE index.
00143     } catch (Throwable ex)  {
00144       throw new DatabaseException(ex);
00145     }
00146   }
00147 }

Generated on Mon Dec 4 11:03:26 2006 for OpenMobileIS by  doxygen 1.5.1-p1