1 17 package org.jahia.services.containers; 18 19 import org.jahia.data.JahiaDBDOMObject; 20 import org.jahia.data.JahiaDOMObject; 21 import org.jahia.exceptions.JahiaException; 22 23 import java.sql.*; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import java.util.Vector ; 27 28 38 39 public class JahiaContainerListPropDB { 40 41 final private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger (JahiaContainerListPropDB.class); 43 44 45 48 public JahiaContainerListPropDB () { 49 } 50 51 63 public Properties getProperties (int containerListID) 64 throws JahiaException { 65 Properties result = new Properties (); 66 Connection dbConn = null; 67 PreparedStatement stmt = null; 68 ResultSet rs = null; 69 try { 70 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 71 72 stmt = dbConn.prepareStatement ("SELECT * FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop=?"); 73 stmt.setInt(1, containerListID); 74 rs = stmt.executeQuery (); 75 76 while (rs.next ()) { 77 int id = rs.getInt ("ctnlistid_ctnlists_prop"); 78 int siteID = rs.getInt ("jahiaid_ctnlists_prop"); 79 String name = rs.getString ("name_ctnlists_prop"); 80 String value = rs.getString ("value_ctnlists_prop"); 81 result.setProperty (name, value); 82 } 83 84 rs.close(); 85 86 } catch (SQLException se) { 87 String errorMsg = "Cannot load container list properties from the database"; 88 logger.warn (errorMsg, se); 89 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 90 JahiaException.CRITICAL_SEVERITY, se); 91 92 } finally { 93 closeStatement (stmt); 94 } 95 return result; 96 97 } 98 99 121 public void setProperties (int containerListID, 122 int jahiaID, 123 Properties containerListProperties) 124 throws JahiaException { 125 126 removeProperties (containerListID); 130 131 Connection dbConn = null; 132 PreparedStatement pstmt = null; 133 try { 134 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 136 137 Enumeration propNames = containerListProperties.keys (); 138 while (propNames.hasMoreElements ()) { 139 String curPropName = (String ) propNames.nextElement (); 140 String curPropValue = containerListProperties.getProperty (curPropName); 141 142 pstmt = 143 dbConn.prepareStatement ( 144 "INSERT INTO jahia_ctnlists_prop VALUES(?,?,?,?)"); 145 pstmt.setInt (1, containerListID); 146 pstmt.setInt (2, jahiaID); 147 pstmt.setString (3, curPropName); 148 pstmt.setString (4, curPropValue); 149 pstmt.execute (); 150 } 151 152 } catch (SQLException se) { 153 String errorMsg = "Cannot set container list properties in the database"; 154 logger.warn (errorMsg, se); 155 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 156 JahiaException.CRITICAL_SEVERITY, se); 157 158 } finally { 159 closeStatement (pstmt); 160 } 161 } 162 163 172 public void removeProperties (int containerListID) 173 throws JahiaException { 174 Connection dbConn = null; 175 Statement stmt = null; 176 try { 177 178 String sqlQuery = 180 "DELETE FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop = " + 181 containerListID; 182 183 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 185 stmt = dbConn.createStatement (); 186 stmt.executeUpdate (sqlQuery); 187 188 } 189 catch (SQLException se) { 191 String errorMsg = "Cannot remove container list properties in the database"; 192 logger.warn (errorMsg, se); 193 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 194 JahiaException.CRITICAL_SEVERITY, se); 195 } finally { 196 197 closeStatement (stmt); 198 } 199 } 200 201 214 public String getProperty (int containerListID, String propertyName) 215 throws JahiaException { 216 String result = null; 217 Connection dbConn = null; 218 PreparedStatement pstmt = null; 219 ResultSet rs = null; 220 try { 221 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 222 String sqlQuery = "SELECT value_ctnlists_prop FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop=? AND name_ctnlists_prop=?"; 223 pstmt = dbConn.prepareStatement (sqlQuery); 224 225 pstmt.setInt (1, containerListID); 226 pstmt.setString (2, propertyName); 227 228 rs = pstmt.executeQuery (); 229 230 if (rs.next ()) { 231 String value = rs.getString ("value_ctnlists_prop"); 232 result = value; 233 } 234 235 } catch (SQLException se) { 236 String errorMsg = "Cannot load container list properties in the database"; 237 logger.warn (errorMsg, se); 238 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 239 JahiaException.CRITICAL_SEVERITY, se); 240 241 } finally { 242 closeStatement (pstmt); 243 } 244 return result; 245 } 246 247 260 public void setProperty (int containerListID, 261 int jahiaID, 262 String propertyName, 263 String propertyValue) 264 throws JahiaException { 265 266 removeProperty (containerListID, propertyName); 267 268 Connection dbConn = null; 269 PreparedStatement pstmt = null; 270 try { 271 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 273 274 pstmt = 275 dbConn.prepareStatement ("INSERT INTO jahia_ctnlists_prop VALUES(?,?,?,?)"); 276 pstmt.setInt (1, containerListID); 277 pstmt.setInt (2, jahiaID); 278 pstmt.setString (3, propertyName); 279 pstmt.setString (4, propertyValue); 280 pstmt.execute (); 281 282 } catch (SQLException se) { 283 String errorMsg = "Cannot load container list properties in the database"; 284 logger.warn (errorMsg, se); 285 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 286 JahiaException.CRITICAL_SEVERITY, se); 287 288 } finally { 289 closeStatement (pstmt); 290 } 291 } 292 293 302 public void removeProperty (int containerListID, 303 String propertyName) 304 throws JahiaException { 305 Connection dbConn = null; 306 PreparedStatement pstmt = null; 307 try { 308 309 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 310 311 String sqlQuery = "DELETE FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop = ? AND name_jahia_ctnlists_prop=?"; 313 314 pstmt = dbConn.prepareStatement (sqlQuery); 316 pstmt.setInt (1, containerListID); 317 pstmt.setString (2, propertyName); 318 pstmt.executeUpdate (); 319 } 320 catch (SQLException se) { 322 String errorMsg = "Cannot delete container list properties in the database"; 323 logger.warn (errorMsg, se); 324 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 325 JahiaException.CRITICAL_SEVERITY, se); 326 327 } finally { 328 closeStatement (pstmt); 329 } 330 } 331 332 333 346 public JahiaDOMObject getPropertiesAsDOM (int siteID) 347 throws JahiaException { 348 Connection dbConn = null; 349 Statement statement = null; 350 JahiaDBDOMObject dom = null; 351 352 try { 353 String sqlQuery = "SELECT DISTINCT * FROM jahia_ctnlists_prop WHERE jahiaid_ctnlists_prop=" + siteID; 354 355 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 356 statement = dbConn.createStatement (); 357 if (statement != null) { 358 ResultSet rs = statement.executeQuery (sqlQuery); 359 if (rs != null) { 360 dom = new JahiaDBDOMObject (); 361 dom.addTable ("jahia_ctnlists_prop", rs); 362 return dom; 363 } 364 } 365 } catch (SQLException se) { 366 String errorMsg = "Cannot load container list properties in the database"; 367 logger.warn (errorMsg, se); 368 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 369 JahiaException.CRITICAL_SEVERITY, se); 370 371 } finally { 372 closeStatement (statement); 373 } 374 375 return dom; 376 } 377 378 379 390 public Vector getCtnListFieldACLs (int siteID) 391 throws JahiaException { 392 393 Connection dbConn = null; 394 Statement statement = null; 395 Vector ids = new Vector (); 396 397 try { 398 399 String sqlQuery = 400 "SELECT DISTINCT name_ctnlists_prop,value_ctnlists_prop FROM jahia_ctnlists_prop WHERE jahiaid_ctnlists_prop=" + 401 siteID; 402 403 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 404 statement = dbConn.createStatement (); 405 if (statement != null) { 406 ResultSet rs = statement.executeQuery (sqlQuery); 407 String name = ""; 408 String value = ""; 409 while (rs.next ()) { 410 name = rs.getString ("name_ctnlists_prop"); 411 value = rs.getString ("value_ctnlists_prop"); 412 if ((value != null) && (!value.trim ().equals ("")) && (name != null) && (name.startsWith ( 413 "view_field_acl_"))) { 414 try { 415 ids.add (new Integer (value)); 416 } catch (Throwable t) { 417 } 418 } 419 } 420 } 421 } catch (SQLException se) { 422 String errorMsg = "Cannot load container list properties in the database"; 423 logger.warn (errorMsg, se); 424 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 425 JahiaException.CRITICAL_SEVERITY, se); 426 427 } finally { 428 closeStatement (statement); 429 } 430 431 return ids; 432 } 433 434 435 private void closeStatement (Statement statement) { 436 try { 438 if (statement != null) { 439 statement.close (); 440 } 441 } catch (SQLException sqlEx) { 442 logger.warn ("Cannot close a statement", sqlEx); 443 } 444 } 445 446 } | Popular Tags |