1 package org.roller.pojos; 2 3 import java.io.Serializable ; 4 import java.util.Iterator ; 5 import java.util.LinkedList ; 6 import java.util.List ; 7 import java.util.Set ; 8 import java.util.TreeSet ; 9 10 import org.roller.RollerException; 11 import org.roller.business.PersistenceStrategy; 12 import org.roller.model.BookmarkManager; 13 import org.roller.model.Roller; 14 import org.roller.model.RollerFactory; 15 16 29 public class FolderData extends HierarchicalPersistentObject 30 implements Serializable , Comparable 31 { 32 static final long serialVersionUID = -6272468884763861944L; 33 34 protected Set bookmarks = new TreeSet (); 35 protected List folders = null; 36 protected WebsiteData website; 37 38 protected String id; 39 protected String name; 40 protected String description; 41 protected String path; 42 43 45 46 public FolderData() 47 { 48 } 49 50 public FolderData( 51 FolderData parent, 52 String name, 53 String desc, 54 WebsiteData website) 55 { 56 mNewParent = parent; 57 this.name = name; 58 this.description = desc; 59 this.website = website; 60 } 61 62 public void setData(org.roller.pojos.PersistentObject otherData) 63 { 64 mNewParent = ((FolderData) otherData).mNewParent; 65 this.bookmarks = ((FolderData) otherData).bookmarks; 66 this.id = ((FolderData) otherData).id; 67 this.name = ((FolderData) otherData).name; 68 this.description = ((FolderData) otherData).description; 69 this.website = ((FolderData) otherData).website; 70 } 71 72 public void save() throws RollerException 73 { 74 if (RollerFactory.getRoller().getBookmarkManager().isDuplicateFolderName(this)) 75 { 76 throw new RollerException("Duplicate folder name"); 77 } 78 super.save(); 79 } 80 81 84 public Class getAssocClass() 85 { 86 return FolderAssoc.class; 87 } 88 89 92 public String getObjectPropertyName() 93 { 94 return "folder"; 95 } 96 97 100 public String getAncestorPropertyName() 101 { 102 return "ancestorFolder"; 103 } 104 105 public boolean isInUse() 106 { 107 try 108 { 109 return RollerFactory.getRoller().getBookmarkManager().isFolderInUse(this); 110 } 111 catch (RollerException e) 112 { 113 throw new RuntimeException (e); 114 } 115 } 116 117 public boolean descendentOf(FolderData ancestor) 118 throws RollerException 119 { 120 return RollerFactory.getRoller().getBookmarkManager().isDescendentOf(this, ancestor); 121 } 122 123 125 131 public String getId() 132 { 133 return this.id; 134 } 135 136 137 public void setId(String id) 138 { 139 this.id = id; 140 } 141 142 152 public String getName() 153 { 154 return this.name; 155 } 156 157 158 public void setName(String name) 159 { 160 this.name = name; 161 } 162 163 170 public String getDescription() 171 { 172 return this.description; 173 } 174 175 176 public void setDescription(String description) 177 { 178 this.description = description; 179 } 180 181 183 184 public String getPath() throws RollerException 185 { 186 if (mNewParent != null) 187 { 188 throw new RollerException( 189 "Folder has a new parent and must be saved before getPath() will work"); 190 } 191 192 if (null == path) 193 { 194 path = RollerFactory.getRoller().getBookmarkManager().getPath(this); 195 } 196 return path; 197 } 198 199 204 public WebsiteData getWebsite() 205 { 206 return website; 207 } 208 209 210 public void setWebsite( WebsiteData website ) 211 { 212 this.website = website; 213 } 214 215 216 public FolderData getParent() throws RollerException 217 { 218 if (mNewParent != null) 219 { 220 return (FolderData)mNewParent; 222 } 223 else if (getParentAssoc() != null) 224 { 225 return ((FolderAssoc)getParentAssoc()).getAncestorFolder(); 227 } 228 else 229 { 230 return null; 231 } 232 } 233 234 235 public void setParent(HierarchicalPersistentObject parent) 236 { 237 mNewParent = parent; 238 } 239 240 241 public List getFolders() throws RollerException 242 { 243 if (folders == null) 244 { 245 folders = new LinkedList (); 246 List childAssocs = getChildAssocs(); 247 Iterator childIter = childAssocs.iterator(); 248 while (childIter.hasNext()) 249 { 250 FolderAssoc assoc = 251 (FolderAssoc) childIter.next(); 252 folders.add(assoc.getFolder()); 253 } 254 } 255 return folders; 256 } 257 258 260 267 public Set getBookmarks() 268 { 269 return this.bookmarks; 270 } 271 272 273 public void setBookmarks(Set bookmarks) 274 { 275 this.bookmarks = bookmarks; 276 } 277 278 279 public void addBookmark(BookmarkData bookmark) throws RollerException 280 { 281 bookmark.setFolder(this); 282 bookmarks.add(bookmark); 283 bookmark.save(); 284 } 285 286 287 public void removeBookmark(BookmarkData bookmark) 288 { 289 bookmarks.remove(bookmark); 290 bookmark.setFolder(null); 291 } 292 293 296 public List retrieveBookmarks(boolean subfolders) throws RollerException 297 { 298 BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager(); 299 return bmgr.retrieveBookmarks(this, subfolders); 300 } 301 302 306 public void moveContents(FolderData dest) throws RollerException 307 { 308 Iterator entries = retrieveBookmarks(true).iterator(); 309 while (entries.hasNext()) 310 { 311 BookmarkData bookmark = (BookmarkData) entries.next(); 312 bookmark.setFolder(dest); 313 bookmark.save(); 314 } 315 } 316 317 319 324 public Assoc createAssoc( 325 HierarchicalPersistentObject object, 326 HierarchicalPersistentObject associatedObject, 327 String relation) throws RollerException 328 { 329 BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager(); 330 return bmgr.createFolderAssoc( 331 (FolderData)object, 332 (FolderData)associatedObject, 333 relation); 334 } 335 336 338 public String toString() 339 { 340 StringBuffer str = new StringBuffer ("{"); 341 str.append( 342 "bookmarks=" + bookmarks + " " 343 + "id=" + id + " " 344 + "name=" + name + " " 345 + "description=" + description); 346 str.append('}'); 347 return (str.toString()); 348 } 349 350 public boolean equals(Object pOther) 351 { 352 if (pOther instanceof FolderData) 353 { 354 FolderData lTest = (FolderData) pOther; 355 boolean lEquals = true; 356 357 366 if (this.id == null) 367 { 368 lEquals = lEquals && (lTest.id == null); 369 } 370 else 371 { 372 lEquals = lEquals && this.id.equals(lTest.id); 373 } 374 375 if (this.name == null) 376 { 377 lEquals = lEquals && (lTest.name == null); 378 } 379 else 380 { 381 lEquals = lEquals && this.name.equals(lTest.name); 382 } 383 384 if (this.description == null) 385 { 386 lEquals = lEquals && (lTest.description == null); 387 } 388 else 389 { 390 lEquals = lEquals && 391 this.description.equals(lTest.description); 392 } 393 394 if (this.website == null) 395 { 396 lEquals = lEquals && (lTest.website == null); 397 } 398 else 399 { 400 lEquals = lEquals && this.website.equals(lTest.website); 401 } 402 403 return lEquals; 404 } 405 else 406 { 407 return false; 408 } 409 } 410 411 public int hashCode() 412 { 413 int result = 17; 414 415 result = (37 * result) + 416 ((this.id != null) ? this.id.hashCode() : 0); 417 result = (37 * result) + 418 ((this.name != null) ? this.name.hashCode() : 0); 419 result = (37 * result) + 420 ((this.description != null) ? this.description.hashCode() : 0); 421 result = (37 * result) + 422 ((this.website != null) ? this.website.hashCode() : 0); 423 424 return result; 425 } 426 427 430 public int compareTo(Object o) 431 { 432 FolderData other = (FolderData)o; 433 return getName().compareTo(other.getName()); 434 } 435 436 437 public void setAssocClassName(String dummy) {}; 438 439 public void setObjectPropertyName(String dummy) {}; 440 441 public void setAncestorPropertyName(String dummy) {}; 442 443 public void setPath(String string) {} 444 445 public void setInUse(boolean flag) {} 446 447 450 protected Assoc getParentAssoc() throws RollerException 451 { 452 return RollerFactory.getRoller().getBookmarkManager().getFolderParentAssoc(this); 453 } 454 455 458 protected List getChildAssocs() throws RollerException 459 { 460 return RollerFactory.getRoller().getBookmarkManager().getFolderChildAssocs(this); 461 } 462 463 466 public List getAllDescendentAssocs() throws RollerException 467 { 468 return RollerFactory.getRoller().getBookmarkManager().getAllFolderDecscendentAssocs(this); 469 } 470 471 474 public List getAncestorAssocs() throws RollerException 475 { 476 return RollerFactory.getRoller().getBookmarkManager().getFolderAncestorAssocs(this); 477 } 478 479 protected void removeDescendent( 480 PersistenceStrategy pstrategy, PersistentObject po) throws RollerException 481 { 482 487 pstrategy.remove(po); 488 } 489 490 public boolean canSave() throws RollerException 491 { 492 Roller roller = RollerFactory.getRoller(); 493 if (roller.getUser().equals(UserData.SYSTEM_USER)) 494 { 495 return true; 496 } 497 if (roller.getUser().equals(getWebsite().getUser())) 498 { 499 return true; 500 } 501 return false; 502 } 503 } 504 | Popular Tags |