1 13 package org.jahia.services.pages; 14 15 import org.jahia.data.JahiaDBDOMObject; 16 import org.jahia.data.JahiaDOMObject; 17 import org.jahia.data.fields.FieldTypes; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.exceptions.JahiaPageNotFoundException; 20 import org.jahia.exceptions.database.JahiaDatabaseException; 21 import org.jahia.services.version.EntryLoadRequest; 22 23 import java.sql.*; 24 import java.util.Set ; 25 import java.util.TreeSet ; 26 import java.util.Vector ; 27 import java.util.ArrayList ; 28 29 40 class JahiaPageUtilsDB { 41 42 private static org.apache.log4j.Logger logger = 43 org.apache.log4j.Logger.getLogger(JahiaPageUtilsDB.class); 44 45 private static JahiaPageUtilsDB mObject = null; 46 47 48 51 private JahiaPageUtilsDB () { 52 } 53 54 55 public static JahiaPageUtilsDB getInstance () { 56 if (mObject == null) { 57 mObject = new JahiaPageUtilsDB (); 58 } 59 return mObject; 60 } 61 62 63 70 public Vector getPageIDsInSite (int jahiaID) throws JahiaException { 71 Connection dbConn = null; 72 Statement stmt = null; 73 ResultSet rs = null; 74 Vector theList = new Vector (); 75 try { 76 String sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data "; 77 sqlQuery += "WHERE jahiaid_jahia_pages_data=" + jahiaID; 78 79 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 80 stmt = dbConn.createStatement (); 81 rs = stmt.executeQuery (sqlQuery); 82 83 while (rs.next ()) { 84 theList.add (new Integer (rs.getInt ("id_jahia_pages_data"))); 85 } 86 87 } catch (SQLException se) { 88 String errorMsg = "Error in db_get_page_ids_in_site : " + se.getMessage (); 89 logger.debug (errorMsg + " -> BAILING OUT"); 90 throw new JahiaException ("Cannot load page data from the database", 91 errorMsg, JahiaException.DATABASE_ERROR, 92 JahiaException.CRITICAL_SEVERITY); 93 } finally { 94 closeStatement(stmt); 95 } 96 return theList; 97 } 98 99 public ArrayList getFirstNPageIDs (int nbToLoad) throws JahiaException { 100 Connection dbConn = null; 101 PreparedStatement stmt = null; 102 ResultSet rs = null; 103 ArrayList result = new ArrayList (); 104 try { 105 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 106 stmt = dbConn.prepareStatement("SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data ORDER BY id_jahia_pages_data"); 107 rs = stmt.executeQuery (); 108 109 int count = 0; 110 while ((rs.next ()) && (count < nbToLoad)) { 111 result.add (new Integer (rs.getInt ("id_jahia_pages_data"))); 112 count++; 113 } 114 rs.close(); 115 116 } catch (SQLException se) { 117 String errorMsg = "Error in getFirstNPageIDs : " + se.getMessage (); 118 logger.debug (errorMsg + " -> BAILING OUT"); 119 throw new JahiaException ("Cannot load page data from the database", 120 errorMsg, JahiaException.DATABASE_ERROR, 121 JahiaException.CRITICAL_SEVERITY, se); 122 } finally { 123 closeStatement(stmt); 124 } 125 return result; 126 } 127 128 129 137 public Vector getPageIDsInSite (int jahiaID, int linkType) 138 throws JahiaException { 139 Vector theList = new Vector (); 140 PreparedStatement pStmt = null; 141 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 142 if (dbConn == null) { 143 return theList; 144 } 145 try { 146 StringBuffer sqlQuery = 147 new StringBuffer ("SELECT DISTINCT id_jahia_pages_data"); 148 sqlQuery.append (" FROM jahia_pages_data"); 149 sqlQuery.append (" WHERE jahiaid_jahia_pages_data = ? AND workflow_state >= ?"); 150 sqlQuery.append (" AND pagetype_jahia_pages_data = ?"); 151 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 152 pStmt.setInt (1, jahiaID); 153 pStmt.setInt (2, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 154 pStmt.setInt (3, linkType); 155 ResultSet rs = pStmt.executeQuery (); 156 while (rs.next ()) { 157 theList.add (new Integer (rs.getInt ("id_jahia_pages_data"))); 158 } 159 160 } catch (SQLException se) { 161 String errorMsg = "Error in db_get_page_ids_in_site : " + se.getMessage (); 162 logger.debug (errorMsg + " -> BAILING OUT"); 163 throw new JahiaException ("Cannot load page data from the database", 164 errorMsg, JahiaException.DATABASE_ERROR, 165 JahiaException.CRITICAL_SEVERITY); 166 } finally { 167 168 closeStatement (pStmt); 169 } 170 return theList; 171 } 172 173 182 public Vector getPageIDsWithTemplate (int templateID) 183 throws JahiaException { 184 Connection dbConn = null; 185 Statement stmt = null; 186 ResultSet rs = null; 187 Vector theList = new Vector (); 188 try { 189 String sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data "; 190 sqlQuery += "WHERE pagedefid_jahia_pages_data=" + templateID 191 + " AND (pagetype_jahia_pages_data=0 OR pagetype_jahia_pages_data=1)"; 192 193 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 194 stmt = dbConn.createStatement (); 195 rs = stmt.executeQuery (sqlQuery); 196 197 while (rs.next ()) { 198 theList.add (new Integer (rs.getInt ("id_jahia_pages_data"))); 199 } 200 201 } catch (SQLException se) { 202 String errorMsg = "Error : " + se.getMessage (); 203 logger.debug (errorMsg + " -> BAILING OUT"); 204 throw new JahiaException ("Cannot load page data from the database", 205 errorMsg, JahiaException.DATABASE_ERROR, 206 JahiaException.CRITICAL_SEVERITY, se); 207 } finally { 208 closeStatement (stmt); 209 } 210 return theList; 211 } 212 213 214 221 public Vector getPageIDsPointingOnPage (int pageID, EntryLoadRequest entryLoadRequest) throws JahiaException { 222 Connection dbConn = null; 223 PreparedStatement stmt = null; 224 ResultSet rs = null; 225 Vector pageList = new Vector (); 226 try { 227 228 String sqlQuery = ""; 230 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 231 232 if (entryLoadRequest.isVersioned()) { 233 sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data WHERE (pagetype_jahia_pages_data=1) AND (pagelinkid_jahia_pages_data=?)"; 234 } else { 235 sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data WHERE (pagetype_jahia_pages_data=1) AND (pagelinkid_jahia_pages_data=?) AND workflow_state>0"; 236 } 237 238 stmt = dbConn.prepareStatement (sqlQuery); 239 240 stmt.setInt(1, pageID); 242 rs = stmt.executeQuery (); 243 while (rs.next ()) { 244 pageList.add (new Integer (rs.getInt (1))); 245 } 246 247 } catch (SQLException se) { 248 String errorMsg = "Error in db_get_page_ids_pointing_on_page : " + se.getMessage (); 249 logger.debug (errorMsg + " -> BAILING OUT"); 250 throw new JahiaException ("Cannot load page data from the database", 251 errorMsg, JahiaException.DATABASE_ERROR, 252 JahiaException.CRITICAL_SEVERITY); 253 } finally { 254 closeStatement (stmt); 255 } 256 return pageList; 257 } 258 259 public int getPageFieldID (int pageID, EntryLoadRequest loadRequest) 260 throws JahiaException { 261 262 int pageFieldID = -1; 263 264 if (loadRequest == null) { 265 pageFieldID = getAnyPageFieldID (pageID); 266 } else if (loadRequest.isCurrent ()) { 267 pageFieldID = getActivePageFieldID (pageID); 268 } else if (loadRequest.isStaging ()) { 269 pageFieldID = getStagedPageFieldID(pageID); 273 274 if ( pageFieldID == -1 ){ 275 pageFieldID = getActivePageFieldID (pageID); 277 } 278 } else if (loadRequest.isVersioned()) { 279 try { 280 ContentPage contentPage = ContentPage.getPage(pageID, false); 281 int parentPageID = contentPage.getParentID(loadRequest); 282 pageFieldID = getVersionedPageFieldID(pageID, parentPageID, 283 loadRequest.getVersionID(), true); 284 } catch ( JahiaPageNotFoundException pnfe ){ 285 logger.debug("ContentPage not found pid[" + pageID + "] returning -1 "); 286 } 287 } 288 289 return pageFieldID; 290 291 } 292 293 private int getAnyPageFieldID (int pageID) 294 throws JahiaException { 295 296 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 298 int result = -1; 299 if (dbConn == null) { 300 return result; 301 } 302 PreparedStatement pStmt = null; 303 try { 304 StringBuffer sqlQuery = new StringBuffer ("SELECT id_jahia_fields_data"); 306 sqlQuery.append (" FROM jahia_fields_data"); 307 sqlQuery.append (" WHERE value_jahia_fields_data = ?"); 308 sqlQuery.append (" AND type_jahia_fields_data = ?"); 309 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 310 pStmt.setString (1, Integer.toString (pageID)); 311 pStmt.setInt (2, FieldTypes.PAGE); 312 ResultSet rs = pStmt.executeQuery (); 313 if (rs != null) { 314 int fieldID = -1; 315 while (rs.next ()) { 316 fieldID = rs.getInt ("id_jahia_fields_data"); 317 result = fieldID; 318 continue; 319 } 320 } 321 } catch (SQLException se) { 322 String errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage (); 323 logger.debug (errorMsg + " -> BAILING OUT"); 324 throw new JahiaException ("Cannot load page data from the database", 325 errorMsg, JahiaException.DATABASE_ERROR, 326 JahiaException.CRITICAL_SEVERITY, se); 327 } finally { 328 329 closeStatement (pStmt); 330 } 331 return result; 332 } 333 334 public int getActivePageFieldID (int pageID) 335 throws JahiaException { 336 337 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 339 int result = -1; 340 if (dbConn == null) { 341 return result; 342 } 343 PreparedStatement pStmt = null; 344 try { 345 StringBuffer sqlQuery = new StringBuffer ( 347 "SELECT id_jahia_fields_data, workflow_state"); 348 sqlQuery.append (" FROM jahia_fields_data"); 349 sqlQuery.append (" WHERE value_jahia_fields_data = ?"); 350 sqlQuery.append (" AND type_jahia_fields_data = ?"); 351 sqlQuery.append (" AND workflow_state = ?"); 352 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 353 pStmt.setString (1, Integer.toString (pageID)); 354 pStmt.setInt (2, FieldTypes.PAGE); 355 pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 356 ResultSet rs = pStmt.executeQuery (); 357 if (rs != null) { 358 int fieldID = -1; 359 while (rs.next ()) { 360 fieldID = rs.getInt ("id_jahia_fields_data"); 361 result = fieldID; 362 } 363 } 364 } catch (SQLException se) { 365 String errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage (); 366 logger.debug (errorMsg + " -> BAILING OUT"); 367 throw new JahiaException ("Cannot load page data from the database", 368 errorMsg, JahiaException.DATABASE_ERROR, 369 JahiaException.CRITICAL_SEVERITY, se); 370 } finally { 371 372 closeStatement (pStmt); 373 } 374 return result; 375 } 376 377 public int getStagedPageFieldID (int pageID) 378 throws JahiaException { 379 380 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 382 int result = -1; 383 if (dbConn == null) { 384 return result; 385 } 386 PreparedStatement pStmt = null; 387 try { 388 StringBuffer sqlQuery = new StringBuffer ( 390 "SELECT id_jahia_fields_data, workflow_state"); 391 sqlQuery.append (" FROM jahia_fields_data"); 392 sqlQuery.append (" WHERE value_jahia_fields_data = ?"); 393 sqlQuery.append (" AND type_jahia_fields_data = ?"); 394 sqlQuery.append (" AND workflow_state > ?"); 395 sqlQuery.append (" AND version_id=0"); 396 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 397 pStmt.setString (1, Integer.toString (pageID)); 398 pStmt.setInt (2, FieldTypes.PAGE); 399 pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 400 ResultSet rs = pStmt.executeQuery (); 401 if (rs != null) { 402 int fieldID = -1; 403 while (rs.next ()) { 404 fieldID = rs.getInt ("id_jahia_fields_data"); 405 result = fieldID; 406 } 407 } 408 } catch (SQLException se) { 409 String errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage (); 410 logger.debug (errorMsg + " -> BAILING OUT"); 411 throw new JahiaException ("Cannot load page data from the database", 412 errorMsg, JahiaException.DATABASE_ERROR, 413 JahiaException.CRITICAL_SEVERITY, se); 414 } finally { 415 416 closeStatement (pStmt); 417 } 418 return result; 419 } 420 421 429 public Vector getStagingAndActivePageFieldIDs(int pageID) throws JahiaException { 430 Vector v = new Vector (); 431 int id = getStagedPageFieldID(pageID); 432 if ( id != -1 ){ 433 v.add(new Integer (id)); 434 } 435 436 id = getActivePageFieldID(pageID); 437 if ( id != -1 && !v.contains(new Integer (id)) ){ 438 v.add(new Integer (id)); 439 } 440 return v; 441 } 442 443 private int getVersionedPageFieldID (int pageID, int parentPageID, int versionID, 444 boolean withActive) 445 throws JahiaException { 446 447 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 449 int result = -1; 450 if (dbConn == null) { 451 return result; 452 } 453 454 ContentPage contentPage = ContentPage.getPage (pageID, false); 455 456 PreparedStatement pStmt = null; 457 try { 458 StringBuffer sqlQuery = new StringBuffer ( 460 "SELECT id_jahia_fields_data, version_id, workflow_state"); 461 sqlQuery.append (" FROM jahia_fields_data"); 462 sqlQuery.append (" WHERE value_jahia_fields_data = ?"); 463 sqlQuery.append (" AND pageid_jahia_fields_data = ?"); 464 sqlQuery.append (" AND type_jahia_fields_data = ?"); 465 sqlQuery.append (" AND workflow_state <= "); 466 if (withActive) { 467 sqlQuery.append (EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 468 } else { 469 sqlQuery.append (EntryLoadRequest.VERSIONED_WORKFLOW_STATE); 470 } 471 sqlQuery.append (" AND version_id<=?"); 472 sqlQuery.append (" ORDER BY version_id DESC, workflow_state DESC"); 473 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 474 pStmt.setString (1, Integer.toString (pageID)); 475 pStmt.setString (2, Integer.toString (parentPageID)); 476 pStmt.setInt (3, FieldTypes.PAGE); 477 pStmt.setInt (4, versionID); 478 ResultSet rs = pStmt.executeQuery (); 479 480 int previousFieldID = 0; 481 int previousVersionID = 0; 482 int previousWS = 0; 483 if (rs != null) { 484 while (rs.next ()) { 485 int fieldID = rs.getInt ("id_jahia_fields_data"); 486 int v = rs.getInt ("version_id"); 487 int ws = rs.getInt ("workflow_state"); 488 if (result == -1) { 489 result = fieldID; 490 } else { 491 if (v < previousVersionID) { 492 break; 493 } 494 } 495 previousFieldID = fieldID; 496 previousVersionID = v; 497 previousWS = ws; 498 } 499 } 500 } catch (SQLException se) { 501 String errorMsg = "Error in getVersionedPageFieldID : " + se.getMessage (); 502 logger.debug (errorMsg + " -> BAILING OUT"); 503 throw new JahiaException ("Cannot load page data from the database", 504 errorMsg, JahiaException.DATABASE_ERROR, 505 JahiaException.CRITICAL_SEVERITY, se); 506 } finally { 507 508 closeStatement (pStmt); 509 } 510 return result; 511 } 512 513 521 public Set getStagingPageFieldIDsInPage (int pageID) throws JahiaException 522 { 523 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 525 Set result = new TreeSet (); 526 if (dbConn == null) { 527 return result; 528 } 529 PreparedStatement pStmt = null; 530 try { 531 StringBuffer sqlQuery = new StringBuffer ("SELECT DISTINCT id_jahia_fields_data"); 533 sqlQuery.append(" FROM jahia_fields_data"); 534 sqlQuery.append(" WHERE (pageid_jahia_fields_data = ?"); 535 sqlQuery.append(" AND type_jahia_fields_data=5 AND workflow_state >1)"); 536 pStmt = dbConn.prepareStatement(sqlQuery.toString()); 537 pStmt.setString(1, Integer.toString(pageID)); 538 ResultSet rs = pStmt.executeQuery(); 539 if (rs != null) { 540 int fieldID = -1; 541 while (rs.next()) { 542 fieldID = rs.getInt ("id_jahia_fields_data"); 543 result.add(new Integer (fieldID)); 544 } 545 } 546 } catch (SQLException se) { 547 String errorMsg = "Error in getStagingPageFieldIDInsInPage : " + se.getMessage(); 548 logger.debug(errorMsg + " -> BAILING OUT"); 549 throw new JahiaException("Cannot load page data from the database", 550 errorMsg, JahiaException.DATABASE_ERROR, 551 JahiaException.CRITICAL_SEVERITY, se ); 552 } finally { 553 554 closeStatement (pStmt); 555 } 556 557 return result; 558 } 559 560 568 public int getPageFieldID (int pageID) throws JahiaException { 569 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 571 if (dbConn == null) { 572 return -1; 573 } 574 PreparedStatement pStmt = null; 575 int fieldID = -1; 576 try { 577 StringBuffer sqlQuery = new StringBuffer ( 579 "SELECT id_jahia_fields_data, workflow_state"); 580 sqlQuery.append (" FROM jahia_fields_data"); 581 sqlQuery.append (" WHERE value_jahia_fields_data = ?"); 582 sqlQuery.append (" AND type_jahia_fields_data = ?"); 583 sqlQuery.append (" AND workflow_state >= ?"); 584 sqlQuery.append (" AND version_id <> -1"); 585 sqlQuery.append (" ORDER BY workflow_state DESC "); 586 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 587 pStmt.setString (1, Integer.toString (pageID)); 588 pStmt.setInt (2, FieldTypes.PAGE); 589 pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE); 590 ResultSet rs = pStmt.executeQuery (); 591 if (rs != null) { 592 int maxWorkflowState = -1; 593 while (rs.next ()) { 594 int workflowState = rs.getInt ("workflow_state"); 595 if (workflowState > maxWorkflowState) { 596 maxWorkflowState = workflowState; 597 fieldID = rs.getInt ("id_jahia_fields_data"); 598 } 599 } 600 } 601 } catch (SQLException se) { 602 String errorMsg = "Error in db_get_page_field : " + se.getMessage (); 603 logger.debug (errorMsg + " -> BAILING OUT"); 604 throw new JahiaException ("Cannot load page data from the database", 605 errorMsg, JahiaException.DATABASE_ERROR, 606 JahiaException.CRITICAL_SEVERITY, se); 607 } finally { 608 609 closeStatement (pStmt); 610 } 611 return fieldID; 612 } 613 614 615 618 public Vector getPageChildIDs (int pageID) throws JahiaException { 619 return getPageChildIDs (pageID, true); 620 } 621 622 623 631 public Vector getPageChildIDs (int pageID, boolean withDeleted) throws JahiaException { 632 Vector result = new Vector (); 633 634 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 636 if (dbConn == null) { 637 return result; 638 } 639 PreparedStatement pStmt = null; 640 try { 641 StringBuffer sqlQuery = 642 new StringBuffer ( 643 "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code"); 644 sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND "); 645 if (!withDeleted) { 646 sqlQuery.append ("workflow_state>0"); 647 } else { 648 sqlQuery.append ("workflow_state<>0"); 649 } 650 sqlQuery.append ( 651 " ORDER BY id_jahia_pages_data, version_id DESC, workflow_state DESC "); 652 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 653 pStmt.setInt (1, pageID); 654 ResultSet rs = pStmt.executeQuery (); 655 if (rs != null) { 656 657 while (rs.next ()) { 658 Integer I = new Integer (rs.getInt ("id_jahia_pages_data")); 659 int versionID = rs.getInt ("version_id"); 660 int ws = rs.getInt ("workflow_state"); 661 String languageCode = rs.getString ("language_code"); 662 if (!result.contains (I)) { 663 try { 664 if (ws == EntryLoadRequest.DELETED_WORKFLOW_STATE) { 665 ContentPage contentPage = ContentPage.getPage (I.intValue (), 666 false); 667 if (!contentPage.isDeletedOrDoesNotExist (versionID) || 668 pageID != contentPage.getParentID ( 669 EntryLoadRequest.STAGED)) { 670 continue; 671 } 672 } 673 result.add (I); 674 } catch (Throwable t) { 675 logger.debug ( 676 "Exception retrieving page childs for pid[" + pageID + "]", 677 t); 678 } 679 } 680 } 681 } 682 } catch (SQLException se) { 683 String errorMsg = "Error in getPageChildIDs : " + se.getMessage () + " -> BAILING OUT"; 684 logger.debug (errorMsg); 685 throw new JahiaException ("Cannot load the page values from the database", 686 errorMsg, JahiaException.DATABASE_ERROR, 687 JahiaException.CRITICAL_SEVERITY); 688 } finally { 689 690 closeStatement (pStmt); 691 } 692 693 return result; 694 } 695 696 704 public Vector getStagingPageChildIDs (int pageID) throws JahiaException { 705 Vector result = new Vector (); 706 707 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 709 if (dbConn == null) { 710 return result; 711 } 712 PreparedStatement pStmt = null; 713 try { 714 StringBuffer sqlQuery = 715 new StringBuffer ( 716 "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code"); 717 sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state > 0"); 718 sqlQuery.append (" ORDER BY id_jahia_pages_data, workflow_state DESC"); 719 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 720 pStmt.setInt (1, pageID); 721 ResultSet rs = pStmt.executeQuery (); 722 if (rs != null) { 723 while (rs.next ()) { 724 Integer I = new Integer (rs.getInt ("id_jahia_pages_data")); 725 if (!result.contains (I)) { 726 result.add (I); 727 } 728 } 729 } 730 } catch (SQLException se) { 731 String errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT"; 732 logger.debug (errorMsg,se); 733 throw new JahiaException ("Cannot load the page values from the database", 734 errorMsg, JahiaException.DATABASE_ERROR, 735 JahiaException.CRITICAL_SEVERITY); 736 } finally { 737 738 closeStatement (pStmt); 739 } 740 return result; 741 } 742 743 750 public Vector getActivePageChildIDs (int pageID) throws JahiaException { 751 Vector result = new Vector (); 752 753 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 755 if (dbConn == null) { 756 return result; 757 } 758 PreparedStatement pStmt = null; 759 try { 760 StringBuffer sqlQuery = 761 new StringBuffer ( 762 "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code"); 763 sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state = 1"); 764 sqlQuery.append (" ORDER BY id_jahia_pages_data, version_id DESC "); 765 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 766 pStmt.setInt (1, pageID); 767 ResultSet rs = pStmt.executeQuery (); 768 if (rs != null) { 769 while (rs.next ()) { 770 Integer I = new Integer (rs.getInt ("id_jahia_pages_data")); 771 if (!result.contains (I)) { 772 result.add (I); 773 } 774 } 775 } 776 } catch (SQLException se) { 777 String errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT"; 778 logger.debug (errorMsg,se); 779 throw new JahiaException ("Cannot load the page values from the database", 780 errorMsg, JahiaException.DATABASE_ERROR, 781 JahiaException.CRITICAL_SEVERITY); 782 } finally { 783 784 closeStatement (pStmt); 785 } 786 return result; 787 } 788 789 800 public Vector getVersioningPageChildIDs (int pageID, int versionID) throws JahiaException { 801 Vector result = new Vector (); 802 803 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 805 if (dbConn == null) { 806 return result; 807 } 808 PreparedStatement pStmt = null; 809 try { 810 StringBuffer sqlQuery = 811 new StringBuffer ( 812 "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code"); 813 sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state <= 1 AND version_id <= ?"); 814 sqlQuery.append (" ORDER BY id_jahia_pages_data, version_id DESC "); 815 pStmt = dbConn.prepareStatement (sqlQuery.toString ()); 816 pStmt.setInt (1, pageID); 817 pStmt.setInt (2, versionID); 818 ResultSet rs = pStmt.executeQuery (); 819 if (rs != null) { 820 while (rs.next ()) { 821 Integer I = new Integer (rs.getInt ("id_jahia_pages_data")); 822 if (!result.contains (I)) { 823 result.add (I); 824 } 825 } 826 } 827 } catch (SQLException se) { 828 String errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT"; 829 logger.debug (errorMsg,se); 830 throw new JahiaException ("Cannot load the page values from the database", 831 errorMsg, JahiaException.DATABASE_ERROR, 832 JahiaException.CRITICAL_SEVERITY); 833 } finally { 834 835 closeStatement (pStmt); 836 } 837 return result; 838 } 839 840 845 public synchronized int getNbPages () 846 throws JahiaDatabaseException { 847 return getNbPages (-1); 848 } 849 850 851 public synchronized int getRealActiveNbPages () 852 throws JahiaDatabaseException { 853 return getRealActiveNbPages (-1); 854 } 855 856 857 862 public synchronized int getNbPages (int siteID) 863 throws JahiaDatabaseException { 864 int counter = 0; 865 866 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 867 if (dbConn != null) { 868 String query = ""; 869 Statement statement = null; 870 871 try { 872 query = "SELECT COUNT(id_jahia_pages_data) as nbItems FROM jahia_pages_data"; 873 874 if (siteID != -1) { 875 query += " where jahiaid_jahia_pages_data=" + siteID; 876 } 877 878 statement = dbConn.createStatement (); 879 if (statement != null) { 880 ResultSet rs = statement.executeQuery (query.toString ()); 881 882 if (rs != null) { 883 if (rs.next ()) { 884 counter = rs.getInt ("nbItems"); 885 } 886 } 887 888 rs = null; 890 } 891 } catch (SQLException ex) { 892 throw new JahiaDatabaseException ("Database error.", ex, 893 JahiaDatabaseException.ERROR_SEVERITY); 894 } finally { 895 query = null; 896 897 closeStatement (statement); 898 } 899 } 900 return counter; 901 } 902 903 904 public synchronized int getRealActiveNbPages (int siteID) 905 throws JahiaDatabaseException { 906 int counter = 0; 907 908 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 909 if (dbConn != null) { 910 String query = ""; 911 Statement statement = null; 912 913 try { 914 query = 915 "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data where pagetype_jahia_pages_data=0 AND workflow_state=1"; 916 917 if (siteID != -1) { 918 query += " and jahiaid_jahia_pages_data=" + siteID; 919 } 920 921 statement = dbConn.createStatement (); 922 if (statement != null) { 923 ResultSet rs = statement.executeQuery (query.toString ()); 924 925 if (rs != null) { 926 while (rs.next ()) { 927 counter++; 928 } 929 } 930 rs.close (); 931 932 rs = null; 934 } 935 } catch (SQLException ex) { 936 throw new JahiaDatabaseException ("Database error.", ex, 937 JahiaDatabaseException.ERROR_SEVERITY); 938 } finally { 939 query = null; 940 941 closeStatement (statement); 942 } 943 } 944 return counter; 945 } 946 947 948 955 public JahiaDOMObject getPagesAsDOM (int siteID) throws JahiaException { 956 957 Connection dbConn = null; 958 Statement statement = null; 959 JahiaDBDOMObject dom = null; 960 961 try { 962 String sqlQuery = "SELECT * FROM jahia_pages_data where jahiaid_jahia_pages_data=" + siteID; 963 964 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 965 statement = dbConn.createStatement (); 966 if (statement != null) { 967 ResultSet rs = statement.executeQuery (sqlQuery); 968 if (rs != null) { 969 dom = new JahiaDBDOMObject (); 970 dom.addTable ("jahia_pages_data", rs); 971 return dom; 972 } 973 } 974 } catch (SQLException se) { 975 String errorMsg = "Error in getPagesAsDOM(int siteID) : " + se.getMessage (); 976 logger.debug (errorMsg + " -> BAILING OUT"); 977 throw new JahiaException ("Cannot load pages from the database", 978 errorMsg, JahiaException.DATABASE_ERROR, 979 JahiaException.CRITICAL_SEVERITY); 980 } finally { 981 982 closeStatement (statement); 983 } 984 985 return dom; 986 } 987 988 989 997 public Vector db_get_all_acl_id (int siteID) 998 throws JahiaException { 999 Connection dbConn = null; 1000 Statement stmt = null; 1001 ResultSet rs = null; 1002 Vector theIDs = new Vector (); 1003 try { 1004 String sqlQuery = "SELECT DISTINCT rights_jahia_pages_data FROM jahia_pages_data " 1005 + "WHERE jahiaid_jahia_pages_data=" + siteID; 1006 1007 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1008 stmt = dbConn.createStatement (); 1009 rs = stmt.executeQuery (sqlQuery); 1010 1011 while (rs.next ()) { 1012 theIDs.add (new Integer (rs.getInt ("rights_jahia_pages_data"))); 1013 } 1014 } catch (SQLException se) { 1015 String errorMsg = "Error in db_get_all_acl_id : " + se.getMessage (); 1016 logger.debug (errorMsg + " -> BAILING OUT"); 1017 throw new JahiaException ("Cannot load acl id from the database", 1018 errorMsg, JahiaException.DATABASE_ERROR, 1019 JahiaException.CRITICAL_SEVERITY); 1020 } finally { 1021 closeStatement(stmt); 1022 } 1023 return theIDs; 1024 1025 } 1026 1027 1028 private void closeStatement (Statement statement) { 1029 try { 1031 if (statement != null) { 1032 statement.close (); 1033 } 1034 } catch (SQLException sqlEx) { 1035 logger.warn ("Cannot close a statement", sqlEx); 1036 } 1037 } 1038 1039 1040} 1041 | Popular Tags |