JournalQueryManager.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  */
00025 
00026 package org.openmobileis.synchro.journal;
00027 
00028 import org.odbms.ObjectSet;
00029 import org.odbms.Query;
00030 import org.openmobileis.common.util.exception.DatabaseException;
00031 import org.openmobileis.common.util.exception.ServiceException;
00032 import org.openmobileis.common.util.log.LogManager;
00033 import org.openmobileis.common.util.PropertiesManager;
00034 import org.openmobileis.common.util.collection.Array;
00035 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00037 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00038 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00039 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00040 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00041 
00050 public class JournalQueryManager {
00051 
00052   public JournalQueryManager() {
00053   }
00054   /*
00055   * check if the sync table exists. IF it does not exist, create it
00056   */
00057   public void createJournalTable() throws ServiceException, DatabaseException {
00058     String dbPath = PropertiesManager.getManager().getProperty("fastobjectdb.database.path");
00059     if (dbPath == null) {
00060       LogManager.traceError(0, "JournalQueryManager FPDB ERROR : FODB path property is not set. Can't create db");
00061     }
00062     try {
00063       FastObjectDB db = FastObjectDBManager.getManager().getCurrentFODB();
00064       if (!db.isCollectionExist("OMJournalEntry")) {
00065         FODBCollectionDescriptor descriptor = new FODBCollectionDescriptor("OMJournalEntry", JournalEntry.class);
00066         descriptor.setSynchronize(false);
00067         db.createCollection(descriptor);
00068         FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getEntryID()", 12, 60);
00069         db.addIndex("OMJournalEntry", IDDescriptor);
00070         FODBStringIndexDescriptor serviceNameDescriptor = new FODBStringIndexDescriptor("ServiceName", FODBIndexDescriptor.MULTIPLE, "getServiceName()", 12, 50, 10);
00071         db.addIndex("OMJournalEntry", serviceNameDescriptor);
00072       }
00073     } catch (Throwable ex) {
00074       LogManager.traceError(0, "JournalQueryManager createJournalTable ERROR :"+ex.toString());
00075       throw new ServiceException(ex);
00076     }
00077   }
00078   /*
00079   * Add a Service journal entry in the database
00080   */
00081   public void createOrUpdate(JournalEntry entry) throws ServiceException, DatabaseException {
00082     try {
00083                         Query q = FastObjectDBManager.getManager().getCurrentFODB().query();
00084                         q.constrain(JournalEntry.class);
00085                         Query subq = q.descend("getEntryID()");
00086                         subq.constrain(entry.getEntryID()).equal();
00087                         ObjectSet set = q.execute();
00088                         if (set.hasNext())      {
00089         FastObjectDBManager.getManager().getCurrentFODB().replace("OMJournalEntry", entry);
00090       } else { //add
00091         FastObjectDBManager.getManager().getCurrentFODB().add("OMJournalEntry", entry);
00092       }
00093     } catch (Throwable ex) {
00094         LogManager.traceError(0, ex);
00095       FastObjectDBManager.getManager().getCurrentFODB().removeCollecionFile("OMJournalEntry");
00096       throw new DatabaseException(ex);
00097     }
00098   }
00099 
00100   /*
00101    * Clear All Journal Entries
00102    */
00103 
00104   /*
00105   * Delete all entries for a service
00106   * parameters SERVICE_NAME
00107   */
00108   public void deleteAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00109     try {
00110       Array entryList = this.getAllEntryForService(serviceName);
00111       for (int i = 0; i < entryList.size(); i++) {
00112         JournalEntry entry = (JournalEntry) entryList.get(i);
00113         FastObjectDBManager.getManager().getCurrentFODB().delete("OMJournalEntry", entry);
00114       }
00115     } catch (Throwable ex) {
00116         LogManager.traceError(0, ex);
00117       FastObjectDBManager.getManager().getCurrentFODB().removeCollecionFile("OMJournalEntry");
00118       throw new DatabaseException(ex);
00119     }
00120   }
00121 
00122   /*
00123   * get all entries for service
00124   * parameters SERVICE_NAME
00125   */
00126   public Array getAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00127     try {
00128 //                      LogManager.traceDebug(0, " FastObjectDBManager.getManager().getCurrentFODB() :"+ new Boolean(FastObjectDBManager.getManager().getCurrentFODB() != null).toString());
00129                         Query q = FastObjectDBManager.getManager().getCurrentFODB().query();
00130         //              LogManager.traceDebug(0, "query :"+q);
00131                         q.constrain(JournalEntry.class);
00132                         Query subq = q.descend("getServiceName()");
00133 //                      LogManager.traceDebug(0, "subq :"+subq);
00134                         subq.constrain(serviceName).equal();
00135                         ObjectSet set = q.execute();
00136 //                      LogManager.traceDebug(0, "set :"+set);
00137                         return ((FODBSodaObjectSet)set).getArrayFromSet();
00138     } catch (Throwable ex) {
00139         LogManager.traceError(0, ex);
00140       FastObjectDBManager.getManager().getCurrentFODB().removeCollecionFile("OMJournalEntry");
00141       throw new DatabaseException(ex);
00142     }
00143   }
00144 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4