Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

SynchroFastObjectDB.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.synchro.client;
00030 
00031 import java.io.File;
00032 import java.io.IOException;
00033 
00034 import org.odbms.Constraint;
00035 import org.odbms.ObjectSet;
00036 import org.odbms.Query;
00037 import org.openmobileis.common.util.exception.BadDataFormatException;
00038 import org.openmobileis.database.DatabaseException;
00039 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00040 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00041 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00042 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00043 import org.openmobileis.database.fastobjectdb.db.index.FODBStringIndex;
00044 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00045 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00046 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00047 
00056 public class SynchroFastObjectDB extends FastObjectDB {
00057   private SynchroFODBConnector syncConnector;
00067   private SynchroFastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action)
00068     throws BadDataFormatException, FODBException {
00069     super(dbRootDir, dbName, fmf, action);
00070   }
00071         
00084         static public FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00085                 String dirPath = "" + dbRootDir + "/" + dbName;
00086                 String completeFilePath =  dirPath + "/" + dbName + ".db";
00087                 File file = new File(completeFilePath);
00088                 FastObjectDB db = null;
00089                 if (file.exists())      {
00090                         FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00091                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00092                 } else  {
00093                         //create directory path if not present.
00094                         File dirFile = new File(dirPath);
00095                         if (!dirFile.exists())  {
00096                                 dirFile.mkdirs();
00097                         }
00098                         FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00099                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00100                 }
00101                 return db;
00102         }
00103         
00117         public boolean createCollection(String collectionName, Class objectType, boolean synchro) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00118                 boolean ret =  super.createCollection(collectionName, objectType, synchro);
00119                 if (ret)        {
00120                         FODBCollection collection = this.getCollection(collectionName);
00121                         collection.setSynchronized(synchro);
00122                         if (synchro)    {
00123                           syncConnector.registerCollection(collectionName);
00124                         }
00125                 }
00126                 return ret;
00127         }
00128         
00141         public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00142                 return this.createCollection(collectionName, objectType, true);
00143         }
00144         
00157         protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00158                 boolean ret =  super.openCollection(collectionName);
00159                 if (ret)        {
00160                         FODBCollection collection = this.getCollection(collectionName);
00161                         if (collection.isSynchronized())        {
00162                           syncConnector.registerCollection(collectionName);
00163                         }
00164                 }
00165                 return ret;
00166         }
00167         
00174         public void registerSynchroFODBReturnListener(String collectionName, SynchroFODBReturnListener listener)        {
00175           syncConnector.registerSynchroFODBReturnListener(collectionName, listener);
00176         }
00177         
00186         public boolean isObjectInCollection(String colName, Object obj) throws DatabaseException        {
00187                 FODBCollection collect = colManager.getCollectionByName(colName);
00188                 Object ret = null;
00189                 if (collect != null)    {
00190                         try     {
00191                                 this.getTransactionManager().begin();
00192                                 try     {
00193                                         this.getTransactionManager().enterTransaction(collect.getName());
00194                                         FODBUniqueIndex index = collect.getCollectionIdIndex();
00195                                         Object key  = ((FODBIndex)index).getKey(obj);
00196                                         long pos = index.getKeyPosition(key);
00197                                         return (pos != Node.NO_NODE);
00198                                 } finally       {
00199                                         this.getTransactionManager().commit();
00200                                 }
00201                         } catch (Throwable ex)  {
00202                                 throw new DatabaseException(ex);
00203                         }                       
00204                 }
00205                 return false;
00206         }
00207         
00217         public Object getObjectFromCollectionWithId(String colName, String id) throws DatabaseException {
00218                 FODBCollection collect = colManager.getCollectionByName(colName);
00219                 Object ret = null;
00220                 if (collect != null)    {
00221                         try     {
00222                                 FODBUniqueIndex index = collect.getCollectionIdIndex();
00223                                 Query q = this.query();
00224                                 q.constrain(collect.getCollectionObjectClass());
00225                                 Query subq = q.descend(((FODBIndex)index).getIndexHeader().getMemberName());
00226                                 Constraint c = subq.constrain(id).equal();
00227                                 ObjectSet set = q.execute(); 
00228                                 if (set.hasNext())      {
00229                                         return set.next();
00230                                 } else  {
00231                                         return null;
00232                                 }
00233                         } catch (Throwable ex)  {
00234                                 throw new DatabaseException(ex);
00235                         }                       
00236                 }
00237                 return null;
00238         }
00239         
00253         public boolean addFODB(String colName, Object obj) throws FODBException {
00254                 return super.add(colName, obj);
00255         }
00256         
00265         public boolean add(String colName, Object obj) throws FODBException {
00266                 boolean ret = false;
00267                 this.getTransactionManager().begin();
00268                 try     {
00269                         ret = super.add(colName, obj);
00270                         if (ret)        {
00271                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00272                                 if (col.isSynchronized())       {
00273                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00274                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00275                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00276                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00277                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.ADD_ACTION));
00278                                         } else  {
00279                                                 col.deleteSingleElement(obj);
00280                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00281                                         }
00282                                 }
00283                         }
00284                 } finally       {
00285                         this.getTransactionManager().commit();
00286                 }
00287                 return ret;
00288         }
00289         
00300         public boolean replaceFODB(String colName, Object obj) throws FODBException {
00301                 return super.replace(colName, obj);
00302         }
00303         
00312         public boolean replace(String colName, Object obj) throws FODBException {
00313                 boolean ret = false;
00314                 this.getTransactionManager().begin();
00315                 try     {
00316                         ret = super.replace(colName, obj);
00317                         if (ret)        {
00318                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00319                                 if (col.isSynchronized())       {
00320                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00321                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00322                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00323                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00324                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.UPDATE_ACTION));
00325                                         } else  {
00326                                                 col.deleteSingleElement(obj);
00327                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00328                                         }
00329                                 }
00330                         }
00331                 } finally       {
00332                         this.getTransactionManager().commit();
00333                 }
00334                 return ret;
00335         }
00336         
00346         public boolean delete(String colName, Object obj) throws FODBException {
00347                 boolean ret = false;
00348                 this.getTransactionManager().begin();
00349                 try     {
00350                         ret = super.delete(colName, obj);
00351                         if (ret)        {
00352                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00353                                 if (col.isSynchronized())       {
00354                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00355                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00356                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00357                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00358                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.DELETE_ACTION));
00359                                         } else  {
00360                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00361                                         }
00362                                 }
00363                         }
00364                 } finally       {
00365                         this.getTransactionManager().commit();
00366                 }
00367                 return ret;
00368         }
00369         
00380         public boolean deleteWithIdFODB(String colName, Object id) throws FODBException {
00381                 return super.deleteWithId(colName, id);
00382         }
00392         public boolean deleteWithId(String colName, Object id) throws FODBException {
00393                 boolean ret = false;
00394                 this.getTransactionManager().begin();
00395                 try     {
00396                         ret = super.deleteWithId(colName, id);
00397                         if (ret)        {
00398                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00399                                 if (col.isSynchronized())       {
00400                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00401                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00402                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00403                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, (String)id, ActionDB.DELETE_ACTION));
00404                                         } else  {
00405                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00406                                         }
00407                                 }
00408                         }
00409                 } finally       {
00410                         this.getTransactionManager().commit();
00411                 }
00412                 return ret;
00413         }
00414 
00415 }

Generated on Thu Oct 6 10:06:34 2005 for OpenMobileIS by  doxygen 1.4.3