1 13 package org.jahia.services.filemanager; 14 15 16 import org.jahia.data.files.JahiaFile; 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.registries.ServicesRegistry; 19 import org.jahia.services.sites.JahiaSite; 20 21 import java.io.File ; 22 import java.util.Hashtable ; 23 import java.util.Vector ; 24 25 26 33 public class Filemanager { 34 35 private static org.apache.log4j.Logger logger = 36 org.apache.log4j.Logger.getLogger (Filemanager.class); 37 38 39 private int m_FilemanagerID = -1; 41 42 private int m_OwnerID; 44 45 private String m_StoragePath = ""; 46 47 48 private Vector m_FolderTree; 49 50 51 private Hashtable parents; 55 56 private Folder m_ExcludeBranchFolder; 59 60 private boolean m_FolderTreeLoaded = false; 61 62 63 private FilemanagerDB m_FmngDB; 64 65 66 private FolderDB m_FolderDB; 67 68 69 72 public Filemanager () { 73 } 74 75 76 84 public boolean create (int ownerID) { 85 86 boolean success = true; 87 88 Filemanager tmpFilemanager = FilemanagerDB.getInstance ().getFilemanagerByOwner ( 90 ownerID); 91 92 if (tmpFilemanager == null) { 93 94 this.setOwnerID (ownerID); 95 96 JahiaSite site = null; 97 try { 98 site = ServicesRegistry.getInstance () 100 .getJahiaSitesService () 101 .getSite (ownerID); 102 } catch (JahiaException je) { 103 return false; 104 } 105 106 if (site == null) { 107 return false; 108 } 109 File storageFolder = ServicesRegistry.getInstance ().getJahiaFilemanagerService () 110 .createDirectory (site); 111 112 this.setStoragePath (storageFolder.getName ()); 113 114 logger.debug ("Insert new Filemanager"); 115 if (!FilemanagerDB.getInstance ().insert (this)) { 116 success = false; 117 } else { 118 119 Folder folder = new Folder (this.getFilemanagerID (), 121 -1, "root"); 123 124 if (!FolderDB.getInstance ().insert (folder)) { FilemanagerDB.getInstance ().delete (this.getFilemanagerID ()); 126 success = false; 127 } 128 } 129 } else { 130 File tmpFile = new File ( 131 ServicesRegistry.getInstance ().getJahiaFilemanagerService () 132 .getFileRepositoryRootPath () + File.separator + tmpFilemanager.getStoragePath ()); 133 if (tmpFile == null || !tmpFile.isDirectory ()) { 134 tmpFile.mkdirs (); 135 136 } 137 } 138 139 return success; 140 } 141 142 143 152 public boolean initializeWithOwner (int ownerID) { 153 154 this.setOwnerID (ownerID); 155 156 Filemanager tmpFilemanager = FilemanagerDB.getInstance ().getFilemanagerByOwner ( 158 this.getOwnerID ()); 159 160 if (tmpFilemanager != null) { 161 this.setStoragePath (tmpFilemanager.getStoragePath ()); 162 this.setFilemanagerID (tmpFilemanager.getFilemanagerID ()); 163 return true; 164 } else { 165 166 if (this.create (ownerID)) { 168 tmpFilemanager = 169 FilemanagerDB.getInstance ().getFilemanagerByOwner (this.getOwnerID ()); 170 171 if (tmpFilemanager != null) { 172 this.setStoragePath (tmpFilemanager.getStoragePath ()); 173 this.setFilemanagerID (tmpFilemanager.getFilemanagerID ()); 174 return true; 175 } else { 176 return false; 177 } 178 } 179 return false; 180 } 181 } 182 183 184 194 public boolean initialize (int filemanagerID) { 195 196 this.setFilemanagerID (filemanagerID); 197 198 Filemanager tmpFilemanager = FilemanagerDB.getInstance ().getFilemanager ( 200 this.getFilemanagerID ()); 201 if (tmpFilemanager != null) { 202 this.setStoragePath (tmpFilemanager.getStoragePath ()); 203 this.setOwnerID (tmpFilemanager.getOwnerID ()); 204 return true; 205 } else { 206 return false; 207 } 208 } 209 210 211 218 public Vector getFolderTree () { 219 220 if (!m_FolderTreeLoaded) { 221 222 this.parents = null; 224 225 String whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID (); 227 String orderSql = " order by parent_jahia_fld, fldname_jahia_fld "; 228 Vector foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql); 229 Vector orderedFolders = new Vector (); 230 231 if (foldersTree != null) { 232 233 Folder rootFolder = (Folder) foldersTree.firstElement (); 234 rootFolder.setDepth (0); 235 orderedFolders.add (rootFolder); 236 foldersTree.removeElementAt (0); 237 this.sortFolders (foldersTree, orderedFolders, rootFolder.getFolderID (), 238 rootFolder.getDepth ()); 239 240 foldersTree = orderedFolders; 241 } 242 logger.debug ("buildFolderTree, ordered folders :" + foldersTree.size ()); 243 244 if (this.m_ExcludeBranchFolder != null) { 246 foldersTree = removeBranch (this.m_ExcludeBranchFolder, orderedFolders); 247 } 248 249 m_FolderTree = foldersTree; 250 m_FolderTreeLoaded = true; 251 } 252 return m_FolderTree; 253 } 254 255 256 264 public Vector getFolderTree (Folder activeFolder) { 265 266 if (!m_FolderTreeLoaded) { 267 268 Hashtable parents = new Hashtable (); 270 activeFolder.getParents (parents); 271 this.setParents (parents); 272 273 String whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID (); 275 String orderSql = " order by parent_jahia_fld, fldname_jahia_fld "; 276 Vector foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql); 277 Vector orderedFolders = new Vector (); 278 279 if (foldersTree != null) { 280 281 Folder rootFolder = (Folder) foldersTree.firstElement (); 282 rootFolder.setDepth (0); 283 orderedFolders.add (rootFolder); 284 foldersTree.removeElementAt (0); 285 this.sortFolders (foldersTree, orderedFolders, rootFolder.getFolderID (), 286 rootFolder.getDepth ()); 287 288 foldersTree = orderedFolders; 289 } 290 logger.debug ("buildFolderTree, ordered folders :" + foldersTree.size ()); 291 292 m_FolderTree = foldersTree; 293 m_FolderTreeLoaded = true; 294 } 295 return m_FolderTree; 296 } 297 298 299 303 public void loadFolderTree () { 304 305 m_FolderTreeLoaded = false; 306 getFolderTree (); 307 } 308 309 310 316 public void loadFolderTree (Folder activeFolder) { 317 318 m_FolderTreeLoaded = false; 319 getFolderTree (activeFolder); 320 } 321 322 323 331 public Vector getBranch (Folder folder) { 332 333 String whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID (); 335 String orderSql = " order by parent_jahia_fld, fldname_jahia_fld "; 336 Vector foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql); 337 Vector orderedFolders = new Vector (); 338 339 if (foldersTree != null) { 340 341 folder.setDepth (0); 342 orderedFolders.add (folder); 343 this.sortFolders (foldersTree, orderedFolders, folder.getFolderID (), 344 folder.getDepth ()); 345 346 foldersTree = orderedFolders; 347 } 348 logger.debug ("buildFolderTree(folder), ordered folders :" + foldersTree.size ()); 349 return foldersTree; 350 } 351 352 353 361 protected void sortFolders (Vector folders, Vector orderedFolders, int parentID, 362 int currentDepth) { 363 364 for (int i = 0; i < folders.size (); i++) { 365 Folder tmpFolder = null; 366 tmpFolder = (Folder) folders.get (i); 367 logger.debug (" parent id is: " + parentID + "\n"); 368 logger.debug (" sorted folder is : " + tmpFolder.getFolderName () + "\n"); 369 370 if (tmpFolder.getParentID () == parentID) { 371 372 logger.debug (" folder " + tmpFolder.getFolderName () + " is added \n"); 373 tmpFolder.setDepth (currentDepth + 1); 374 375 if (this.parents != null && (parents.size () > 0)) { 376 if (parents.get (new Integer (tmpFolder.getFolderID ())) != null) { 377 orderedFolders.add (tmpFolder); 378 sortFolders (folders, orderedFolders, tmpFolder.getFolderID (), 381 currentDepth + 1); 382 } else { 383 orderedFolders.add (tmpFolder); 384 } 387 } else { 388 orderedFolders.add (tmpFolder); 389 sortFolders (folders, orderedFolders, tmpFolder.getFolderID (), 392 currentDepth + 1); 393 394 } 395 396 } 397 } 398 } 399 400 401 411 protected Vector removeBranch (Folder activeFolder, Vector folderTree) { 412 413 logger.debug (" removeBranch started "); 414 415 int size = folderTree.size (); 416 Folder folder = null; 417 Vector newTree = new Vector (); 418 419 int depth = 0; 420 421 for (int i = 0; i < size; i++) { 422 folder = (Folder) folderTree.get (i); 423 424 logger.debug ( 425 "removeBranch: " + folder.getFolderName () + ", depth=" + folder.getDepth ()); 426 427 if (folder.getFolderID () == activeFolder.getFolderID ()) { 428 429 activeFolder.setDepth (folder.getDepth ()); 431 432 i += 1; for (int j = i; j < size; j++) { 434 folder = (Folder) folderTree.get (j); 435 logger.debug ( 436 "removeBranch: remove child " + folder.getFolderName () + ", depth=" + folder.getDepth ()); 437 438 if (!(folder.getDepth () > activeFolder.getDepth ())) { 440 i = (j - 1); 441 break; 442 } else if (j == (size - 1)) { i = size; 444 } 445 } 446 } else { 447 newTree.add (folder); 448 } 449 } 450 451 logger.debug (" removeBranch finished "); 452 453 return newTree; 454 455 } 456 457 458 463 public int getFilemanagerID () { 464 return m_FilemanagerID; 465 } 466 467 472 protected void setFilemanagerID (int id) { 473 m_FilemanagerID = id; 474 } 475 476 481 public String getStoragePath () { 482 return m_StoragePath; 483 } 484 485 490 protected void setStoragePath (String path) { 491 m_StoragePath = path; 492 } 493 494 495 500 public int getOwnerID () { 501 return m_OwnerID; 502 } 503 504 509 protected void setOwnerID (int id) { 510 m_OwnerID = id; 511 } 512 513 514 522 public boolean deleteFiles (Folder activeFolder, String [] fileIDs) { 523 524 boolean success = true; 525 526 Vector files = activeFolder.getFiles (); 528 if (files != null) { 529 530 JahiaFile aFile = null; 531 int nbFile = files.size (); 532 String filePath = ""; 533 534 for (int i = 0; i < nbFile; i++) { 535 536 aFile = (JahiaFile) files.get (i); 537 if (inValues (String.valueOf (aFile.getFileID ()), fileIDs)) { 538 539 filePath = this.getFullPath () + File.separator + aFile.getStorageName (); 540 File storedFile = new File (filePath); 541 542 if (storedFile != null && storedFile.exists ()) { 543 544 if (!storedFile.delete ()) { 545 logger.debug ( 546 "deteteFiles , error while deleting file: " + filePath + " from disk"); 547 success = false; 548 break; 549 } 550 551 if (!ServicesRegistry.getInstance ().getJahiaFilemanagerService () 552 .deleteFileDB (aFile.getFileID ())) { 553 success = false; 554 break; 555 } 556 } else { 557 if (!ServicesRegistry.getInstance ().getJahiaFilemanagerService () 558 .deleteFileDB (aFile.getFileID ())) { 559 success = false; 560 break; 561 } 562 } 563 } 564 } 565 } 566 return success; 567 } 568 569 570 578 public boolean deleteFolder (Folder folder) { 579 580 logger.debug (" deleteFolder started "); 581 582 boolean success = true; 583 if (folder != null) { 584 585 Vector folderTree = this.getBranch (folder); 586 Vector files = new Vector (); 587 588 if (folderTree != null) { 589 590 Folder aFolder = null; 591 int size = folderTree.size (); 592 int nbFile = 0; 593 String filePath = ""; 594 595 for (int i = size; i > 0; i--) { 596 597 aFolder = (Folder) folderTree.get (i - 1); 598 files = aFolder.getFiles (); 600 601 nbFile = files.size (); 602 603 try { 605 if (!ServicesRegistry.getInstance ().getJahiaFilemanagerService () 606 .deleteFilesDB (aFolder.getFolderID ())) { 607 logger.debug ( 608 " HandleFolderDelete , error while deleting files from database"); 609 success = false; 610 break; 611 } else { 612 613 if (files != null) { 614 615 String [] filenames = new String [files.size ()]; 616 nbFile = files.size (); 617 for (int j = 0; j < nbFile; j++) { 618 filenames[j] = 619 ((JahiaFile) files.get (j)).getStorageName (); 620 } 621 622 for (int j = 0; j < nbFile; j++) { 623 filePath = 624 this.getFullPath () + File.separator + filenames[j]; 625 File storedFile = new File (filePath); 626 if (storedFile != null && storedFile.exists ()) { 627 if (!storedFile.delete ()) { 628 logger.debug ( 629 "HandleFolderDelete , error while deleting file: " + filePath + " from disk"); 630 success = false; 631 break; 632 } 633 } else { 634 logger.debug ( 635 "HandleFolderDelete ,cannot find file to delete: " + filePath + " from disk"); 636 } 637 } 638 } 639 } 640 641 if (!FolderDB.getInstance ().delete (aFolder.getFolderID ())) { 643 logger.debug ( 644 " HandleFolderDelete , error while deleting folder: " + aFolder.getFolderName () + " from database"); 645 success = false; 646 break; 647 } 648 } catch (Throwable e) { 649 return false; 650 } 651 } 652 } 653 654 if (!FolderDB.getInstance ().delete (folder.getFolderID ())) { 656 logger.debug ( 657 " HandleFolderDelete , error while deleting folder: " + folder.getFolderName () + " from database"); 658 success = false; 659 } 660 661 662 } else { 663 return false; 664 } 665 return success; 666 667 } 668 669 670 682 683 public boolean moveFolder (Folder folder, Folder destFolder) { 684 685 logger.debug (" moveFolder started "); 686 687 boolean success = true; 688 689 if (folder != null && destFolder != null) { 690 691 folder.setParentID (destFolder.getFolderID ()); 692 if (!(FolderDB.getInstance ().update (folder))) { 693 success = false; 694 } 695 696 } else { 697 success = false; 698 } 699 700 return success; 701 } 702 703 704 711 public void setParents (Hashtable parents) { 712 this.parents = parents; 713 } 714 715 716 722 public void setExcludeBranchFolder (Folder folder) { 723 this.m_ExcludeBranchFolder = folder; 724 } 725 726 727 732 public void resetParents () { 733 this.parents = null; 734 } 735 736 737 743 public Folder getRootFolder () { 744 745 Folder folder = FolderDB.getInstance ().getRootFolder (this.getFilemanagerID ()); 746 return folder; 747 } 748 749 750 755 public String getFullPath () { 756 return (ServicesRegistry.getInstance ().getJahiaFilemanagerService () 757 .getFileRepositoryRootPath () + File.separator + this.getStoragePath ()); 758 } 759 760 761 770 protected boolean inValues (String aValue, String [] values) { 771 if (values != null) { 772 for (int i = 0; i < values.length; i++) { 773 if (aValue.equals (values[i])) { 774 return true; 775 } 776 } 777 } 778 return false; 779 } 780 781 } 782 | Popular Tags |