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

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4