001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creating date: Feb 17, 2003 / 10:47:29 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.entities;
044:
045: import java.io.Serializable;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.Set;
053: import java.util.TreeSet;
054:
055: import net.jforum.SessionFacade;
056: import net.jforum.exceptions.ForumOrderChangedException;
057: import net.jforum.repository.SecurityRepository;
058: import net.jforum.security.PermissionControl;
059: import net.jforum.security.SecurityConstants;
060: import net.jforum.util.ForumOrderComparator;
061:
062: /**
063: * Represents a category in the System.
064: * Each category holds a reference to all its forums, which
065: * can be retrieved by calling either @link #getForums(),
066: * @link #getForum(int) and related methods.
067: *
068: * <br />
069: *
070: * This class also controls the access to its forums, so a call
071: * to @link #getForums() will only return the forums accessible
072: * to the user who make the call tho the method.
073: *
074: * @author Rafael Steil
075: * @version $Id: Category.java,v 1.22 2007/08/18 07:03:20 andowson Exp $
076: */
077: public class Category implements Serializable {
078: private int id;
079: private int order;
080: private boolean moderated;
081: private String name;
082: private Map forumsIdMap = new HashMap();
083: private Set forums = new TreeSet(new ForumOrderComparator());
084:
085: public Category() {
086: }
087:
088: public Category(int id) {
089: this .id = id;
090: }
091:
092: public Category(String name, int id) {
093: this .name = name;
094: this .id = id;
095: }
096:
097: public Category(Category c) {
098: this .name = c.getName();
099: 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: this .moderated = status;
110: }
111:
112: public boolean isModerated() {
113: return this .moderated;
114: }
115:
116: /**
117: * @return int
118: */
119: public int getId() {
120: return this .id;
121: }
122:
123: /**
124: * @return String
125: */
126: public String getName() {
127: return this .name;
128: }
129:
130: /**
131: * @return int
132: */
133: public int getOrder() {
134: return this .order;
135: }
136:
137: /**
138: * Sets the id.
139: * @param id The id to set
140: */
141: public void setId(int id) {
142: this .id = id;
143: }
144:
145: /**
146: * Sets the name.
147: * @param name The name to set
148: */
149: public void setName(String name) {
150: this .name = name;
151: }
152:
153: /**
154: * Sets the order.
155: * @param order The order to set
156: */
157: public void setOrder(int order) {
158: this .order = order;
159: }
160:
161: /**
162: * Adds a forum to this category
163: *
164: * @param forum Forum
165: */
166: public void addForum(Forum forum) {
167: this .forumsIdMap.put(new Integer(forum.getId()), forum);
168: this .forums.add(forum);
169: }
170:
171: /**
172: * Reloads a forum.
173: * The forum should already be in the cache and <b>SHOULD NOT</b>
174: * have its order changed. If the forum's order was changed,
175: * then you <b>MUST CALL</b> @link #changeForumOrder(Forum) <b>BEFORE</b>
176: * calling this method.
177: *
178: * @param forum The forum to reload its information
179: * @see #changeForumOrder(Forum)
180: */
181: public void reloadForum(Forum forum) {
182: Forum currentForum = this .getForum(forum.getId());
183:
184: if (forum.getOrder() != currentForum.getOrder()) {
185: throw new ForumOrderChangedException(
186: "Forum #"
187: + forum.getId()
188: + " cannot be reloaded, since its "
189: + "display order was changed. You must call Category#changeForumOrder(Forum)"
190: + "first");
191: }
192:
193: Set tmpSet = new TreeSet(new ForumOrderComparator());
194: tmpSet.addAll(this .forums);
195: tmpSet.remove(currentForum);
196: tmpSet.add(forum);
197: this .forumsIdMap.put(new Integer(forum.getId()), forum);
198:
199: this .forums = tmpSet;
200: }
201:
202: /**
203: * Changes a forum's display order.
204: * This method changes the position of the
205: * forum in the current display order of the
206: * forum instance passed as argument, if applicable.
207: *
208: * @param forum The forum to change
209: */
210: public void changeForumOrder(Forum forum) {
211: Forum current = this .getForum(forum.getId());
212: Forum currentAtOrder = this .findByOrder(forum.getOrder());
213:
214: Set tmpSet = new TreeSet(new ForumOrderComparator());
215: tmpSet.addAll(this .forums);
216:
217: // Remove the forum in the current order
218: // where the changed forum will need to be
219: if (currentAtOrder != null) {
220: tmpSet.remove(currentAtOrder);
221: }
222:
223: tmpSet.add(forum);
224: this .forumsIdMap.put(new Integer(forum.getId()), forum);
225:
226: // Remove the forum in the position occupied
227: // by the changed forum before its modification,
228: // so then we can add the another forum into
229: // its position
230: if (currentAtOrder != null) {
231: tmpSet.remove(current);
232: currentAtOrder.setOrder(current.getOrder());
233: tmpSet.add(currentAtOrder);
234:
235: this .forumsIdMap.put(new Integer(currentAtOrder.getId()),
236: currentAtOrder);
237: }
238:
239: this .forums = tmpSet;
240: }
241:
242: private Forum findByOrder(int order) {
243: for (Iterator iter = this .forums.iterator(); iter.hasNext();) {
244: Forum f = (Forum) iter.next();
245: if (f.getOrder() == order) {
246: return f;
247: }
248: }
249:
250: return null;
251: }
252:
253: /**
254: * Removes a forum from the list.
255: * @param forumId int
256: */
257: public void removeForum(int forumId) {
258: this .forums.remove(this .getForum(forumId));
259: this .forumsIdMap.remove(new Integer(forumId));
260: }
261:
262: /**
263: * Gets a forum.
264: *
265: * @param userId The user's id who is trying to see the forum
266: * @param forumId The id of the forum to get
267: * @return The <code>Forum</code> instance if found, or <code>null</code>
268: * otherwhise.
269: * @see #getForum(int)
270: */
271: public Forum getForum(int userId, int forumId) {
272: PermissionControl pc = SecurityRepository.get(userId);
273: if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer
274: .toString(forumId))) {
275: return (Forum) this .forumsIdMap.get(new Integer(forumId));
276: }
277:
278: return null;
279: }
280:
281: /**
282: * Gets a forum.
283: *
284: * @param forumId The forum's id
285: * @return The requested forum, if found, or <code>null</code> if
286: * the forum does not exists or access to it is denied.
287: * @see #getForum(int, int)
288: */
289: public Forum getForum(int forumId) {
290: return this .getForum(
291: SessionFacade.getUserSession().getUserId(), forumId);
292: }
293:
294: /**
295: * Get all forums from this category.
296: *
297: * @return All forums, regardless it is accessible
298: * to the user or not.
299: */
300: public Collection getForums() {
301: if (this .forums.size() == 0) {
302: return this .forums;
303: }
304:
305: return this .getForums(SessionFacade.getUserSession()
306: .getUserId());
307: }
308:
309: /**
310: * Gets all forums from this category.
311: *
312: * @return The forums available to the user who make the call
313: * @see #getForums()
314: * @param userId int
315: */
316: public Collection getForums(int userId) {
317: PermissionControl pc = SecurityRepository.get(userId);
318: List forums = new ArrayList();
319:
320: for (Iterator iter = this .forums.iterator(); iter.hasNext();) {
321: Forum f = (Forum) iter.next();
322: if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer
323: .toString(f.getId()))) {
324: forums.add(f);
325: }
326: }
327:
328: return forums;
329: }
330:
331: /**
332: * @see java.lang.Object#hashCode()
333: */
334: public int hashCode() {
335: return this .id;
336: }
337:
338: /**
339: * @see java.lang.Object#equals(java.lang.Object)
340: */
341: public boolean equals(Object o) {
342: return ((o instanceof Category) && (((Category) o).getId() == this .id));
343: }
344:
345: /**
346: * @see java.lang.Object#toString()
347: */
348: public String toString() {
349: return "[" + this .name + ", id=" + this .id + ", order="
350: + this .order + "]";
351: }
352:
353: }
|