1 38 39 48 49 50 package org.jahia.services.containers; 51 52 import org.jahia.data.JahiaDBDOMObject; 53 import org.jahia.data.JahiaDOMObject; 54 import org.jahia.data.containers.JahiaContainerDefinition; 55 import org.jahia.data.containers.JahiaContainerSubDefinition; 56 import org.jahia.exceptions.JahiaException; 57 import org.jahia.registries.ServicesRegistry; 58 import org.jahia.services.database.ConnectionDispenser; 59 import org.jahia.utils.JahiaTools; 60 61 import java.sql.Connection ; 62 import java.sql.PreparedStatement ; 63 import java.sql.ResultSet ; 64 import java.sql.SQLException ; 65 import java.sql.Statement ; 66 import java.util.Enumeration ; 67 import java.util.Hashtable ; 68 import java.util.Vector ; 69 70 71 public class JahiaContainerDefinitionsDB { 72 final private static org.apache.log4j.Logger logger = 73 org.apache.log4j.Logger.getLogger (JahiaContainerDefinitionsDB.class); 74 75 private JahiaContainerStructuresDB c_struct; 76 77 78 81 public JahiaContainerDefinitionsDB () { 82 c_struct = new JahiaContainerStructuresDB (); 83 } 84 85 86 95 public JahiaContainerDefinition db_load_container_definition (int defID) 96 throws JahiaException { 97 Connection dbConn = null; 98 Statement stmt = null; 99 Statement stmt2 = null; 100 ResultSet rs = null; 101 ResultSet rs2 = null; 102 JahiaContainerDefinition theDef = null; 103 try { 104 105 dbConn = ConnectionDispenser.getConnection (); 107 stmt = dbConn.createStatement (); 108 stmt2 = dbConn.createStatement (); 109 110 String sqlQuery = "SELECT * FROM jahia_ctn_def WHERE id_jahia_ctn_def = " + defID; 112 rs = stmt.executeQuery (sqlQuery); 113 if (rs.next ()) { 114 int ID = rs.getInt ("id_jahia_ctn_def"); 115 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_def"); 116 String name = rs.getString ("name_jahia_ctn_def"); 117 118 sqlQuery = "SELECT * FROM jahia_ctn_def_properties "; 119 sqlQuery += "WHERE ctndefid_jahia_ctn_def_prop=" + ID; 120 rs2 = stmt2.executeQuery (sqlQuery); 121 122 Hashtable subDefs = new Hashtable (); 123 124 while (rs2.next ()) { 125 int subDefID = rs2.getInt ("id_jahia_ctn_def_properties"); 126 int templateID = rs2.getInt ("pagedefid_jahia_ctn_def_prop"); 127 String title = rs2.getString ("title_jahia_ctn_def_properties"); 128 Vector structure = c_struct.db_load_container_structure (subDefID); 129 subDefs.put (new Integer (templateID), 130 new JahiaContainerSubDefinition (subDefID, templateID, title, 131 structure)); 132 } 133 134 theDef = new JahiaContainerDefinition (ID, jahiaID, name, subDefs); 135 } 136 137 } catch (SQLException se) { 138 String errorMsg = "Error in db_load_container_definition : " + se.getMessage () + " -> BAILING OUT"; 139 logger.error (errorMsg); 140 throw new JahiaException ("Cannot load fields from the database", 141 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 142 } finally { 143 closeStatement(stmt); 144 closeStatement(stmt2); 145 } 146 return theDef; 147 } 148 149 150 159 public JahiaContainerDefinition db_load_container_definition (int siteID, String defName) 160 throws JahiaException { 161 Connection dbConn = null; 162 Statement stmt = null; 163 Statement stmt2 = null; 164 ResultSet rs = null; 165 ResultSet rs2 = null; 166 JahiaContainerDefinition theDef = null; 167 try { 168 169 dbConn = ConnectionDispenser.getConnection (); 171 stmt = dbConn.createStatement (); 172 stmt2 = dbConn.createStatement (); 173 174 String sqlQuery = "SELECT * FROM jahia_ctn_def WHERE jahiaid_jahia_ctn_def = " + 176 siteID + " and name_jahia_ctn_def='" + defName + "'"; 177 rs = stmt.executeQuery (sqlQuery); 178 if (rs.next ()) { 179 int ID = rs.getInt ("id_jahia_ctn_def"); 180 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_def"); 181 String name = rs.getString ("name_jahia_ctn_def"); 182 183 sqlQuery = "SELECT * FROM jahia_ctn_def_properties "; 184 sqlQuery += "WHERE ctndefid_jahia_ctn_def_prop=" + ID; 185 rs2 = stmt2.executeQuery (sqlQuery); 186 187 Hashtable subDefs = new Hashtable (); 188 189 while (rs2.next ()) { 190 int subDefID = rs2.getInt ("id_jahia_ctn_def_properties"); 191 int templateID = rs2.getInt ("pagedefid_jahia_ctn_def_prop"); 192 String title = rs2.getString ("title_jahia_ctn_def_properties"); 193 Vector structure = c_struct.db_load_container_structure (subDefID); 194 subDefs.put (new Integer (templateID), 195 new JahiaContainerSubDefinition (subDefID, templateID, title, 196 structure)); 197 } 198 199 theDef = new JahiaContainerDefinition (ID, jahiaID, name, subDefs); 200 } 201 202 } catch (SQLException se) { 203 String errorMsg = "Error in db_load_container_definition : " + se.getMessage () + " -> BAILING OUT"; 204 throw new JahiaException ("Cannot load fields from the database", 205 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, 206 se); 207 } finally { 208 closeStatement(stmt); 209 closeStatement(stmt2); 210 } 211 return theDef; 212 } 213 214 215 224 public void db_create_container_definition (JahiaContainerDefinition theDef) 225 throws JahiaException { 226 Connection dbConn = null; 227 PreparedStatement stmt = null; 228 try { 229 int theDefID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ( 231 ).autoIncrement ("jahia_ctn_def"); 232 theDef.setID (theDefID); 233 String sqlQuery = 235 "INSERT INTO jahia_ctn_def (id_jahia_ctn_def,jahiaid_jahia_ctn_def,name_jahia_ctn_def) VALUES(?,?,?)"; 236 237 dbConn = ConnectionDispenser.getConnection (); 239 stmt = dbConn.prepareStatement (sqlQuery); 240 stmt.setInt (1, theDef.getID ()); 241 stmt.setInt (2, theDef.getJahiaID ()); 242 stmt.setString (3, theDef.getName ()); 243 stmt.executeUpdate (); 244 245 db_update_container_defprop (theDef); 247 } 248 catch (SQLException se) { 250 String errorMsg = "Error in db_create_container_definition : " + se.getMessage (); 251 logger.error (errorMsg); 252 throw new JahiaException ( 253 "Cannot insert new container definitions in the database", 254 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 255 256 } finally { 257 closeStatement(stmt); 258 } 259 } 260 261 270 public void db_create_container_defprop(JahiaContainerDefinition theDef) throws JahiaException { 271 Connection dbConn = null; 272 PreparedStatement stmt = null; 273 try { 274 dbConn = ConnectionDispenser.getConnection (); 275 String sqlQuery = "INSERT INTO jahia_ctn_def_properties (id_jahia_ctn_def_properties,ctndefid_jahia_ctn_def_prop,pagedefid_jahia_ctn_def_prop,title_jahia_ctn_def_properties) VALUES(?,?,?,?)"; 276 277 stmt = dbConn.prepareStatement (sqlQuery); 278 Hashtable subDefs = theDef.getSubDefs(); 280 Enumeration subKeys = subDefs.keys(); 281 while (subKeys.hasMoreElements()) { 282 int pageDefID = ((Integer ) subKeys.nextElement()).intValue(); 283 JahiaContainerSubDefinition theSubDef = (JahiaContainerSubDefinition) subDefs 284 .get(new Integer (pageDefID)); 285 286 if (theSubDef.getID() == 0) { 288 int theSubDefID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( 289 "jahia_ctn_def_properties"); 290 theSubDef.setID(theSubDefID); 291 } 292 293 if (logger.isDebugEnabled()) { 294 StringBuffer buffer = new StringBuffer ("Updating sub container def ["); 295 buffer.append(theSubDef.getTitle()); 296 buffer.append(","); 297 buffer.append(theSubDef.getID()); 298 buffer.append("] for container def "); 299 buffer.append(theDef.getID()); 300 logger.debug(buffer.toString()); 301 } 302 303 if (!theSubDef.getTitle().equals("")) { 304 stmt.setInt(1, theSubDef.getID()); 306 stmt.setInt(2, theDef.getID()); 307 stmt.setInt(3, pageDefID); 308 stmt.setString(4, JahiaTools.quote(theSubDef.getTitle())); 309 310 stmt.executeUpdate(); 311 } 312 313 c_struct.db_update_container_structure(theSubDef); 315 } 316 } catch (SQLException se) { 317 String errorMsg = "Error in db_update_container_definition : " + se.getMessage(); 318 logger.error(errorMsg); 319 throw new JahiaException("Cannot update container definitions in the database", errorMsg, 320 JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 321 } finally { 322 closeStatement(stmt); 323 } 324 325 } 326 327 336 public void db_update_container_defprop (JahiaContainerDefinition theDef) 337 throws JahiaException { 338 Connection dbConn = null; 339 PreparedStatement stmt = null; 340 String sqlQuery = ""; 341 try { 342 dbConn = ConnectionDispenser.getConnection (); 344 sqlQuery = "DELETE FROM jahia_ctn_def_properties WHERE ctndefid_jahia_ctn_def_prop=?"; 345 346 stmt = dbConn.prepareStatement (sqlQuery); 347 stmt.setInt(1,theDef.getID()); 348 349 stmt.executeUpdate (); 351 352 db_create_container_defprop(theDef); 353 } 354 catch (SQLException se) { 356 String errorMsg = "Error in db_update_container_definition : " + se.getMessage (); 357 logger.error (errorMsg); 358 throw new JahiaException ("Cannot update container definitions in the database", 359 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 360 } finally { 361 closeStatement(stmt); 362 } 363 } 364 365 366 374 public void db_delete_container_definition (int theDefID) 375 throws JahiaException { 376 Connection dbConn = null; 377 Statement stmt = null; 378 try { 379 String sqlQuery = "DELETE FROM jahia_ctn_def WHERE id_jahia_ctn_def=" + theDefID; 381 382 dbConn = ConnectionDispenser.getConnection (); 384 stmt = dbConn.createStatement (); 385 stmt.executeUpdate (sqlQuery); 386 387 388 } 389 catch (SQLException se) { 391 String errorMsg = "Error in db_delete_container_definition : " + se.getMessage (); 392 logger.error (errorMsg); 393 throw new JahiaException ("Cannot delete container definitions in the database", 394 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 395 } finally { 396 closeStatement(stmt); 397 } 398 } 399 400 401 409 public void db_delete_container_sub_definition (int ctnDefID) 410 throws JahiaException { 411 Connection dbConn = null; 412 Statement stmt = null; 413 try { 414 String sqlQuery = 416 "DELETE FROM jahia_ctn_def_properties WHERE ctndefid_jahia_ctn_def_prop=" + 417 ctnDefID; 418 419 dbConn = ConnectionDispenser.getConnection (); 421 stmt = dbConn.createStatement (); 422 stmt.executeUpdate (sqlQuery); 423 } 424 catch (SQLException se) { 426 String errorMsg = "Error in db_delete_container_sub_definition : " + se.getMessage (); 427 logger.error (errorMsg); 428 throw new JahiaException ( 429 "Cannot delete container sub definitions in the database", 430 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 431 } finally { 432 closeStatement(stmt); 433 } 434 } 435 436 437 444 public JahiaDOMObject getContainerDefsAsDOM (int siteID) 445 throws JahiaException { 446 447 Connection dbConn = null; 448 Statement statement = null; 449 JahiaDBDOMObject dom = null; 450 451 try { 452 String sqlQuery = "SELECT * FROM jahia_ctn_def where jahiaid_jahia_ctn_def=" + siteID; 453 454 dbConn = ConnectionDispenser.getConnection (); 455 statement = dbConn.createStatement (); 456 if (statement != null) { 457 ResultSet rs = statement.executeQuery (sqlQuery); 458 if (rs != null) { 459 dom = new JahiaDBDOMObject (); 460 dom.addTable ("jahia_ctn_def", rs); 461 return dom; 462 } 463 } 464 } catch (SQLException se) { 465 String errorMsg = "Error in getContainerDefsAsDOM(int siteID) : " + se.getMessage (); 466 logger.error (errorMsg); 467 throw new JahiaException ("Cannot load container defs from the database", 468 errorMsg, JahiaException.DATABASE_ERROR, 469 JahiaException.CRITICAL_SEVERITY); 470 } finally { 471 472 closeStatement (statement); 473 } 474 475 return dom; 476 } 477 478 479 486 public JahiaDOMObject getContainerDefPropsAsDOM (int siteID) 487 throws JahiaException { 488 489 Connection dbConn = null; 490 Statement statement = null; 491 492 JahiaDBDOMObject dom = null; 493 494 try { 495 496 String sqlQuery = "SELECT DISTINCT jahia_ctn_def_properties.id_jahia_ctn_def_properties,jahia_ctn_def_properties.ctndefid_jahia_ctn_def_prop,jahia_ctn_def_properties.pagedefid_jahia_ctn_def_prop,jahia_ctn_def_properties.title_jahia_ctn_def_properties FROM jahia_ctn_def_properties,jahia_ctn_def where jahia_ctn_def_properties.ctndefid_jahia_ctn_def_prop=jahia_ctn_def.id_jahia_ctn_def AND jahia_ctn_def.jahiaid_jahia_ctn_def=" + 497 siteID; 498 499 dbConn = ConnectionDispenser.getConnection (); 500 statement = dbConn.createStatement (); 501 if (statement != null) { 502 ResultSet rs = statement.executeQuery (sqlQuery); 503 if (rs != null) { 504 dom = new JahiaDBDOMObject (); 505 dom.addTable ("jahia_ctn_def_properties", rs); 506 return dom; 507 } 508 } 509 } catch (SQLException se) { 510 String errorMsg = "Error in getContainerDefPropsAsDOM(int siteID) : " + se.getMessage (); 511 logger.error (errorMsg); 512 throw new JahiaException ("Cannot load container defs prop from the database", 513 errorMsg, JahiaException.DATABASE_ERROR, 514 JahiaException.CRITICAL_SEVERITY); 515 } finally { 516 517 closeStatement (statement); 518 } 519 520 return dom; 521 } 522 523 524 private void closeStatement (Statement statement) { 525 try { 527 if (statement != null) { 528 statement.close (); 529 } 530 531 } catch (SQLException sqlEx) { 532 logger.warn ("Cannot close a statement", sqlEx); 533 } 534 } 535 536 } 537 | Popular Tags |