Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JDBCGlobalPropertyFactory.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2004  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2004 Creation P.Delrieu
00025  * 
00026  */
00027 package org.openmobileis.modules.crm.database.common.jdbc;
00028 
00029 import java.sql.ResultSet;
00030 
00031 import org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.common.util.exception.ServiceException;
00033 import org.openmobileis.common.util.log.LogManager;
00034 import org.openmobileis.database.DatabaseException;
00035 import org.openmobileis.modules.crm.data.common.GlobalProperty;
00036 import org.openmobileis.modules.crm.data.common.GlobalPropertyFactory;
00037 
00046 public class JDBCGlobalPropertyFactory implements GlobalPropertyFactory {
00047 
00048  
00049         private static JDBCGlobalPropertyFactory factory = null;
00050         private JDBCGlobalPropertyQueryManager propsQuery = null;
00051 
00055   public JDBCGlobalPropertyFactory() {
00056                 super();
00057                 propsQuery = new JDBCGlobalPropertyQueryManager();
00058                 try     {
00059                         propsQuery.createSyncTable();
00060                 } catch (Throwable ex)  {
00061                         LogManager.traceError(0, ex);
00062                 }
00063   }
00064 
00065         public static JDBCGlobalPropertyFactory getManager() {
00066                         if (factory == null)  {
00067                         synchronized(JDBCGlobalPropertyFactory.class) {
00068                                 if (factory == null)  {
00069                                         factory = new JDBCGlobalPropertyFactory();
00070                                 }
00071                         }
00072                 }
00073                 return factory;
00074         }
00075 
00076         /* (non-Javadoc)
00077          * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String)
00078          */
00079         public GlobalProperty getProperty(String service, String key) throws  ServiceException {
00080                 GlobalProperty prop = null;
00081                 try     {
00082                         String[] param = new String[]{service, key};
00083                         ResultSet result = propsQuery.getGlobalProperty(param);
00084                         if (result.next()) {
00085                                 prop =  propsQuery.convertResultSetToProperty(result);
00086                         }
00087                         result.close();
00088                 } catch (Exception ex)  {
00089                         throw new ServiceException(ex);
00090                 } finally {
00091                         propsQuery.getDbManager().garbageOpenedConnection();
00092                 }
00093                 return prop;
00094         }
00095 
00096         /* (non-Javadoc)
00097          * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String)
00098          */
00099         public Array getPropertiesForService(String service) throws  ServiceException {
00100                 Array ret = new Array();
00101                 try     {
00102                         String[] param = new String[]{service};
00103                         ResultSet result = propsQuery.getPropertiesForService(param);
00104                         while (result.next()) {
00105                                 ret.add(propsQuery.convertResultSetToProperty(result));
00106                         }
00107                         result.close();
00108                 } catch (Exception ex)  {
00109                         throw new ServiceException(ex);
00110                 } finally {
00111                         propsQuery.getDbManager().garbageOpenedConnection();
00112                 }
00113                 return ret;
00114         }
00115 
00116         /* (non-Javadoc)
00117          * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String)
00118          */
00119         public Array getAllGlobalProperties() throws  ServiceException {
00120                 Array ret = new Array();
00121                 try     {
00122                         ResultSet result = propsQuery.getAllGlobalProperties();
00123                         while (result.next()) {
00124                                 ret.add(propsQuery.convertResultSetToProperty(result));
00125                         }
00126                         result.close();
00127                 } catch (Exception ex)  {
00128                         throw new ServiceException(ex);
00129                 } finally {
00130                         propsQuery.getDbManager().garbageOpenedConnection();
00131                 }
00132                 return ret;
00133         }
00134 
00135   /* (non-Javadoc)
00136    * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#storeProperty(org.openmobileis.modules.crm.data.common.GlobalProperty)
00137    */
00138   public void storeProperty(GlobalProperty property) throws DatabaseException  {
00139                 try     {
00140                         GlobalProperty dbprops = this.getProperty(property.getServiceProperty(), property.getPropertyKey());
00141                         if (dbprops == null)    {
00142                                 propsQuery.create(propsQuery.getInsertParamFromProperty(property));
00143                         } else  {
00144                                 propsQuery.update(propsQuery.getUpdateParamFromProperty(property));
00145                         }                       
00146                 } catch (Throwable ex)  {
00147                         throw new DatabaseException(ex);
00148                 } finally {
00149                         propsQuery.getDbManager().garbageOpenedConnection();
00150                 }
00151   }
00152 
00153   /* (non-Javadoc)
00154    * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#deleteProperty(java.lang.String, java.lang.String)
00155    */
00156   public void deleteProperty(String service, String key) throws DatabaseException  {
00157                 try     {
00158                         String[] param = {service, key};
00159                         propsQuery.delete(param);
00160                 } catch (Throwable ex)  {
00161                         throw new DatabaseException(ex);
00162                 } finally {
00163                         propsQuery.getDbManager().garbageOpenedConnection();
00164                 }
00165   }
00166 
00167 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4