JDBCGlobalPropertyQueryManager.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  *  Modifications :
00025  *  Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.modules.common.database.jdbc;
00029 
00030 //import java.sql.ResultSet;
00031 import java.sql.*;
00032 
00033 import org.openmobileis.common.context.ApplicationContext;
00034 import org.openmobileis.common.context.ApplicationContextManager;
00035 import org.openmobileis.common.util.database.AbstractQueryManager;
00036 import org.openmobileis.common.util.database.JdbcPoolManagerDB;
00037 import org.openmobileis.common.util.exception.DatabaseException;
00038 import org.openmobileis.common.util.exception.ServiceException;
00039 import org.openmobileis.modules.common.data.DefaultGlobalProperty;
00040 import org.openmobileis.modules.common.data.GlobalProperty;
00041 
00048 public class JDBCGlobalPropertyQueryManager extends AbstractQueryManager        {
00049         
00050         protected static String queryGetGlobalProperty = "SELECT idservice, propkey, propvalue FROM oooglobalproperties where idservice='%0%' AND propkey='%1%'";
00051         protected static String queryGetPropertiesForService = "SELECT idservice, propkey, propvalue FROM oooglobalproperties where idservice='%0%'";
00052         protected static String queryGetAllGlobalProperties = "SELECT idservice, propkey, propvalue FROM oooglobalproperties";
00053 
00054         protected static String queryInsertGlobalProperty = "INSERT INTO oooglobalproperties (idservice, propkey, propvalue)"
00055                 +" VALUES ('%0%', '%1%', '%2%')";
00056 
00057         //update from synoptic ONLT no fidelisation managed
00058         protected static String queryUpdateGlobalProperty = "UPDATE oooglobalproperties " 
00059                 +"SET propvalue='%0%'"  
00060                 +" WHERE idservice='%1%' AND propkey='%2%'";
00061         protected static String queryDeleteGlobalProperty = "DELETE FROM oooglobalproperties where  idservice='%0%' AND propkey='%1%'"; 
00062 
00063         // query pattern used to create table
00064         private static String queryCreateTable  =
00065                                 "CREATE TABLE oooglobalproperties ("
00066                                 +"idservice varchar(25) NOT NULL,"
00067                                 +"propkey varchar(15) NOT NULL,"
00068                                 +"propvalue varchar(50) NOT NULL,"
00069                                 +"CONSTRAINT oooglobalpropertieskey PRIMARY KEY (idservice, propkey)"
00070                                 +");";
00071 
00075   public JDBCGlobalPropertyQueryManager() {
00076     super();
00077                 ApplicationContext context = ApplicationContextManager.getManager().getApplicationContext();
00078                 JdbcPoolManagerDB pool = (JdbcPoolManagerDB)context.getObject("server.querymanager.dbpool");
00079                 this.registerManagerDB(pool);
00080   }
00081 
00082         /*
00083         * check if the sync table exists. IF it does not exist, create it
00084         */
00085         public void createSyncTable() throws ServiceException, java.sql.SQLException, DatabaseException {
00086                 try {
00087                         Connection con = this.getDbManager().getConnection();
00088                         DatabaseMetaData dbmd = con.getMetaData();
00089                         String[] tableType = {"TABLE"};
00090                         ResultSet result = dbmd.getTables (null,null,"oooglobalproperties", tableType);
00091                         if (!result.next()) {
00092                                 this.executeUpdate(queryCreateTable, null);
00093                         }
00094                 } finally {
00095                         this.getDbManager().garbageOpenedConnection();
00096                 }
00097         }
00098         
00099         // insert a row
00100         public void create (String parameters[]) throws DatabaseException {
00101                 this.executeUpdate(JDBCGlobalPropertyQueryManager.queryInsertGlobalProperty, parameters);
00102         }
00103 
00104         // delete a row
00105         public void delete (String parameters[]) throws DatabaseException  {
00106                 this.executeUpdate(JDBCGlobalPropertyQueryManager.queryDeleteGlobalProperty, parameters);
00107         }
00108 
00109         
00110         // update a row
00111         public void update (String parameters[]) throws DatabaseException {
00112                 this.executeUpdate(JDBCGlobalPropertyQueryManager.queryUpdateGlobalProperty, parameters);
00113         }
00114     
00115         // get all client in db
00116         public ResultSet getGlobalProperty (String[] param) throws DatabaseException {
00117                 return this.executeQuery(JDBCGlobalPropertyQueryManager.queryGetGlobalProperty, param);
00118         }
00119     
00120         // get all client in db
00121         public ResultSet getPropertiesForService (String[] param) throws DatabaseException {
00122                 return this.executeQuery(JDBCGlobalPropertyQueryManager.queryGetPropertiesForService, param);
00123         }
00124     
00125         // get all client in db
00126         public ResultSet getAllGlobalProperties () throws DatabaseException {
00127                 return this.executeQuery(JDBCGlobalPropertyQueryManager.queryGetAllGlobalProperties, null);
00128         }
00129 
00130         public GlobalProperty convertResultSetToProperty (ResultSet result) throws DatabaseException    {
00131                 try     {
00132                         GlobalProperty property =  new DefaultGlobalProperty(result.getString(1), result.getString(2));
00133                         //idservice, key, value
00134                         property.setPropertyValue(result.getString(3));
00135                         return property;
00136                 } catch (Throwable ex)  {
00137                         throw new DatabaseException(ex);
00138                 }
00139         }
00140         public String[] getInsertParamFromProperty(GlobalProperty property)     {
00141                 String[] param = {                      
00142                         property.getServiceProperty()
00143                         , property.getPropertyKey()
00144                         , property.getPropertyValue()
00145                 };
00146                 return param;
00147         }
00148         
00149         public String[] getUpdateParamFromProperty(GlobalProperty property)     {
00150                 String[] param = {                      
00151                         property.getPropertyValue()
00152                         , property.getServiceProperty()
00153                         , property.getPropertyKey()
00154                 };
00155                 return param;
00156         }
00157 }

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