KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > pojos > FolderData


1 package org.roller.pojos;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.LinkedList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.TreeSet JavaDoc;
9
10 import org.roller.RollerException;
11 import org.roller.business.PersistenceStrategy;
12 import org.roller.model.BookmarkManager;
13 import org.roller.model.Roller;
14 import org.roller.model.RollerFactory;
15
16 /**
17  * <p>Folder that holds Bookmarks and other Folders. A Roller Website has a
18  * set of Folders (there is no one root folder) and each Folder may contain
19  * Folders or Bookmarks. Don't construct one of these yourself, instead use
20  * the create method in your BookmarkManager implementation.</p>
21  *
22  * @struts.form include-all="true"
23  * extends="org.apache.struts.validator.ValidatorForm"
24  * @ejb:bean name="FolderData"
25  *
26  * @hibernate.class table="folder"
27  * hibernate.jcs-cache usage="read-write"
28  */

29 public class FolderData extends HierarchicalPersistentObject
30     implements Serializable JavaDoc, Comparable JavaDoc
31 {
32     static final long serialVersionUID = -6272468884763861944L;
33     
34     protected Set JavaDoc bookmarks = new TreeSet JavaDoc();
35     protected List JavaDoc folders = null;
36     protected WebsiteData website;
37     
38     protected String JavaDoc id;
39     protected String JavaDoc name;
40     protected String JavaDoc description;
41     protected String JavaDoc path;
42     
43     //----------------------------------------------------------- Constructors
44

45     /** For use by BookmarkManager implementations only. */
46     public FolderData()
47     {
48     }
49     
50     public FolderData(
51         FolderData parent,
52         String JavaDoc name,
53         String JavaDoc desc,
54         WebsiteData website)
55     {
56         mNewParent = parent;
57         this.name = name;
58         this.description = desc;
59         this.website = website;
60     }
61
62     public void setData(org.roller.pojos.PersistentObject otherData)
63     {
64         mNewParent = ((FolderData) otherData).mNewParent;
65         this.bookmarks = ((FolderData) otherData).bookmarks;
66         this.id = ((FolderData) otherData).id;
67         this.name = ((FolderData) otherData).name;
68         this.description = ((FolderData) otherData).description;
69         this.website = ((FolderData) otherData).website;
70     }
71
72     public void save() throws RollerException
73     {
74         if (RollerFactory.getRoller().getBookmarkManager().isDuplicateFolderName(this))
75         {
76             throw new RollerException("Duplicate folder name");
77         }
78         super.save();
79     }
80     
81     /**
82      * @see org.roller.pojos.HierarchicalPersistentObject#getAssocClass()
83      */

84     public Class JavaDoc getAssocClass()
85     {
86         return FolderAssoc.class;
87     }
88
89     /**
90      * @see org.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
91      */

92     public String JavaDoc getObjectPropertyName()
93     {
94         return "folder";
95     }
96
97     /**
98      * @see org.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
99      */

100     public String JavaDoc getAncestorPropertyName()
101     {
102         return "ancestorFolder";
103     }
104
105     public boolean isInUse()
106     {
107         try
108         {
109             return RollerFactory.getRoller().getBookmarkManager().isFolderInUse(this);
110         }
111         catch (RollerException e)
112         {
113             throw new RuntimeException JavaDoc(e);
114         }
115     }
116     
117     public boolean descendentOf(FolderData ancestor)
118         throws RollerException
119     {
120         return RollerFactory.getRoller().getBookmarkManager().isDescendentOf(this, ancestor);
121     }
122
123     //------------------------------------------------------------- Attributes
124

125     /**
126      * @ejb:persistent-field
127      *
128      * @hibernate.id column="id" type="string"
129      * generator-class="uuid.hex" unsaved-value="null"
130      */

131     public String JavaDoc getId()
132     {
133         return this.id;
134     }
135
136     /** @ejb:persistent-field */
137     public void setId(String JavaDoc id)
138     {
139         this.id = id;
140     }
141
142     /**
143      * @struts.validator type="required" msgkey="errors.required"
144      * @struts.validator type="mask" msgkey="errors.noslashes"
145      * @struts.validator-var name="mask" value="${noslashes}"
146      * @struts.validator-args arg0resource="folderForm.name"
147      *
148      * @ejb:persistent-field
149      *
150      * @hibernate.property column="name" non-null="true" unique="false"
151      */

152     public String JavaDoc getName()
153     {
154         return this.name;
155     }
156
157     /** @ejb:persistent-field */
158     public void setName(String JavaDoc name)
159     {
160         this.name = name;
161     }
162
163     /**
164      * Description
165      *
166      * @ejb:persistent-field
167      *
168      * @hibernate.property column="description" non-null="true" unique="false"
169      */

170     public String JavaDoc getDescription()
171     {
172         return this.description;
173     }
174
175     /** @ejb:persistent-field */
176     public void setDescription(String JavaDoc description)
177     {
178         this.description = description;
179     }
180
181     //---------------------------------------------------------- Relationships
182

183     /** Get path to this bookmark folder. */
184     public String JavaDoc getPath() throws RollerException
185     {
186         if (mNewParent != null)
187         {
188             throw new RollerException(
189                 "Folder has a new parent and must be saved before getPath() will work");
190         }
191         
192         if (null == path)
193         {
194             path = RollerFactory.getRoller().getBookmarkManager().getPath(this);
195         }
196         return path;
197     }
198         
199     /**
200      * @ejb:persistent-field
201      *
202      * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
203      */

204     public WebsiteData getWebsite()
205     {
206         return website;
207     }
208
209     /** @ejb:persistent-field */
210     public void setWebsite( WebsiteData website )
211     {
212         this.website = website;
213     }
214
215     /** Return parent category, or null if category is root of hierarchy. */
216     public FolderData getParent() throws RollerException
217     {
218         if (mNewParent != null)
219         {
220             // Category has new parent, so return that
221
return (FolderData)mNewParent;
222         }
223         else if (getParentAssoc() != null)
224         {
225             // Return parent found in database
226
return ((FolderAssoc)getParentAssoc()).getAncestorFolder();
227         }
228         else
229         {
230             return null;
231         }
232     }
233
234     /** Set parent category, database will be updated when object is saved. */
235     public void setParent(HierarchicalPersistentObject parent)
236     {
237         mNewParent = parent;
238     }
239
240     /** Query to get child categories of this category. */
241     public List JavaDoc getFolders() throws RollerException
242     {
243         if (folders == null)
244         {
245             folders = new LinkedList JavaDoc();
246             List JavaDoc childAssocs = getChildAssocs();
247             Iterator JavaDoc childIter = childAssocs.iterator();
248             while (childIter.hasNext())
249             {
250                 FolderAssoc assoc =
251                     (FolderAssoc) childIter.next();
252                 folders.add(assoc.getFolder());
253             }
254         }
255         return folders;
256     }
257
258     //------------------------------------------------------ Bookmark children
259

260     /**
261       * @ejb:persistent-field
262      *
263      * @hibernate.set lazy="true" order-by="name" inverse="true" cascade="delete"
264      * @hibernate.collection-key column="folderid"
265      * @hibernate.collection-one-to-many class="org.roller.pojos.BookmarkData"
266      */

267     public Set JavaDoc getBookmarks()
268     {
269         return this.bookmarks;
270     }
271
272     /** @ejb:persistent-field */
273     public void setBookmarks(Set JavaDoc bookmarks)
274     {
275         this.bookmarks = bookmarks;
276     }
277     
278     /** Store bookmark and add to folder */
279     public void addBookmark(BookmarkData bookmark) throws RollerException
280     {
281         bookmark.setFolder(this);
282         bookmarks.add(bookmark);
283         bookmark.save();
284     }
285
286     /** Remove boomkark from folder */
287     public void removeBookmark(BookmarkData bookmark)
288     {
289         bookmarks.remove(bookmark);
290         bookmark.setFolder(null);
291     }
292
293     /**
294      * @param subfolders
295      */

296     public List JavaDoc retrieveBookmarks(boolean subfolders) throws RollerException
297     {
298         BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
299         return bmgr.retrieveBookmarks(this, subfolders);
300     }
301
302     /**
303      * Move all bookmarks that exist in this folder and all
304      * subfolders of this folder to a single new folder.
305      */

306     public void moveContents(FolderData dest) throws RollerException
307     {
308         Iterator JavaDoc entries = retrieveBookmarks(true).iterator();
309         while (entries.hasNext())
310         {
311             BookmarkData bookmark = (BookmarkData) entries.next();
312             bookmark.setFolder(dest);
313             bookmark.save();
314         }
315     }
316
317     //------------------------------------------------------------------------
318

319     /**
320      * @see org.roller.pojos.HierarchicalPersistentObject#createAssoc(
321      * org.roller.pojos.HierarchicalPersistentObject,
322      * org.roller.pojos.HierarchicalPersistentObject, java.lang.String)
323      */

324     public Assoc createAssoc(
325         HierarchicalPersistentObject object,
326         HierarchicalPersistentObject associatedObject,
327         String JavaDoc relation) throws RollerException
328     {
329         BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
330         return bmgr.createFolderAssoc(
331             (FolderData)object,
332             (FolderData)associatedObject,
333             relation);
334     }
335
336     //------------------------------------------------------- Good citizenship
337

338     public String JavaDoc toString()
339     {
340         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
341         str.append(
342               "bookmarks=" + bookmarks + " "
343             + "id=" + id + " "
344             + "name=" + name + " "
345             + "description=" + description);
346         str.append('}');
347         return (str.toString());
348     }
349
350     public boolean equals(Object JavaDoc pOther)
351     {
352         if (pOther instanceof FolderData)
353         {
354             FolderData lTest = (FolderData) pOther;
355             boolean lEquals = true;
356
357 // if (this.bookmarks == null)
358
// {
359
// lEquals = lEquals && (lTest.bookmarks == null);
360
// }
361
// else
362
// {
363
// lEquals = lEquals && this.bookmarks.equals(lTest.bookmarks);
364
// }
365

366             if (this.id == null)
367             {
368                 lEquals = lEquals && (lTest.id == null);
369             }
370             else
371             {
372                 lEquals = lEquals && this.id.equals(lTest.id);
373             }
374
375             if (this.name == null)
376             {
377                 lEquals = lEquals && (lTest.name == null);
378             }
379             else
380             {
381                 lEquals = lEquals && this.name.equals(lTest.name);
382             }
383
384             if (this.description == null)
385             {
386                 lEquals = lEquals && (lTest.description == null);
387             }
388             else
389             {
390                 lEquals = lEquals &&
391                           this.description.equals(lTest.description);
392             }
393
394             if (this.website == null)
395             {
396                 lEquals = lEquals && (lTest.website == null);
397             }
398             else
399             {
400                 lEquals = lEquals && this.website.equals(lTest.website);
401             }
402
403             return lEquals;
404         }
405         else
406         {
407             return false;
408         }
409     }
410
411     public int hashCode()
412     {
413         int result = 17;
414                          
415         result = (37 * result) +
416                  ((this.id != null) ? this.id.hashCode() : 0);
417         result = (37 * result) +
418                  ((this.name != null) ? this.name.hashCode() : 0);
419         result = (37 * result) +
420                  ((this.description != null) ? this.description.hashCode() : 0);
421         result = (37 * result) +
422                  ((this.website != null) ? this.website.hashCode() : 0);
423
424         return result;
425     }
426
427     /**
428      * @see java.lang.Comparable#compareTo(java.lang.Object)
429      */

430     public int compareTo(Object JavaDoc o)
431     {
432         FolderData other = (FolderData)o;
433         return getName().compareTo(other.getName());
434     }
435
436     /** TODO: fix Struts form generation template so this is not needed. */
437     public void setAssocClassName(String JavaDoc dummy) {};
438     /** TODO: fix Struts form generation template so this is not needed. */
439     public void setObjectPropertyName(String JavaDoc dummy) {};
440     /** TODO: fix Struts form generation template so this is not needed. */
441     public void setAncestorPropertyName(String JavaDoc dummy) {};
442     /** TODO: fix formbean generation so this is not needed. */
443     public void setPath(String JavaDoc string) {}
444     /** TODO: fix formbean generation so this is not needed. */
445     public void setInUse(boolean flag) {}
446
447     /**
448      * @see org.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
449      */

450     protected Assoc getParentAssoc() throws RollerException
451     {
452         return RollerFactory.getRoller().getBookmarkManager().getFolderParentAssoc(this);
453     }
454
455     /**
456      * @see org.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
457      */

458     protected List JavaDoc getChildAssocs() throws RollerException
459     {
460         return RollerFactory.getRoller().getBookmarkManager().getFolderChildAssocs(this);
461     }
462
463     /**
464      * @see org.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
465      */

466     public List JavaDoc getAllDescendentAssocs() throws RollerException
467     {
468         return RollerFactory.getRoller().getBookmarkManager().getAllFolderDecscendentAssocs(this);
469     }
470
471     /**
472      * @see org.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
473      */

474     public List JavaDoc getAncestorAssocs() throws RollerException
475     {
476         return RollerFactory.getRoller().getBookmarkManager().getFolderAncestorAssocs(this);
477     }
478     
479     protected void removeDescendent(
480             PersistenceStrategy pstrategy, PersistentObject po) throws RollerException
481     {
482         /*FolderData folder = (FolderData)po;
483         Iterator bookmarks = folder.getBookmarks().iterator();
484         while (bookmarks.hasNext()) {
485             bookmarks.remove();
486         }*/

487         pstrategy.remove(po);
488     }
489
490     public boolean canSave() throws RollerException
491     {
492         Roller roller = RollerFactory.getRoller();
493         if (roller.getUser().equals(UserData.SYSTEM_USER))
494         {
495             return true;
496         }
497         if (roller.getUser().equals(getWebsite().getUser()))
498         {
499             return true;
500         }
501         return false;
502     }
503 }
504
Popular Tags