KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.pojos;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.roller.RollerException;
8 import org.roller.model.Roller;
9 import org.roller.model.RollerFactory;
10 import org.roller.model.WeblogManager;
11 import org.roller.util.PojoUtil;
12
13 /**
14  * WeblogCategory bean.
15  * @author David M Johnson
16  *
17  * @ejb:bean name="WeblogCategoryData"
18  * @struts.form include-all="true"
19  * @hibernate.class table="weblogcategory"
20  * hibernate.jcs-cache usage="read-write"
21  */

22 public class WeblogCategoryData extends HierarchicalPersistentObject
23 {
24     static final long serialVersionUID = 1435782148712018954L;
25
26     protected java.lang.String JavaDoc id = null;
27     protected java.lang.String JavaDoc name;
28     protected java.lang.String JavaDoc description;
29     protected java.lang.String JavaDoc image;
30     
31     protected String JavaDoc cachedPath = null;
32
33     protected WebsiteData mWebsite;
34     protected List JavaDoc mWeblogCategories;
35
36     public WeblogCategoryData()
37     {
38     }
39
40     public WeblogCategoryData(
41         java.lang.String JavaDoc id,
42         WebsiteData website,
43         WeblogCategoryData parent,
44         java.lang.String JavaDoc name,
45         java.lang.String JavaDoc description,
46         java.lang.String JavaDoc image)
47     {
48         this.id = id;
49         this.mWebsite = website;
50         this.mNewParent = parent;
51         this.name = name;
52         this.description = description;
53         this.image = image;
54     }
55
56     public WeblogCategoryData(WeblogCategoryData otherData)
57     {
58         this.id = otherData.id;
59         this.mWebsite = otherData.mWebsite;
60         this.mNewParent = otherData.mNewParent;
61         this.name = otherData.name;
62         this.description = otherData.description;
63         this.image = otherData.image;
64     }
65
66     /** Setter is needed in RollerImpl.storePersistentObject(). */
67     public void setData(org.roller.pojos.PersistentObject otherData)
68     {
69         this.id = ((WeblogCategoryData) otherData).id;
70         this.mWebsite = ((WeblogCategoryData) otherData).mWebsite;
71         this.mNewParent = ((WeblogCategoryData) otherData).mNewParent;
72         this.name = ((WeblogCategoryData) otherData).name;
73         this.description = ((WeblogCategoryData) otherData).description;
74         this.image = ((WeblogCategoryData) otherData).image;
75     }
76
77     public void save() throws RollerException
78     {
79         if (RollerFactory.getRoller().getWeblogManager().isDuplicateWeblogCategoryName(this))
80         {
81             throw new RollerException("Duplicate category name");
82         }
83         super.save();
84     }
85    
86     /**
87      * Remove this category and recategorize all entries in this category and
88      * in all subcategories to a specified destination category (destCat).
89      * @param destCat New category for entries in remove categories (or null if none).
90      */

91     public void remove(WeblogCategoryData destCat) throws RollerException
92     {
93         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
94         
95         // recategorize entries in this category
96
if (destCat != null)
97         {
98             wmgr.moveWeblogCategoryContents(getId(), destCat.getId());
99         }
100         // delete this category
101
super.remove();
102         
103         if (getWebsite().getBloggerCategory().equals(this))
104         {
105             WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(getWebsite());
106             getWebsite().setBloggerCategory(rootCat);
107         }
108         
109         if (getWebsite().getDefaultCategory().equals(this))
110         {
111             WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(getWebsite());
112             getWebsite().setDefaultCategory(rootCat);
113         }
114         
115         getWebsite().save();
116     }
117     
118     /**
119      * @see org.roller.pojos.HierarchicalPersistentObject#getAssocClass()
120      */

121     public Class JavaDoc getAssocClass()
122     {
123         return WeblogCategoryAssoc.class;
124     }
125
126     /**
127      * @see org.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
128      */

129     public String JavaDoc getObjectPropertyName()
130     {
131         return "category";
132     }
133
134     /**
135      * @see org.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
136      */

137     public String JavaDoc getAncestorPropertyName()
138     {
139         return "ancestorCategory";
140     }
141     
142     //------------------------------------------------------- Simple properties
143

144     /**
145      * @ejb:persistent-field
146       * @hibernate.id column="id" type="string"
147       * generator-class="uuid.hex" unsaved-value="null"
148      */

149     public java.lang.String JavaDoc getId()
150     {
151         return this.id;
152     }
153     /** @ejb:persistent-field */
154     public void setId(java.lang.String JavaDoc id)
155     {
156         this.id = id;
157     }
158
159     /**
160      * @ejb:persistent-field
161      * @hibernate.property column="name" non-null="true" unique="false"
162      */

163     public java.lang.String JavaDoc getName()
164     {
165         return this.name;
166     }
167     /** @ejb:persistent-field */
168     public void setName(java.lang.String JavaDoc name)
169     {
170         this.name = name;
171     }
172
173     /**
174      * Description
175      * @ejb:persistent-field
176      * @hibernate.property column="description" non-null="true" unique="false"
177      */

178     public java.lang.String JavaDoc getDescription()
179     {
180         return this.description;
181     }
182     /** @ejb:persistent-field */
183     public void setDescription(java.lang.String JavaDoc description)
184     {
185         this.description = description;
186     }
187
188     /**
189      * @ejb:persistent-field
190      * @hibernate.property column="image" non-null="true" unique="false"
191      */

192     public java.lang.String JavaDoc getImage()
193     {
194         return this.image;
195     }
196     /** @ejb:persistent-field */
197     public void setImage(java.lang.String JavaDoc image)
198     {
199         this.image = image;
200     }
201
202     /**
203      * Get path in category hierarhcy.
204      */

205     public String JavaDoc getPath()
206     {
207         if (null == cachedPath)
208         {
209             try
210             {
211                 cachedPath = RollerFactory.getRoller().getWeblogManager().getPath(this);
212             }
213             catch (RollerException e)
214             {
215                 throw new RuntimeException JavaDoc(e);
216             }
217         }
218         return cachedPath;
219     }
220
221     //------------------------------------------------------------ Associations
222

223     /**
224      * @ejb:persistent-field
225      *
226      * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
227      */

228     public WebsiteData getWebsite()
229     {
230         return mWebsite;
231     }
232     /** @ejb:persistent-field */
233     public void setWebsite(WebsiteData website)
234     {
235         mWebsite = website;
236     }
237
238 // /**
239
// * @ejb:persistent-field
240
// *
241
// * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
242
// */
243
// public WeblogCategoryAssoc getWeblogCategoryAssoc()
244
// {
245
// return mWeblogCategoryAssoc;
246
// }
247
// /** @ejb:persistent-field */
248
// public void setWeblogCategoryAssoc(WebsiteData website)
249
// {
250
// WeblogCategoryAssoc = weblogCategoryAssoc;
251
// }
252

253     /** Return parent category, or null if category is root of hierarchy. */
254     public WeblogCategoryData getParent() throws RollerException
255     {
256         if (mNewParent != null)
257         {
258             // Category has new parent, so return that
259
return (WeblogCategoryData)mNewParent;
260         }
261         else if (getParentAssoc() != null)
262         {
263             // Return parent found in database
264
return ((WeblogCategoryAssoc)getParentAssoc()).getAncestorCategory();
265         }
266         else
267         {
268             return null;
269         }
270     }
271
272     /** Set parent category, database will be updated when object is saved. */
273     public void setParent(HierarchicalPersistentObject parent)
274     {
275         mNewParent = parent;
276     }
277
278     /** Query to get child categories of this category. */
279     public List JavaDoc getWeblogCategories() throws RollerException
280     {
281         if (mWeblogCategories == null)
282         {
283             mWeblogCategories = new LinkedList JavaDoc();
284             List JavaDoc childAssocs = getChildAssocs();
285             Iterator JavaDoc childIter = childAssocs.iterator();
286             while (childIter.hasNext())
287             {
288                 WeblogCategoryAssoc assoc =
289                     (WeblogCategoryAssoc) childIter.next();
290                 mWeblogCategories.add(assoc.getCategory());
291             }
292         }
293         return mWeblogCategories;
294     }
295
296     public boolean descendentOf(WeblogCategoryData ancestor)
297         throws RollerException
298     {
299         return RollerFactory.getRoller().getWeblogManager().isDescendentOf(this, ancestor);
300     }
301     
302     /**
303      * Determine if category is in use. Returns true if any weblog entries
304      * use this category or any of it's subcategories.
305      */

306     public boolean isInUse()
307     {
308         try
309         {
310             return RollerFactory.getRoller().getWeblogManager().isWeblogCategoryInUse(this);
311         }
312         catch (RollerException e)
313         {
314             throw new RuntimeException JavaDoc(e);
315         }
316     }
317     
318     /** TODO: fix form generation so this is not needed. */
319     public void setInUse(boolean dummy) {}
320
321     //------------------------------------------------------------------------
322

323     /**
324      * @see org.roller.pojos.HierarchicalPersistentObject#createAssoc(
325      * org.roller.pojos.HierarchicalPersistentObject,
326      * org.roller.pojos.HierarchicalPersistentObject, java.lang.String)
327      */

328     public Assoc createAssoc(
329         HierarchicalPersistentObject object,
330         HierarchicalPersistentObject associatedObject,
331         String JavaDoc relation) throws RollerException
332     {
333         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
334         return wmgr.createWeblogCategoryAssoc(
335             (WeblogCategoryData)object,
336             (WeblogCategoryData)associatedObject,
337             relation);
338     }
339
340     //------------------------------------------------------------------------
341

342     /**
343      * Move all weblog entries that exist in this category and all
344      * subcategories of this category to a single new category.
345      */

346     public void moveContents(WeblogCategoryData dest) throws RollerException
347     {
348         Iterator JavaDoc entries = retrieveWeblogEntries(true).iterator();
349         while (entries.hasNext())
350         {
351             WeblogEntryData entry = (WeblogEntryData) entries.next();
352             entry.setCategory(dest);
353             entry.save();
354         }
355     }
356     
357     /**
358      * Retrieve all weblog entries in this category and, optionally, include
359      * weblog entries all sub-categories.
360      * @param subcats True if entries from sub-categories are to be returned.
361      * @return List of WeblogEntryData objects.
362      * @throws RollerException
363      */

364     public List JavaDoc retrieveWeblogEntries(boolean subcats)
365         throws RollerException
366     {
367         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
368         return wmgr.retrieveWeblogEntries(this, subcats);
369     }
370
371     //-------------------------------------------------------- Good citizenship
372

373     public String JavaDoc toString()
374     {
375         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
376
377         str.append(
378             "id="
379                 + id
380                 + " "
381                 + "name="
382                 + name
383                 + " "
384                 + "description="
385                 + description
386                 + " "
387                 + "image="
388                 + image);
389         str.append('}');
390
391         return (str.toString());
392     }
393
394     public boolean equals(Object JavaDoc pOther)
395     {
396         if (pOther == null) return false;
397         if (pOther instanceof WeblogCategoryData)
398         {
399             WeblogCategoryData lTest = (WeblogCategoryData) pOther;
400             boolean lEquals = true;
401             lEquals = PojoUtil.equals(lEquals, this.id, lTest.id);
402             lEquals = PojoUtil.equals(lEquals, this.mWebsite.getId(), lTest.mWebsite.getId());
403             lEquals = PojoUtil.equals(lEquals, this.name, lTest.name);
404             lEquals = PojoUtil.equals(lEquals, this.description, lTest.description);
405             lEquals = PojoUtil.equals(lEquals, this.image, lTest.image);
406             return lEquals;
407         }
408         else
409         {
410             return false;
411         }
412     }
413
414     public int hashCode()
415     {
416         int result = 17;
417         result = 37 * result + ((this.id != null) ? this.id.hashCode() : 0);
418         result =
419             37 * result
420                 + ((this.mWebsite != null) ? this.mWebsite.hashCode() : 0);
421         result = 37 * result + ((this.name != null) ? this.name.hashCode() : 0);
422         result =
423             37 * result
424                 + ((this.description != null) ? this.description.hashCode() : 0);
425         result =
426             37 * result + ((this.image != null) ? this.image.hashCode() : 0);
427         return result;
428     }
429     
430     /** TODO: fix Struts form generation template so this is not needed. */
431     public void setAssocClassName(String JavaDoc dummy) {};
432     
433     /** TODO: fix Struts form generation template so this is not needed. */
434     public void setObjectPropertyName(String JavaDoc dummy) {};
435     
436     /** TODO: fix Struts form generation template so this is not needed. */
437     public void setAncestorPropertyName(String JavaDoc dummy) {};
438     
439     /** TODO: fix formbean generation so this is not needed. */
440     public void setPath(String JavaDoc string) {}
441
442     /**
443      * @see org.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
444      */

445     protected Assoc getParentAssoc() throws RollerException
446     {
447         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryParentAssoc(this);
448     }
449
450     /**
451      * @see org.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
452      */

453     protected List JavaDoc getChildAssocs() throws RollerException
454     {
455         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryChildAssocs(this);
456     }
457
458     /**
459      * @see org.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
460      */

461     public List JavaDoc getAllDescendentAssocs() throws RollerException
462     {
463         return RollerFactory.getRoller().getWeblogManager().getAllWeblogCategoryDecscendentAssocs(this);
464     }
465
466     /**
467      * @see org.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
468      */

469     public List JavaDoc getAncestorAssocs() throws RollerException
470     {
471         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryAncestorAssocs(this);
472     }
473
474     public boolean canSave() throws RollerException
475     {
476         Roller roller = RollerFactory.getRoller();
477         if (roller.getUser().equals(UserData.SYSTEM_USER))
478         {
479             return true;
480         }
481         if (roller.getUser().equals(getWebsite().getUser()))
482         {
483             return true;
484         }
485         return false;
486     }
487
488 }
489
Popular Tags