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 00032 import org.openmobileis.common.util.collection.Array; 00033 import org.openmobileis.common.util.exception.DatabaseException; 00034 import org.openmobileis.common.util.exception.ServiceException; 00035 import org.openmobileis.common.util.log.LogManager; 00036 import org.openmobileis.modules.common.data.GlobalProperty; 00037 import org.openmobileis.modules.common.data.GlobalPropertyFactory; 00038 00047 public class JDBCGlobalPropertyFactory implements GlobalPropertyFactory { 00048 00049 00050 private static JDBCGlobalPropertyFactory factory = null; 00051 private JDBCGlobalPropertyQueryManager propsQuery = null; 00052 00056 public JDBCGlobalPropertyFactory() { 00057 super(); 00058 propsQuery = new JDBCGlobalPropertyQueryManager(); 00059 try { 00060 propsQuery.createSyncTable(); 00061 } catch (Throwable ex) { 00062 LogManager.traceError(0, ex); 00063 } 00064 } 00065 00066 public static JDBCGlobalPropertyFactory getManager() { 00067 if (factory == null) { 00068 synchronized(JDBCGlobalPropertyFactory.class) { 00069 if (factory == null) { 00070 factory = new JDBCGlobalPropertyFactory(); 00071 } 00072 } 00073 } 00074 return factory; 00075 } 00076 00077 /* (non-Javadoc) 00078 * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String) 00079 */ 00080 public GlobalProperty getProperty(String service, String key) throws ServiceException { 00081 GlobalProperty prop = null; 00082 try { 00083 String[] param = new String[]{service, key}; 00084 ResultSet result = propsQuery.getGlobalProperty(param); 00085 if (result.next()) { 00086 prop = propsQuery.convertResultSetToProperty(result); 00087 } 00088 result.close(); 00089 } catch (Exception ex) { 00090 throw new ServiceException(ex); 00091 } finally { 00092 propsQuery.getDbManager().garbageOpenedConnection(); 00093 } 00094 return prop; 00095 } 00096 00097 /* (non-Javadoc) 00098 * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String) 00099 */ 00100 public Array getPropertiesForService(String service) throws ServiceException { 00101 Array ret = new Array(); 00102 try { 00103 String[] param = new String[]{service}; 00104 ResultSet result = propsQuery.getPropertiesForService(param); 00105 while (result.next()) { 00106 ret.add(propsQuery.convertResultSetToProperty(result)); 00107 } 00108 result.close(); 00109 } catch (Exception ex) { 00110 throw new ServiceException(ex); 00111 } finally { 00112 propsQuery.getDbManager().garbageOpenedConnection(); 00113 } 00114 return ret; 00115 } 00116 00117 /* (non-Javadoc) 00118 * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#getProperty(java.lang.String, java.lang.String) 00119 */ 00120 public Array getAllGlobalProperties() throws ServiceException { 00121 Array ret = new Array(); 00122 try { 00123 ResultSet result = propsQuery.getAllGlobalProperties(); 00124 while (result.next()) { 00125 ret.add(propsQuery.convertResultSetToProperty(result)); 00126 } 00127 result.close(); 00128 } catch (Exception ex) { 00129 throw new ServiceException(ex); 00130 } finally { 00131 propsQuery.getDbManager().garbageOpenedConnection(); 00132 } 00133 return ret; 00134 } 00135 00136 /* (non-Javadoc) 00137 * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#storeProperty(org.openmobileis.modules.crm.data.common.GlobalProperty) 00138 */ 00139 public void storeProperty(GlobalProperty property) throws DatabaseException { 00140 try { 00141 GlobalProperty dbprops = this.getProperty(property.getServiceProperty(), property.getPropertyKey()); 00142 if (dbprops == null) { 00143 propsQuery.create(propsQuery.getInsertParamFromProperty(property)); 00144 } else { 00145 propsQuery.update(propsQuery.getUpdateParamFromProperty(property)); 00146 } 00147 } catch (Throwable ex) { 00148 throw new DatabaseException(ex); 00149 } finally { 00150 propsQuery.getDbManager().garbageOpenedConnection(); 00151 } 00152 } 00153 00154 /* (non-Javadoc) 00155 * @see org.openmobileis.modules.crm.data.common.GlobalPropertyFactory#deleteProperty(java.lang.String, java.lang.String) 00156 */ 00157 public void deleteProperty(String service, String key) throws DatabaseException { 00158 try { 00159 String[] param = {service, key}; 00160 propsQuery.delete(param); 00161 } catch (Throwable ex) { 00162 throw new DatabaseException(ex); 00163 } finally { 00164 propsQuery.getDbManager().garbageOpenedConnection(); 00165 } 00166 } 00167 00168 }