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

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

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