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.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.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.getCurrentFODB().replace("OMJournalEntry", entry);
00090       } else { //add
00091         FastObjectDBManager.getCurrentFODB().add("OMJournalEntry", entry);
00092       }
00093     } catch (Throwable ex) {
00094       throw new DatabaseException(ex);
00095     }
00096   }
00097 
00098   /*
00099   * Delete all entries for a service
00100   * parameters SERVICE_NAME
00101   */
00102   public void deleteAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00103     try {
00104       Array entryList = this.getAllEntryForService(serviceName);
00105       for (int i = 0; i < entryList.size(); i++) {
00106         JournalEntry entry = (JournalEntry) entryList.get(i);
00107         FastObjectDBManager.getCurrentFODB().delete("OMJournalEntry", entry);
00108       }
00109     } catch (Throwable ex) {
00110       throw new DatabaseException(ex);
00111     }
00112   }
00113 
00114   /*
00115   * get all entries for service
00116   * parameters SERVICE_NAME
00117   */
00118   public Array getAllEntryForService(String serviceName) throws ServiceException, DatabaseException {
00119     try {
00120                         Query q = FastObjectDBManager.getCurrentFODB().query();
00121                         q.constrain(JournalEntry.class);
00122                         Query subq = q.descend("getServiceName()");
00123                         subq.constrain(serviceName).equal();
00124                         ObjectSet set = q.execute(); 
00125                         return ((FODBSodaObjectSet)set).getArrayFromSet();
00126     } catch (Throwable ex) {
00127       throw new DatabaseException(ex);
00128     }
00129   }
00130 }

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