1 13 package org.jahia.services.containers; 14 15 import org.jahia.data.JahiaDBDOMObject; 16 import org.jahia.data.JahiaDOMObject; 17 import org.jahia.data.containers.JahiaContainerList; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.registries.ServicesRegistry; 20 import org.jahia.services.database.ConnectionDispenser; 21 import org.jahia.services.version.EntryLoadRequest; 22 import org.jahia.services.version.JahiaSaveVersion; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.sql.PreparedStatement ; 29 30 31 37 public class JahiaContainerListsDB { 38 39 final private static org.apache.log4j.Logger logger = 40 org.apache.log4j.Logger.getLogger (JahiaContainerListsDB.class); 41 42 43 46 public JahiaContainerListsDB () { 47 } 48 49 50 61 public JahiaContainerList db_load_container_list (int listID, EntryLoadRequest loadVersion) 62 throws JahiaException { 63 Connection dbConn = null; 64 PreparedStatement stmt = null; 65 ResultSet rs = null; 66 JahiaContainerList theList = null; 67 try { 68 dbConn = ConnectionDispenser.getConnection (); 69 70 if (loadVersion.isStaging ()) { 72 stmt = dbConn.prepareStatement("SELECT * FROM jahia_ctn_lists WHERE (id_jahia_ctn_lists=? AND workflow_state>0) ORDER BY workflow_state DESC"); 73 stmt.setInt(1, listID); 74 } 75 76 if (loadVersion.isVersioned ()) { 78 stmt = dbConn.prepareStatement("SELECT * FROM jahia_ctn_lists WHERE (id_jahia_ctn_lists=? AND workflow_state<2 AND version_id<=?) ORDER BY version_id DESC"); 79 stmt.setInt(1, listID); 80 stmt.setInt(2, loadVersion.getVersionID()); 81 } 82 83 if (stmt == null) { 84 stmt = dbConn.prepareStatement("SELECT * FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=?"); 85 stmt.setInt(1, listID); 86 } 87 88 rs = stmt.executeQuery (); 89 90 if (rs.next ()) { 91 int id = rs.getInt ("id_jahia_ctn_lists"); 92 int parentid = rs.getInt ("parententryid_jahia_ctn_lists"); 93 int pageid = rs.getInt ("pageid_jahia_ctn_lists"); 94 int defid = rs.getInt ("ctndefid_jahia_ctn_lists"); 95 int rights = rs.getInt ("rights_jahia_ctn_lists"); 96 theList = new JahiaContainerList (id, parentid, pageid, defid, rights); 97 } 98 rs.close(); 99 100 } catch (SQLException se) { 101 String errorMsg = "Cannot load containers from the database -> BAILING OUT"; 102 logger.warn (errorMsg, se); 103 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 104 JahiaException.CRITICAL_SEVERITY, se); 105 106 } finally { 107 try { 108 if (stmt != null) 109 stmt.close (); 110 111 } catch (SQLException ex) { 112 logger.warn ("Cannot free resources", ex); 113 } 114 } 115 return theList; 116 } 117 118 119 128 public void db_create_container_list2 (JahiaContainerList theContainerList, 129 JahiaSaveVersion saveVersion) 130 throws JahiaException { 131 Connection dbConn = null; 132 Statement stmt = null; 133 try { 134 dbConn = ConnectionDispenser.getConnection (); 136 stmt = dbConn.createStatement (); 137 138 StringBuffer sqlQuery = new StringBuffer ( 140 "INSERT INTO jahia_ctn_lists(id_jahia_ctn_lists,parententryid_jahia_ctn_lists,pageid_jahia_ctn_lists,ctndefid_jahia_ctn_lists,rights_jahia_ctn_lists,workflow_state,version_id) VALUES ("); 141 sqlQuery.append (theContainerList.getID ()); 142 sqlQuery.append (","); 143 sqlQuery.append (theContainerList.getParentEntryID ()); 144 sqlQuery.append (","); 145 sqlQuery.append (theContainerList.getPageID ()); 146 sqlQuery.append (","); 147 sqlQuery.append (theContainerList.getctndefid ()); 148 sqlQuery.append (","); 149 sqlQuery.append (theContainerList.getAclID ()); 150 sqlQuery.append (","); 151 sqlQuery.append (saveVersion.getWorkflowState ()); 152 sqlQuery.append (","); 153 sqlQuery.append ( 154 ((saveVersion.getWorkflowState () > 1) ? 0 : saveVersion.getVersionID ())); 155 sqlQuery.append (")"); 156 157 stmt.execute (sqlQuery.toString ()); 159 160 } catch (SQLException se) { 161 String errorMsg = "Cannot create container in the database"; 162 logger.warn (errorMsg, se); 163 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 164 JahiaException.CRITICAL_SEVERITY, se); 165 } finally { 166 try { 167 168 if (stmt != null) stmt.close (); 169 } catch (SQLException ex) { 170 logger.warn ("Cannot free resources", ex); 171 } 172 } 173 } 174 175 176 185 public void db_create_container_list (JahiaContainerList theContainerList, 186 JahiaSaveVersion saveVersion) 187 throws JahiaException { 188 int theListID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 190 .autoIncrement ("jahia_ctn_lists"); 191 theContainerList.setID (theListID); 192 db_create_container_list2 (theContainerList, saveVersion); 193 } 194 195 196 204 public void db_update_container_list (JahiaContainerList theContainerList, 205 JahiaSaveVersion saveVersion) 206 throws JahiaException { 207 Connection dbConn = null; 208 Statement stmt = null; 209 210 try { 211 212 dbConn = ConnectionDispenser.getConnection (); 213 stmt = dbConn.createStatement (); 214 215 int ctnListID = theContainerList.getID (); 216 217 if (saveVersion.isStaging ()) { 219 StringBuffer sqlQuery = new StringBuffer ( 221 "UPDATE jahia_ctn_lists SET parententryid_jahia_ctn_lists = "); 222 sqlQuery.append (theContainerList.getParentEntryID ()); 223 sqlQuery.append (","); 224 sqlQuery.append ("pageid_jahia_ctn_lists = "); 225 sqlQuery.append (theContainerList.getPageID ()); 226 sqlQuery.append (","); 227 sqlQuery.append ("ctndefid_jahia_ctn_lists = "); 228 sqlQuery.append (theContainerList.getctndefid ()); 229 sqlQuery.append (","); 230 sqlQuery.append ("rights_jahia_ctn_lists = "); 231 sqlQuery.append (theContainerList.getAclID ()); 232 sqlQuery.append (","); 233 sqlQuery.append ("version_id = 0 "); sqlQuery.append ("WHERE id_jahia_ctn_lists = "); 235 sqlQuery.append (theContainerList.getID ()); 236 sqlQuery.append (" "); 237 sqlQuery.append ("AND workflow_state>1"); 238 239 int rows = stmt.executeUpdate (sqlQuery.toString ()); 241 242 if (rows == 0) { 244 createStagedContainerList (stmt, ctnListID, saveVersion); 245 stmt.executeUpdate (sqlQuery.toString ()); 247 } 248 249 } else { 251 if (saveVersion.isVersioned ()) { 252 backupContainerListVersion (stmt, ctnListID); 253 } 254 StringBuffer sqlQuery = new StringBuffer ( 256 "UPDATE jahia_ctn_lists SET parententryid_jahia_ctn_lists = "); 257 sqlQuery.append (theContainerList.getParentEntryID ()); 258 sqlQuery.append (","); 259 sqlQuery.append ("pageid_jahia_ctn_lists = "); 260 sqlQuery.append (theContainerList.getPageID ()); 261 sqlQuery.append (","); 262 sqlQuery.append ("ctndefid_jahia_ctn_lists = "); 263 sqlQuery.append (theContainerList.getctndefid ()); 264 sqlQuery.append (","); 265 sqlQuery.append ("rights_jahia_ctn_lists = "); 266 sqlQuery.append (theContainerList.getAclID ()); 267 sqlQuery.append (","); 268 sqlQuery.append ("version_id = "); 269 sqlQuery.append (saveVersion.getVersionID ()); 270 sqlQuery.append (" "); 271 sqlQuery.append ("WHERE id_jahia_ctn_lists = "); 272 sqlQuery.append (theContainerList.getID ()); 273 sqlQuery.append (" "); 274 sqlQuery.append ("AND workflow_state=1"); 275 stmt.executeUpdate (sqlQuery.toString ()); 277 278 } 280 281 } catch (SQLException se) { 283 String errorMsg = "Cannot update containers 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 try { 290 if (stmt != null) 291 stmt.close (); 292 293 } catch (SQLException ex) { 294 logger.warn ("Cannot free resources", ex); 295 } 296 } 297 } 298 299 300 308 public void db_delete_container_list (int listID, JahiaSaveVersion saveVersion) 309 throws JahiaException { 310 ContentContainerList containerList = 311 ContentContainerList.getContainerList (listID); 312 313 Connection dbConn = null; 314 Statement stmt = null; 315 try { 316 dbConn = ConnectionDispenser.getConnection (); 317 stmt = dbConn.createStatement (); 318 319 if (saveVersion.isStaging ()) { 321 if (containerList != null && containerList.hasActiveEntries ()) { 322 String sqlQuery = "UPDATE jahia_ctn_lists SET version_id = -1 WHERE id_jahia_ctn_lists = " + 324 listID + " AND workflow_state>1"; 325 326 int rows = stmt.executeUpdate (sqlQuery); 327 if (rows == 0) { 329 createStagedContainerList (stmt, listID, saveVersion); 330 stmt.executeUpdate (sqlQuery); 331 } 332 } else { 333 String sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID + 334 " AND workflow_state>1"; 335 stmt.executeUpdate (sqlQuery); 336 } 337 } else 338 if (saveVersion.isVersioned ()) { 340 backupContainerListVersion (stmt, listID); 341 String sqlQuery = "UPDATE jahia_ctn_lists SET workflow_state = -1, version_id = " + 342 saveVersion.getVersionID () + 343 " WHERE id_jahia_ctn_lists = " + listID + 344 " AND workflow_state=1"; 345 stmt.executeUpdate (sqlQuery); 346 } else 347 { 349 String sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID + 350 " AND workflow_state=1"; 351 stmt.executeUpdate (sqlQuery); 352 } 353 354 } catch (SQLException se) { 356 String errorMsg = "Cannot delete containerlists in the database"; 357 logger.warn (errorMsg, se); 358 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 359 JahiaException.CRITICAL_SEVERITY, se); 360 361 } finally { 362 try { 363 if (stmt != null) 364 stmt.close (); 365 366 } catch (SQLException ex) { 367 logger.warn ("Cannot free resources", ex); 368 } 369 } 370 } 371 372 373 378 public void purgeContainerList (int listID) 379 throws JahiaException { 380 Connection dbConn = null; 381 Statement stmt = null; 382 try { 383 dbConn = ConnectionDispenser.getConnection (); 384 stmt = dbConn.createStatement (); 385 386 String sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID; 387 stmt.executeUpdate (sqlQuery); 388 } 389 catch (SQLException se) { 391 String errorMsg = "Cannot purge containerlist in the database"; 392 logger.warn (errorMsg, se); 393 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 394 JahiaException.CRITICAL_SEVERITY, se); 395 396 } finally { 397 try { 398 if (stmt != null) 399 stmt.close (); 400 401 } catch (SQLException ex) { 402 logger.warn ("Cannot free resources", ex); 403 } 404 } 405 } 406 407 408 412 private void backupContainerListVersion (Statement stmt, int ctnListID) 413 throws SQLException { 414 String sqlQuery = "SELECT * FROM jahia_ctn_lists WHERE " + 415 "id_jahia_ctn_lists = " + ctnListID + 416 " AND workflow_state=1"; 417 ResultSet rs = stmt.executeQuery (sqlQuery); 418 if (rs != null) 419 if (rs.next ()) { 420 sqlQuery = "INSERT INTO jahia_ctn_lists (" + 421 "id_jahia_ctn_lists," + 422 "parententryid_jahia_ctn_lists," + 423 "pageid_jahia_ctn_lists," + 424 "ctndefid_jahia_ctn_lists," + 425 "rights_jahia_ctn_lists," + 426 "version_id," + 427 "workflow_state) VALUES (" + 428 ctnListID + "," + 429 rs.getInt ("parententryid_jahia_ctn_lists") + "," + 430 rs.getInt ("pageid_jahia_ctn_lists") + "," + 431 rs.getInt ("ctndefid_jahia_ctn_lists") + "," + 432 rs.getInt ("rights_jahia_ctn_lists") + "," + 433 rs.getInt ("version_id") + "," + 434 "0 )"; 435 stmt.executeUpdate (sqlQuery); 436 } 437 } 438 439 440 445 private void createStagedContainerList (Statement stmt, int ctnListID, 446 JahiaSaveVersion saveVersion) 447 throws SQLException { 448 String sqlQuery = "SELECT * FROM jahia_ctn_lists WHERE " + 449 "id_jahia_ctn_lists = " + ctnListID + 450 " AND workflow_state=1"; 451 ResultSet rs = stmt.executeQuery (sqlQuery); 452 if (rs != null) 453 if (rs.next ()) { 454 sqlQuery = "INSERT INTO jahia_ctn_lists (" + 455 "id_jahia_ctn_lists," + 456 "parententryid_jahia_ctn_lists," + 457 "pageid_jahia_ctn_lists," + 458 "ctndefid_jahia_ctn_lists," + 459 "rights_jahia_ctn_lists," + 460 "version_id," + 461 "workflow_state) VALUES (" + 462 ctnListID + "," + 463 rs.getInt ("parententryid_jahia_ctn_lists") + "," + 464 rs.getInt ("pageid_jahia_ctn_lists") + "," + 465 rs.getInt ("ctndefid_jahia_ctn_lists") + "," + 466 rs.getInt ("rights_jahia_ctn_lists") + "," + 467 saveVersion.getVersionID () + "," + 468 saveVersion.getWorkflowState () + ")"; 469 stmt.execute (sqlQuery); 470 } 471 } 472 473 474 482 public void db_validate_staged_container_list (int listID, JahiaSaveVersion saveVersion) 483 throws JahiaException { 484 Connection dbConn = null; 485 Statement stmt = null; 486 try { 487 dbConn = ConnectionDispenser.getConnection (); 488 if (!ConnectionDispenser.isUsingUserTransactions()) 489 dbConn.setAutoCommit (false); 490 stmt = dbConn.createStatement (); 491 String sqlQuery = "SELECT * FROM jahia_ctn_lists WHERE " + 492 "id_jahia_ctn_lists = " + listID + 493 " AND workflow_state>1"; 494 ResultSet rs = stmt.executeQuery (sqlQuery); 495 if (rs != null) 496 if (rs.next ()) { 497 int aclID = rs.getInt ("rights_jahia_ctn_lists"); 498 int list_parententryID = rs.getInt ("parententryid_jahia_ctn_lists"); 499 int list_pageID = rs.getInt ("pageid_jahia_ctn_lists"); 500 int list_ctndefID = rs.getInt ("ctndefid_jahia_ctn_lists"); 501 int versionID = rs.getInt ("version_id"); 502 int workflowState = rs.getInt ("workflow_state"); 503 504 if (saveVersion.isVersioned ()) 506 backupContainerListVersion (stmt, listID); 507 508 if (versionID == -1) { 510 if (saveVersion.isVersioned ()) { 511 sqlQuery = "UPDATE jahia_ctn_lists SET " 513 + "version_id = " + saveVersion.getVersionID () + "," 514 + "workflow_state = -1 " 515 + "WHERE id_jahia_ctn_lists = " + listID + " " 516 + "AND workflow_state>1"; 517 stmt.executeUpdate (sqlQuery); 518 } 519 sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID + 521 " AND workflow_state>=1"; 522 stmt.executeUpdate (sqlQuery); 523 } else 524 { 526 sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID + 528 " AND workflow_state=1"; 529 stmt.executeUpdate (sqlQuery); 530 531 sqlQuery = "UPDATE jahia_ctn_lists SET " 533 + "parententryid_jahia_ctn_lists = " + list_parententryID + "," 534 + "pageid_jahia_ctn_lists = " + list_pageID + "," 535 + "ctndefid_jahia_ctn_lists = " + list_ctndefID + "," 536 + "rights_jahia_ctn_lists = " + aclID + "," 537 + "version_id = " + saveVersion.getVersionID () + ", " 538 + "workflow_state=1 " 539 + "WHERE id_jahia_ctn_lists = " + listID + " " 540 + "AND workflow_state=" + workflowState; 541 int rows = stmt.executeUpdate (sqlQuery); 542 if (rows == 0) { 544 sqlQuery = "INSERT INTO jahia_ctn_lists (" + 545 "id_jahia_ctn_lists," + 546 "parententryid_jahia_ctn_lists," + 547 "pageid_jahia_ctn_lists," + 548 "ctndefid_jahia_ctn_lists," + 549 "rights_jahia_ctn_lists," + 550 "version_id," + 551 "workflow_state) VALUES (" + 552 listID + "," + 553 list_parententryID + "," + 554 list_pageID + "," + 555 list_ctndefID + "," + 556 aclID + "," + 557 saveVersion.getVersionID () + "," + 558 "1)"; 559 stmt.execute (sqlQuery); 560 } 561 562 563 sqlQuery = "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=" + listID + 565 " AND workflow_state>1"; 566 stmt.executeUpdate (sqlQuery); 567 } 568 } 569 } catch (SQLException se) { 570 if (ConnectionDispenser.isUsingUserTransactions()) 571 ConnectionDispenser.abortConnection(); 572 String errorMsg = "Cannot update containerlists in the database"; 573 logger.warn (errorMsg, se); 574 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 575 JahiaException.CRITICAL_SEVERITY, se); 576 } catch (RuntimeException re) { 577 if (ConnectionDispenser.isUsingUserTransactions()) 578 ConnectionDispenser.abortConnection(); 579 throw re; 580 } finally { 581 if (!ConnectionDispenser.isUsingUserTransactions()) 582 { 583 try { 584 585 if (stmt != null) stmt.close (); 586 } catch (SQLException ex) { 587 logger.warn ("Cannot free resources", ex); 588 } 589 } 590 } 591 } 592 593 594 601 public JahiaDOMObject getContainerListsAsDOM (int siteID) 602 throws JahiaException { 603 604 Connection dbConn = null; 605 Statement statement = null; 606 JahiaDBDOMObject dom = null; 607 608 try { 609 String sqlQuery = "SELECT DISTINCT jahia_ctn_lists.id_jahia_ctn_lists,jahia_ctn_lists.parententryid_jahia_ctn_lists,jahia_ctn_lists.pageid_jahia_ctn_lists,jahia_ctn_lists.ctndefid_jahia_ctn_lists,jahia_ctn_lists.rights_jahia_ctn_lists FROM jahia_ctn_lists,jahia_ctn_def where jahia_ctn_lists.ctndefid_jahia_ctn_lists=jahia_ctn_def.id_jahia_ctn_def AND jahia_ctn_def.jahiaid_jahia_ctn_def=" + 610 siteID; 611 612 dbConn = ConnectionDispenser.getConnection (); 613 statement = dbConn.createStatement (); 614 if (statement != null) { 615 ResultSet rs = statement.executeQuery (sqlQuery); 616 if (rs != null) { 617 dom = new JahiaDBDOMObject (); 618 dom.addTable ("jahia_ctn_lists", rs); 619 return dom; 620 } 621 } 622 623 } catch (SQLException se) { 624 String errorMsg = "Cannot load container lists in the database"; 625 logger.warn (errorMsg, se); 626 throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR, 627 JahiaException.CRITICAL_SEVERITY, se); 628 629 } finally { 630 closeStatement (statement); 631 } 632 633 return dom; 634 } 635 636 637 private void closeStatement (Statement statement) { 638 try { 640 if (statement != null) { 641 statement.close (); 642 } 643 644 } catch (SQLException sqlEx) { 645 logger.warn ("Cannot close a statement", sqlEx); 646 } 647 } 648 649 } 650 | Popular Tags |