1 13 package org.jahia.services.filemanager; 14 15 16 import org.jahia.data.JahiaDBDOMObject; 17 import org.jahia.data.JahiaDOMObject; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.registries.ServicesRegistry; 20 21 import java.sql.Connection ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.util.Vector ; 26 27 28 34 public class FolderDB { 35 private static org.apache.log4j.Logger logger = 36 org.apache.log4j.Logger.getLogger (FolderDB.class); 37 38 private static boolean m_debugQuery = false; 39 private static FolderDB m_instance = null; 40 41 44 protected FolderDB () { 45 } 46 47 53 public static FolderDB getInstance () { 54 if (m_instance == null) { 55 m_instance = new FolderDB (); 56 } 57 return m_instance; 58 } 59 60 61 69 public boolean insert (Folder folder) { 70 boolean success = false; 71 Connection dbConn = null; 72 Statement stmt = null; 73 74 try { 75 int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 76 .autoIncrement ("jahia_filemgr_folders"); 77 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 79 80 81 String query = " insert into jahia_filemgr_folders (id_jahia_fld,filemanagerid_jahia_fld, parent_jahia_fld, fldname_jahia_fld)" + 83 " values(" + 84 id + "," + 85 folder.getFilemanagerID () + "," + 86 folder.getParentID () + "," + 87 "'" + filter (folder.getFolderName ()) + "')"; 88 89 stmt = dbConn.createStatement (); 90 logger.debug (query); 91 stmt.execute (query); 92 success = true; 93 } catch (SQLException ex) { 94 processSQLException ("FolderDB::insert()", ex); 95 } catch (JahiaException je) { 96 logger.error (je.getMessage ()); 97 } finally { 98 try { 99 100 if (stmt != null) stmt.close (); 101 } catch (SQLException ex) { 102 processSQLException ("FolderDB:: insert() freeing connection fails", ex); 103 } 104 } 105 106 return success; 107 108 } 109 110 111 119 public boolean update (Folder folder) { 120 boolean success = false; 121 Connection dbConn = null; 122 Statement stmt = null; 123 124 try { 125 126 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 128 129 String query = " update jahia_filemgr_folders set " + 131 "filemanagerid_jahia_fld=" + folder.getFilemanagerID () + "," + 132 "parent_jahia_fld=" + folder.getParentID () + "," + 133 "fldname_jahia_fld='" + filter (folder.getFolderName ()) + "'" + 134 " where id_jahia_fld=" + folder.getFolderID (); 135 136 stmt = dbConn.createStatement (); 137 logger.debug (query); 138 stmt.executeUpdate (query); 139 success = true; 140 } catch (SQLException ex) { 141 processSQLException ("FolderDB::update()", ex); 142 } finally { 143 try { 144 145 if (stmt != null) stmt.close (); 146 } catch (SQLException ex) { 147 processSQLException ("FolderDB:: update() freeing connection fails", ex); 148 } 149 } 150 return success; 151 152 } 153 154 155 163 public boolean delete (int folderID) { 164 boolean success = false; 165 Connection dbConn = null; 166 Statement stmt = null; 167 168 try { 169 170 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 172 173 String query = " delete from jahia_filemgr_folders where id_jahia_fld=" + folderID; 175 176 stmt = dbConn.createStatement (); 177 logger.debug (query); 178 stmt.execute (query); 179 success = true; 180 } catch (SQLException ex) { 181 processSQLException ("FolderDB::delete()", ex); 182 } finally { 183 try { 184 185 if (stmt != null) stmt.close (); 186 } catch (SQLException ex) { 187 processSQLException ("FolderDB:: delete() freeing connection fails", ex); 188 } 189 } 190 return success; 191 192 } 193 194 195 203 public Folder getFolder (int folderID) { 204 205 Folder folder = null; 206 Connection dbConn = null; 207 Statement stmt = null; 208 ResultSet rs = null; 209 210 try { 211 212 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 214 215 String query = " select * from jahia_filemgr_folders where id_jahia_fld=" + folderID; 217 218 stmt = dbConn.createStatement (); 219 logger.debug (query); 220 rs = stmt.executeQuery (query); 221 while (rs.next ()) { 222 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld"); 223 int parentID = rs.getInt ("parent_jahia_fld"); 224 String folderName = rs.getString ("fldname_jahia_fld"); 225 folder = new Folder (filemanagerID, 226 parentID, 227 folderName); 228 229 folder.setFolderID (folderID); 230 } 231 } catch (SQLException ex) { 232 processSQLException ("FolderDB::getFolder(id)", ex); 233 } finally { 234 try { 235 236 if (stmt != null) stmt.close (); 237 if (rs != null) rs.close (); 238 } catch (SQLException ex) { 239 processSQLException ("FolderDB::getFolder(folderID) freeing connection fails", 240 ex); 241 } 242 } 243 return folder; 244 245 } 246 247 248 256 public Folder getFolderByFile (int fileID) { 257 258 Folder folder = null; 259 Connection dbConn = null; 260 Statement stmt = null; 261 ResultSet rs = null; 262 int folderID = -1; 263 264 try { 265 266 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 268 269 String query = " select * from jahia_filemgr_files where id_jahia_file=" + fileID; 271 272 stmt = dbConn.createStatement (); 273 logger.debug (query); 274 rs = stmt.executeQuery (query); 275 if (rs.next ()) { 276 folderID = rs.getInt ("fldid_jahia_file"); 277 278 } 279 } catch (SQLException ex) { 280 processSQLException ("FolderDB::getFolder(id)", ex); 281 } finally { 282 try { 283 284 if (stmt != null) stmt.close (); 285 } catch (SQLException ex) { 286 processSQLException ("FolderDB::getFolder(folderID) freeing connection fails", 287 ex); 288 } 289 } 290 291 if (folderID != -1) { 292 folder = FolderDB.getInstance ().getFolder (folderID); 293 } 294 return folder; 295 296 } 297 298 299 307 public Vector getFoldersByParent (int parentID) { 308 309 Folder folder = null; 310 Vector folders = null; 311 Connection dbConn = null; 312 Statement stmt = null; 313 ResultSet rs = null; 314 315 try { 316 317 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 319 320 String query = "select * from jahia_filemgr_folders where bm_parent_id=" + parentID; 322 323 stmt = dbConn.createStatement (); 324 logger.debug (query); 325 rs = stmt.executeQuery (query); 326 while (rs.next ()) { 327 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld"); 328 int folderID = rs.getInt ("fld_folder_id"); 329 String folderName = rs.getString ("fldname_jahia_fld"); 330 folder = new Folder (filemanagerID, 331 parentID, 332 folderName); 333 334 folder.setFolderID (folderID); 335 folders.add (folder); 336 } 337 } catch (SQLException ex) { 338 processSQLException ("FolderDB::getFoldersByParent(id)", ex); 339 } finally { 340 try { 341 342 if (stmt != null) stmt.close (); 343 } catch (SQLException ex) { 344 processSQLException ( 345 "FolderDB:: getFoldersByParent(parentID) freeing connection fails", 346 ex); 347 } 348 } 349 return folders; 350 351 } 352 353 354 363 public Vector getFolders (String whereSql, String orderSql) { 364 365 Folder folder = null; 366 Vector folders = new Vector (); 367 Connection dbConn = null; 368 Statement stmt = null; 369 ResultSet rs = null; 370 371 if (whereSql == null) { 372 whereSql = ""; 373 } 374 375 if (orderSql == null) { 376 orderSql = ""; 377 } 378 379 try { 380 381 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 383 384 String query = "select * from jahia_filemgr_folders " + whereSql + " " + orderSql; 386 387 stmt = dbConn.createStatement (); 388 logger.debug (query); 389 rs = stmt.executeQuery (query); 390 while (rs.next ()) { 391 int folderID = rs.getInt ("id_jahia_fld"); 392 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld"); 393 int parentID = rs.getInt ("parent_jahia_fld"); 394 String folderName = rs.getString ("fldname_jahia_fld"); 395 folder = new Folder (filemanagerID, 396 parentID, 397 folderName); 398 399 folder.setFolderID (folderID); 400 folders.addElement (folder); 401 } 402 } catch (SQLException ex) { 403 processSQLException ("FolderDB::getFolders( where, order )", ex); 404 } finally { 405 try { 406 407 if (stmt != null) stmt.close (); 408 } catch (SQLException ex) { 409 processSQLException ( 410 "FolderDB::getFolders(where, order) freeing connection fails", ex); 411 } 412 } 413 414 return folders; 415 416 } 417 418 419 427 public Vector getFoldersByFilemanager (int filemanagerID) { 428 429 Folder folder = null; 430 Vector folders = null; 431 Connection dbConn = null; 432 Statement stmt = null; 433 ResultSet rs = null; 434 435 try { 436 437 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 439 440 String query = "select * from jahia_filemgr_folders where filemanagerid_jahia_fld=" + filemanagerID; 442 443 stmt = dbConn.createStatement (); 444 logger.debug (query); 445 rs = stmt.executeQuery (query); 446 while (rs.next ()) { 447 int folderID = rs.getInt ("fld_folder_id"); 448 int parentID = rs.getInt ("parent_jahia_fld"); 449 String folderName = rs.getString ("fldname_jahia_fld"); 450 folder = new Folder (filemanagerID, 451 parentID, 452 folderName); 453 454 folder.setFolderID (folderID); 455 folders.add (folder); 456 } 457 } catch (SQLException ex) { 458 processSQLException ("FolderDB::getFoldersByFilemanager(id)", ex); 459 } finally { 460 try { 461 462 if (stmt != null) stmt.close (); 463 } catch (SQLException ex) { 464 processSQLException ( 465 "FolderDB::getFoldersByFilemanager(filemanagerID) freeing connection fails", 466 ex); 467 } 468 } 469 470 return folders; 471 472 } 473 474 475 483 public Folder getRootFolder (int fmngID) { 484 485 Folder folder = null; 486 Connection dbConn = null; 487 Statement stmt = null; 488 ResultSet rs = null; 489 490 try { 491 492 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 494 495 String query = " select * from jahia_filemgr_folders where parent_jahia_fld=-1 and filemanagerid_jahia_fld=" + fmngID; 497 498 stmt = dbConn.createStatement (); 499 logger.debug (query); 500 rs = stmt.executeQuery (query); 501 while (rs.next ()) { 502 int folderID = rs.getInt ("id_jahia_fld"); 503 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld"); 504 int parentID = rs.getInt ("parent_jahia_fld"); 505 String folderName = rs.getString ("fldname_jahia_fld"); 506 folder = new Folder (filemanagerID, 507 parentID, 508 folderName); 509 510 folder.setFolderID (folderID); 511 } 512 } catch (SQLException ex) { 513 processSQLException ("FolderDB::getRootFolder()", ex); 514 } finally { 515 try { 516 517 if (stmt != null) stmt.close (); 518 } catch (SQLException ex) { 519 processSQLException ("FolderDB::getRootFolder() freeing connection fails", ex); 520 } 521 } 522 523 return folder; 524 525 } 526 527 528 538 public JahiaDOMObject getFileMgrFoldersAsDOM (int siteID) 539 throws JahiaException { 540 541 Connection dbConn = null; 542 Statement statement = null; 543 544 String output = null; 545 JahiaDBDOMObject dom = null; 546 547 try { 548 549 Filemanager fmng = FilemanagerDB.getInstance ().getFilemanagerByOwner (siteID); 550 if (fmng == null) { 551 dom = new JahiaDBDOMObject (); 552 dom.addTable ("jahia_filemgr_folders", null); 553 return dom; 554 } 555 556 String sqlQuery = "SELECT * FROM jahia_filemgr_folders where filemanagerid_jahia_fld=" + fmng.getFilemanagerID (); 557 558 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 559 statement = dbConn.createStatement (); 560 if (statement != null) { 561 ResultSet rs = statement.executeQuery (sqlQuery); 562 if (rs != null) { 563 dom = new JahiaDBDOMObject (); 564 dom.addTable ("jahia_filemgr_folders", rs); 565 return dom; 566 } 567 } 568 } catch (SQLException se) { 569 String errorMsg = "Error in getFileMgrFoldersAsDOM(int siteID) : " + se.getMessage (); 570 logger.error (errorMsg + " -> BAILING OUT"); 571 throw new JahiaException ("Cannot load filemanager folders from the database", 572 errorMsg, JahiaException.DATABASE_ERROR, 573 JahiaException.CRITICAL_SEVERITY); 574 } finally { 575 576 closeStatement (statement); 577 } 578 579 return dom; 580 } 581 582 583 private void closeStatement (Statement statement) { 585 try { 587 if (statement != null) { 588 statement.close (); 589 } 590 } catch (SQLException sqlEx) { 591 JahiaException je = new JahiaException ("Cannot close a statement", 594 "Cannot close a statement", JahiaException.DATABASE_ERROR, 595 JahiaException.WARNING_SEVERITY, sqlEx); 596 logger.error ("Error:", je); 597 } 598 } 599 600 601 609 public boolean executeQuery (String query) { 610 611 Connection dbConn = null; 612 Statement stmt = null; 613 boolean success = false; 614 615 try { 616 617 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 619 620 stmt = dbConn.createStatement (); 621 logger.debug (query); 622 stmt.executeQuery (query); 623 success = true; 624 } catch (SQLException ex) { 625 processSQLException ("FolderDB::executeQuery()", ex); 626 } finally { 627 try { 628 629 if (stmt != null) stmt.close (); 630 } catch (SQLException ex) { 631 processSQLException ("FolderDB:: executeQuery() freeing connection fails", ex); 632 } 633 return success; 634 } 635 } 636 637 645 public static String filter (String input) { 646 StringBuffer filtered = new StringBuffer (input.length ()); 647 char c; 648 for (int i = 0; i < input.length (); i++) { 649 c = input.charAt (i); 650 if (c == '\'') { 651 filtered.append ("'"); 652 } else if (c == '"') { 653 filtered.append ("""); 654 } else { 655 filtered.append (c); 656 } 657 } 658 return (filtered.toString ()); 659 } 660 661 668 public void processSQLException (String func, SQLException ex) { 669 if (m_debugQuery) { 670 671 while (ex != null) { 672 logger.debug ( 673 "SQL EXCEPTION in function [" + func + "]: SqlState = " + ex.getSQLState () + " Error code = " + ex.getErrorCode () + " Error msg = " + ex.toString ()); 674 ex = ex.getNextException (); 675 } 676 } 677 } 678 679 } 680 681 | Popular Tags |