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

SynchroFastObjectDB.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  *  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.ObjectSet;
00035 import org.odbms.Query;
00036 import org.openmobileis.common.util.exception.BadDataFormatException;
00037 import org.openmobileis.common.util.exception.DatabaseException;
00038 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
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.FODBIntIndex;
00044 import org.openmobileis.database.fastobjectdb.db.index.FODBLongIndex;
00045 import org.openmobileis.database.fastobjectdb.db.index.FODBStringIndex;
00046 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00047 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00048 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00049 import org.openmobileis.synchro.client.SynchroManager;
00050 import org.openmobileis.synchro.client.SynchroProcessor;
00051 import org.openmobileis.synchro.journal.JournalManager;
00052 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00053 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00054 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00055 import org.openmobileis.synchro.openmsp.client.db.ActionDB;
00056 import org.openmobileis.synchro.openmsp.client.db.SyncActionDBManager;
00057 
00066 public class SynchroFastObjectDB extends FastObjectDB {
00067   private static SynchroFODBConnector syncConnector;
00077   private SynchroFastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action)
00078     throws BadDataFormatException, FODBException {
00079     super(dbRootDir, dbName, fmf, action);
00080   }
00081         
00094         public static FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00095                 String dirPath = "" + dbRootDir + "/" + dbName;
00096                 String completeFilePath =  dirPath + "/" + dbName + ".db";
00097                 File file = new File(completeFilePath);
00098                 FastObjectDB db = null;
00099         SynchroFastObjectDB.initDefaultOpenMSPSynchro();
00100                 if (file.exists())      {
00101                         FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00102                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00103                 } else  {
00104                         //create directory path if not present.
00105                         File dirFile = new File(dirPath);
00106                         if (!dirFile.exists())  {
00107                                 dirFile.mkdirs();
00108                         }
00109                         FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00110                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00111                 }
00112                 return db;
00113         }
00114   
00115   public static void registerSynchroFODBConnector(SynchroFODBConnector connector)  {
00116     SynchroFastObjectDB.syncConnector = connector;
00117   }
00118         
00132         public boolean createCollection(FODBCollectionDescriptor descriptor) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00133                 boolean ret =  super.createCollection(descriptor);
00134                 if (ret)        {
00135                         if (descriptor.isSynchronize()) {
00136                           syncConnector.registerCollection(descriptor.getCollectionName());
00137                         }
00138                 }
00139                 return ret;
00140         }
00141         
00154         public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00155                 return this.createCollection(new FODBCollectionDescriptor(collectionName, objectType));
00156         }
00157         
00170         protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00171                 boolean ret =  super.openCollection(collectionName);
00172                 if (ret)        {
00173                         FODBCollection collection = this.getCollection(collectionName);
00174                         if (collection.isSynchronized())        {
00175               //don't register collection if synchro listener already exist.
00176               // keep created listener 
00177               //this method is call after each synchro to reload complete synchro files.
00178               OpenMSPSyncListener listener = OpenMSPSynchroManager.getManager().getListenerByName(collectionName);
00179               if (listener == null) {
00180                 syncConnector.registerCollection(collectionName);
00181               }
00182                         }
00183                 }
00184                 return ret;
00185         }
00186         
00193         public void registerSynchroFODBReturnListener(String collectionName, SynchroFODBReturnListener listener)        {
00194           syncConnector.registerSynchroFODBReturnListener(collectionName, listener);
00195         }
00196   
00204  public void registerJournalLogRenderer(String collectionName, String journalinfo) {
00205     JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(collectionName, journalinfo));
00206   }
00207 
00216         public boolean isObjectInCollection(String colName, Object obj) throws DatabaseException        {
00217                 FODBCollection collect = colManager.getCollectionByName(colName);
00218                 if (collect != null)    {
00219                         try     {
00220                                 this.getTransactionManager().begin();
00221                                 try     {
00222                                         this.getTransactionManager().enterTransaction(collect.getName());
00223                                         FODBUniqueIndex index = collect.getCollectionIdIndex();
00224                                         Object key  = ((FODBIndex)index).getKey(obj);
00225                                         long pos = index.getKeyPosition(key);
00226                                         return (pos != Node.NO_NODE);
00227                                 } finally       {
00228                                         this.getTransactionManager().commit();
00229                                 }
00230                         } catch (Throwable ex)  {
00231                                 throw new DatabaseException(ex);
00232                         }                       
00233                 }
00234                 return false;
00235         }
00236         
00246         public Object getObjectFromCollectionWithId(String colName, String id) throws DatabaseException {
00247                 FODBCollection collect = colManager.getCollectionByName(colName);
00248                 Object objId = id;
00249         if (collect != null)    {
00250                         try     {
00251                                 FODBUniqueIndex index = collect.getCollectionIdIndex();
00252                 if (index instanceof FODBLongIndex) {
00253                   objId = Long.getLong(id);
00254                 } else if (index instanceof FODBIntIndex) {
00255                   objId = Integer.getInteger(id);
00256                 }
00257                                 Query q = this.query();
00258                                 q.constrain(collect.getCollectionObjectClass());
00259                                 Query subq = q.descend(((FODBIndex)index).getIndexHeader().getMemberName());
00260                                 subq.constrain(objId).equal();
00261                                 ObjectSet set = q.execute(); 
00262                                 if (set.hasNext())      {
00263                                         return set.next();
00264                                 } else  {
00265                                         return null;
00266                                 }
00267                         } catch (Throwable ex)  {
00268                                 throw new DatabaseException(ex);
00269                         }                       
00270                 }
00271                 return null;
00272         }
00273         
00287         public boolean addFODB(String colName, Object obj) throws FODBException {
00288                 return super.add(colName, obj);
00289         }
00290         
00299         public boolean add(String colName, Object obj) throws FODBException {
00300                 boolean ret = false;
00301                 this.getTransactionManager().begin();
00302                 try     {
00303                         ret = super.add(colName, obj);
00304                         if (ret)        {
00305                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00306                                 if (col.isSynchronized())       {
00307                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00308                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00309                     if ((index != null) && (index instanceof FODBStringIndex)){
00310                         String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00311                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.ADD_ACTION));
00312                     } else if ((index != null) && (index instanceof FODBLongIndex)){
00313                       Long key = (Long)((FODBLongIndex)index).getKey(obj);
00314                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.ADD_ACTION));
00315                     } else if ((index != null) && (index instanceof FODBIntIndex)){
00316                       Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00317                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.ADD_ACTION));
00318                                         } else  {
00319                                                 col.deleteSingleElement(obj);
00320                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00321                                         }
00322                                 }
00323                         }
00324                 } finally       {
00325                         this.getTransactionManager().commit();
00326                 }
00327                 return ret;
00328         }
00329         
00340         public boolean replaceFODB(String colName, Object obj) throws FODBException {
00341                 return super.replace(colName, obj);
00342         }
00343         
00352         public boolean replace(String colName, Object obj) throws FODBException {
00353                 boolean ret = false;
00354                 this.getTransactionManager().begin();
00355                 try     {
00356                         ret = super.replace(colName, obj);
00357                         if (ret)        {
00358                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00359                                 if (col.isSynchronized())       {
00360                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00361                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00362                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00363                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00364                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.UPDATE_ACTION));
00365                      } else if ((index != null) && (index instanceof FODBLongIndex)){
00366                         Long key = (Long)((FODBLongIndex)index).getKey(obj);
00367                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.UPDATE_ACTION));
00368                       } else if ((index != null) && (index instanceof FODBIntIndex)){
00369                         Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00370                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.UPDATE_ACTION));
00371                                         } else  {
00372                                                 col.deleteSingleElement(obj);
00373                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00374                                         }
00375                                 }
00376                         }
00377                 } finally       {
00378                         this.getTransactionManager().commit();
00379                 }
00380                 return ret;
00381         }
00382         
00392         public boolean delete(String colName, Object obj) throws FODBException {
00393                 boolean ret = false;
00394                 this.getTransactionManager().begin();
00395                 try     {
00396                         ret = super.delete(colName, obj);
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                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00404                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.DELETE_ACTION));
00405                     } else if ((index != null) && (index instanceof FODBLongIndex)){
00406                       Long key = (Long)((FODBLongIndex)index).getKey(obj);
00407                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.DELETE_ACTION));
00408                     } else if ((index != null) && (index instanceof FODBIntIndex)){
00409                       Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00410                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.DELETE_ACTION));
00411                                         } else  {
00412                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00413                                         }
00414                                 }
00415                         }
00416                 } finally       {
00417                         this.getTransactionManager().commit();
00418                 }
00419                 return ret;
00420         }
00421         
00432         public boolean deleteWithIdFODB(String colName, Object id) throws FODBException {
00433                 return super.deleteWithId(colName, id);
00434         }
00444         public boolean deleteWithId(String colName, Object id) throws FODBException {
00445                 boolean ret = false;
00446                 this.getTransactionManager().begin();
00447                 try     {
00448                         ret = super.deleteWithId(colName, id);
00449                         if (ret)        {
00450                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00451                                 if (col.isSynchronized())       {
00452                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00453                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00454                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00455                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, (String)id, ActionDB.DELETE_ACTION));
00456                                         } else  {
00457                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00458                                         }
00459                                 }
00460                         }
00461                 } finally       {
00462                         this.getTransactionManager().commit();
00463                 }
00464                 return ret;
00465         }
00466   
00467   private static void initDefaultOpenMSPSynchro() {
00468     SynchroProcessor openMSPproc = SynchroManager.getManager().getSynchroProcessor(OpenMSPSynchroManager.OPENMSPPROCESSORNAME);
00469     if (openMSPproc == null)  {
00470       SynchroManager.getManager().registerSynchroProcessor(OpenMSPSynchroManager.getManager());
00471     }
00472     if (syncConnector == null)  {
00473       syncConnector = new DefaultOpenMSPSynchroFODBConnector();
00474     }
00475 
00476   }
00477 
00478 }

Generated on Mon Jul 10 10:29:32 2006 for OpenMobileIS by  doxygen 1.4.4