00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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
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
00115 syncQueryManager.create(syncParams);
00116 ResultSet result = syncQueryManager.getSyncNumberbysn(syncParams);
00117 SynchroNumber syncNumber = null;
00118 if (result.next()) {
00119
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 }