DefaultSyncNumberManagerDelegate.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.algo.syncnumber.impl;
00027 
00028 import java.sql.ResultSet;
00029 import org.openmobileis.common.util.log.*;
00030 import org.openmobileis.common.util.database.ManagerDB;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 import org.openmobileis.common.util.exception.ServiceException;
00033 import org.openmobileis.synchro.algo.syncnumber.SyncNumberManagerDelegate;
00034 import org.openmobileis.synchro.algo.syncnumber.SyncNumberNotFoundException;
00035 import org.openmobileis.synchro.algo.syncnumber.SynchroNumber;
00036 
00044 public class DefaultSyncNumberManagerDelegate implements SyncNumberManagerDelegate {
00045   private long currentSyncNumber;
00046   protected SyncNumberQueryManager syncQueryManager = null;
00047 
00048         public DefaultSyncNumberManagerDelegate() {
00049         }
00050 
00051         public DefaultSyncNumberManagerDelegate(SyncNumberQueryManager queryManager) {
00052                 this.syncQueryManager = queryManager;
00053         }
00054   
00055         public void initDelegate()      {
00056                 try {
00057         //              syncQueryManager.createSyncTable();
00058       
00059                         // get last sync number
00060                         syncQueryManager.createSyncTable();
00061                         ResultSet result = null;
00062                         result = syncQueryManager.getLastSyncNumber();
00063                         if (result == null) {
00064                                 String[] syncParams = {"0", "0"};
00065                                 syncQueryManager.create(syncParams);
00066                                 result = syncQueryManager.getLastSyncNumber();
00067                         }
00068                         if (result.next())  {
00069                                 currentSyncNumber =  result.getLong(1);
00070                         } else  {
00071                                 LogManager.traceAlert(LogServices.SERVERSERVICE," No current synchro number set to 0");
00072                                 currentSyncNumber = 0;
00073                         }
00074                 }  catch (java.sql.SQLException e) {
00075                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00076         //              System.exit(1);
00077                 }  catch (DatabaseException e) {
00078                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00079 //                      System.exit(1);
00080                 }finally {
00081                         ManagerDB.getManager().garbageOpenedConnection();;
00082           }
00083         }
00084   
00085   public synchronized void setSyncNumberFactory(SyncNumberQueryManager fac)  {
00086     syncQueryManager = fac;
00087   }
00088 
00089   public SynchroNumber getSynchroNumber(long ns) throws SyncNumberNotFoundException {
00090     String[] syncParams = {String.valueOf(ns)};
00091     try {
00092       ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00093       if (!result.next()) {
00094         throw new SyncNumberNotFoundException(LogPriorities.INFO);
00095       } else {
00096         return new SynchroNumber(ns, result.getLong(1));
00097       }
00098     }  catch (java.sql.SQLException e) {
00099       LogManager.traceError(LogServices.SERVERSERVICE, new ServiceException("Error during getSynchroNumberBySN for synchro number :"+ns,e));
00100       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00101     }  catch (DatabaseException e) {
00102       LogManager.traceError(LogServices.SERVERSERVICE, e);
00103       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00104     } finally {
00105       ManagerDB.getManager().garbageOpenedConnection();;
00106     }
00107   }
00108 
00109   public SynchroNumber getNextSynchroNumber() throws DatabaseException {
00110     try {
00111       // update sync number
00112       long date = System.currentTimeMillis();
00113       long ns = this.incrementSynchoNumber();
00114       String[] syncParams = {String.valueOf(ns), String.valueOf(date)};
00115       syncQueryManager.create(syncParams);
00116       SynchroNumber syncNumber = new SynchroNumber(ns, date);
00117       return syncNumber;
00118     } catch (DatabaseException ex)  {
00119       throw new DatabaseException(ex);
00120     } finally {
00121       ManagerDB.getManager().garbageOpenedConnection();;
00122     }
00123   }
00124 
00125   public long getCurrentSynchroNumber() throws DatabaseException {
00126     try {
00127       // update sync number
00128       ResultSet result = syncQueryManager.getLastSyncNumber();
00129       if (result.next())  {
00130        return result.getLong(1);
00131       }
00132     } catch (Throwable ex)  {
00133       throw new DatabaseException(ex);
00134     } finally {
00135       ManagerDB.getManager().garbageOpenedConnection();;
00136     }
00137     throw new DatabaseException("No sync number in db. Db init error");
00138   }
00139 
00140   public void deleteSyncNumber(SynchroNumber ns) throws DatabaseException {
00141             String[] syncParams = {String.valueOf(ns.getSynchroNumber())};
00142             syncQueryManager.delete(syncParams);
00143   }
00144 
00145   public void createSynchroNumber(long ns, long timestamp)  throws DatabaseException  {
00146     String nsString = Long.toString(ns);
00147     String[] syncParams = {nsString, Long.toString(timestamp)};
00148     syncQueryManager.create(syncParams);
00149   }
00150   
00154   public void setTimeStampForSynchroNumber(long syncNumber) throws SyncNumberNotFoundException  {
00155           try  {        
00156       long timestamp = System.currentTimeMillis();
00157                 try     {
00158                                 this.getSynchroNumber(syncNumber);  //see if already exist.
00159                     String nsString = Long.toString(syncNumber);
00160                 String[] syncParams = {nsString, Long.toString(timestamp), nsString};   
00161                 syncQueryManager.update(syncParams);    
00162                 } catch (SyncNumberNotFoundException ex)        {
00163                         createSynchroNumber(syncNumber, timestamp);
00164                 }
00165     } catch (DatabaseException ex)  {
00166       throw new SyncNumberNotFoundException(ex);
00167     }
00168   }
00169   
00173   public long getTimeStampForSynchroNumber(long syncNumber)  throws SyncNumberNotFoundException {
00174     return this.getSynchroNumber(syncNumber).getTimeStamp();
00175   }
00176 
00177   private synchronized long incrementSynchoNumber() {
00178     return ++currentSyncNumber;
00179   }
00180 }

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