KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > entities > Category


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creating date: Feb 17, 2003 / 10:47:29 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.entities;
44
45 import java.io.Serializable JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Set JavaDoc;
53 import java.util.TreeSet JavaDoc;
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 /**
63  * Represents a category in the System.
64  * Each category holds a reference to all its forums, which
65  * can be retrieved by calling either @link #getForums(),
66  * @link #getForum(int) and related methods.
67  *
68  * <br/>
69  *
70  * This class also controls the access to its forums, so a call
71  * to @link #getForums() will only return the forums accessible
72  * to the user who make the call tho the method.
73  *
74  * @author Rafael Steil
75  * @version $Id: Category.java,v 1.19 2005/07/26 03:04:49 rafaelsteil Exp $
76  */

77 public class Category implements Serializable JavaDoc
78 {
79     private int id;
80     private int order;
81     private boolean moderated;
82     private String JavaDoc name;
83     private Map JavaDoc forumsIdMap = new HashMap JavaDoc();
84     private Set JavaDoc forums = new TreeSet JavaDoc(new ForumOrderComparator());
85         
86     public Category() {}
87     
88     public Category(int id) {
89         this.id = id;
90     }
91     
92     public Category(String JavaDoc 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 JavaDoc 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     /**
119      * @return int
120      */

121     public int getId() {
122         return this.id;
123     }
124
125     /**
126      * @return String
127      */

128     public String JavaDoc getName() {
129         return this.name;
130     }
131
132     /**
133      * @return int
134      */

135     public int getOrder() {
136         return this.order;
137     }
138
139     /**
140      * Sets the id.
141      * @param id The id to set
142      */

143     public void setId(int id) {
144         this.id = id;
145     }
146
147     /**
148      * Sets the name.
149      * @param name The name to set
150      */

151     public void setName(String JavaDoc name) {
152         this.name = name;
153     }
154
155     /**
156      * Sets the order.
157      * @param order The order to set
158      */

159     public void setOrder(int order) {
160         this.order = order;
161     }
162     
163     /**
164      * Adds a forum to this category
165      *
166      * @param forum
167      */

168     public void addForum(Forum forum) {
169         this.forumsIdMap.put(new Integer JavaDoc(forum.getId()), forum);
170         this.forums.add(forum);
171     }
172     
173     /**
174      * Reloads a forum.
175      * The forum should already be in the cache and <b>SHOULD NOT</b>
176      * have its order changed. If the forum's order was changed,
177      * then you <b>MUST CALL</b> @link #changeForumOrder(Forum) <b>BEFORE</b>
178      * calling this method.
179      *
180      * @param forum The forum to reload its information
181      * @throws ForumChangedException if the forum given as parameter
182      * has a modified display order
183      * @throws Exception
184      * @see #changeForumOrder(Forum)
185      */

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 JavaDoc tmpSet = new TreeSet JavaDoc(new ForumOrderComparator());
196         tmpSet.addAll(this.forums);
197         tmpSet.remove(currentForum);
198         tmpSet.add(forum);
199         this.forumsIdMap.put(new Integer JavaDoc(forum.getId()), forum);
200         
201         this.forums = tmpSet;
202     }
203     
204     /**
205      * Changes a forum's display order.
206      * This method changes the position of the
207      * forum in the current display order of the
208      * forum instance passed as argument, if applicable.
209      *
210      * @param forum The forum to change
211      */

212     public void changeForumOrder(Forum forum)
213     {
214         Forum current = this.getForum(forum.getId());
215         Forum currentAtOrder = this.findByOrder(forum.getOrder());
216         
217         Set JavaDoc tmpSet = new TreeSet JavaDoc(new ForumOrderComparator());
218         tmpSet.addAll(this.forums);
219         
220         // Remove the forum in the current order
221
// where the changed forum will need to be
222
if (currentAtOrder != null) {
223             tmpSet.remove(currentAtOrder);
224         }
225         
226         tmpSet.add(forum);
227         this.forumsIdMap.put(new Integer JavaDoc(forum.getId()), forum);
228         
229         // Remove the forum in the position occupied
230
// by the changed forum before its modification,
231
// so then we can add the another forum into
232
// its position
233
if (currentAtOrder != null) {
234             tmpSet.remove(current);
235             currentAtOrder.setOrder(current.getOrder());
236             tmpSet.add(currentAtOrder);
237
238             this.forumsIdMap.put(new Integer JavaDoc(currentAtOrder.getId()), currentAtOrder);
239         }
240         
241         this.forums = tmpSet;
242     }
243     
244     private Forum findByOrder(int order)
245     {
246         for (Iterator JavaDoc 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     /**
257      * Removes a forum from the list.
258      * @param forumId
259      */

260     public void removeForum(int forumId) {
261         this.forums.remove(this.getForum(forumId));
262         this.forumsIdMap.remove(new Integer JavaDoc(forumId));
263     }
264
265     /**
266      * Gets a forum.
267      *
268      * @param userId The user's id who is trying to see the forum
269      * @param forumId The id of the forum to get
270      * @return The <code>Forum</code> instance if found, or <code>null</code>
271      * otherwhise.
272      * @see #getForum(int)
273      */

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 JavaDoc(forumId));
279         }
280         
281         return null;
282     }
283
284     /**
285      * Gets a forum.
286      *
287      * @param forumId The forum's id
288      * @return The requested forum, if found, or <code>null</code> if
289      * the forum does not exists or access to it is denied.
290      * @see #getForum(int, int)
291      */

292     public Forum getForum(int forumId)
293     {
294         return this.getForum(SessionFacade.getUserSession().getUserId(), forumId);
295     }
296
297     /**
298      * Get all forums from this category.
299      *
300      * @return All forums, regardless it is accessible
301      * to the user or not.
302      */

303     public Collection JavaDoc getForums()
304     {
305         if (this.forums.size() == 0) {
306             return this.forums;
307         }
308
309         return this.getForums(SessionFacade.getUserSession().getUserId());
310     }
311
312     /**
313      * Gets all forums from this category.
314      *
315      * @return The forums available to the user who make the call
316      * @see #getForums()
317      */

318     public Collection JavaDoc getForums(int userId)
319     {
320         PermissionControl pc = SecurityRepository.get(userId);
321         List JavaDoc forums = new ArrayList JavaDoc();
322
323         for (Iterator JavaDoc 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     /**
334      * @see java.lang.Object#hashCode()
335      */

336     public int hashCode()
337     {
338         return this.id;
339     }
340
341     /**
342      * @see java.lang.Object#equals(java.lang.Object)
343      */

344     public boolean equals(Object JavaDoc o)
345     {
346         return ((o instanceof Category) && (((Category)o).getId() == this.id));
347     }
348     
349     /**
350      * @see java.lang.Object#toString()
351      */

352     public String JavaDoc toString() {
353         return "[" + this.name + ", id=" + this.id + ", order=" + this.order + "]";
354     }
355
356 }
357
Popular Tags