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

DefaultSyncNumberManagerDelegate.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.synchro.algo.syncnumber.impl;
00029 
00030 import java.sql.ResultSet;
00031 import org.openmobileis.common.util.log.*;
00032 import org.openmobileis.common.util.database.ManagerDB;
00033 import org.openmobileis.common.util.exception.ServiceException;
00034 import org.openmobileis.synchro.algo.syncnumber.SyncNumberManagerDelegate;
00035 import org.openmobileis.synchro.algo.syncnumber.SyncNumberNotFoundException;
00036 import org.openmobileis.synchro.algo.syncnumber.SynchroNumber;
00037 
00045 public class DefaultSyncNumberManagerDelegate implements SyncNumberManagerDelegate {
00046   private long currentSyncNumber;
00047   protected SyncNumberQueryManager syncQueryManager = null;
00048 
00049         public DefaultSyncNumberManagerDelegate() {
00050         }
00051 
00052         public DefaultSyncNumberManagerDelegate(SyncNumberQueryManager queryManager) {
00053                 this.syncQueryManager = queryManager;
00054         }
00055   
00056         public void initDelegate()      {
00057                 try {
00058         //              syncQueryManager.createSyncTable();
00059       
00060                         // get last sync number
00061                         syncQueryManager.createSyncTable();
00062                         ResultSet result = null;
00063                         result = syncQueryManager.getLastSyncNumber();
00064                         if (result == null) {
00065                                 String[] syncParams = {"0", "0"};
00066                                 syncQueryManager.create(syncParams);
00067                                 result = syncQueryManager.getLastSyncNumber();
00068                         }
00069                         if (result.next())  {
00070                                 currentSyncNumber =  result.getLong(1);
00071                         } else  {
00072                                 LogManager.traceAlert(LogServices.SERVERSERVICE," No current synchro number set to 0");
00073                                 currentSyncNumber = 0;
00074                         }
00075                 }  catch (java.sql.SQLException e) {
00076                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00077         //              System.exit(1);
00078                 }  catch (ServiceException e) {
00079                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00080 //                      System.exit(1);
00081                 }
00082         }
00083   
00084   public synchronized void setSyncNumberFactory(SyncNumberQueryManager fac)  {
00085     syncQueryManager = fac;
00086   }
00087 
00088   public SynchroNumber getSynchroNumber(long ns) throws SyncNumberNotFoundException {
00089     String[] syncParams = {String.valueOf(ns)};
00090     try {
00091       ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00092       if (!result.next()) {
00093         throw new SyncNumberNotFoundException(LogPriorities.INFO);
00094       } else {
00095         return new SynchroNumber(ns, result.getLong(1));
00096       }
00097     }  catch (java.sql.SQLException e) {
00098       LogManager.traceError(LogServices.SERVERSERVICE, new ServiceException("Error during getSynchroNumberBySN for synchro number :"+ns,e));
00099       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00100     }  catch (ServiceException e) {
00101       LogManager.traceError(LogServices.SERVERSERVICE, e);
00102       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00103     } finally {
00104       ManagerDB.getManager().garbageOpenedConnection();;
00105     }
00106   }
00107 
00108   public SynchroNumber getNextSynchroNumber() throws SyncNumberNotFoundException {
00109     try {
00110             // update sync number
00111             long date = System.currentTimeMillis();
00112             long ns = this.incrementSynchoNumber();
00113             String[] syncParams = {String.valueOf(ns), String.valueOf(date)};
00114             syncQueryManager.create(syncParams);
00115             SynchroNumber syncNumber = new SynchroNumber(ns, date);
00116             return syncNumber;
00117     } catch (ServiceException ex)  {
00118       throw new SyncNumberNotFoundException(ex);
00119     }
00120   }
00121 
00122   public void deleteSyncNumber(SynchroNumber ns) {
00123     try {
00124             String[] syncParams = {String.valueOf(ns.getSynchroNumber())};
00125             syncQueryManager.delete(syncParams);
00126     } catch (ServiceException ex)  {
00127       LogManager.trace(ex);
00128     }
00129   }
00130 
00131   public void createSynchroNumber(long ns, long timestamp)  throws ServiceException  {
00132     String nsString = Long.toString(ns);
00133     String[] syncParams = {nsString, Long.toString(timestamp)};
00134     syncQueryManager.create(syncParams);
00135   }
00136   
00140   public void setTimeStampForSynchroNumber(long syncNumber)     {
00141         try     {
00142                 long timestamp = System.currentTimeMillis();
00143                 try     {
00144                                 this.getSynchroNumber(syncNumber);  //see if already exist.
00145                     String nsString = Long.toString(syncNumber);
00146                 String[] syncParams = {nsString, Long.toString(timestamp), nsString};   
00147                 syncQueryManager.update(syncParams);    
00148                 } catch (SyncNumberNotFoundException ex)        {
00149                         createSynchroNumber(syncNumber, timestamp);
00150                 }
00151     } catch (ServiceException ex)  {
00152       LogManager.trace(ex);
00153     }
00154   }
00155   
00159   public long getTimeStampForSynchroNumber(long syncNumber)  throws SyncNumberNotFoundException {
00160     return this.getSynchroNumber(syncNumber).getTimeStamp();
00161   }
00162 
00163   private synchronized long incrementSynchoNumber() {
00164     return ++currentSyncNumber;
00165   }
00166 }

Generated on Wed Dec 14 21:05:32 2005 for OpenMobileIS by  doxygen 1.4.4