KickJava   Java API By Example, From Geeks To Geeks.

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


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.registries.ServicesRegistry;
18
19 import java.util.Hashtable JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 /**
23  * Class Folder.
24  * A folder item
25  *
26  * @author Khue ng
27  * @version 1.0
28  */

29 public class Folder {
30
31     private static org.apache.log4j.Logger logger =
32             org.apache.log4j.Logger.getLogger (Folder.class);
33
34     /** the folder identifier * */
35     private int m_FolderID = -1;
36     /** the filemanager id * */
37     private int m_FilemanagerID = -1;
38     /** the parent folder id * */
39     private int m_ParentID = -1;
40     /** the depth , used for quick search purpose * */
41     private int m_Depth = -1;
42     /** the folder name * */
43     private String JavaDoc m_FolderName = "";
44
45     /** a reference to an instance of FolderDB class * */
46     private FolderDB m_FolderDB;
47
48
49     /**
50      * Constructor
51      */

52     protected Folder () {
53     }
54
55     /**
56      * Constructor
57      *
58      * @param the filemanager id
59      * @param the parent folder id
60      * @param the folder name
61      */

62     public Folder (
63             int filemanagerID,
64             int parentID,
65             String JavaDoc folderName) {
66
67         m_FilemanagerID = filemanagerID;
68         m_ParentID = parentID;
69         m_FolderName = folderName;
70     }
71
72     public int getFolderID () {
73         return m_FolderID;
74     }
75
76     protected void setFolderID (int id) {
77         m_FolderID = id;
78     }
79
80     public int getFilemanagerID () {
81         return m_FilemanagerID;
82     }
83
84     protected void setFilemanager (int id) {
85         m_FilemanagerID = id;
86     }
87
88
89     public int getParentID () {
90         return m_ParentID;
91     }
92
93     public void setParentID (int id) {
94         m_ParentID = id;
95         this.setDepth (this.getParent ().getDepth () + 1);
96     }
97
98     public int getDepth () {
99         return m_Depth;
100     }
101
102     protected void setDepth (int depth) {
103         m_Depth = depth;
104     }
105
106     public String JavaDoc getFolderName () {
107         return m_FolderName;
108     }
109
110     public void setFolderName (String JavaDoc folderName) {
111         m_FolderName = folderName;
112     }
113
114
115     /**
116      * Method getParent<br>
117      *
118      * @return parent folder
119      */

120     public Folder getParent () {
121         return FolderDB.getInstance ().getFolder (this.getParentID ());
122     }
123
124
125     /**
126      * Method getParents<br>
127      * get all parent of this folder branch
128      */

129     public void getParents (Hashtable JavaDoc parents) {
130
131         parents.put (new Integer JavaDoc (this.getFolderID ()), new Integer JavaDoc (this.getFolderID ()));
132         Folder parent = FolderDB.getInstance ().getFolder (this.getParentID ());
133         if (parent != null) {
134             logger.debug ("getParents():" + parent.getFolderID ());
135             //parents.put( new Integer(parent.getFolderID()), new Integer(parent.getFolderID()) );
136
parent.getParents (parents);
137         }
138     }
139
140     /**
141      * Method getFiles<br>
142      * Return files contained in this folder
143      */

144     public Vector JavaDoc getFiles () {
145
146         Vector JavaDoc files = null;
147
148         // get Files
149
String JavaDoc orderSql = " order by title_jahia_file";
150         files =
151                 ServicesRegistry.getInstance ().getJahiaFilemanagerService ().getFilesByFolderDB (
152                         this.getFolderID (), orderSql);
153         return files;
154     }
155
156
157     /**
158      * Method deleteFiles<br>
159      * Delete files from folder.
160      *
161      * @param fileIDs String array of file id to delete
162      *
163      * @return false on error
164      */

165     public boolean deleteFiles (String JavaDoc[] fileIDs) {
166
167         boolean success = true;
168
169         // get active folder's files
170
Vector JavaDoc files = this.getFiles ();
171         if (files != null) {
172             JahiaFile aFile = null;
173             int nbFile = files.size ();
174
175             for (int i = 0; i < nbFile; i++) {
176
177                 aFile = (JahiaFile) files.get (i);
178                 if (inValues (String.valueOf (aFile.getFileID ()), fileIDs)) {
179                     if (!ServicesRegistry.getInstance ().getJahiaFilemanagerService ()
180                             .deleteFileDB (aFile.getFileID ())) {
181                         success = false;
182                         break;
183                     }
184                 }
185             }
186         }
187         return success;
188     }
189
190
191     /**
192      * Method addFolder<br>
193      *
194      * @param folderName
195      *
196      * @return false on any error
197      */

198     public boolean addFolder (String JavaDoc folderName) {
199
200         if (folderName.length () > 0) {
201
202             // create new folder
203
Folder folder = new Folder (this.getFilemanagerID (),
204                     this.getFolderID (), // parent folder
205
folderName
206             );
207
208             if (!FolderDB.getInstance ().insert (folder)) {
209                 logger.debug ("addFolder, error create new Folder in db");
210                 return false;
211             }
212         }
213         return true;
214     }
215
216     /**
217      * Method moveFiles<br>
218      * Move some files in this folder to another destination folder
219      *
220      * @param destFolder the destination folder
221      * @param fileIDs a string array of file ids
222      *
223      * @return false on error
224      */

225     public boolean moveFiles (Folder destFolder, String JavaDoc[] fileIDs) {
226
227         logger.debug (" moveFiles started ");
228
229         boolean success = true;
230
231         if (fileIDs != null && destFolder != null) {
232
233             // get folder's files
234
Vector JavaDoc files = this.getFiles ();
235             JahiaFile aFile = null;
236
237             if (files != null) {
238
239                 int nbFile = files.size ();
240
241                 for (int i = 0; i < nbFile; i++) {
242
243                     aFile = (JahiaFile) files.get (i);
244                     if (inValues (String.valueOf (aFile.getFileID ()), fileIDs)) {
245                         aFile.setFolderID (destFolder.getFolderID ());
246                         if (!(ServicesRegistry.getInstance ().getJahiaFilemanagerService ()
247                                 .updateFileDB (aFile, aFile.getVersion ()))) {
248                             // error moving files
249
logger.debug ("moveFiles, error updating files info");
250                             success = false;
251                             break;
252                         }
253                     }
254                 }
255             }
256         } else {
257             success = false;
258         }
259
260         return success;
261
262     }
263
264
265     /**
266      * Method renameFolder<br>
267      * Rename the folder name
268      *
269      * @param folderName the new folder name
270      *
271      * @return false on any error
272      */

273     public boolean renameFolder (String JavaDoc folderName) {
274
275         logger.debug (" renameFolder started ");
276
277         if (folderName.length () > 0) {
278
279             this.setFolderName (folderName);
280             if (!FolderDB.getInstance ().update (this)) {
281                 return false;
282             }
283         }
284         return true;
285     }
286
287
288     /**
289      * Method inValues<br>
290      * Check if a string value is in an array of string
291      *
292      * @param aValue a string value
293      * @param values an array of String
294      *
295      * @return true if value found in array
296      */

297     protected boolean inValues (String JavaDoc aValue, String JavaDoc[] values) {
298         if (values != null) {
299             for (int i = 0; i < values.length; i++) {
300                 if (aValue.equals (values[i])) {
301                     return true;
302                 }
303             }
304         }
305         return false;
306     }
307
308 }
309
Popular Tags