OracleAtomicObjectQueryManager.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  *  
00023  *  Modifications :
00024  *  2006 Creation P.Delrieu
00025  * 
00026  */
00027 package org.openmobileis.synchro.algo.replication.utils.impl;
00028 
00029 import java.sql.*;
00030 
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 
00033 
00041 public class OracleAtomicObjectQueryManager extends AtomicObjectQueryManager {
00042   // query pattern used to insert a new UID
00043         private static String queryUpdate  =
00044                 "UPDATE SYNC_UID SET TIME_STAMP=%0%, CHECKSUM='%1%', MODIFICATION_TYPE =%2% , USERLINK ='%3%' " +
00045                 "WHERE UIDI='%4%' AND SERVICEID='%5%'";
00046 
00047          private static String queryCreate  =
00048                  "INSERT INTO SYNC_UID (UIDI , SERVICEID, TIME_STAMP, CHECKSUM, MODIFICATION_TYPE, CREATION_DATE, USERLINK) " +
00049                         "VALUES ('%0%', '%1%', %2%, '%3%', %4%, %5%, '%6%')";
00050 
00051   private static String createIndex =
00052     "CREATE INDEX serviceid ON SYNC_UID (SERVICEID)";
00053 
00054   private static String modifIndex =
00055     "CREATE INDEX modifid ON SYNC_UID (SERVICEID, TIME_STAMP,  MODIFICATION_TYPE)";
00056 
00057   // query pattern used to create syncNB table
00058   private static String queryCreateTable  =
00059     "CREATE TABLE SYNC_UID ("
00060     +"UIDI      VARCHAR2(250 CHAR) NOT NULL,"
00061     +"SERVICEID VARCHAR2(50 CHAR) NOT NULL,"
00062     +"TIME_STAMP NUMBER(16),"
00063     +"CHECKSUM  VARCHAR2(25 CHAR),"
00064     +"MODIFICATION_TYPE SMALLINT,"
00065     +"CREATION_DATE NUMBER(16),"
00066           +"USERLINK    VARCHAR2(25 CHAR) ,"
00067           +" CONSTRAINT uidd PRIMARY KEY (UIDI, SERVICEID)"
00068     +") ";
00069 
00070   // delete all atomic object that are deleted and with a TS inferior to the specified date.
00071   private static String purgeDetetedAtomicObject =
00072     "DELETE FROM SYNC_UID WHERE SERVICEID = '%0%' AND TIME_STAMP < %1% AND MODIFICATION_TYPE=3";
00073 
00074 
00075   /********  SELECT QUERIES ********************/
00076 
00077   // query pattern used to get all Atomic Object for a service id
00078   private static String queryGetAtomicObject  =
00079     "SELECT UIDI, CHECKSUM, TIME_STAMP, MODIFICATION_TYPE, CREATION_DATE FROM SYNC_UID WHERE UIDI='%0%' AND SERVICEID = '%1%'";
00080 
00081   // query pattern used to get all modified Atomic Object for a service id since date
00082         private static String queryGetAllModifiedAtomicObjectForServiceSince  =
00083                 "SELECT UIDI, MODIFICATION_TYPE, CREATION_DATE FROM SYNC_UID WHERE SERVICEID = '%0%' AND TIME_STAMP > %1% AND USERLINK='%2%'";
00084 
00085   public void createAtomicObject(String[] parameters) throws DatabaseException {
00086     this.executeUpdate(queryCreate, parameters);
00087   }
00088 
00089  /*
00090  * check if the sync table exists. IF it does not exist, create it
00091  */
00092  public void createTable() throws DatabaseException, java.sql.SQLException {
00093      try  {
00094       Connection con = this.getDbManager().getConnection();
00095       DatabaseMetaData dbmd = con.getMetaData();
00096       String[] tableType = {"TABLE"};
00097       ResultSet result = dbmd.getTables (null,null,"SYNC_UID", tableType);
00098       if (!result.next()) {
00099         this.executeUpdate(queryCreateTable, null);
00100         this.executeUpdate(createIndex, null);
00101         this.executeUpdate(modifIndex, null);
00102         String[] data = {"0", "0", "0","0", "0", "0", "FWKBIDON"};
00103         this.createAtomicObject(data);
00104       }
00105     } finally {
00106                         this.getDbManager().garbageOpenedConnection();
00107     }
00108   }
00109 
00110  public void updateAtomicObject(String[] parameters) throws DatabaseException {
00111     this.executeUpdate(queryUpdate, parameters);
00112   }
00113 
00114   public void purgeDeletedAtomicObject(String[] parameters) throws DatabaseException {
00115                 this.executeUpdate(purgeDetetedAtomicObject, parameters);
00116         }
00117 
00118   /* getAtomicObject() get the atomic object for specified UID
00119   * @param     : array containing atomic object uid
00120   * @return    ResultSet : rows containing UID, CHECKSUM, TIME_STAMP, MODIFICATION_TYPE, CREATION_DATE
00121   * @exception ServiceException
00122   */
00123   public ResultSet getAtomicObject(String parameters[]) throws DatabaseException {
00124     return (this.executeQuery(queryGetAtomicObject, parameters));
00125   }
00126 
00127   /* getAtomicObjectsForService() return all atomic object uid modified since specified date for service
00128   * @param     : array containing service id and date
00129   * @return    ResultSet : rows containing UID
00130   * @exception ServiceException
00131   */
00132   public ResultSet getAllModifiedAtomicObjectForServiceSince (String parameters[]) throws DatabaseException {
00133     return (this.executeQuery(queryGetAllModifiedAtomicObjectForServiceSince, parameters));
00134   }
00135 
00136 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1