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 SynchroNumber getSynchroNumber(long ns) throws SyncNumberNotFoundException {
00086     String[] syncParams = {String.valueOf(ns)};
00087     try {
00088       ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00089       if (!result.next()) {
00090         throw new SyncNumberNotFoundException(LogPriorities.INFO);
00091       } else {
00092         return new SynchroNumber(ns, result.getLong(1));
00093       }
00094     }  catch (java.sql.SQLException e) {
00095       LogManager.traceError(LogServices.SERVERSERVICE, new ServiceException("Error during getSynchroNumberBySN for synchro number :"+ns,e));
00096       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00097     }  catch (DatabaseException e) {
00098       LogManager.traceError(LogServices.SERVERSERVICE, e);
00099       throw new SyncNumberNotFoundException(LogPriorities.INFO);
00100     } finally {
00101       ManagerDB.getManager().garbageOpenedConnection();;
00102     }
00103   }
00104 
00105   public SynchroNumber getNextSynchroNumber() throws DatabaseException {
00106     try {
00107       // update sync number
00108       long date = System.currentTimeMillis();
00109       long ns = this.incrementSynchoNumber();
00110       String[] syncParams = {String.valueOf(ns), String.valueOf(date)};
00111       syncQueryManager.create(syncParams);
00112       SynchroNumber syncNumber = new SynchroNumber(ns, date);
00113       return syncNumber;
00114     } catch (DatabaseException ex)  {
00115       throw new DatabaseException(ex);
00116     } finally {
00117       ManagerDB.getManager().garbageOpenedConnection();;
00118     }
00119   }
00120 
00121   public long getCurrentSynchroNumber() throws DatabaseException {
00122     try {
00123       // update sync number
00124       ResultSet result = syncQueryManager.getLastSyncNumber();
00125       if (result.next())  {
00126        return result.getLong(1);
00127       }
00128     } catch (Throwable ex)  {
00129       throw new DatabaseException(ex);
00130     } finally {
00131       ManagerDB.getManager().garbageOpenedConnection();;
00132     }
00133     throw new DatabaseException("No sync number in db. Db init error");
00134   }
00135 
00136   private synchronized long incrementSynchoNumber() {
00137     return ++currentSyncNumber;
00138   }
00139 }

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