1 40 41 package org.jahia.services.containers; 42 43 import org.jahia.data.JahiaDBDOMObject; 44 import org.jahia.data.JahiaDOMObject; 45 import org.jahia.exceptions.JahiaException; 46 47 import java.sql.Connection ; 48 import java.sql.ResultSet ; 49 import java.sql.SQLException ; 50 import java.sql.Statement ; 51 import java.util.Enumeration ; 52 import java.util.Properties ; 53 54 64 65 public class JahiaContainerDefPropDB { 66 67 private static org.apache.log4j.Logger logger = 68 org.apache.log4j.Logger.getLogger (JahiaContainerDefPropDB.class); 69 70 73 public JahiaContainerDefPropDB () { 74 } 75 76 88 public Properties getProperties (int containerListID) 89 throws JahiaException { 90 Properties result = new Properties (); 91 Connection dbConn = null; 92 Statement stmt = null; 93 ResultSet rs = null; 94 try { 95 String sqlQuery = "SELECT * FROM jahia_ctndef_prop"; 96 sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID; 97 98 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 99 stmt = dbConn.createStatement (); 100 rs = stmt.executeQuery (sqlQuery); 101 102 while (rs.next ()) { 103 int id = rs.getInt ("id_jahia_ctn_def"); 104 String name = rs.getString ("name_jahia_ctndef_prop"); 105 String value = rs.getString ("value_jahia_ctndef_prop"); 106 result.setProperty (name, value); 107 } 108 109 } catch (SQLException se) { 110 String errorMsg = "Error in JahiaContainerDefPropDB.getProperties() : " + 111 se.getMessage () + " -> BAILING OUT"; 112 logger.warn (errorMsg); 113 throw new JahiaException ("Cannot load container def properties from the database", 114 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 115 } finally { 116 try { 117 118 if (stmt != null) stmt.close (); 119 } catch (SQLException ex) { 120 logger.warn ("cannot free resources", ex); 121 } 122 } 123 return result; 124 125 } 126 127 148 public void setProperties (int containerListID, 149 Properties containerListProperties) 150 throws JahiaException { 151 152 removeProperties (containerListID); 156 157 Connection dbConn = null; 158 Statement stmt = null; 159 try { 160 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 162 stmt = dbConn.createStatement (); 163 164 Enumeration propNames = containerListProperties.keys (); 165 while (propNames.hasMoreElements ()) { 166 String curPropName = (String ) propNames.nextElement (); 167 String curPropValue = containerListProperties.getProperty (curPropName); 168 169 String sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" + 170 containerListID + "," + 171 "'" + curPropName + "'," + 172 "'" + curPropValue + "')"; 173 174 stmt.execute (sqlQuery); 176 } 177 178 } catch (SQLException se) { 179 String errorMsg = "Error in JahiaContainerDefPropDB.setProperties : " + se.getMessage () + " -> BAILING OUT"; 180 logger.warn (errorMsg); 181 throw new JahiaException ( 182 "Cannot create container definition properties in the database", 183 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 184 } finally { 185 try { 186 187 if (stmt != null) stmt.close (); 188 } catch (SQLException ex) { 189 logger.warn ("cannot free resources", ex); 190 } 191 } 192 } 193 194 203 public void removeProperties (int containerListID) 204 throws JahiaException { 205 Connection dbConn = null; 206 Statement stmt = null; 207 try { 208 209 String sqlQuery = "DELETE FROM jahia_ctndef_prop "; 211 sqlQuery += "WHERE id_jahia_ctn_def = " + containerListID; 212 213 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 215 stmt = dbConn.createStatement (); 216 stmt.executeUpdate (sqlQuery); 217 218 } 219 catch (SQLException se) { 221 String errorMsg = "Error in JahiaContainerDefPropDB.removeProperties : " + se.getMessage (); 222 logger.warn (errorMsg + " -> BAILING OUT"); 223 throw new JahiaException ( 224 "Cannot delete container definition properties in the database", 225 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 226 } finally { 227 try { 228 229 if (stmt != null) stmt.close (); 230 } catch (SQLException ex) { 231 logger.warn ("cannot free resources", ex); 232 } 233 } 234 } 235 236 249 public String getProperty (int containerListID, 250 String propertyName) 251 throws JahiaException { 252 String result = null; 253 Connection dbConn = null; 254 Statement stmt = null; 255 ResultSet rs = null; 256 try { 257 String sqlQuery = "SELECT value_jahia_ctndef_prop FROM jahia_ctndef_prop"; 258 sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID; 259 sqlQuery += " AND name_jahia_ctndef_prop='" + propertyName + "'"; 260 261 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 262 stmt = dbConn.createStatement (); 263 rs = stmt.executeQuery (sqlQuery); 264 265 if (rs.next ()) { 266 String value = rs.getString ("value_jahia_ctndef_prop"); 267 result = value; 268 } 269 270 } catch (SQLException se) { 271 String errorMsg = "Error in JahiaContainerListsPropDB.getProperty : " + 272 se.getMessage () + " -> BAILING OUT"; 273 logger.warn (errorMsg); 274 throw new JahiaException ( 275 "Cannot load container definition property from the database", 276 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 277 } finally { 278 try { 279 280 if (stmt != null) stmt.close (); 281 } catch (SQLException ex) { 282 logger.warn ("cannot free resources", ex); 283 } 284 } 285 return result; 286 } 287 288 300 public void setProperty (int containerListID, 301 String propertyName, 302 String propertyValue) 303 throws JahiaException { 304 305 removeProperty (containerListID, propertyName); 306 307 Connection dbConn = null; 308 Statement stmt = null; 309 310 try { 311 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 313 stmt = dbConn.createStatement (); 314 315 String sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" + 316 containerListID + "," + 317 "'" + propertyName + "'," + 318 "'" + propertyValue + "')"; 319 320 stmt.execute (sqlQuery); 322 323 try { 324 325 if (stmt != null) stmt.close (); 326 } catch (SQLException ex) { 327 logger.warn ("cannot free resources", ex); 328 } 329 330 } catch (SQLException se) { 331 String errorMsg = "Error in JahiaContainerDefPropDB.setProperty : " + se.getMessage () + " -> BAILING OUT"; 332 logger.warn (errorMsg); 333 throw new JahiaException ( 334 "Cannot create container definition property in the database", 335 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 336 } finally { 337 try { 338 339 if (stmt != null) stmt.close (); 340 } catch (SQLException ex) { 341 logger.warn ("cannot free resources", ex); 342 } 343 } 344 } 345 346 355 public void removeProperty (int containerListID, 356 String propertyName) 357 throws JahiaException { 358 Connection dbConn = null; 359 Statement stmt = null; 360 try { 361 362 String sqlQuery = "DELETE FROM jahia_ctndef_prop "; 364 sqlQuery += "WHERE id_jahia_ctn_lists = " + containerListID; 365 sqlQuery += " AND name_jahia_ctnlists_prop='" + propertyName + "'"; 366 367 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 369 stmt = dbConn.createStatement (); 370 stmt.executeUpdate (sqlQuery); 371 372 } 373 catch (SQLException se) { 375 String errorMsg = "Error in JahiaContainerDefPropDB.removeProperty : " + se.getMessage (); 376 logger.warn (errorMsg + " -> BAILING OUT"); 377 throw new JahiaException ( 378 "Cannot delete container definition property in the database", 379 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 380 } finally { 381 try { 382 383 if (stmt != null) stmt.close (); 384 } catch (SQLException ex) { 385 logger.warn ("cannot free resources", ex); 386 } 387 } 388 } 389 390 404 public JahiaDOMObject getPropertiesAsDOM (int siteID) 405 throws JahiaException { 406 407 Connection dbConn = null; 408 Statement statement = null; 409 410 JahiaDBDOMObject dom = null; 411 412 try { 413 414 String sqlQuery = "SELECT DISTINCT jahia_ctndef_prop.id_jahia_ctn_def," 415 + "jahia_ctndef_prop.name_jahia_ctndef_prop,jahia_ctndef_prop.value_jahia_ctndef_prop" 416 + " FROM jahia_ctndef_prop,jahia_ctn_def where " 417 + "jahia_ctndef_prop.id_jahia_ctn_def=jahia_ctn_def.id_jahia_ctn_def AND " 418 + "jahia_ctn_def.jahiaid_jahia_ctn_def=" + siteID; 419 420 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 421 statement = dbConn.createStatement (); 422 if (statement != null) { 423 ResultSet rs = statement.executeQuery (sqlQuery); 424 if (rs != null) { 425 dom = new JahiaDBDOMObject (); 426 dom.addTable ("jahia_ctndef_prop", rs); 427 return dom; 428 } 429 } 430 } catch (SQLException se) { 431 String errorMsg = "Error in JahiaContainerDefPropDB.getPropertiesAsDOM : " + se.getMessage (); 432 logger.warn (errorMsg + " -> BAILING OUT"); 433 throw new JahiaException ( 434 "Cannot load container definition properties from the database", 435 errorMsg, JahiaException.DATABASE_ERROR, 436 JahiaException.CRITICAL_SEVERITY); 437 } finally { 438 439 closeStatement (statement); 440 } 441 442 return dom; 443 } 444 445 446 private void closeStatement (Statement statement) { 448 try { 450 if (statement != null) { 451 statement.close (); 452 } 453 } catch (SQLException sqlEx) { 454 logger.warn ("Cannot close a statement", sqlEx); 455 } 456 } 457 458 } | Popular Tags |