KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.roller.RollerException;
25 import org.apache.roller.model.Roller;
26 import org.apache.roller.model.RollerFactory;
27 import org.apache.roller.model.WeblogManager;
28 import org.apache.roller.util.PojoUtil;
29
30 /**
31  * WeblogCategory bean.
32  * @author David M Johnson
33  *
34  * @ejb:bean name="WeblogCategoryData"
35  * @struts.form include-all="true"
36  * @hibernate.class lazy="false" table="weblogcategory"
37  * @hibernate.cache usage="read-write"
38  */

39 public class WeblogCategoryData extends HierarchicalPersistentObject {
40     public static final long serialVersionUID = 1435782148712018954L;
41     
42     private String JavaDoc id = null;
43     private String JavaDoc name = null;
44     private String JavaDoc description = null;
45     private String JavaDoc image = null;
46     
47     private String JavaDoc cachedPath = null;
48     
49     private WebsiteData website = null;
50     private List JavaDoc weblogCategories = null;
51     
52     
53     public WeblogCategoryData() {
54     }
55     
56     public WeblogCategoryData(
57             java.lang.String JavaDoc id,
58             WebsiteData website,
59             WeblogCategoryData parent,
60             java.lang.String JavaDoc name,
61             java.lang.String JavaDoc description,
62             java.lang.String JavaDoc image) {
63         this.id = id;
64         this.website = website;
65         this.mNewParent = parent;
66         this.name = name;
67         this.description = description;
68         this.image = image;
69     }
70     
71     public WeblogCategoryData(WeblogCategoryData otherData) {
72         this.setData(otherData);
73     }
74     
75     /** Setter is needed in RollerImpl.storePersistentObject(). */
76     public void setData(org.apache.roller.pojos.PersistentObject otherData) {
77         WeblogCategoryData other = (WeblogCategoryData) otherData;
78         
79         this.id = other.getId();
80         this.website = other.getWebsite();
81         this.name = other.getName();
82         this.description = other.getDescription();
83         this.image = other.getImage();
84         
85         try {
86             this.mNewParent = other.getParent();
87         } catch(RollerException re) {
88             // why does this throw an exception?
89
}
90     }
91     
92     
93     /**
94      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAssocClass()
95      */

96     public Class JavaDoc getAssocClass() {
97         return WeblogCategoryAssoc.class;
98     }
99     
100     /**
101      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
102      *
103      * @roller.wrapPojoMethod type="simple"
104      */

105     public String JavaDoc getObjectPropertyName() {
106         return "category";
107     }
108     
109     /**
110      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
111      *
112      * @roller.wrapPojoMethod type="simple"
113      */

114     public String JavaDoc getAncestorPropertyName() {
115         return "ancestorCategory";
116     }
117     
118     //------------------------------------------------------- Simple properties
119

120     /**
121      * @roller.wrapPojoMethod type="simple"
122      * @ejb:persistent-field
123      * @hibernate.id column="id"
124      * generator-class="uuid.hex" unsaved-value="null"
125      */

126     public java.lang.String JavaDoc getId() {
127         return this.id;
128     }
129     /** @ejb:persistent-field */
130     public void setId(java.lang.String JavaDoc id) {
131         this.id = id;
132     }
133     
134     /**
135      * @roller.wrapPojoMethod type="simple"
136      * @ejb:persistent-field
137      * @hibernate.property column="name" non-null="true" unique="false"
138      */

139     public java.lang.String JavaDoc getName() {
140         return this.name;
141     }
142     /** @ejb:persistent-field */
143     public void setName(java.lang.String JavaDoc name) {
144         this.name = name;
145     }
146     
147     /**
148      * Description
149      *
150      * @roller.wrapPojoMethod type="simple"
151      * @ejb:persistent-field
152      * @hibernate.property column="description" non-null="true" unique="false"
153      */

154     public java.lang.String JavaDoc getDescription() {
155         return this.description;
156     }
157     /** @ejb:persistent-field */
158     public void setDescription(java.lang.String JavaDoc description) {
159         this.description = description;
160     }
161     
162     /**
163      * @roller.wrapPojoMethod type="simple"
164      * @ejb:persistent-field
165      * @hibernate.property column="image" non-null="true" unique="false"
166      */

167     public java.lang.String JavaDoc getImage() {
168         return this.image;
169     }
170     /** @ejb:persistent-field */
171     public void setImage(java.lang.String JavaDoc image) {
172         this.image = image;
173     }
174     
175     /**
176      * Get path in category hierarhcy.
177      *
178      * @roller.wrapPojoMethod type="simple"
179      */

180     public String JavaDoc getPath() {
181         if (null == cachedPath) {
182             try {
183                 cachedPath = RollerFactory.getRoller().getWeblogManager().getPath(this);
184             } catch (RollerException e) {
185                 throw new RuntimeException JavaDoc(e);
186             }
187         }
188         return cachedPath;
189     }
190     
191     //------------------------------------------------------------ Associations
192

193     /**
194      * @roller.wrapPojoMethod type="pojo"
195      * @ejb:persistent-field
196      *
197      * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
198      */

199     public WebsiteData getWebsite() {
200         return website;
201     }
202     /** @ejb:persistent-field */
203     public void setWebsite(WebsiteData website) {
204         this.website = website;
205     }
206     
207     
208     /**
209      * Return parent category, or null if category is root of hierarchy.
210      *
211      * @roller.wrapPojoMethod type="pojo"
212      */

213     public WeblogCategoryData getParent() throws RollerException {
214         if (mNewParent != null) {
215             // Category has new parent, so return that
216
return (WeblogCategoryData)mNewParent;
217         } else if (getParentAssoc() != null) {
218             // Return parent found in database
219
return ((WeblogCategoryAssoc)getParentAssoc()).getAncestorCategory();
220         } else {
221             return null;
222         }
223     }
224     
225     /** Set parent category, database will be updated when object is saved. */
226     public void setParent(HierarchicalPersistentObject parent) {
227         mNewParent = parent;
228     }
229     
230     /**
231      * Query to get child categories of this category.
232      *
233      * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.WeblogCategoryData"
234      */

235     public List JavaDoc getWeblogCategories() throws RollerException {
236         if (weblogCategories == null) {
237             weblogCategories = new LinkedList JavaDoc();
238             List JavaDoc childAssocs = getChildAssocs();
239             Iterator JavaDoc childIter = childAssocs.iterator();
240             while (childIter.hasNext()) {
241                 WeblogCategoryAssoc assoc =
242                         (WeblogCategoryAssoc) childIter.next();
243                 weblogCategories.add(assoc.getCategory());
244             }
245         }
246         return weblogCategories;
247     }
248     
249     /**
250      * @roller.wrapPojoMethod type="simple"
251      */

252     public boolean descendentOf(WeblogCategoryData ancestor)
253     throws RollerException {
254         return RollerFactory.getRoller().getWeblogManager().isDescendentOf(this, ancestor);
255     }
256     
257     /**
258      * Determine if category is in use. Returns true if any weblog entries
259      * use this category or any of it's subcategories.
260      *
261      * @roller.wrapPojoMethod type="simple"
262      */

263     public boolean isInUse() {
264         try {
265             return RollerFactory.getRoller().getWeblogManager().isWeblogCategoryInUse(this);
266         } catch (RollerException e) {
267             throw new RuntimeException JavaDoc(e);
268         }
269     }
270     
271     /** TODO: fix form generation so this is not needed. */
272     public void setInUse(boolean dummy) {}
273     
274     //------------------------------------------------------------------------
275

276     /**
277      * @see org.apache.roller.pojos.HierarchicalPersistentObject#createAssoc(
278      * org.apache.roller.pojos.HierarchicalPersistentObject,
279      * org.apache.roller.pojos.HierarchicalPersistentObject, java.lang.String)
280      */

281     public Assoc createAssoc(
282             HierarchicalPersistentObject object,
283             HierarchicalPersistentObject associatedObject,
284             String JavaDoc relation) throws RollerException {
285         return new WeblogCategoryAssoc(null,
286                 (WeblogCategoryData)object,
287                 (WeblogCategoryData)associatedObject,
288                 relation);
289     }
290     
291     
292     /**
293      * Retrieve all weblog entries in this category and, optionally, include
294      * weblog entries all sub-categories.
295      *
296      * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.WeblogEntryData"
297      *
298      * @param subcats True if entries from sub-categories are to be returned.
299      * @return List of WeblogEntryData objects.
300      * @throws RollerException
301      */

302     public List JavaDoc retrieveWeblogEntries(boolean subcats)
303     throws RollerException {
304         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
305         return wmgr.getWeblogEntries(this, subcats);
306     }
307     
308     //-------------------------------------------------------- Good citizenship
309

310     public String JavaDoc toString() {
311         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
312         
313         str.append(
314                 "id="
315                 + id
316                 + " "
317                 + "name="
318                 + name
319                 + " "
320                 + "description="
321                 + description
322                 + " "
323                 + "image="
324                 + image);
325         str.append('}');
326         
327         return (str.toString());
328     }
329     
330     public boolean equals(Object JavaDoc pOther) {
331         if (pOther == null) return false;
332         if (pOther instanceof WeblogCategoryData) {
333             WeblogCategoryData lTest = (WeblogCategoryData) pOther;
334             boolean lEquals = true;
335             lEquals = PojoUtil.equals(lEquals, this.getId(), lTest.getId());
336             lEquals = PojoUtil.equals(lEquals, this.getName(), lTest.getName());
337             lEquals = PojoUtil.equals(lEquals, this.getDescription(), lTest.getDescription());
338             lEquals = PojoUtil.equals(lEquals, this.getImage(), lTest.getImage());
339             return lEquals;
340         } else {
341             return false;
342         }
343     }
344     
345     public int hashCode() {
346         int result = 17;
347         result = 37 * result + ((this.id != null) ? this.id.hashCode() : 0);
348         result =
349                 37 * result
350                 + ((this.website != null) ? this.website.hashCode() : 0);
351         result = 37 * result + ((this.name != null) ? this.name.hashCode() : 0);
352         result =
353                 37 * result
354                 + ((this.description != null) ? this.description.hashCode() : 0);
355         result =
356                 37 * result + ((this.image != null) ? this.image.hashCode() : 0);
357         return result;
358     }
359     
360     /** TODO: fix Struts form generation template so this is not needed. */
361     public void setAssocClassName(String JavaDoc dummy) {};
362     
363     /** TODO: fix Struts form generation template so this is not needed. */
364     public void setObjectPropertyName(String JavaDoc dummy) {};
365     
366     /** TODO: fix Struts form generation template so this is not needed. */
367     public void setAncestorPropertyName(String JavaDoc dummy) {};
368     
369     /** TODO: fix formbean generation so this is not needed. */
370     public void setPath(String JavaDoc string) {}
371     
372     /**
373      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
374      */

375     public Assoc getParentAssoc() throws RollerException {
376         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryParentAssoc(this);
377     }
378     
379     /**
380      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
381      */

382     public List JavaDoc getChildAssocs() throws RollerException {
383         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryChildAssocs(this);
384     }
385     
386     /**
387      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
388      */

389     public List JavaDoc getAllDescendentAssocs() throws RollerException {
390         return RollerFactory.getRoller().getWeblogManager().getAllWeblogCategoryDecscendentAssocs(this);
391     }
392     
393     /**
394      * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
395      */

396     public List JavaDoc getAncestorAssocs() throws RollerException {
397         return RollerFactory.getRoller().getWeblogManager().getWeblogCategoryAncestorAssocs(this);
398     }
399     
400 }
401
Popular Tags