1 43 package net.jforum.entities; 44 45 import java.io.Serializable ; 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Map ; 52 import java.util.Set ; 53 import java.util.TreeSet ; 54 55 import net.jforum.SessionFacade; 56 import net.jforum.exceptions.ForumOrderChangedException; 57 import net.jforum.repository.SecurityRepository; 58 import net.jforum.security.PermissionControl; 59 import net.jforum.security.SecurityConstants; 60 import net.jforum.util.ForumOrderComparator; 61 62 77 public class Category implements Serializable 78 { 79 private int id; 80 private int order; 81 private boolean moderated; 82 private String name; 83 private Map forumsIdMap = new HashMap (); 84 private Set forums = new TreeSet (new ForumOrderComparator()); 85 86 public Category() {} 87 88 public Category(int id) { 89 this.id = id; 90 } 91 92 public Category(String name, int id) { 93 this.name = name; 94 this.id = id; 95 } 96 97 public Category(Category c) { 98 this.name = c.getName(); 99 this.id = c.getId(); 100 this.order = c.getOrder(); 101 this.moderated = c.isModerated(); 102 103 for (Iterator iter = c.getForums().iterator(); iter.hasNext(); ) { 104 this.addForum(new Forum((Forum)iter.next())); 105 } 106 } 107 108 public void setModerated(boolean status) 109 { 110 this.moderated = status; 111 } 112 113 public boolean isModerated() 114 { 115 return this.moderated; 116 } 117 118 121 public int getId() { 122 return this.id; 123 } 124 125 128 public String getName() { 129 return this.name; 130 } 131 132 135 public int getOrder() { 136 return this.order; 137 } 138 139 143 public void setId(int id) { 144 this.id = id; 145 } 146 147 151 public void setName(String name) { 152 this.name = name; 153 } 154 155 159 public void setOrder(int order) { 160 this.order = order; 161 } 162 163 168 public void addForum(Forum forum) { 169 this.forumsIdMap.put(new Integer (forum.getId()), forum); 170 this.forums.add(forum); 171 } 172 173 186 public void reloadForum(Forum forum) { 187 Forum currentForum = this.getForum(forum.getId()); 188 189 if (forum.getOrder() != currentForum.getOrder()) { 190 throw new ForumOrderChangedException("Forum #" + forum.getId() + " cannot be reloaded, since its " 191 + "display order was changed. You must call Category#changeForumOrder(Forum)" 192 + "first"); 193 } 194 195 Set tmpSet = new TreeSet (new ForumOrderComparator()); 196 tmpSet.addAll(this.forums); 197 tmpSet.remove(currentForum); 198 tmpSet.add(forum); 199 this.forumsIdMap.put(new Integer (forum.getId()), forum); 200 201 this.forums = tmpSet; 202 } 203 204 212 public void changeForumOrder(Forum forum) 213 { 214 Forum current = this.getForum(forum.getId()); 215 Forum currentAtOrder = this.findByOrder(forum.getOrder()); 216 217 Set tmpSet = new TreeSet (new ForumOrderComparator()); 218 tmpSet.addAll(this.forums); 219 220 if (currentAtOrder != null) { 223 tmpSet.remove(currentAtOrder); 224 } 225 226 tmpSet.add(forum); 227 this.forumsIdMap.put(new Integer (forum.getId()), forum); 228 229 if (currentAtOrder != null) { 234 tmpSet.remove(current); 235 currentAtOrder.setOrder(current.getOrder()); 236 tmpSet.add(currentAtOrder); 237 238 this.forumsIdMap.put(new Integer (currentAtOrder.getId()), currentAtOrder); 239 } 240 241 this.forums = tmpSet; 242 } 243 244 private Forum findByOrder(int order) 245 { 246 for (Iterator iter = this.forums.iterator(); iter.hasNext(); ) { 247 Forum f = (Forum)iter.next(); 248 if (f.getOrder() == order) { 249 return f; 250 } 251 } 252 253 return null; 254 } 255 256 260 public void removeForum(int forumId) { 261 this.forums.remove(this.getForum(forumId)); 262 this.forumsIdMap.remove(new Integer (forumId)); 263 } 264 265 274 public Forum getForum(int userId, int forumId) 275 { 276 PermissionControl pc = SecurityRepository.get(userId); 277 if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(forumId))) { 278 return (Forum)this.forumsIdMap.get(new Integer (forumId)); 279 } 280 281 return null; 282 } 283 284 292 public Forum getForum(int forumId) 293 { 294 return this.getForum(SessionFacade.getUserSession().getUserId(), forumId); 295 } 296 297 303 public Collection getForums() 304 { 305 if (this.forums.size() == 0) { 306 return this.forums; 307 } 308 309 return this.getForums(SessionFacade.getUserSession().getUserId()); 310 } 311 312 318 public Collection getForums(int userId) 319 { 320 PermissionControl pc = SecurityRepository.get(userId); 321 List forums = new ArrayList (); 322 323 for (Iterator iter = this.forums.iterator(); iter.hasNext(); ) { 324 Forum f = (Forum)iter.next(); 325 if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(f.getId()))) { 326 forums.add(f); 327 } 328 } 329 330 return forums; 331 } 332 333 336 public int hashCode() 337 { 338 return this.id; 339 } 340 341 344 public boolean equals(Object o) 345 { 346 return ((o instanceof Category) && (((Category)o).getId() == this.id)); 347 } 348 349 352 public String toString() { 353 return "[" + this.name + ", id=" + this.id + ", order=" + this.order + "]"; 354 } 355 356 } 357 | Popular Tags |