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, "Client Factory FPDB ERROR : Unknown error during creation");
00075       LogManager.traceError(0, ex);
00076     }
00077   }
00078   /*
00079   * Add a Service journal entry in the database
00080   */
00081   public void createOrUpdate(JournalEntry entry) throws 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       throw new DatabaseException(ex);
00095     }
00096   }
00097 
00098   /*
00099    * Clear All Journal Entries
00100    */
00101 
00102   /*
00103   * Delete all entries for a service
00104   * parameters SERVICE_NAME
00105   */
00106   public void deleteAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00107     try {
00108       Array entryList = this.getAllEntryForService(serviceName);
00109       for (int i = 0; i < entryList.size(); i++) {
00110         JournalEntry entry = (JournalEntry) entryList.get(i);
00111         FastObjectDBManager.getManager().getCurrentFODB().delete("OMJournalEntry", entry);
00112       }
00113     } catch (Throwable ex) {
00114       throw new DatabaseException(ex);
00115     }
00116   }
00117 
00118   /*
00119   * get all entries for service
00120   * parameters SERVICE_NAME
00121   */
00122   public Array getAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00123     try {
00124                         Query q = FastObjectDBManager.getManager().getCurrentFODB().query();
00125                         q.constrain(JournalEntry.class);
00126                         Query subq = q.descend("getServiceName()");
00127                         subq.constrain(serviceName).equal();
00128                         ObjectSet set = q.execute();
00129                         return ((FODBSodaObjectSet)set).getArrayFromSet();
00130     } catch (Throwable ex) {
00131       throw new DatabaseException(ex);
00132     }
00133   }
00134 }

Generated on Tue May 22 23:01:10 2007 for OpenMobileIS by  doxygen 1.5.1-p1