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.FODBStringIndexDescriptor;
00040 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00041 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00042 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00043 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00044 import org.openmobileis.database.fastobjectdb.db.index.FODBIntIndex;
00045 import org.openmobileis.database.fastobjectdb.db.index.FODBLongIndex;
00046 import org.openmobileis.database.fastobjectdb.db.index.FODBStringIndex;
00047 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00048 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00049 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00050 import org.openmobileis.synchro.client.SynchroManager;
00051 import org.openmobileis.synchro.client.SynchroProcessor;
00052 import org.openmobileis.synchro.journal.JournalManager;
00053 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00054 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00055 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00056 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00057 import org.openmobileis.synchro.openmsp.client.db.ActionDB;
00058 import org.openmobileis.synchro.openmsp.client.db.SyncActionDBManager;
00059 
00068 public class SynchroFastObjectDB extends FastObjectDB {
00069   private static SynchroFODBConnector syncConnector;
00079   private SynchroFastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action)
00080     throws BadDataFormatException, FODBException {
00081     super(dbRootDir, dbName, fmf, action);
00082   }
00083         
00096         public static FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00097                 String dirPath = "" + dbRootDir + "/" + dbName;
00098                 String completeFilePath =  dirPath + "/" + dbName + ".db";
00099                 File file = new File(completeFilePath);
00100                 FastObjectDB db = null;
00101         SynchroFastObjectDB.initDefaultOpenMSPSynchro();
00102                 if (file.exists())      {
00103                         FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00104                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00105                 } else  {
00106                         //create directory path if not present.
00107                         File dirFile = new File(dirPath);
00108                         if (!dirFile.exists())  {
00109                                 dirFile.mkdirs();
00110                         }
00111                         FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00112                         db =  new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00113                 }
00114                 return db;
00115         }
00116   
00117   public static void registerSynchroFODBConnector(SynchroFODBConnector connector)  {
00118     SynchroFastObjectDB.syncConnector = connector;
00119   }
00120         
00134         public boolean createCollection(FODBCollectionDescriptor descriptor) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00135                 boolean ret =  super.createCollection(descriptor);
00136                 if (ret)        {
00137                         if (descriptor.isSynchronize()) {
00138                                 // remove all sync number.
00139                                 NumSyncManagerDB.getManager().deleteSyncNumberForService(descriptor.getCollectionName());
00140                           syncConnector.registerCollection(descriptor.getCollectionName());
00141                         }
00142                 }
00143                 return ret;
00144         }
00145         
00158         public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00159                 return this.createCollection(new FODBCollectionDescriptor(collectionName, objectType));
00160         }
00161         
00174         protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00175                 boolean ret =  super.openCollection(collectionName);
00176                 if (ret)        {
00177                         FODBCollection collection = this.getCollection(collectionName);
00178                         if (collection.isSynchronized())        {
00179               //don't register collection if synchro listener already exist.
00180               // keep created listener 
00181               //this method is call after each synchro to reload complete synchro files.
00182               OpenMSPSyncListener listener = OpenMSPSynchroManager.getManager().getListenerByName(collectionName);
00183               if (listener == null) {
00184                 syncConnector.registerCollection(collectionName);
00185               }
00186                         }
00187                 }
00188                 return ret;
00189         }
00190         
00197         public void registerSynchroFODBReturnListener(String collectionName, SynchroFODBReturnListener listener)        {
00198           syncConnector.registerSynchroFODBReturnListener(collectionName, listener);
00199         }
00200   
00208  public void registerJournalLogRenderer(String collectionName, String journalinfo) {
00209     JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(collectionName, journalinfo));
00210   }
00211 
00220         public boolean isObjectInCollection(String colName, Object obj) throws DatabaseException        {
00221                 FODBCollection collect = colManager.getCollectionByName(colName);
00222                 if (collect != null)    {
00223                         try     {
00224                                 this.getTransactionManager().begin();
00225                                 try     {
00226                                         this.getTransactionManager().enterTransaction(collect.getName());
00227                                         FODBUniqueIndex index = collect.getCollectionIdIndex();
00228                                         Object key  = ((FODBIndex)index).getKey(obj);
00229                                         long pos = index.getKeyPosition(key);
00230                                         return (pos != Node.NO_NODE);
00231                                 } finally       {
00232                                         this.getTransactionManager().commit();
00233                                 }
00234                         } catch (Throwable ex)  {
00235                                 throw new DatabaseException(ex);
00236                         }                       
00237                 }
00238                 return false;
00239         }
00240         
00250         public Object getObjectFromCollectionWithId(String colName, String id) throws DatabaseException {
00251                 FODBCollection collect = colManager.getCollectionByName(colName);
00252                 Object objId = id;
00253         if (collect != null)    {
00254                         try     {
00255                                 FODBUniqueIndex index = collect.getCollectionIdIndex();
00256                 if (index instanceof FODBLongIndex) {
00257                   objId = Long.getLong(id);
00258                 } else if (index instanceof FODBIntIndex) {
00259                   objId = Integer.getInteger(id);
00260                 }
00261                                 Query q = this.query();
00262                                 q.constrain(collect.getCollectionObjectClass());
00263                                 Query subq = q.descend(((FODBIndex)index).getIndexHeader().getMemberName());
00264                                 subq.constrain(objId).equal();
00265                                 ObjectSet set = q.execute(); 
00266                                 if (set.hasNext())      {
00267                                         return set.next();
00268                                 } else  {
00269                                         return null;
00270                                 }
00271                         } catch (Throwable ex)  {
00272                                 throw new DatabaseException(ex);
00273                         }                       
00274                 }
00275                 return null;
00276         }
00277         
00291         public boolean addFODB(String colName, Object obj) throws FODBException {
00292                 return super.add(colName, obj);
00293         }
00294         
00303         public boolean add(String colName, Object obj) throws FODBException {
00304                 boolean ret = false;
00305                 this.getTransactionManager().begin();
00306                 try     {
00307                         ret = super.add(colName, obj);
00308                         if (ret)        {
00309                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00310                                 if (col.isSynchronized())       {
00311                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00312                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00313                     if ((index != null) && (index instanceof FODBStringIndex)){
00314                       FODBStringIndex strindex = (FODBStringIndex)index;
00315                         String key = (String)strindex.getKeySensitive(obj, ((FODBStringIndexDescriptor)strindex.getIndexDescriptor()).isCaseSensitive());
00316                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.ADD_ACTION));
00317                     } else if ((index != null) && (index instanceof FODBLongIndex)){
00318                       Long key = (Long)((FODBLongIndex)index).getKey(obj);
00319                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.ADD_ACTION));
00320                     } else if ((index != null) && (index instanceof FODBIntIndex)){
00321                       Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00322                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.ADD_ACTION));
00323                                         } else  {
00324                                                 col.deleteSingleElement(obj);
00325                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00326                                         }
00327                                 }
00328                         }
00329                 } finally       {
00330                         this.getTransactionManager().commit();
00331                 }
00332                 return ret;
00333         }
00334         
00345         public boolean replaceFODB(String colName, Object obj) throws FODBException {
00346                 return super.replace(colName, obj);
00347         }
00348         
00357         public boolean replace(String colName, Object obj) throws FODBException {
00358                 boolean ret = false;
00359                 this.getTransactionManager().begin();
00360                 try     {
00361                         ret = super.replace(colName, obj);
00362                         if (ret)        {
00363                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00364                                 if (col.isSynchronized())       {
00365                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00366                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00367                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00368                          FODBStringIndex strindex = (FODBStringIndex)index;
00369                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, ((FODBStringIndexDescriptor)strindex.getIndexDescriptor()).isCaseSensitive());
00370                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.UPDATE_ACTION));
00371                      } else if ((index != null) && (index instanceof FODBLongIndex)){
00372                         Long key = (Long)((FODBLongIndex)index).getKey(obj);
00373                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.UPDATE_ACTION));
00374                       } else if ((index != null) && (index instanceof FODBIntIndex)){
00375                         Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00376                         SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.UPDATE_ACTION));
00377                                         } else  {
00378                                                 col.deleteSingleElement(obj);
00379                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00380                                         }
00381                                 }
00382                         }
00383                 } finally       {
00384                         this.getTransactionManager().commit();
00385                 }
00386                 return ret;
00387         }
00388         
00398         public boolean delete(String colName, Object obj) throws FODBException {
00399                 boolean ret = false;
00400                 this.getTransactionManager().begin();
00401                 try     {
00402                         ret = super.delete(colName, obj);
00403                         if (ret)        {
00404                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00405                                 if (col.isSynchronized())       {
00406                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00407                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00408                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00409                                                 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00410                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.DELETE_ACTION));
00411                     } else if ((index != null) && (index instanceof FODBLongIndex)){
00412                       Long key = (Long)((FODBLongIndex)index).getKey(obj);
00413                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.DELETE_ACTION));
00414                     } else if ((index != null) && (index instanceof FODBIntIndex)){
00415                       Integer key = (Integer)((FODBIntIndex)index).getKey(obj);
00416                       SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key.toString(), ActionDB.DELETE_ACTION));
00417                                         } else  {
00418                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00419                                         }
00420                                 }
00421                         }
00422                 } finally       {
00423                         this.getTransactionManager().commit();
00424                 }
00425                 return ret;
00426         }
00427         
00438         public boolean deleteWithIdFODB(String colName, Object id) throws FODBException {
00439                 return super.deleteWithId(colName, id);
00440         }
00450         public boolean deleteWithId(String colName, Object id) throws FODBException {
00451                 boolean ret = false;
00452                 this.getTransactionManager().begin();
00453                 try     {
00454                         ret = super.deleteWithId(colName, id);
00455                         if (ret)        {
00456                                 FODBCollection col = this.colManager.getCollectionByName(colName);
00457                                 if (col.isSynchronized())       {
00458                                         this.getTransactionManager().enterTransaction("FWKACTIONDB");
00459                                         FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00460                                         if ((index != null)     && (index instanceof FODBStringIndex)){
00461                                                 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, (String)id, ActionDB.DELETE_ACTION));
00462                                         } else  {
00463                                                 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00464                                         }
00465                                 }
00466                         }
00467                 } finally       {
00468                         this.getTransactionManager().commit();
00469                 }
00470                 return ret;
00471         }
00472   
00473   private static void initDefaultOpenMSPSynchro() {
00474     SynchroProcessor openMSPproc = SynchroManager.getManager().getSynchroProcessor(OpenMSPSynchroManager.OPENMSPPROCESSORNAME);
00475     if (openMSPproc == null)  {
00476       SynchroManager.getManager().registerSynchroProcessor(OpenMSPSynchroManager.getManager());
00477     }
00478     if (syncConnector == null)  {
00479       syncConnector = new DefaultOpenMSPSynchroFODBConnector();
00480     }
00481 
00482   }
00483 
00484 }

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