KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > filemanager > Filemanager


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

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 JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24
25
26 /**
27  * Class Filemanager.<br>
28  * The Filemanager core object
29  *
30  * @author Khue ng
31  * @version 1.0
32  */

33 public class Filemanager {
34
35     private static org.apache.log4j.Logger logger =
36             org.apache.log4j.Logger.getLogger (Filemanager.class);
37
38     /** the filemanager identifier * */
39     private int m_FilemanagerID = -1; // by default does not exists in db
40

41     /** the owner of this filemanager * */
42     private int m_OwnerID; // the ownerID
43

44     /** the Path to the File Repository folder on disk * */
45     private String JavaDoc m_StoragePath = "";
46
47     /** the folders tree for this filemanager * */
48     private Vector JavaDoc m_FolderTree;
49
50     /** parents * */
51     private Hashtable JavaDoc parents; // if size > 0, getFolderTree() only return children for
52
// those folders otherwise return children of all folders
53
// of this filemanager
54

55     /** excludeBranchFolder * */
56     private Folder m_ExcludeBranchFolder; // a folder and all its children to exclude
57
// by getFolderTree()
58

59     /** if the folderTree is already loaded or not * */
60     private boolean m_FolderTreeLoaded = false;
61
62     /** a reference to an instance of FilemanagerDB class * */
63     private FilemanagerDB m_FmngDB;
64
65     /** a reference to an instance of FolderDB class * */
66     private FolderDB m_FolderDB;
67
68
69     /**
70      * Constructor
71      */

72     public Filemanager () {
73     }
74
75
76     /**
77      * Method create<br>
78      * Create a new filemanager in database
79      *
80      * @param the owner ID
81      *
82      * @return true if done correctly
83      */

84     public boolean create (int ownerID) {
85
86         boolean success = true;
87
88         // Check if a filemanager already exist for this owner
89
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                 // Create a new one
99
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                 // create Root Folder for this filemanager
120
Folder folder = new Folder (this.getFilemanagerID (),
121                         -1, // no parent
122
"root");
123
124                 if (!FolderDB.getInstance ().insert (folder)) { // success
125
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     /**
144      * Method initializeWithOwner<br>
145      * Initialize filemanager's data by owner id.<br>
146      * Load filemanager's data : filemanager id, owner id<br>
147      *
148      * @param ownerID the id of the owner
149      *
150      * @return true if done correctly
151      */

152     public boolean initializeWithOwner (int ownerID) {
153
154         this.setOwnerID (ownerID);
155
156         // get filemanager data
157
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             // try to create a new one
167
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     /**
185      * Method initialize<br>
186      * Initialize filemanager's data with filemanager id.<br>
187      * Load filemanager's data : filemanager id, owner id<br>
188      * The default active folder is the root folder
189      *
190      * @param filemanagerID the filemanager id
191      *
192      * @return true if done correctly
193      */

194     public boolean initialize (int filemanagerID) {
195
196         this.setFilemanagerID (filemanagerID);
197
198         // get filemanager data
199
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     /**
212      * Method getFolderTree
213      * Get the Folder tree for this filemanager, deploy all branch
214      * return a tree starting at the filemanager's folder root
215      *
216      * @return a Vector of Folder objects or null
217      */

218     public Vector JavaDoc getFolderTree () {
219
220         if (!m_FolderTreeLoaded) {
221
222             // reset parents
223
this.parents = null;
224
225             // get Folder list
226
String JavaDoc whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID ();
227             String JavaDoc orderSql = " order by parent_jahia_fld, fldname_jahia_fld ";
228             Vector JavaDoc foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql);
229             Vector JavaDoc orderedFolders = new Vector JavaDoc ();
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             // Remove the branch m_ExcludeBranchFolder and all it's children
245
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     /**
257      * Method getFolderTree
258      * Get the Folder tree for this filemanager, deploy only one branch
259      *
260      * @param activeFolder the active folder for which to deploy branch
261      *
262      * @return a Vector of Folder objects or null
263      */

264     public Vector JavaDoc getFolderTree (Folder activeFolder) {
265
266         if (!m_FolderTreeLoaded) {
267
268             // get active folder's parent
269
Hashtable JavaDoc parents = new Hashtable JavaDoc ();
270             activeFolder.getParents (parents);
271             this.setParents (parents);
272
273             // get Folder list
274
String JavaDoc whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID ();
275             String JavaDoc orderSql = " order by parent_jahia_fld, fldname_jahia_fld ";
276             Vector JavaDoc foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql);
277             Vector JavaDoc orderedFolders = new Vector JavaDoc ();
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     /**
300      * Method void loadFolderTree<br>
301      * Force to reload folder tree
302      */

303     public void loadFolderTree () {
304
305         m_FolderTreeLoaded = false;
306         getFolderTree ();
307     }
308
309
310     /**
311      * Method void loadFolderTree<br>
312      * Force to reload folder tree
313      *
314      * @param activerFolder the active folder for which to deploy all branch
315      */

316     public void loadFolderTree (Folder activeFolder) {
317
318         m_FolderTreeLoaded = false;
319         getFolderTree (activeFolder);
320     }
321
322
323     /**
324      * Method getBranch(Folder folder)<br>
325      * Return a branch starting from a gived folder
326      *
327      * @param the folder from where to get the Tree
328      *
329      * @return a Vector of Folder objects or null
330      */

331     public Vector JavaDoc getBranch (Folder folder) {
332
333         // get Folder list
334
String JavaDoc whereSql = " where filemanagerid_jahia_fld=" + this.getFilemanagerID ();
335         String JavaDoc orderSql = " order by parent_jahia_fld, fldname_jahia_fld ";
336         Vector JavaDoc foldersTree = FolderDB.getInstance ().getFolders (whereSql, orderSql);
337         Vector JavaDoc orderedFolders = new Vector JavaDoc ();
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     /**
354      * Recursively build the folder tree
355      *
356      * @param folders a vector of folders ordered by parentid
357      * @param orderedFolders the vector to store ordered folders
358      * @param parentID the parent folder's id to get children
359      * @param the current tree depth
360      */

361     protected void sortFolders (Vector JavaDoc folders, Vector JavaDoc 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 JavaDoc (tmpFolder.getFolderID ())) != null) {
377                         orderedFolders.add (tmpFolder);
378                         //folders.removeElementAt(i);
379
//i-=1;
380
sortFolders (folders, orderedFolders, tmpFolder.getFolderID (),
381                                 currentDepth + 1);
382                     } else {
383                         orderedFolders.add (tmpFolder);
384                         //folders.removeElementAt(i);
385
//i-=1;
386
}
387                 } else {
388                     orderedFolders.add (tmpFolder);
389                     //folders.removeElementAt(i);
390
//i-=1;
391
sortFolders (folders, orderedFolders, tmpFolder.getFolderID (),
392                             currentDepth + 1);
393
394                 }
395
396             }
397         }
398     }
399
400
401     /**
402      * Method removeBranch<br>
403      * Remove an entire branch starting from a gived folder<br>
404      * Called by getChildren()<br>
405      *
406      * @param activeFolder folder remove
407      * @param folderTree a vector of filtered Folder
408      *
409      * @return newTree the new tree
410      */

411     protected Vector JavaDoc removeBranch (Folder activeFolder, Vector JavaDoc folderTree) {
412
413         logger.debug (" removeBranch started ");
414
415         int size = folderTree.size ();
416         Folder folder = null;
417         Vector JavaDoc newTree = new Vector JavaDoc ();
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                 // getActivefolder depth
430
activeFolder.setDepth (folder.getDepth ());
431
432                 i += 1; // exclude itself
433
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                     // remove all its children
439
if (!(folder.getDepth () > activeFolder.getDepth ())) {
440                         i = (j - 1);
441                         break;
442                     } else if (j == (size - 1)) { // last element
443
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     /**
459      * Method getFilemanagerID<br>
460      *
461      * @return the filemanager id
462      */

463     public int getFilemanagerID () {
464         return m_FilemanagerID;
465     }
466
467     /**
468      * Method setFilemanagerID<br>
469      *
470      * @param id the filemanager id
471      */

472     protected void setFilemanagerID (int id) {
473         m_FilemanagerID = id;
474     }
475
476     /**
477      * Method getStoragePath<br>
478      *
479      * @return the filemanager storage path
480      */

481     public String JavaDoc getStoragePath () {
482         return m_StoragePath;
483     }
484
485     /**
486      * Method setStoragePath<br>
487      *
488      * @param path the storage path
489      */

490     protected void setStoragePath (String JavaDoc path) {
491         m_StoragePath = path;
492     }
493
494
495     /**
496      * Method getOwnerID<br>
497      *
498      * @return the owner id
499      */

500     public int getOwnerID () {
501         return m_OwnerID;
502     }
503
504     /**
505      * Method setOwnerID<br>
506      *
507      * @param id the owner id
508      */

509     protected void setOwnerID (int id) {
510         m_OwnerID = id;
511     }
512
513
514     /**
515      * Method deleteFiles<br>
516      * Delete files from folder.
517      *
518      * @param fileIDs String array of file id to delete
519      *
520      * @return false on error
521      */

522     public boolean deleteFiles (Folder activeFolder, String JavaDoc[] fileIDs) {
523
524         boolean success = true;
525
526         // get active folder's files
527
Vector JavaDoc files = activeFolder.getFiles ();
528         if (files != null) {
529
530             JahiaFile aFile = null;
531             int nbFile = files.size ();
532             String JavaDoc 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     /**
571      * Method deleteFolder<br>
572      * Delete a folder and all it's children
573      *
574      * @param folder the folder to delete
575      *
576      * @return false on error
577      */

578     public boolean deleteFolder (Folder folder) {
579
580         logger.debug (" deleteFolder started ");
581
582         boolean success = true;
583         if (folder != null) {
584
585             Vector JavaDoc folderTree = this.getBranch (folder);
586             Vector JavaDoc files = new Vector JavaDoc ();
587
588             if (folderTree != null) {
589
590                 Folder aFolder = null;
591                 int size = folderTree.size ();
592                 int nbFile = 0;
593                 String JavaDoc filePath = "";
594
595                 for (int i = size; i > 0; i--) {
596
597                     aFolder = (Folder) folderTree.get (i - 1);
598                     // get folder's files
599
files = aFolder.getFiles ();
600
601                     nbFile = files.size ();
602
603                     // delete files from db
604
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 JavaDoc[] filenames = new String JavaDoc[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                         // delete folder from db
642
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 JavaDoc e) {
649                         return false;
650                     }
651                 }
652             }
653
654             // delete folder from db
655
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     /**
671      * Method moveFolder
672      * Move a Folder to a new location
673      *
674      * @param folder to move
675      * @param destFolder destination folder
676      *
677      * @return false on error
678      *
679      * @throws ServletException
680      * @throws IOException
681      */

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     /**
705      * Method setParents<br>
706      * Pass an hashTable of folder id
707      * used by getFolderTree to determine which folder to get chidren for
708      *
709      * @param parents
710      */

711     public void setParents (Hashtable JavaDoc parents) {
712         this.parents = parents;
713     }
714
715
716     /**
717      * Method setExcludeBranchFolder<br>
718      * Tell getFolderTree to exclude this folder and all its children
719      *
720      * @param parents
721      */

722     public void setExcludeBranchFolder (Folder folder) {
723         this.m_ExcludeBranchFolder = folder;
724     }
725
726
727     /**
728      * Method resetParents<br>
729      * Set parents to null, this way getFolderTree
730      * will get children for all folder of this filemanager
731      */

732     public void resetParents () {
733         this.parents = null;
734     }
735
736
737     /**
738      * Method getRootFolder<br>
739      * Return the root folder of this filemanager
740      *
741      * @return a folder object else null
742      */

743     public Folder getRootFolder () {
744
745         Folder folder = FolderDB.getInstance ().getRootFolder (this.getFilemanagerID ());
746         return folder;
747     }
748
749
750     /**
751      * Return the full storage path associated with this filemanager<br>
752      *
753      * @return path the full path to the storage dir associated with this filemanager
754      */

755     public String JavaDoc getFullPath () {
756         return (ServicesRegistry.getInstance ().getJahiaFilemanagerService ()
757                 .getFileRepositoryRootPath () + File.separator + this.getStoragePath ());
758     }
759
760
761     /**
762      * Method inValues<br>
763      * Check if a string value is in an array of string
764      *
765      * @param aValue a string value
766      * @param values an array of String
767      *
768      * @return true if value found in array
769      */

770     protected boolean inValues (String JavaDoc aValue, String JavaDoc[] 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