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

OracleSyncNumberManagerDelegate.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 OracleSyncNumberManagerDelegate implements SyncNumberManagerDelegate {
00046   private long currentSyncNumber;
00047   protected static OracleSafeServerSyncNumberQueryManager syncQueryManager = null;
00048 
00049   public OracleSyncNumberManagerDelegate() {
00050   }
00051   
00052         public void initDelegate()      {
00053                 syncQueryManager = new OracleSafeServerSyncNumberQueryManager();
00054                 try {
00055                         syncQueryManager.createSyncTable();
00056       
00057                         // get last sync number
00058                         ResultSet result = null;
00059                         result = syncQueryManager.getLastSyncNumber();
00060                         if (result.next())  {
00061                                 currentSyncNumber =  result.getLong(1);
00062                         } else  {
00063                                 LogManager.traceAlert(LogServices.SERVERSERVICE," No current synchro number set to 0");
00064                                 currentSyncNumber = 0;
00065                         }
00066                 }  catch (java.sql.SQLException e) {
00067                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00068                         System.exit(1);
00069                 }  catch (ServiceException e) {
00070                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Number manager. Exit System",e));
00071                         System.exit(1);
00072                 }
00073         }
00074 
00075   
00076   public synchronized void setSyncNumberFactory(SyncNumberQueryManager fac)  {
00077     syncQueryManager = (OracleSafeServerSyncNumberQueryManager)fac;
00078   }
00079 
00080   public SynchroNumber getSynchroNumber(long ns) throws SyncNumberNotFoundException {
00081     String[] syncParams = {String.valueOf(ns)};
00082     try {
00083       ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00084       if (!result.next()) {
00085         throw new SyncNumberNotFoundException(LogPriorities.INFO);
00086       } else {
00087 //        return new SynchroNumber(ns, result.getDate(1).getTime());
00088                                 return new SynchroNumber(ns, result.getTimestamp(1).getTime());
00089       }
00090     }  catch (java.sql.SQLException e) {
00091       LogManager.traceError(LogServices.SERVERSERVICE, new ServiceException("Error during getSynchroNumberBySN for synchro number :"+ns,e));
00092       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00093     }  catch (ServiceException e) {
00094       LogManager.traceError(LogServices.SERVERSERVICE, e);
00095       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00096     } finally {
00097       ManagerDB.getManager().garbageOpenedConnection();;
00098     }
00099   }
00100 
00101   public SynchroNumber getNextSynchroNumber() throws SyncNumberNotFoundException {
00102                 long ns = this.incrementSynchoNumber();
00103                 String[] syncParams = {String.valueOf(ns)};
00104     try {
00105             // update sync number
00106             syncQueryManager.create(syncParams);
00107             ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00108                         SynchroNumber syncNumber = null;
00109             if (result.next()) {
00110 //                              syncNumber = new SynchroNumber(ns, result.getDate(1).getTime());
00111                                 syncNumber = new SynchroNumber(ns, result.getTimestamp(1).getTime());
00112             }
00113             return syncNumber;
00114                 }  catch (java.sql.SQLException e) {
00115                         LogManager.traceError(LogServices.SERVERSERVICE, new ServiceException("Error during getNextSynchroNumber for synchro number :"+ns,e));
00116                         throw new SyncNumberNotFoundException(LogPriorities.INFO);
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[] syncParams = {Long.toString(ns)};
00133     syncQueryManager.create(syncParams);
00134   }
00135   
00139   public void setTimeStampForSynchroNumber(long syncNumber)     {
00140   }
00141   
00145   public long getTimeStampForSynchroNumber(long syncNumber)  throws SyncNumberNotFoundException {
00146     return this.getSynchroNumber(syncNumber).getTimeStamp();
00147   }
00148 
00149   private synchronized long incrementSynchoNumber() {
00150     return ++currentSyncNumber;
00151   }
00152 }

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