1 package org.jahia.services.pages; 2 3 import org.jahia.data.JahiaDBDOMObject; 4 import org.jahia.data.JahiaDOMObject; 5 import org.jahia.exceptions.JahiaException; 6 7 import java.sql.*; 8 import java.util.Enumeration ; 9 import java.util.Properties ; 10 import java.util.Vector ; 11 12 22 23 class JahiaPageDefinitionPropDB { 24 25 private static org.apache.log4j.Logger logger = 26 org.apache.log4j.Logger.getLogger (JahiaPageDefinitionPropDB.class); 27 28 private static final String CLASS_NAME = JahiaPageDefinitionPropDB.class.getName (); 29 30 31 41 public static Properties getProperties (int id) 42 throws JahiaException { 43 Properties result = new Properties (); 44 Connection dbConn = null; 45 Statement stmt = null; 46 ResultSet rs = null; 47 try { 48 String sqlQuery = "SELECT * FROM jahia_pages_def_prop"; 49 sqlQuery += " WHERE id_jahia_pages_def_prop=" + id; 50 51 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 52 stmt = dbConn.createStatement (); 53 rs = stmt.executeQuery (sqlQuery); 54 55 while (rs.next ()) { 56 id = rs.getInt ("id_jahia_pages_def_prop"); 57 int siteID = rs.getInt ("jahiaid_pages_def_prop"); 58 String name = rs.getString ("name_pages_def_prop"); 59 String value = rs.getString ("value_pages_def_prop"); 60 result.setProperty (name, value); 61 } 62 63 } catch (SQLException se) { 64 String errorMsg = "Error in " + CLASS_NAME + ".getProperties() : " + 65 se.getMessage (); 66 logger.error (errorMsg); 67 throw new JahiaException ("Cannot load page def properties from the database", 68 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 69 70 } finally { 71 closeStatement (stmt); 72 } 73 return result; 74 75 } 76 77 78 99 public static void setProperties (JahiaPageDefinition pageDef, Properties props) 100 throws JahiaException { 101 102 if (pageDef == null) 103 return; 104 105 removeProperties (pageDef.getID ()); 109 110 Connection dbConn = null; 111 PreparedStatement pstmt = null; 112 113 try { 114 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 116 117 Enumeration propNames = props.keys (); 118 while (propNames.hasMoreElements ()) { 119 String curPropName = (String ) propNames.nextElement (); 120 String curPropValue = props.getProperty (curPropName); 121 122 pstmt = 123 dbConn.prepareStatement ( 124 "INSERT INTO jahia_pages_def_prop VALUES(?,?,?,?)"); 125 pstmt.setInt (1, pageDef.getID ()); 126 pstmt.setInt (2, pageDef.getJahiaID ()); 127 pstmt.setString (3, curPropName); 128 pstmt.setString (4, curPropValue); 129 pstmt.execute (); 130 } 131 132 } catch (SQLException se) { 133 String errorMsg = "Error in " + CLASS_NAME + ".setProperties : " + se.getMessage (); 134 logger.error (errorMsg); 135 throw new JahiaException ("Cannot create page def properties in the database", 136 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 137 } finally { 138 closeStatement ((Statement) pstmt); 139 } 140 } 141 142 143 151 public static void removeProperties (int id) 152 throws JahiaException { 153 Connection dbConn = null; 154 Statement stmt = null; 155 try { 156 157 String sqlQuery = "DELETE FROM jahia_pages_def_prop "; 159 sqlQuery += "WHERE id_jahia_pages_def_prop = " + id; 160 161 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 163 stmt = dbConn.createStatement (); 164 stmt.executeUpdate (sqlQuery); 165 166 } 167 catch (SQLException se) { 169 String errorMsg = "Error in " + CLASS_NAME + ".removeProperties : " + se.getMessage (); 170 logger.error (errorMsg); 171 throw new JahiaException ("Cannot delete page def properties in the database", 172 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 173 } finally { 174 closeStatement (stmt); 175 } 176 } 177 178 179 192 public static String getProperty (int id, String name) 193 throws JahiaException { 194 String result = null; 195 Connection dbConn = null; 196 PreparedStatement pstmt = null; 197 ResultSet rs = null; 198 try { 199 200 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 201 202 String sqlQuery = "SELECT value_pages_def_prop FROM jahia_pages_def_prop"; 203 sqlQuery += " WHERE id_jahia_pages_def_prop=? AND name_pages_def_prop=?"; 204 205 pstmt = dbConn.prepareStatement (sqlQuery); 206 207 pstmt.setInt (1, id); 208 pstmt.setString (2, name); 209 210 rs = pstmt.executeQuery (); 211 212 if (rs.next ()) { 213 String value = rs.getString ("value_pages_def_prop"); 214 result = value; 215 } 216 217 } catch (SQLException se) { 218 String errorMsg = "Error in " + CLASS_NAME + ".getProperty : " + se.getMessage (); 219 logger.error (errorMsg); 220 throw new JahiaException ("Cannot load page def property from the database", 221 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 222 } finally { 223 closeStatement ((Statement) pstmt); 224 } 225 return result; 226 } 227 228 229 241 public static void setProperty (JahiaPageDefinition pageDef, String name, String value) 242 throws JahiaException { 243 244 if (pageDef == null) 245 return; 246 247 removeProperty (pageDef.getID (), name); 248 249 Connection dbConn = null; 250 PreparedStatement pstmt = null; 251 try { 252 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 254 String sqlQuery = "INSERT INTO jahia_pages_def_prop VALUES(?,?,?,?)"; 255 256 pstmt = dbConn.prepareStatement (sqlQuery); 257 pstmt.setInt (1, pageDef.getID ()); 258 pstmt.setInt (2, pageDef.getJahiaID ()); 259 pstmt.setString (3, name); 260 pstmt.setString (4, value); 261 262 pstmt.execute (sqlQuery); 264 265 266 closeStatement ((Statement) pstmt); 267 268 } catch (SQLException se) { 269 String errorMsg = "Error in " + CLASS_NAME + ".setProperty : " + se.getMessage (); 270 logger.error (errorMsg); 271 throw new JahiaException ("Cannot create page def property in the database", 272 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 273 } finally { 274 closeStatement ((Statement) pstmt); 275 } 276 } 277 278 279 288 public static void removeProperty (int id, String name) 289 throws JahiaException { 290 Connection dbConn = null; 291 PreparedStatement pstmt = null; 292 try { 293 294 String sqlQuery = "DELETE FROM jahia_pages_def_prop WHERE id_jahia_pages_def_prop = ? AND name_pages_def_prop = ?"; 296 297 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 299 pstmt = dbConn.prepareStatement (sqlQuery); 300 pstmt.setInt (1, id); 301 pstmt.setString (2, name); 302 pstmt.executeUpdate (); 303 304 } 305 catch (SQLException se) { 307 String errorMsg = "Error in " + CLASS_NAME + ".removeProperty : " + se.getMessage (); 308 logger.error (errorMsg); 309 throw new JahiaException ("Cannot delete page def property in the database", 310 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 311 } finally { 312 closeStatement ((Statement) pstmt); 313 } 314 } 315 316 317 329 public static JahiaDOMObject getPageDefPropsAsDOM (int siteID) 330 throws JahiaException { 331 332 Connection dbConn = null; 333 Statement statement = null; 334 335 JahiaDBDOMObject dom = null; 336 337 try { 338 339 String sqlQuery = "SELECT * FROM jahia_pages_def_prop WHERE jahiaid_pages_def_prop=" + siteID; 340 341 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 342 statement = dbConn.createStatement (); 343 if (statement != null) { 344 ResultSet rs = statement.executeQuery (sqlQuery); 345 if (rs != null) { 346 dom = new JahiaDBDOMObject (); 347 dom.addTable ("jahia_pages_def_prop", rs); 348 return dom; 349 } 350 } 351 } catch (SQLException se) { 352 String errorMsg = "Error in " + CLASS_NAME + ".getPropertiesAsDOM : " + se.getMessage (); 353 logger.error (errorMsg); 354 throw new JahiaException ("Error loading pages def properties from the database", 355 errorMsg, JahiaException.DATABASE_ERROR, 356 JahiaException.CRITICAL_SEVERITY); 357 } finally { 358 closeStatement (statement); 359 } 360 361 return dom; 362 } 363 364 369 public static Vector db_get_all_acl_id (int siteID) 370 throws JahiaException { 371 Connection dbConn = null; 372 Statement stmt = null; 373 ResultSet rs = null; 374 Vector theIDs = new Vector (); 375 try { 376 String sqlQuery = "SELECT DISTINCT value_pages_def_prop FROM jahia_pages_def_prop " 377 + "WHERE jahiaid_pages_def_prop=" + siteID + " AND name_pages_def_prop='" + JahiaPageDefinition.ACLID_PROP + "'"; 378 379 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 380 stmt = dbConn.createStatement (); 381 rs = stmt.executeQuery (sqlQuery); 382 383 String value = null; 384 while (rs.next ()) { 385 value = rs.getString ("value_pages_def_prop"); 386 if (value != null) { 387 try { 388 theIDs.add (new Integer (value)); 389 } catch (Throwable t) { 390 t.printStackTrace (); 391 } 392 } 393 } 394 } catch (SQLException se) { 395 String errorMsg = "Error in db_get_all_acl_id : " + se.getMessage (); 396 logger.error (errorMsg); 397 throw new JahiaException ("Cannot load acl id from the database", 398 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 399 } finally { 400 closeStatement (stmt); 401 } 402 return theIDs; 403 404 } 405 406 407 private static void closeStatement (Statement statement) { 408 try { 409 if (statement != null) { 410 statement.close (); 411 } 412 } catch (SQLException sqlEx) { 413 logger.warn("Cannot close a statement", sqlEx); 414 } 415 } 416 417 } | Popular Tags |