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.io.Serializable;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Set;
025: import java.util.TreeSet;
026:
027: import org.apache.roller.RollerException;
028: import org.apache.roller.business.BookmarkManager;
029: import org.apache.roller.business.RollerFactory;
030:
031: /**
032: * <p>Folder that holds Bookmarks and other Folders. A Roller Website has a
033: * set of Folders (there is no one root folder) and each Folder may contain
034: * Folders or Bookmarks. Don't construct one of these yourself, instead use
035: * the create method in your BookmarkManager implementation.</p>
036: *
037: * @struts.form include-all="true"
038: * extends="org.apache.struts.validator.ValidatorForm"
039: * @ejb:bean name="FolderData"
040: *
041: * @hibernate.class lazy="false" table="folder"
042: * @hibernate.cache usage="read-write"
043: */
044: public class FolderData extends HierarchicalPersistentObject implements
045: Serializable, Comparable {
046: static final long serialVersionUID = -6272468884763861944L;
047:
048: private Set bookmarks = new TreeSet();
049: private List folders = null;
050: private WebsiteData website;
051:
052: private String id;
053: private String name;
054: private String description;
055: private String path;
056:
057: //----------------------------------------------------------- Constructors
058:
059: /** For use by BookmarkManager implementations only. */
060: public FolderData() {
061: }
062:
063: public FolderData(FolderData parent, String name, String desc,
064: WebsiteData website) {
065: mNewParent = parent;
066: this .name = name;
067: this .description = desc;
068: this .website = website;
069: }
070:
071: public void setData(
072: org.apache.roller.pojos.PersistentObject otherData) {
073: mNewParent = ((FolderData) otherData).mNewParent;
074: this .id = ((FolderData) otherData).getId();
075: this .name = ((FolderData) otherData).getName();
076: this .description = ((FolderData) otherData).getDescription();
077: this .website = ((FolderData) otherData).getWebsite();
078: this .setBookmarks(((FolderData) otherData).getBookmarks());
079: }
080:
081: /**
082: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAssocClass()
083: */
084: public Class getAssocClass() {
085: return FolderAssoc.class;
086: }
087:
088: /**
089: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getObjectPropertyName()
090: *
091: * @roller.wrapPojoMethod type="simple"
092: */
093: public String getObjectPropertyName() {
094: return "folder";
095: }
096:
097: /**
098: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorPropertyName()
099: *
100: * @roller.wrapPojoMethod type="simple"
101: */
102: public String getAncestorPropertyName() {
103: return "ancestorFolder";
104: }
105:
106: /**
107: * @roller.wrapPojoMethod type="simple"
108: */
109: public boolean isInUse() {
110: try {
111: return RollerFactory.getRoller().getBookmarkManager()
112: .isFolderInUse(this );
113: } catch (RollerException e) {
114: throw new RuntimeException(e);
115: }
116: }
117:
118: /**
119: * @roller.wrapPojoMethod type="simple"
120: */
121: public boolean descendentOf(FolderData ancestor)
122: throws RollerException {
123: return RollerFactory.getRoller().getBookmarkManager()
124: .isDescendentOf(this , ancestor);
125: }
126:
127: //------------------------------------------------------------- Attributes
128:
129: /**
130: * @roller.wrapPojoMethod type="simple"
131: *
132: * @ejb:persistent-field
133: *
134: * @hibernate.id column="id"
135: * generator-class="uuid.hex" unsaved-value="null"
136: */
137: public String getId() {
138: return this .id;
139: }
140:
141: /** @ejb:persistent-field */
142: public void setId(String id) {
143: this .id = id;
144: }
145:
146: /**
147: * @roller.wrapPojoMethod type="simple"
148: *
149: * @struts.validator type="required" msgkey="errors.required"
150: * @struts.validator type="mask" msgkey="errors.noslashes"
151: * @struts.validator-var name="mask" value="${noslashes}"
152: * @struts.validator-args arg0resource="folderForm.name"
153: *
154: * @ejb:persistent-field
155: *
156: * @hibernate.property column="name" non-null="true" unique="false"
157: */
158: public String getName() {
159: return this .name;
160: }
161:
162: /** @ejb:persistent-field */
163: public void setName(String name) {
164: this .name = name;
165: }
166:
167: /**
168: * Description
169: *
170: * @roller.wrapPojoMethod type="simple"
171: *
172: * @ejb:persistent-field
173: *
174: * @hibernate.property column="description" non-null="true" unique="false"
175: */
176: public String getDescription() {
177: return this .description;
178: }
179:
180: /** @ejb:persistent-field */
181: public void setDescription(String description) {
182: this .description = description;
183: }
184:
185: //---------------------------------------------------------- Relationships
186:
187: /**
188: * Get path to this bookmark folder.
189: *
190: * @roller.wrapPojoMethod type="simple"
191: */
192: public String getPath() throws RollerException {
193: if (mNewParent != null) {
194: throw new RollerException(
195: "Folder has a new parent and must be saved before getPath() will work");
196: }
197:
198: if (null == path) {
199: path = RollerFactory.getRoller().getBookmarkManager()
200: .getPath(this );
201: }
202: return path;
203: }
204:
205: /**
206: * @roller.wrapPojoMethod type="pojo"
207: *
208: * @ejb:persistent-field
209: *
210: * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
211: */
212: public WebsiteData getWebsite() {
213: return website;
214: }
215:
216: /** @ejb:persistent-field */
217: public void setWebsite(WebsiteData website) {
218: this .website = website;
219: }
220:
221: /**
222: * Return parent category, or null if category is root of hierarchy.
223: *
224: * @roller.wrapPojoMethod type="pojo"
225: */
226: public FolderData getParent() throws RollerException {
227: if (mNewParent != null) {
228: // Category has new parent, so return that
229: return (FolderData) mNewParent;
230: } else if (getParentAssoc() != null) {
231: // Return parent found in database
232: return ((FolderAssoc) getParentAssoc()).getAncestorFolder();
233: } else {
234: return null;
235: }
236: }
237:
238: /** Set parent category, database will be updated when object is saved. */
239: public void setParent(HierarchicalPersistentObject parent) {
240: mNewParent = parent;
241: }
242:
243: /**
244: * Query to get child categories of this category.
245: *
246: * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.FolderData"
247: */
248: public List getFolders() throws RollerException {
249: if (folders == null) {
250: folders = new LinkedList();
251: List childAssocs = getChildAssocs();
252: Iterator childIter = childAssocs.iterator();
253: while (childIter.hasNext()) {
254: FolderAssoc assoc = (FolderAssoc) childIter.next();
255: folders.add(assoc.getFolder());
256: }
257: }
258: return folders;
259: }
260:
261: //------------------------------------------------------ Bookmark children
262:
263: /**
264: * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.BookmarkData"
265: *
266: * @ejb:persistent-field
267: *
268: * @hibernate.set lazy="true" order-by="name" inverse="true" cascade="all-delete-orphan"
269: * @hibernate.collection-key column="folderid"
270: * @hibernate.collection-one-to-many class="org.apache.roller.pojos.BookmarkData"
271: */
272: public Set getBookmarks() {
273: return this .bookmarks;
274: }
275:
276: // this is private to force the use of add/remove bookmark methods.
277: private void setBookmarks(Set bookmarks) {
278: this .bookmarks = bookmarks;
279: }
280:
281: /** Store bookmark and add to folder */
282: public void addBookmark(BookmarkData bookmark)
283: throws RollerException {
284: bookmark.setFolder(this );
285: getBookmarks().add(bookmark);
286: }
287:
288: /** Remove boomkark from folder */
289: public void removeBookmark(BookmarkData bookmark) {
290: getBookmarks().remove(bookmark);
291: }
292:
293: /**
294: * @roller.wrapPojoMethod type="pojo-collection" class="org.apache.roller.pojos.BookmarkData"
295: *
296: * @param subfolders
297: */
298: public List retrieveBookmarks(boolean subfolders)
299: throws RollerException {
300: BookmarkManager bmgr = RollerFactory.getRoller()
301: .getBookmarkManager();
302: return bmgr.getBookmarks(this , subfolders);
303: }
304:
305: /**
306: * Move all bookmarks that exist in this folder and all
307: * subfolders of this folder to a single new folder.
308: */
309: public void moveContents(FolderData dest) throws RollerException {
310: Iterator entries = retrieveBookmarks(true).iterator();
311: while (entries.hasNext()) {
312: BookmarkData bookmark = (BookmarkData) entries.next();
313:
314: // just add bookmarks to new folder
315: // this breaks the old folder/bkmrk relationship
316: // so it's not necessary to explicitly remove
317: dest.addBookmark(bookmark);
318: }
319: }
320:
321: //------------------------------------------------------------------------
322:
323: /**
324: * @see org.apache.roller.pojos.HierarchicalPersistentObject#createAssoc(
325: * org.apache.roller.pojos.HierarchicalPersistentObject,
326: * org.apache.roller.pojos.HierarchicalPersistentObject, java.lang.String)
327: */
328: public Assoc createAssoc(HierarchicalPersistentObject object,
329: HierarchicalPersistentObject associatedObject,
330: String relation) throws RollerException {
331: return new FolderAssoc(null, (FolderData) object,
332: (FolderData) associatedObject, relation);
333: }
334:
335: //------------------------------------------------------- Good citizenship
336:
337: public String toString() {
338: StringBuffer str = new StringBuffer("{");
339: str.append("bookmarks=" + bookmarks + " " + "id=" + id + " "
340: + "name=" + name + " " + "description=" + description);
341: str.append('}');
342: return (str.toString());
343: }
344:
345: public boolean equals(Object pOther) {
346: if (pOther instanceof FolderData) {
347: FolderData lTest = (FolderData) pOther;
348: boolean lEquals = true;
349:
350: // if (this.bookmarks == null)
351: // {
352: // lEquals = lEquals && (lTest.bookmarks == null);
353: // }
354: // else
355: // {
356: // lEquals = lEquals && this.bookmarks.equals(lTest.bookmarks);
357: // }
358:
359: if (this .id == null) {
360: lEquals = lEquals && (lTest.getId() == null);
361: } else {
362: lEquals = lEquals && this .id.equals(lTest.getId());
363: }
364:
365: if (this .name == null) {
366: lEquals = lEquals && (lTest.getName() == null);
367: } else {
368: lEquals = lEquals && this .name.equals(lTest.getName());
369: }
370:
371: if (this .description == null) {
372: lEquals = lEquals && (lTest.getDescription() == null);
373: } else {
374: lEquals = lEquals
375: && this .description.equals(lTest
376: .getDescription());
377: }
378:
379: if (this .website == null) {
380: lEquals = lEquals && (lTest.getWebsite() == null);
381: } else {
382: lEquals = lEquals
383: && this .website.equals(lTest.getWebsite());
384: }
385:
386: return lEquals;
387: } else {
388: return false;
389: }
390: }
391:
392: public int hashCode() {
393: int result = 17;
394:
395: result = (37 * result)
396: + ((this .id != null) ? this .id.hashCode() : 0);
397: result = (37 * result)
398: + ((this .name != null) ? this .name.hashCode() : 0);
399: result = (37 * result)
400: + ((this .description != null) ? this .description
401: .hashCode() : 0);
402: result = (37 * result)
403: + ((this .website != null) ? this .website.hashCode() : 0);
404:
405: return result;
406: }
407:
408: /**
409: * @see java.lang.Comparable#compareTo(java.lang.Object)
410: */
411: public int compareTo(Object o) {
412: FolderData other = (FolderData) o;
413: return getName().compareTo(other.getName());
414: }
415:
416: /** TODO: fix Struts form generation template so this is not needed. */
417: public void setAssocClassName(String dummy) {
418: };
419:
420: /** TODO: fix Struts form generation template so this is not needed. */
421: public void setObjectPropertyName(String dummy) {
422: };
423:
424: /** TODO: fix Struts form generation template so this is not needed. */
425: public void setAncestorPropertyName(String dummy) {
426: };
427:
428: /** TODO: fix formbean generation so this is not needed. */
429: public void setPath(String string) {
430: }
431:
432: /** TODO: fix formbean generation so this is not needed. */
433: public void setInUse(boolean flag) {
434: }
435:
436: /**
437: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getParentAssoc()
438: */
439: public Assoc getParentAssoc() throws RollerException {
440: return RollerFactory.getRoller().getBookmarkManager()
441: .getFolderParentAssoc(this );
442: }
443:
444: /**
445: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getChildAssocs()
446: */
447: public List getChildAssocs() throws RollerException {
448: return RollerFactory.getRoller().getBookmarkManager()
449: .getFolderChildAssocs(this );
450: }
451:
452: /**
453: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAllDescendentAssocs()
454: */
455: public List getAllDescendentAssocs() throws RollerException {
456: return RollerFactory.getRoller().getBookmarkManager()
457: .getAllFolderDecscendentAssocs(this );
458: }
459:
460: /**
461: * @see org.apache.roller.pojos.HierarchicalPersistentObject#getAncestorAssocs()
462: */
463: public List getAncestorAssocs() throws RollerException {
464: return RollerFactory.getRoller().getBookmarkManager()
465: .getFolderAncestorAssocs(this);
466: }
467:
468: }
|