1 13 package org.jahia.services.usermanager; 14 15 import org.jahia.exceptions.JahiaException; 16 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 17 import org.jahia.exceptions.database.JahiaDatabaseException; 18 19 import java.sql.*; 20 import java.util.Properties ; 21 22 23 27 class JahiaGroupDBUtils { 28 static private JahiaGroupDBUtils mObject = null; 29 30 31 private JahiaGroupDBUtils () { 33 } 34 35 36 42 static public JahiaGroupDBUtils getInstance () { 43 if (mObject == null) { 44 mObject = new JahiaGroupDBUtils (); 45 } 46 return mObject; 47 } 48 49 50 58 public synchronized boolean removeProperty (String propertyKey, int grpID, 59 String providerName, String groupKey) 60 throws JahiaDatabaseException, 61 JahiaDatabaseConnectionException, 62 JahiaException { 63 boolean result = false; 64 65 String query = "DELETE FROM jahia_grp_prop WHERE" + 66 " id_jahia_grp=" + grpID + 67 " AND name_jahia_grp_prop='" + propertyKey + "'" + 68 " AND provider_jahia_grp_prop='" + providerName + "'" + 69 " AND grpkey_jahia_grp_prop='" + groupKey + "'"; 70 71 return makeQuery (query); 72 } 73 74 75 85 public synchronized boolean addProperty (String key, String value, 86 int grpID, String providerName, String groupKey) 87 throws JahiaDatabaseException, 88 JahiaDatabaseConnectionException, 89 JahiaException { 90 boolean result = false; 91 92 String query = "INSERT INTO jahia_grp_prop (id_jahia_grp, " + 93 "name_jahia_grp_prop, value_jahia_grp_prop, provider_jahia_grp_prop, grpkey_jahia_grp_prop)" + 94 " VALUES (" + grpID + ",'" + key + "','" + value + "','" + providerName + "','" + groupKey + "')"; 95 96 return makeQuery (query); 97 } 98 99 100 110 public synchronized boolean updateProperty (String key, String value, 111 int grpID, String providerName, 112 String groupKey) 113 throws JahiaDatabaseException, 114 JahiaDatabaseConnectionException, 115 JahiaException { 116 boolean result = false; 117 118 String query = "UPDATE jahia_grp_prop SET " + 119 "value_jahia_grp_prop='" + value + "'" + 120 " WHERE id_jahia_grp=" + grpID + 121 " AND name_jahia_grp_prop='" + key + "'" + 122 " AND provider_jahia_grp_prop='" + providerName + "'" + 123 " AND grpkey_jahia_grp_prop='" + groupKey + "'"; 124 125 return makeQuery (query); 126 } 127 128 public Properties getGroupProperties (int groupID, String providerName, String groupKey) 129 throws JahiaDatabaseException { 130 final String query = "SELECT name_jahia_grp_prop, value_jahia_grp_prop FROM jahia_grp_prop WHERE id_jahia_grp=? AND provider_jahia_grp_prop=? AND grpkey_jahia_grp_prop=?"; 132 133 Properties properties = new Properties (); 134 PreparedStatement statement = null; 136 try { 137 138 Connection dbConn = org.jahia.services.database.ConnectionDispenser. 139 getConnection (); 140 if (dbConn == null) { 141 return null; 142 } 143 statement = dbConn.prepareStatement (query); 145 statement.setInt (1, groupID); 146 statement.setString (2, providerName); 147 statement.setString (3, groupKey); 148 149 ResultSet rs = statement.executeQuery (); 150 151 if (rs != null) { 152 153 String propName = null; 154 String propVal = null; 155 156 while (rs.next ()) { 157 158 propName = rs.getString ("name_jahia_grp_prop"); 159 propVal = rs.getString ("value_jahia_grp_prop"); 160 if (propVal == null) { 161 propVal = ""; 162 } 163 164 if (propName != null) { 165 properties.put (propName, propVal); 166 } 167 } 168 } 169 170 } catch (SQLException sqlEx) { 171 throw new JahiaDatabaseException ( 172 "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 173 } finally { 174 CloseStatement (statement); 175 } 176 return properties; 177 } 178 179 private boolean makeQuery (String query) 184 throws JahiaDatabaseException, 185 JahiaDatabaseConnectionException, 186 JahiaException { 187 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 189 if (dbConn == null) { 190 return false; 191 } 192 193 boolean result = false; 194 Statement statement = null; 195 196 try { 197 statement = dbConn.createStatement (); 198 if (statement != null) { 199 synchronized (this) { 200 statement.executeUpdate (query); 201 result = true; 202 } 203 } 204 } catch (SQLException sqlEx) { 205 throw new JahiaDatabaseException ( 206 "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 207 } finally { 208 CloseStatement (statement); 209 } 210 211 return result; 212 } 213 214 215 private void CloseStatement (Statement statement) 217 throws JahiaDatabaseException { 218 try { 220 if (statement != null) { 221 statement.close (); 222 } 223 } catch (SQLException sqlEx) { 224 throw new JahiaDatabaseException ( 225 "Could not close a statement in JahiaGroupDBUtils", 226 sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 227 } 228 } 229 230 } 231 | Popular Tags |