KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.pojos;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeSet JavaDoc;
26
27 import org.apache.roller.RollerException;
28 import org.apache.roller.model.BookmarkManager;
29 import org.apache.roller.model.RollerFactory;
30
31 /**
32  * <p>Folder that holds Bookmarks and other Folders. A Roller Website has a
33  * set of Folders (there is no one root folder) and each Folder may contain
34  * Folders or Bookmarks. Don't construct one of these yourself, instead use
35  * the create method in your BookmarkManager implementation.</p>
36  *
37  * @struts.form include-all="true"
38  * extends="org.apache.struts.validator.ValidatorForm"
39  * @ejb:bean name="FolderData"
40  *
41  * @hibernate.class lazy="false" table="folder"
42  * @hibernate.cache usage="read-write"
43  */

44 public class FolderData extends HierarchicalPersistentObject
45     implements Serializable JavaDoc, Comparable JavaDoc
46 {
47     static final long serialVersionUID = -6272468884763861944L;
48     
49     private Set JavaDoc bookmarks = new TreeSet JavaDoc();
50     private List JavaDoc folders = null;
51     private WebsiteData website;
52     
53     private String JavaDoc id;
54     private String JavaDoc name;
55     private String JavaDoc description;
56     private String JavaDoc path;
57     
58     //----------------------------------------------------------- Constructors
59

60     /** For use by BookmarkManager implementations only. */
61     public FolderData()
62     {
63     }
64     
65     public FolderData(
66         FolderData parent,
67         String JavaDoc name,
68         String JavaDoc desc,
69         WebsiteData website)
70     {
71         mNewParent = parent;
72         this.name = name;
73         this.description = desc;
74         this.website = website;
75     }
76
77     public void setData(org.apache.roller.pojos.PersistentObject otherData)
78     {
79         mNewParent = ((FolderData) otherData).mNewParent;
80         this.id = ((FolderData) otherData).getId();
81         this.name = ((FolderData) otherData).getName();
82         this.description = ((FolderData) otherData).getDescription();
83         this.website = ((FolderData) otherData).getWebsite();
84         this.setBookmarks(((FolderData) otherData).getBookmarks());
85     }
86     
87     
88     /**
89      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAssocClass()
90      */

91     public Class JavaDoc getAssocClass()
92     {
93         return FolderAssoc.class;
94     }
95
96     /**
97      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
98      *
99      * @roller.wrapPojoMethod type="simple"
100      */

101     public String JavaDoc getObjectPropertyName()
102     {
103         return "folder";
104     }
105
106     /**
107      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
108      *
109      * @roller.wrapPojoMethod type="simple"
110      */

111     public String JavaDoc getAncestorPropertyName()
112     {
113         return "ancestorFolder";
114     }
115
116     /**
117      * @roller.wrapPojoMethod type="simple"
118      */

119     public boolean isInUse()
120     {
121         try
122         {
123             return RollerFactory.getRoller().getBookmarkManager().isFolderInUse(this);
124         }
125         catch (RollerException e)
126         {
127             throw new RuntimeException JavaDoc(e);
128         }
129     }
130     
131     /**
132      * @roller.wrapPojoMethod type="simple"
133      */

134     public boolean descendentOf(FolderData ancestor)
135         throws RollerException
136     {
137         return RollerFactory.getRoller().getBookmarkManager().isDescendentOf(this, ancestor);
138     }
139
140     //------------------------------------------------------------- Attributes
141

142     /**
143      * @roller.wrapPojoMethod type="simple"
144      *
145      * @ejb:persistent-field
146      *
147      * @hibernate.id column="id"
148      * generator-class="uuid.hex" unsaved-value="null"
149      */

150     public String JavaDoc getId()
151     {
152         return this.id;
153     }
154
155     /** @ejb:persistent-field */
156     public void setId(String JavaDoc id)
157     {
158         this.id = id;
159     }
160
161     /**
162      * @roller.wrapPojoMethod type="simple"
163      *
164      * @struts.validator type="required" msgkey="errors.required"
165      * @struts.validator type="mask" msgkey="errors.noslashes"
166      * @struts.validator-var name="mask" value="${noslashes}"
167      * @struts.validator-args arg0resource="folderForm.name"
168      *
169      * @ejb:persistent-field
170      *
171      * @hibernate.property column="name" non-null="true" unique="false"
172      */

173     public String JavaDoc getName()
174     {
175         return this.name;
176     }
177
178     /** @ejb:persistent-field */
179     public void setName(String JavaDoc name)
180     {
181         this.name = name;
182     }
183
184     /**
185      * Description
186      *
187      * @roller.wrapPojoMethod type="simple"
188      *
189      * @ejb:persistent-field
190      *
191      * @hibernate.property column="description" non-null="true" unique="false"
192      */

193     public String JavaDoc getDescription()
194     {
195         return this.description;
196     }
197
198     /** @ejb:persistent-field */
199     public void setDescription(String JavaDoc description)
200     {
201         this.description = description;
202     }
203
204     //---------------------------------------------------------- Relationships
205

206     /**
207      * Get path to this bookmark folder.
208      *
209      * @roller.wrapPojoMethod type="simple"
210      */

211     public String JavaDoc getPath() throws RollerException
212     {
213         if (mNewParent != null)
214         {
215             throw new RollerException(
216                 "Folder has a new parent and must be saved before getPath() will work");
217         }
218         
219         if (null == path)
220         {
221             path = RollerFactory.getRoller().getBookmarkManager().getPath(this);
222         }
223         return path;
224     }
225         
226     /**
227      * @roller.wrapPojoMethod type="pojo"
228      *
229      * @ejb:persistent-field
230      *
231      * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
232      */

233     public WebsiteData getWebsite()
234     {
235         return website;
236     }
237
238     /** @ejb:persistent-field */
239     public void setWebsite( WebsiteData website )
240     {
241         this.website = website;
242     }
243
244     /**
245      * Return parent category, or null if category is root of hierarchy.
246      *
247      * @roller.wrapPojoMethod type="pojo"
248      */

249     public FolderData getParent() throws RollerException
250     {
251         if (mNewParent != null)
252         {
253             // Category has new parent, so return that
254
return (FolderData)mNewParent;
255         }
256         else if (getParentAssoc() != null)
257         {
258             // Return parent found in database
259
return ((FolderAssoc)getParentAssoc()).getAncestorFolder();
260         }
261         else
262         {
263             return null;
264         }
265     }
266
267     /** Set parent category, database will be updated when object is saved. */
268     public void setParent(HierarchicalPersistentObject parent)
269     {
270         mNewParent = parent;
271     }
272
273     /**
274      * Query to get child categories of this category.
275      *
276      * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.FolderData"
277      */

278     public List JavaDoc getFolders() throws RollerException
279     {
280         if (folders == null)
281         {
282             folders = new LinkedList JavaDoc();
283             List JavaDoc childAssocs = getChildAssocs();
284             Iterator JavaDoc childIter = childAssocs.iterator();
285             while (childIter.hasNext())
286             {
287                 FolderAssoc assoc =
288                     (FolderAssoc) childIter.next();
289                 folders.add(assoc.getFolder());
290             }
291         }
292         return folders;
293     }
294
295     //------------------------------------------------------ Bookmark children
296

297     /**
298      * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.BookmarkData"
299      *
300      * @ejb:persistent-field
301      *
302      * @hibernate.set lazy="true" order-by="name" inverse="true" cascade="all-delete-orphan"
303      * @hibernate.collection-key column="folderid"
304      * @hibernate.collection-one-to-many class="org.apache.roller.pojos.BookmarkData"
305      */

306     public Set JavaDoc getBookmarks()
307     {
308         return this.bookmarks;
309     }
310
311     // this is private to force the use of add/remove bookmark methods.
312
private void setBookmarks(Set JavaDoc bookmarks)
313     {
314         this.bookmarks = bookmarks;
315     }
316     
317     /** Store bookmark and add to folder */
318     public void addBookmark(BookmarkData bookmark) throws RollerException
319     {
320         bookmark.setFolder(this);
321         getBookmarks().add(bookmark);
322     }
323
324     /** Remove boomkark from folder */
325     public void removeBookmark(BookmarkData bookmark)
326     {
327         getBookmarks().remove(bookmark);
328     }
329
330     /**
331      * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.BookmarkData"
332      *
333      * @param subfolders
334      */

335     public List JavaDoc retrieveBookmarks(boolean subfolders) throws RollerException
336     {
337         BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
338         return bmgr.getBookmarks(this, subfolders);
339     }
340
341     /**
342      * Move all bookmarks that exist in this folder and all
343      * subfolders of this folder to a single new folder.
344      */

345     public void moveContents(FolderData dest) throws RollerException
346     {
347         Iterator JavaDoc entries = retrieveBookmarks(true).iterator();
348         while (entries.hasNext())
349         {
350             BookmarkData bookmark = (BookmarkData) entries.next();
351             
352             // just add bookmarks to new folder
353
// this breaks the old folder/bkmrk relationship
354
// so it's not necessary to explicitly remove
355
dest.addBookmark(bookmark);
356         }
357     }
358
359     //------------------------------------------------------------------------
360

361     /**
362      * @see org.apache.roller.pojos.HierarchicalPersistentObject#createAssoc(
363      * org.apache.roller.pojos.HierarchicalPersistentObject,
364      * org.apache.roller.pojos.HierarchicalPersistentObject, java.lang.String)
365      */

366     public Assoc createAssoc(
367         HierarchicalPersistentObject object,
368         HierarchicalPersistentObject associatedObject,
369         String JavaDoc relation) throws RollerException
370     {
371         return new FolderAssoc(
372                 null,
373             (FolderData)object,
374             (FolderData)associatedObject,
375             relation);
376     }
377
378     //------------------------------------------------------- Good citizenship
379

380     public String JavaDoc toString()
381     {
382         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
383         str.append(
384               "bookmarks=" + bookmarks + " "
385             + "id=" + id + " "
386             + "name=" + name + " "
387             + "description=" + description);
388         str.append('}');
389         return (str.toString());
390     }
391
392     public boolean equals(Object JavaDoc pOther)
393     {
394         if (pOther instanceof FolderData)
395         {
396             FolderData lTest = (FolderData) pOther;
397             boolean lEquals = true;
398
399 // if (this.bookmarks == null)
400
// {
401
// lEquals = lEquals && (lTest.bookmarks == null);
402
// }
403
// else
404
// {
405
// lEquals = lEquals && this.bookmarks.equals(lTest.bookmarks);
406
// }
407

408             if (this.id == null)
409             {
410                 lEquals = lEquals && (lTest.getId() == null);
411             }
412             else
413             {
414                 lEquals = lEquals && this.id.equals(lTest.getId());
415             }
416
417             if (this.name == null)
418             {
419                 lEquals = lEquals && (lTest.getName() == null);
420             }
421             else
422             {
423                 lEquals = lEquals && this.name.equals(lTest.getName());
424             }
425
426             if (this.description == null)
427             {
428                 lEquals = lEquals && (lTest.getDescription() == null);
429             }
430             else
431             {
432                 lEquals = lEquals &&
433                           this.description.equals(lTest.getDescription());
434             }
435
436             if (this.website == null)
437             {
438                 lEquals = lEquals && (lTest.getWebsite() == null);
439             }
440             else
441             {
442                 lEquals = lEquals && this.website.equals(lTest.getWebsite());
443             }
444
445             return lEquals;
446         }
447         else
448         {
449             return false;
450         }
451     }
452
453     public int hashCode()
454     {
455         int result = 17;
456                          
457         result = (37 * result) +
458                  ((this.id != null) ? this.id.hashCode() : 0);
459         result = (37 * result) +
460                  ((this.name != null) ? this.name.hashCode() : 0);
461         result = (37 * result) +
462                  ((this.description != null) ? this.description.hashCode() : 0);
463         result = (37 * result) +
464                  ((this.website != null) ? this.website.hashCode() : 0);
465
466         return result;
467     }
468
469     /**
470      * @see java.lang.Comparable#compareTo(java.lang.Object)
471      */

472     public int compareTo(Object JavaDoc o)
473     {
474         FolderData other = (FolderData)o;
475         return getName().compareTo(other.getName());
476     }
477
478     /** TODO: fix Struts form generation template so this is not needed. */
479     public void setAssocClassName(String JavaDoc dummy) {};
480     /** TODO: fix Struts form generation template so this is not needed. */
481     public void setObjectPropertyName(String JavaDoc dummy) {};
482     /** TODO: fix Struts form generation template so this is not needed. */
483     public void setAncestorPropertyName(String JavaDoc dummy) {};
484     /** TODO: fix formbean generation so this is not needed. */
485     public void setPath(String JavaDoc string) {}
486     /** TODO: fix formbean generation so this is not needed. */
487     public void setInUse(boolean flag) {}
488
489     /**
490      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
491      */

492     public Assoc getParentAssoc() throws RollerException
493     {
494         return RollerFactory.getRoller().getBookmarkManager().getFolderParentAssoc(this);
495     }
496
497     /**
498      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
499      */

500     public List JavaDoc getChildAssocs() throws RollerException
501     {
502         return RollerFactory.getRoller().getBookmarkManager().getFolderChildAssocs(this);
503     }
504
505     /**
506      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
507      */

508     public List JavaDoc getAllDescendentAssocs() throws RollerException
509     {
510         return RollerFactory.getRoller().getBookmarkManager().getAllFolderDecscendentAssocs(this);
511     }
512
513     /**
514      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
515      */

516     public List JavaDoc getAncestorAssocs() throws RollerException
517     {
518         return RollerFactory.getRoller().getBookmarkManager().getFolderAncestorAssocs(this);
519     }
520
521 }
522
Popular Tags