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

FODBSyncActionDBQueryManager.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 
00030 package org.openmobileis.database.fastobjectdb.synchro.client;
00031 
00032 
00033 
00034 import org.odbms.ObjectSet;
00035 import org.odbms.Query;
00036 
00037 import org.openmobileis.common.util.collection.Array;
00038 import org.openmobileis.database.DatabaseException;
00039 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00040 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00041 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00042 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00043 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00044 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00045 import org.openmobileis.synchro.openmsp.client.db.ActionDB;
00046 import org.openmobileis.synchro.openmsp.client.db.SyncActionDBQueryManager;
00047 
00048 
00056 public class FODBSyncActionDBQueryManager implements SyncActionDBQueryManager {
00057 
00058   private static int SERVICE_NAME_MAX_LENGTH = 40;
00059   
00060   private static int uidkeylength;
00061 
00062 
00063 
00064 
00065 
00066   public FODBSyncActionDBQueryManager() {
00067     uidkeylength = 100;
00068   }
00069 
00070   public FODBSyncActionDBQueryManager(int uidlength) {
00071     uidkeylength = uidlength;
00072   }
00073 
00074 
00075  /*
00076 
00077  * check if the table exists. IF it does not exist, create it
00078 
00079  */
00080 
00081   public void initDB() throws DatabaseException {
00082 
00083     try {
00084                         FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00085                         if (!db.isCollectionExist("FWKACTIONDB"))       {
00086         FODBCollectionDescriptor descriptor = new FODBCollectionDescriptor("FWKACTIONDB", ActionDB.class);
00087         descriptor.setSynchronize(false);
00088                                 db.createCollection(descriptor);
00089         int keylength = SERVICE_NAME_MAX_LENGTH + uidkeylength;
00090                                 FODBStringIndexDescriptor KeyDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getActionID()", 10, keylength);
00091                                 db.addIndex("FWKACTIONDB", KeyDescriptor);
00092                                 FODBStringIndexDescriptor debKeyDescriptor = new FODBStringIndexDescriptor("SERV", FODBIndexDescriptor.MULTIPLE, "getActionServiceName()", 10, SERVICE_NAME_MAX_LENGTH, 10);
00093                                 db.addIndex("FWKACTIONDB", debKeyDescriptor);
00094                         }
00095     } catch (Throwable ex)  {
00096       throw new DatabaseException(ex);
00097     }
00098 
00099   }
00100 
00101   public Array getActionsForService(String serviceName) throws DatabaseException  {
00102     if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00103       throw new DatabaseException("SODA SyncActionDBQueryManager getActionsForService ServiceName to long or null abort "+serviceName);
00104     }
00105                 //query SELECT * FROM TESTSEARCH
00106                 Query q = FastObjectDBManager.getCurrentFODB().query();
00107                 q.constrain(ActionDB.class);
00108                 Query subq = q.descend("getActionServiceName()");
00109                 subq.constrain(serviceName).equal();
00110                 FODBSodaObjectSet set = (FODBSodaObjectSet)q.execute();  
00111                 return set.getArrayFromSet();  
00112   }
00113   
00114   public ActionDB getActionDB(String serviceName, String objectUID) throws DatabaseException  {
00115     if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00116       throw new DatabaseException("SODA SyncActionDBQueryManager getAction ServiceName to long or null abort "+serviceName);
00117     }
00118                 String actionID = ActionDB.getActionIdFromServiceAndUID(serviceName, objectUID);
00119                 Query q = FastObjectDBManager.getCurrentFODB().query();
00120                 q.constrain(ActionDB.class);
00121                 Query subq = q.descend("getActionID()");
00122                 subq.constrain(actionID).equal();
00123                 ObjectSet set = q.execute();  
00124                 if (set.hasNext())      {
00125                         return (ActionDB) set.next();
00126                 }
00127                 return null;  
00128   }
00129 
00130 
00131 
00132   // insert a row
00133 
00134   public void create (ActionDB action) throws DatabaseException  {
00135     if ((action.getActionServiceName()== null) || (action.getActionServiceName().length() > SERVICE_NAME_MAX_LENGTH)) {
00136       throw new DatabaseException("SODA SyncActionDBQueryManager create ServiceName to long or null abort "+action.getActionServiceName());
00137     }
00138     if ((action.getActionObjectUID()== null) || (action.getActionObjectUID().length() > uidkeylength)) {
00139       throw new DatabaseException("SODA SyncActionDBQueryManager create Action uid to long or null abort "+action.getActionObjectUID());
00140     }
00141       /* resolve action conflic. Rules :
00142         if new action add remove old action and add new one
00143         if new action delete and old update remove update add delete
00144         if new action delete and old add remove add do not add delete
00145         if new action update and old add keep old add
00146         if new action update and old update keep old.
00147         if new action update and old delete keep old.
00148         for update if oldaction do nothing
00149        
00150        if no old action add action.
00151        */
00152       
00153       ActionDB oldAction =  this.getActionDB(action.getActionServiceName(), action.getActionObjectUID());
00154       if (oldAction != null)  { //resolve conflic
00155         switch (action.getActionType()) {
00156           case ActionDB.ADD_ACTION :
00157             FastObjectDBManager.getCurrentFODB().replace("FWKACTIONDB", action);
00158             break;
00159           case ActionDB.DELETE_ACTION :
00160             if (oldAction.getActionType() == ActionDB.ADD_ACTION) {
00161                                                         FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", oldAction);
00162             } else  {
00163                                                         FastObjectDBManager.getCurrentFODB().replace("FWKACTIONDB", action);
00164             }
00165             break;
00166           case ActionDB.UPDATE_ACTION :
00167           default :
00168             break;
00169         }
00170       } else  {
00171                                 FastObjectDBManager.getCurrentFODB().add("FWKACTIONDB", action);
00172       }
00173   }
00174   
00175   public void deleteAllActionForService(String serviceName)  throws DatabaseException  {
00176     if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00177       throw new DatabaseException("SODA SyncActionDBQueryManager getActionsForService ServiceName to long or null abort "+serviceName);
00178     }
00179     Array array  = this.getActionsForService(serviceName);
00180     for (int i=0; i<array.size(); i++)  {
00181         ActionDB action = (ActionDB) array.get(i);
00182                         FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", action);
00183     }
00184   }
00185 
00186   // delete a row
00187 
00188   public void delete (String serviceName, String uid) throws DatabaseException  {
00189                 FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", new ActionDB(serviceName, uid, 0));
00190   }
00191 
00192 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4