1 package org.jahia.services.sites; 18 19 20 import org.jahia.data.JahiaDBDOMObject; 21 import org.jahia.data.JahiaDOMObject; 22 import org.jahia.exceptions.JahiaException; 23 import org.jahia.exceptions.database.JahiaDatabaseException; 24 import org.jahia.registries.ServicesRegistry; 25 import org.jahia.services.acl.JahiaBaseACL; 26 import org.jahia.services.database.ConnectionDispenser; 27 import org.jahia.utils.JahiaTools; 28 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 38 43 class JahiaSitesPersistance implements SitesDBInterface { 44 private static org.apache.log4j.Logger logger 45 = org.apache.log4j.Logger.getLogger (JahiaSitesPersistance.class); 46 47 private static final String CLASS_NAME = JahiaSitesPersistance.class.getName (); 48 49 private static JahiaSitesPersistance instance = null; 50 51 public static final int ORDER_BY_SERVER_NAME = 1; 52 public static final int ORDER_BY_TITLE = 2; 53 54 55 58 protected JahiaSitesPersistance () { 59 60 } 61 62 63 66 public static synchronized JahiaSitesPersistance getInstance () { 67 68 if (instance == null) { 69 instance = new JahiaSitesPersistance (); 70 } 71 return instance; 72 } 73 74 75 80 public Vector dbGetSites () throws JahiaException { 81 82 return dbGetSites (ORDER_BY_SERVER_NAME); 84 } 85 86 87 94 public Vector dbGetSites (int orderType) throws JahiaException { 95 96 Connection dbConn = null; 97 PreparedStatement statement = null; 98 99 Vector sites = new Vector (); 100 JahiaSite site = null; 101 102 try { 103 String sqlQuery = "SELECT * FROM jahia_sites" + getOrderCond (orderType); 104 105 dbConn = ConnectionDispenser.getConnection (); 106 statement = dbConn.prepareStatement (sqlQuery); 107 ResultSet rs = statement.executeQuery (); 108 if (rs != null) { 109 while (rs.next ()) { 110 site = getJahiaSiteFromResultSet (rs); 111 sites.add (site); 112 } 113 } 114 115 } catch (SQLException se) { 116 String errorMsg = "Error in dbGetSites : " + se.getMessage (); 117 logger.debug ("Error retrieving sites : ", se); 118 throw new JahiaException ("Cannot load sites from the database", 119 errorMsg, JahiaException.DATABASE_ERROR, 120 JahiaException.CRITICAL_SEVERITY, se); 121 } finally { 122 closeStatement (statement); 123 } 124 125 Enumeration enumeration = sites.elements (); 126 while (enumeration.hasMoreElements ()) { 127 JahiaSite el = (JahiaSite) enumeration.nextElement (); 128 Properties props = this.getSiteProps (el.getID ()); 129 el.setSettings (props); 130 } 131 132 return sites; 133 } 134 135 136 143 public JahiaSite dbGetSite (int id) throws JahiaException { 144 145 Connection dbConn = null; 146 PreparedStatement statement = null; 147 148 JahiaSite site = null; 149 150 try { 151 String sqlQuery = "SELECT * FROM jahia_sites where id_jahia_sites=?"; 152 153 dbConn = ConnectionDispenser.getConnection (); 154 statement = dbConn.prepareStatement (sqlQuery); 155 statement.setInt(1, id); 156 ResultSet rs = statement.executeQuery (); 157 if (rs != null && rs.next ()) { 158 site = getJahiaSiteFromResultSet (rs); 159 } 160 } catch (SQLException se) { 161 String errorMsg = "Error in dbGetSite(int id) : " + se.getMessage (); 162 logger.debug ("Error retrieving site from the database : ", se); 163 throw new JahiaException ("Cannot load site from the database", 164 errorMsg, JahiaException.DATABASE_ERROR, 165 JahiaException.CRITICAL_SEVERITY, se); 166 } finally { 167 168 closeStatement (statement); 169 } 170 171 return site; 172 } 173 174 175 182 public JahiaSite dbGetSiteByKey (String siteKey) throws JahiaException { 183 184 Connection dbConn = null; 185 PreparedStatement statement = null; 186 187 JahiaSite site = null; 188 189 try { 190 String sqlQuery = 191 "SELECT * FROM jahia_sites where key_jahia_sites=?"; 192 193 dbConn = ConnectionDispenser.getConnection (); 194 statement = dbConn.prepareStatement (sqlQuery); 195 statement.setString(1, siteKey); 196 197 ResultSet rs = statement.executeQuery (); 198 if (rs != null && rs.next ()) { 199 site = getJahiaSiteFromResultSet (rs); 200 } 201 } catch (SQLException se) { 202 String errorMsg = "Error in dbGetSiteByKey(" + siteKey + 203 ") : " + se.getMessage (); 204 logger.debug ("Error retrieving site by key " + siteKey, se); 205 throw new JahiaException ("Cannot load site from the database", 206 errorMsg, JahiaException.DATABASE_ERROR, 207 JahiaException.CRITICAL_SEVERITY, 208 se); 209 } finally { 210 211 closeStatement (statement); 212 } 213 214 return site; 215 } 216 217 224 public JahiaSite dbGetSiteByServerName (String serverName) throws JahiaException { 225 226 Connection dbConn = null; 227 PreparedStatement statement = null; 228 229 JahiaSite site = null; 230 231 try { 232 String sqlQuery = 233 "SELECT * FROM jahia_sites where servername_jahia_sites=?"; 234 235 dbConn = ConnectionDispenser.getConnection (); 236 statement = dbConn.prepareStatement (sqlQuery); 237 statement.setString(1, serverName); 238 ResultSet rs = statement.executeQuery (); 239 if (rs != null && rs.next ()) { 240 site = getJahiaSiteFromResultSet (rs); 241 } 242 } catch (SQLException se) { 243 String errorMsg = "Error in dbGetSiteByServerName(" + serverName + 244 ") : " + se.getMessage (); 245 logger.debug ("Error retrieving site by key " + serverName, se); 246 throw new JahiaException ("Cannot load site from the database", 247 errorMsg, JahiaException.DATABASE_ERROR, 248 JahiaException.CRITICAL_SEVERITY, 249 se); 250 } finally { 251 252 closeStatement (statement); 253 } 254 255 return site; 256 } 257 258 265 public JahiaSite dbGetSite (String name) throws JahiaException { 266 267 Connection dbConn = null; 268 PreparedStatement statement = null; 269 270 JahiaSite site = null; 271 272 try { 273 String sqlQuery = 274 "SELECT * FROM jahia_sites WHERE servername_jahia_sites=?"; 275 276 dbConn = ConnectionDispenser.getConnection (); 277 statement = dbConn.prepareStatement (sqlQuery); 278 statement.setString(1, JahiaTools.quote (name)); 279 ResultSet rs = statement.executeQuery (); 280 if (rs != null && rs.next ()) { 281 site = getJahiaSiteFromResultSet (rs); 282 } 283 } catch (SQLException se) { 284 String errorMsg = "Error in dbGetSite(String name) : " + se.getMessage (); 285 logger.debug ("Error while retrieving site by name [" + name + "]", se); 286 throw new JahiaException ("Cannot load site from the database", 287 errorMsg, 288 JahiaException.DATABASE_ERROR, 289 JahiaException.CRITICAL_SEVERITY, 290 se); 291 } finally { 292 293 closeStatement (statement); 294 } 295 296 return site; 297 } 298 299 300 305 public void dbAddSite (JahiaSite site) throws JahiaException { 306 307 try { 308 309 int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 310 .autoIncrement ("jahia_sites"); 311 StringBuffer sqlQuery = new StringBuffer ("INSERT INTO jahia_sites VALUES("); 312 sqlQuery.append (id); 313 sqlQuery.append (",'"); 314 sqlQuery.append (JahiaTools.quote (site.getTitle ())); 315 sqlQuery.append ("','"); 316 sqlQuery.append (JahiaTools.quote (site.getServerName ())); 317 sqlQuery.append ("','"); 318 sqlQuery.append (JahiaTools.quote (site.getSiteKey ())); 319 sqlQuery.append ("',"); 320 321 if (site.isActive ()) { 322 sqlQuery.append (1); 323 } else { 324 sqlQuery.append (0); 325 } 326 327 sqlQuery.append (","); 328 sqlQuery.append (site.getHomePageID ()); 329 sqlQuery.append (","); 330 sqlQuery.append (site.getDefaultTemplateID ()); 331 sqlQuery.append (","); 332 333 if (site.getTemplatesAutoDeployMode ()) { 334 sqlQuery.append (1); 335 } else { 336 sqlQuery.append (0); 337 } 338 339 sqlQuery.append (","); 340 if (site.getWebAppsAutoDeployMode ()) { 341 sqlQuery.append (1); 342 } else { 343 sqlQuery.append (0); 344 } 345 346 sqlQuery.append (","); 347 sqlQuery.append (site.getAclID ()); 348 sqlQuery.append (",'"); 349 sqlQuery.append (JahiaTools.quote (site.getDescr ())); 350 sqlQuery.append ("')"); 351 352 executeQueryNoResultSet (sqlQuery.toString ()); 353 354 logger.debug ("Adding site query=[" + sqlQuery + "]"); 355 356 site.setID (id); 357 358 } catch (JahiaException je) { 359 String errorMsg = "Error in dbAddSite(JahiaSite site) : " + je.getMessage (); 360 logger.debug ("Error while adding site in database : ", je); 361 throw new JahiaException ("Cannot add site in the database", 362 errorMsg, 363 JahiaException.DATABASE_ERROR, 364 JahiaException.CRITICAL_SEVERITY, 365 je); 366 } 367 } 368 369 370 375 public void dbRemoveSite (int id) throws JahiaException { 376 377 try { 378 String sqlQuery = "DELETE FROM jahia_sites WHERE id_jahia_sites=" + id; 379 executeQueryNoResultSet (sqlQuery); 380 } catch (JahiaException je) { 381 String errorMsg = "Error in dbRemoveSite(int id) : " + je.getMessage (); 382 logger.debug ("Error while removing site : ", je); 383 throw new JahiaException ("Cannot remove site in the database", 384 errorMsg, 385 JahiaException.DATABASE_ERROR, 386 JahiaException.CRITICAL_SEVERITY, 387 je); 388 } 389 } 390 391 392 397 public void dbUpdateSite (JahiaSite site) throws JahiaException { 398 399 if ( site == null ){ 400 return; 401 } 402 if ( site.getID()<=0 ){ 403 throw new JahiaException("Error updating JahiaSite", 404 "Invalid site Id[" + site.getID() + "]", 405 JahiaException.DATA_ERROR, 406 JahiaException.ERROR_SEVERITY); 407 } 408 409 try { 410 StringBuffer sqlQuery = new StringBuffer ("UPDATE jahia_sites SET "); 411 sqlQuery.append ("id_jahia_sites="); 412 sqlQuery.append (site.getID ()); 413 sqlQuery.append (", title_jahia_sites='"); 414 sqlQuery.append (JahiaTools.quote (site.getTitle ())); 415 sqlQuery.append ("', servername_jahia_sites='"); 416 sqlQuery.append (JahiaTools.quote (site.getServerName ())); 417 sqlQuery.append ("', key_jahia_sites='"); 418 sqlQuery.append (JahiaTools.quote (site.getSiteKey ())); 419 420 sqlQuery.append ("', active_jahia_sites="); 421 422 if (site.isActive ()) { 423 sqlQuery.append (1); 424 } else { 425 sqlQuery.append (0); 426 } 427 428 sqlQuery.append (", defaultpageid_jahia_sites="); 429 sqlQuery.append (site.getHomePageID ()); 430 sqlQuery.append (", defaulttemplateid_jahia_sites="); 431 sqlQuery.append (site.getDefaultTemplateID ()); 432 433 sqlQuery.append (", tpl_deploymode_jahia_sites="); 434 435 if (site.getTemplatesAutoDeployMode ()) { 436 sqlQuery.append (1); 437 } else { 438 sqlQuery.append (0); 439 } 440 441 sqlQuery.append (", webapps_deploymode_jahia_sites="); 442 443 if (site.getWebAppsAutoDeployMode ()) { 444 sqlQuery.append (1); 445 } else { 446 sqlQuery.append (0); 447 } 448 449 sqlQuery.append (", rights_jahia_sites="); 450 sqlQuery.append (site.getAclID ()); 451 sqlQuery.append (", descr_jahia_sites='"); 452 sqlQuery.append (JahiaTools.quote (site.getDescr ())); 453 sqlQuery.append ("' where id_jahia_sites="); 454 sqlQuery.append (site.getID ()); 455 456 executeQueryNoResultSet (sqlQuery.toString ()); 457 458 } catch (JahiaException je) { 459 String errorMsg = "Error in dbUpdateSite( JahiaSite site ) : " + je.getMessage (); 460 logger.debug ("Error while updating site : ", je); 461 throw new JahiaException ("Cannot update site in the database", 462 errorMsg, 463 JahiaException.DATABASE_ERROR, 464 JahiaException.CRITICAL_SEVERITY, 465 je); 466 } 467 468 updateSiteProps (site); 469 } 470 471 472 477 protected JahiaSite getJahiaSiteFromResultSet (ResultSet rs) throws JahiaException { 478 479 JahiaSite site = null; 480 481 if (rs != null) { 482 483 int id = 0; 484 String title = ""; 485 String serverName = ""; 486 String siteKey = ""; 487 boolean isActive = false; 488 int defaultPageID = -1; 489 int defaultTemplateID = -1; 490 boolean templatesAutoDeployMode = true; 491 boolean webAppsAutoDeployMode = true; 492 String descr = ""; 493 int aclID = 0; 494 495 try { 496 497 id = rs.getInt ("id_jahia_sites"); 498 title = rs.getString ("title_jahia_sites"); 499 serverName = rs.getString ("servername_jahia_sites"); 500 siteKey = rs.getString ("key_jahia_sites"); 501 isActive = (rs.getInt ("active_jahia_sites") == 1); 502 defaultPageID = rs.getInt ("defaultpageid_jahia_sites"); 503 defaultTemplateID = rs.getInt ("defaulttemplateid_jahia_sites"); 504 templatesAutoDeployMode = (rs.getInt ("tpl_deploymode_jahia_sites") == 1); 505 webAppsAutoDeployMode = (rs.getInt ("webapps_deploymode_jahia_sites") == 1); 506 aclID = rs.getInt ("rights_jahia_sites"); 507 descr = rs.getString ("descr_jahia_sites"); 508 509 } catch (SQLException se) { 510 String errorMsg = "Error in getJahiaSiteFromResultSet(rs) : " + se.getMessage (); 511 logger.debug ("Error while converting result set to JahiaSite : ", se); 512 throw new JahiaException ("Cannot read data from resultset", 513 errorMsg, 514 JahiaException.DATABASE_ERROR, 515 JahiaException.CRITICAL_SEVERITY, 516 se); 517 } 518 519 JahiaBaseACL acl = null; 520 try { 521 acl = new JahiaBaseACL (); 522 acl.load (aclID); 523 524 } catch (JahiaException je) { 525 logger.debug ("Error while retrieving site ACL : ", je); 526 acl = null; 527 } 528 529 site = new JahiaSite ( 530 id, 531 title, 532 serverName, 533 siteKey, 534 isActive, 535 defaultPageID, 536 descr, 537 acl, 538 null 539 ); 540 site.setDefaultTemplateID (defaultTemplateID); 541 site.setTemplatesAutoDeployMode (templatesAutoDeployMode); 542 site.setWebAppsAutoDeployMode (webAppsAutoDeployMode); 543 } 544 545 546 return site; 547 } 548 549 550 553 private String getOrderCond (int orderType) { 554 555 String orderCond = " ORDER BY title_jahia_sites "; 557 558 switch (orderType) { 559 case ORDER_BY_SERVER_NAME: 560 orderCond = " ORDER BY servername_jahia_sites "; 561 break; 562 } 563 564 return orderCond; 565 } 566 567 568 573 public synchronized int getNbSites () 574 throws JahiaDatabaseException { 575 int counter = -1; 576 577 Connection dbConn = null; 578 PreparedStatement statement = null; 579 580 try { 581 dbConn = ConnectionDispenser.getConnection (); 582 583 String query = "SELECT COUNT(key_jahia_sites) as nbItems FROM jahia_sites"; 584 585 statement = dbConn.prepareStatement (query); 586 ResultSet rs = statement.executeQuery (); 587 if (rs != null && rs.next ()) { 588 counter = rs.getInt ("nbItems"); 589 } 590 } catch (SQLException ex) { 591 throw new JahiaDatabaseException ("Database error.", ex, 592 JahiaDatabaseException. 593 ERROR_SEVERITY); 594 } finally { 595 closeStatement (statement); 596 } 597 return counter; 598 } 599 600 601 607 String getProperty (int siteID, String key) 608 throws JahiaException { 609 610 Connection dbConn = null; 611 PreparedStatement statement = null; 612 String value = null; 613 614 if (key == null || key.trim ().length() == 0) 615 return null; 616 617 try { 618 String query = 619 "SELECT value_jahia_site_prop FROM jahia_site_prop WHERE id_jahia_site=? AND name_jahia_site_prop=?"; 620 621 dbConn = ConnectionDispenser.getConnection (); 622 statement = dbConn.prepareStatement (query); 623 statement.setInt(1, siteID); 624 statement.setString(2, JahiaTools.quote (key)); 625 ResultSet rs = statement.executeQuery (); 626 if (rs != null && rs.next ()) { 627 value = rs.getString ("value_jahia_site_prop"); 628 } 629 } catch (SQLException se) { 630 logger.debug ("Error while retrieving property siteID=" + siteID + " key=" + key, 631 se); 632 throw new JahiaException (CLASS_NAME + ".getProperty(siteID,key)", 633 "Cannot load site's prop from the database" 634 + se.getMessage (), 635 JahiaException.DATABASE_ERROR, 636 JahiaException.CRITICAL_SEVERITY, 637 se); 638 } finally { 639 640 closeStatement (statement); 641 } 642 643 return value; 644 645 } 646 647 648 656 void addProperty (int siteID, String key, String value) throws JahiaException { 657 658 if (key == null || key.trim().length() == 0) 659 return; 660 661 Connection dbConn = null; 662 PreparedStatement statement = null; 663 String query = 664 "INSERT INTO jahia_site_prop VALUES (?, ?, ?)"; 665 666 try { 667 dbConn = ConnectionDispenser.getConnection (); 668 statement = dbConn.prepareStatement (query); 669 statement.setInt(1, siteID); 670 statement.setString(2, JahiaTools.quote(key)); 671 statement.setString(3, JahiaTools.quote (value)); 672 673 statement.executeUpdate (); 674 } catch (SQLException se) { 675 String errorMsg = "Error in addProperty : " + se.getMessage (); 676 logger.debug ("Error while executing update-type query (" + query + ") : ", se); 677 throw new JahiaException ("Cannot execute query" + query, 678 errorMsg, JahiaException.DATABASE_ERROR, 679 JahiaException.CRITICAL_SEVERITY, se); 680 } finally { 681 682 closeStatement (statement); 683 } 684 } 685 686 693 void updateProperty (int siteID, String key, String value) throws JahiaException { 694 695 if (key == null || key.trim().length() == 0) 696 return; 697 698 Connection dbConn = null; 699 PreparedStatement statement = null; 700 String query = 701 "UPDATE jahia_site_prop SET value_jahia_site_prop=? WHERE id_jahia_site=? AND name_jahia_site_prop=?"; 702 703 try { 704 dbConn = ConnectionDispenser.getConnection (); 705 statement = dbConn.prepareStatement (query); 706 statement.setString(1, JahiaTools.quote (value)); 707 statement.setInt(2, siteID); 708 statement.setString(3, JahiaTools.quote(key)); 709 710 int rowCount = statement.executeUpdate (); 711 if (rowCount == 0) { 712 addProperty(siteID, key, value); 713 } 714 } catch (SQLException se) { 715 String errorMsg = "Error in addProperty : " + se.getMessage (); 716 logger.debug ("Error while executing update-type query (" + query + ") : ", se); 717 throw new JahiaException ("Cannot execute query" + query, 718 errorMsg, JahiaException.DATABASE_ERROR, 719 JahiaException.CRITICAL_SEVERITY, se); 720 } finally { 721 closeStatement (statement); 722 } 723 } 724 725 730 void deleteProperties (int siteID) throws JahiaException { 731 732 try { 733 StringBuffer buff = 734 new StringBuffer ("DELETE FROM jahia_site_prop WHERE id_jahia_site="); 735 buff.append (siteID); 736 executeQueryNoResultSet (buff.toString ()); 737 } catch (JahiaException je) { 738 String errorMsg = "Error in deleteProperties(siteID) : " 739 + je.getMessage (); 740 logger.debug ("Error while deleting site properties for siteID=" + siteID, je); 741 742 throw new JahiaException ("Cannot delete all site's properties in the database", 743 errorMsg, 744 JahiaException.DATABASE_ERROR, 745 JahiaException.CRITICAL_SEVERITY, je); 746 } 747 } 748 749 750 756 void deleteProperty (int id, String key) throws JahiaException { 757 758 if (key == null || key.trim ().length() == 0) 759 return; 760 Connection dbConn = null; 761 PreparedStatement statement = null; 762 String sqlQuery = 763 "DELETE FROM jahia_site_prop WHERE id_jahia_site=? AND name_jahia_site_prop=?"; 764 try { 765 dbConn = ConnectionDispenser.getConnection (); 766 statement = dbConn.prepareStatement (sqlQuery); 767 statement.setInt(1, id); 768 statement.setString(2, JahiaTools.quote (key)); 769 770 statement.executeUpdate (); 771 } catch (SQLException se) { 772 String errorMsg = "Error in deleteProperty : " + se.getMessage (); 773 logger.debug ("Error while executing update-type query (" + sqlQuery + ") : ", se); 774 throw new JahiaException ("Cannot execute query" + sqlQuery, 775 errorMsg, JahiaException.DATABASE_ERROR, 776 JahiaException.CRITICAL_SEVERITY, se); 777 } finally { 778 closeStatement (statement); 779 } 780 } 781 782 private void executeQueryNoResultSet (String queryStr) throws JahiaException { 783 784 Connection dbConn = null; 785 Statement statement = null; 786 787 try { 788 dbConn = ConnectionDispenser.getConnection (); 789 statement = dbConn.createStatement (); 790 if (statement != null) { 791 statement.executeUpdate (queryStr); 792 } 793 } catch (SQLException se) { 794 String errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage (); 795 logger.debug ("Error while executing update-type query (" + queryStr + ") : ", se); 796 throw new JahiaException ("Cannot execute query" + queryStr, 797 errorMsg, JahiaException.DATABASE_ERROR, 798 JahiaException.CRITICAL_SEVERITY, se); 799 } finally { 800 801 closeStatement (statement); 802 } 803 804 } 805 806 807 private void closeStatement (Statement statement) { 808 try { 810 if (statement != null) { 811 statement.close (); 812 statement = null; 813 } 814 } catch (SQLException sqlEx) { 815 logger.warn("Cannot close a statement", sqlEx); 816 } 817 } 818 819 820 827 public JahiaDOMObject getSiteAsDOM (int id) throws JahiaException { 828 829 Connection dbConn = null; 830 Statement statement = null; 831 JahiaDBDOMObject dom = null; 832 833 try { 834 String sqlQuery = "SELECT * FROM jahia_sites where id_jahia_sites=" + id; 835 836 dbConn = ConnectionDispenser.getConnection (); 837 statement = dbConn.createStatement (); 838 if (statement != null) { 839 ResultSet rs = statement.executeQuery (sqlQuery); 840 if (rs != null) { 841 dom = new JahiaDBDOMObject (); 842 dom.addTable ("jahia_sites", rs); 843 return dom; 844 } 845 } 846 } catch (SQLException se) { 847 String errorMsg = "Error in getSiteAsDOM(int id) : " + se.getMessage (); 848 logger.debug ("Error while converting site " + id + " to DOM : ", se); 849 throw new JahiaException ("Cannot load site from the database", 850 errorMsg, JahiaException.DATABASE_ERROR, 851 JahiaException.CRITICAL_SEVERITY, se); 852 } finally { 853 854 closeStatement (statement); 855 } 856 857 return dom; 858 } 859 860 861 868 public JahiaDOMObject getSitePropsAsDOM (int id) throws JahiaException { 869 870 Connection dbConn = null; 871 Statement statement = null; 872 JahiaDBDOMObject dom = null; 873 874 try { 875 String sqlQuery = "SELECT * FROM jahia_site_prop where id_jahia_site=" + id; 876 877 dbConn = ConnectionDispenser.getConnection (); 878 879 statement = dbConn.createStatement (); 880 if (statement != null) { 881 ResultSet rs = statement.executeQuery (sqlQuery); 882 if (rs != null) { 883 dom = new JahiaDBDOMObject (); 884 dom.addTable ("jahia_site_prop", rs); 885 return dom; 886 } 887 } 888 } catch (SQLException se) { 889 String errorMsg = "Error in getSitePropsAsDOM(int id) : " + se.getMessage (); 890 logger.debug ("Error while converting site properties to DOM for siteID=" + id, se); 891 throw new JahiaException ("Cannot load data from the database", 892 errorMsg, JahiaException.DATABASE_ERROR, 893 JahiaException.CRITICAL_SEVERITY, se); 894 } finally { 895 896 closeStatement (statement); 897 } 898 899 return dom; 900 } 901 902 903 912 public Properties getSiteProps (int id) throws JahiaException { 913 914 Connection dbConn = null; 915 PreparedStatement statement = null; 916 Properties props = new Properties (); 917 918 try { 919 String sqlQuery = "SELECT * FROM jahia_site_prop where id_jahia_site=?"; 920 921 dbConn = ConnectionDispenser.getConnection (); 922 923 statement = dbConn.prepareStatement (sqlQuery); 924 statement.setInt(1, id); 925 ResultSet rs = statement.executeQuery (); 926 while (rs.next ()) { 927 String propName = rs.getString ("name_jahia_site_prop"); 928 String propValue = rs.getString ("value_jahia_site_prop"); 929 props.setProperty (propName, propValue); 930 } 931 932 } catch (SQLException se) { 933 String errorMsg = "Error in getSitePropsAsDOM(int id) : " + se.getMessage (); 934 logger.debug ("Error while converting site properties to DOM for siteID=" + id, se); 935 throw new JahiaException ("Cannot load data from the database", 936 errorMsg, JahiaException.DATABASE_ERROR, 937 JahiaException.CRITICAL_SEVERITY, se); 938 939 } finally { 940 closeStatement (statement); 941 } 942 943 return props; 944 } 945 946 947 954 void updateSiteProps (JahiaSite site) throws JahiaException { 955 Properties props = site.getSettings (); 956 if (props == null) { 957 return; 958 } 959 960 Enumeration enumeration = props.propertyNames (); 961 while (enumeration.hasMoreElements ()) { 962 String name = (String ) enumeration.nextElement (); 963 String value = props.getProperty (name); 964 965 updateProperty (site.getID (), name, value); 966 } 967 } 968 969 } 970 | Popular Tags |