001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.pojos;
019:
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023:
024: import org.apache.roller.RollerException;
025: import org.apache.roller.business.Roller;
026: import org.apache.roller.business.RollerFactory;
027: import org.apache.roller.business.WeblogManager;
028: import org.apache.roller.util.PojoUtil;
029:
030: /**
031: * WeblogCategory bean.
032: * @author David M Johnson
033: *
034: * @ejb:bean name="WeblogCategoryData"
035: * @struts.form include-all="true"
036: * @hibernate.class lazy="false" table="weblogcategory"
037: * @hibernate.cache usage="read-write"
038: */
039: public class WeblogCategoryData extends HierarchicalPersistentObject {
040: public static final long serialVersionUID = 1435782148712018954L;
041:
042: private String id = null;
043: private String name = null;
044: private String description = null;
045: private String image = null;
046:
047: private String cachedPath = null;
048:
049: private WebsiteData website = null;
050: private List weblogCategories = null;
051:
052: public WeblogCategoryData() {
053: }
054:
055: public WeblogCategoryData(java.lang.String id, WebsiteData website,
056: WeblogCategoryData parent, java.lang.String name,
057: java.lang.String description, java.lang.String image) {
058: this .id = id;
059: this .website = website;
060: this .mNewParent = parent;
061: this .name = name;
062: this .description = description;
063: this .image = image;
064: }
065:
066: public WeblogCategoryData(WeblogCategoryData otherData) {
067: this .setData(otherData);
068: }
069:
070: /** Setter is needed in RollerImpl.storePersistentObject(). */
071: public void setData(
072: org.apache.roller.pojos.PersistentObject otherData) {
073: WeblogCategoryData other = (WeblogCategoryData) otherData;
074:
075: this .id = other.getId();
076: this .website = other.getWebsite();
077: this .name = other.getName();
078: this .description = other.getDescription();
079: this .image = other.getImage();
080:
081: try {
082: this .mNewParent = other.getParent();
083: } catch (RollerException re) {
084: // why does this throw an exception?
085: }
086: }
087:
088: /**
089: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAssocClass()
090: */
091: public Class getAssocClass() {
092: return WeblogCategoryAssoc.class;
093: }
094:
095: /**
096: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
097: *
098: * @roller.wrapPojoMethod type="simple"
099: */
100: public String getObjectPropertyName() {
101: return "category";
102: }
103:
104: /**
105: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
106: *
107: * @roller.wrapPojoMethod type="simple"
108: */
109: public String getAncestorPropertyName() {
110: return "ancestorCategory";
111: }
112:
113: //------------------------------------------------------- Simple properties
114:
115: /**
116: * @roller.wrapPojoMethod type="simple"
117: * @ejb:persistent-field
118: * @hibernate.id column="id"
119: * generator-class="uuid.hex" unsaved-value="null"
120: */
121: public java.lang.String getId() {
122: return this .id;
123: }
124:
125: /** @ejb:persistent-field */
126: public void setId(java.lang.String id) {
127: this .id = id;
128: }
129:
130: /**
131: * @roller.wrapPojoMethod type="simple"
132: * @ejb:persistent-field
133: * @hibernate.property column="name" non-null="true" unique="false"
134: */
135: public java.lang.String getName() {
136: return this .name;
137: }
138:
139: /** @ejb:persistent-field */
140: public void setName(java.lang.String name) {
141: this .name = name;
142: }
143:
144: /**
145: * Description
146: *
147: * @roller.wrapPojoMethod type="simple"
148: * @ejb:persistent-field
149: * @hibernate.property column="description" non-null="true" unique="false"
150: */
151: public java.lang.String getDescription() {
152: return this .description;
153: }
154:
155: /** @ejb:persistent-field */
156: public void setDescription(java.lang.String description) {
157: this .description = description;
158: }
159:
160: /**
161: * @roller.wrapPojoMethod type="simple"
162: * @ejb:persistent-field
163: * @hibernate.property column="image" non-null="true" unique="false"
164: */
165: public java.lang.String getImage() {
166: return this .image;
167: }
168:
169: /** @ejb:persistent-field */
170: public void setImage(java.lang.String image) {
171: this .image = image;
172: }
173:
174: /**
175: * Get path in category hierarhcy.
176: *
177: * @roller.wrapPojoMethod type="simple"
178: */
179: public String getPath() {
180: if (null == cachedPath) {
181: try {
182: cachedPath = RollerFactory.getRoller()
183: .getWeblogManager().getPath(this );
184: } catch (RollerException e) {
185: throw new RuntimeException(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:
203: /** @ejb:persistent-field */
204: public void setWebsite(WebsiteData website) {
205: this .website = website;
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())
220: .getAncestorCategory();
221: } else {
222: return null;
223: }
224: }
225:
226: /** Set parent category, database will be updated when object is saved. */
227: public void setParent(HierarchicalPersistentObject parent) {
228: mNewParent = parent;
229: }
230:
231: /**
232: * Query to get child categories of this category.
233: *
234: * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.WeblogCategoryData"
235: */
236: public List getWeblogCategories() throws RollerException {
237: if (weblogCategories == null) {
238: weblogCategories = new LinkedList();
239: List childAssocs = getChildAssocs();
240: Iterator childIter = childAssocs.iterator();
241: while (childIter.hasNext()) {
242: WeblogCategoryAssoc assoc = (WeblogCategoryAssoc) childIter
243: .next();
244: weblogCategories.add(assoc.getCategory());
245: }
246: }
247: return weblogCategories;
248: }
249:
250: /**
251: * @roller.wrapPojoMethod type="simple"
252: */
253: public boolean descendentOf(WeblogCategoryData ancestor)
254: throws RollerException {
255: return RollerFactory.getRoller().getWeblogManager()
256: .isDescendentOf(this , ancestor);
257: }
258:
259: /**
260: * Determine if category is in use. Returns true if any weblog entries
261: * use this category or any of it's subcategories.
262: *
263: * @roller.wrapPojoMethod type="simple"
264: */
265: public boolean isInUse() {
266: try {
267: return RollerFactory.getRoller().getWeblogManager()
268: .isWeblogCategoryInUse(this );
269: } catch (RollerException e) {
270: throw new RuntimeException(e);
271: }
272: }
273:
274: /** TODO: fix form generation so this is not needed. */
275: public void setInUse(boolean dummy) {
276: }
277:
278: //------------------------------------------------------------------------
279:
280: /**
281: * @see org.apache.roller.pojos.HierarchicalPersistentObject#createAssoc(
282: * org.apache.roller.pojos.HierarchicalPersistentObject,
283: * org.apache.roller.pojos.HierarchicalPersistentObject, java.lang.String)
284: */
285: public Assoc createAssoc(HierarchicalPersistentObject object,
286: HierarchicalPersistentObject associatedObject,
287: String relation) throws RollerException {
288: return new WeblogCategoryAssoc(null,
289: (WeblogCategoryData) object,
290: (WeblogCategoryData) associatedObject, relation);
291: }
292:
293: /**
294: * Retrieve all weblog entries in this category and, optionally, include
295: * weblog entries all sub-categories.
296: *
297: * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.WeblogEntryData"
298: *
299: * @param subcats True if entries from sub-categories are to be returned.
300: * @return List of WeblogEntryData objects.
301: * @throws RollerException
302: */
303: public List retrieveWeblogEntries(boolean subcats)
304: throws RollerException {
305: WeblogManager wmgr = RollerFactory.getRoller()
306: .getWeblogManager();
307: return wmgr.getWeblogEntries(this , subcats);
308: }
309:
310: //-------------------------------------------------------- Good citizenship
311:
312: public String toString() {
313: StringBuffer str = new StringBuffer("{");
314:
315: str
316: .append("id=" + id + " " + "name=" + name + " "
317: + "description=" + description + " " + "image="
318: + image);
319: str.append('}');
320:
321: return (str.toString());
322: }
323:
324: public boolean equals(Object pOther) {
325: if (pOther == null)
326: return false;
327: if (pOther instanceof WeblogCategoryData) {
328: WeblogCategoryData lTest = (WeblogCategoryData) pOther;
329: boolean lEquals = true;
330: lEquals = PojoUtil.equals(lEquals, this .getId(), lTest
331: .getId());
332: lEquals = PojoUtil.equals(lEquals, this .getName(), lTest
333: .getName());
334: lEquals = PojoUtil.equals(lEquals, this .getDescription(),
335: lTest.getDescription());
336: lEquals = PojoUtil.equals(lEquals, this .getImage(), lTest
337: .getImage());
338: return lEquals;
339: } else {
340: return false;
341: }
342: }
343:
344: public int hashCode() {
345: int result = 17;
346: result = 37 * result
347: + ((this .id != null) ? this .id.hashCode() : 0);
348: result = 37
349: * result
350: + ((this .website != null) ? this .website.hashCode() : 0);
351: result = 37 * result
352: + ((this .name != null) ? this .name.hashCode() : 0);
353: result = 37
354: * result
355: + ((this .description != null) ? this .description
356: .hashCode() : 0);
357: result = 37 * result
358: + ((this .image != null) ? this .image.hashCode() : 0);
359: return result;
360: }
361:
362: /** TODO: fix Struts form generation template so this is not needed. */
363: public void setAssocClassName(String dummy) {
364: };
365:
366: /** TODO: fix Struts form generation template so this is not needed. */
367: public void setObjectPropertyName(String dummy) {
368: };
369:
370: /** TODO: fix Struts form generation template so this is not needed. */
371: public void setAncestorPropertyName(String dummy) {
372: };
373:
374: /** TODO: fix formbean generation so this is not needed. */
375: public void setPath(String string) {
376: }
377:
378: /**
379: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
380: */
381: public Assoc getParentAssoc() throws RollerException {
382: return RollerFactory.getRoller().getWeblogManager()
383: .getWeblogCategoryParentAssoc(this );
384: }
385:
386: /**
387: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
388: */
389: public List getChildAssocs() throws RollerException {
390: return RollerFactory.getRoller().getWeblogManager()
391: .getWeblogCategoryChildAssocs(this );
392: }
393:
394: /**
395: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
396: */
397: public List getAllDescendentAssocs() throws RollerException {
398: return RollerFactory.getRoller().getWeblogManager()
399: .getAllWeblogCategoryDecscendentAssocs(this );
400: }
401:
402: /**
403: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
404: */
405: public List getAncestorAssocs() throws RollerException {
406: return RollerFactory.getRoller().getWeblogManager()
407: .getWeblogCategoryAncestorAssocs(this);
408: }
409:
410: }
|