Category.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 9k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.io.Serializable;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.HashMap;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.Set;
  52. import java.util.TreeSet;
  53. import net.jforum.SessionFacade;
  54. import net.jforum.exceptions.ForumOrderChangedException;
  55. import net.jforum.repository.SecurityRepository;
  56. import net.jforum.security.PermissionControl;
  57. import net.jforum.security.SecurityConstants;
  58. import net.jforum.util.ForumOrderComparator;
  59. /**
  60.  * Represents a category in the System.
  61.  * Each category holds a reference to all its forums, which 
  62.  * can be retrieved by calling either @link #getForums(), 
  63.  * @link #getForum(int) and related methods. 
  64.  * 
  65.  * <br />
  66.  * 
  67.  * This class also controls the access to its forums, so a call
  68.  * to @link #getForums() will only return the forums accessible
  69.  * to the user who make the call tho the method. 
  70.  * 
  71.  * @author Rafael Steil
  72.  * @version $Id: Category.java,v 1.22 2007/08/18 07:03:20 andowson Exp $
  73.  */
  74. public class Category  implements Serializable
  75. {
  76. private int id;
  77. private int order;
  78. private boolean moderated;
  79. private String name;
  80. private Map forumsIdMap = new HashMap();
  81. private Set forums = new TreeSet(new ForumOrderComparator());
  82. public Category() {}
  83. public Category(int id) {
  84. this.id = id;
  85. }
  86. public Category(String name, int id) {
  87. this.name = name;
  88. this.id = id;
  89. }
  90. public Category(Category c) {
  91. this.name = c.getName();
  92. this.id = c.getId();
  93. this.order = c.getOrder();
  94. this.moderated = c.isModerated();
  95. for (Iterator iter = c.getForums().iterator(); iter.hasNext(); ) {
  96. this.addForum(new Forum((Forum)iter.next()));
  97. }
  98. }
  99. public void setModerated(boolean status)
  100. {
  101. this.moderated = status;
  102. }
  103. public boolean isModerated() 
  104. {
  105. return this.moderated;
  106. }
  107. /**
  108.  * @return int
  109.  */
  110. public int getId() {
  111. return this.id;
  112. }
  113. /**
  114.  * @return String
  115.  */
  116. public String getName() {
  117. return this.name;
  118. }
  119. /**
  120.  * @return int
  121.  */
  122. public int getOrder() {
  123. return this.order;
  124. }
  125. /**
  126.  * Sets the id.
  127.  * @param id The id to set
  128.  */
  129. public void setId(int id) {
  130. this.id = id;
  131. }
  132. /**
  133.  * Sets the name.
  134.  * @param name The name to set
  135.  */
  136. public void setName(String name) {
  137. this.name = name;
  138. }
  139. /**
  140.  * Sets the order.
  141.  * @param order The order to set
  142.  */
  143. public void setOrder(int order) {
  144. this.order = order;
  145. }
  146. /**
  147.  * Adds a forum to this category
  148.  * 
  149.  * @param forum Forum
  150.  */
  151. public void addForum(Forum forum) {
  152. this.forumsIdMap.put(new Integer(forum.getId()), forum);
  153. this.forums.add(forum);
  154. }
  155. /**
  156.  * Reloads a forum.
  157.  * The forum should already be in the cache and <b>SHOULD NOT</b>
  158.  * have its order changed. If the forum's order was changed, 
  159.  * then you <b>MUST CALL</b> @link #changeForumOrder(Forum) <b>BEFORE</b>
  160.  * calling this method.
  161.  * 
  162.  * @param forum The forum to reload its information
  163.  * @see #changeForumOrder(Forum)
  164.  */
  165. public void reloadForum(Forum forum) {
  166. Forum currentForum = this.getForum(forum.getId());
  167. if (forum.getOrder() != currentForum.getOrder()) {
  168. throw new ForumOrderChangedException("Forum #" + forum.getId() + " cannot be reloaded, since its "
  169. + "display order was changed. You must call Category#changeForumOrder(Forum)"
  170. + "first");
  171. }
  172. Set tmpSet = new TreeSet(new ForumOrderComparator());
  173. tmpSet.addAll(this.forums);
  174. tmpSet.remove(currentForum);
  175. tmpSet.add(forum);
  176. this.forumsIdMap.put(new Integer(forum.getId()), forum);
  177. this.forums = tmpSet;
  178. }
  179. /**
  180.  * Changes a forum's display order. 
  181.  * This method changes the position of the
  182.  * forum in the current display order of the
  183.  * forum instance passed as argument, if applicable.
  184.  * 
  185.  * @param forum The forum to change
  186.  */
  187. public void changeForumOrder(Forum forum)
  188. {
  189. Forum current = this.getForum(forum.getId());
  190. Forum currentAtOrder = this.findByOrder(forum.getOrder());
  191. Set tmpSet = new TreeSet(new ForumOrderComparator());
  192. tmpSet.addAll(this.forums);
  193. // Remove the forum in the current order
  194. // where the changed forum will need to be
  195. if (currentAtOrder != null) {
  196. tmpSet.remove(currentAtOrder);
  197. }
  198. tmpSet.add(forum);
  199. this.forumsIdMap.put(new Integer(forum.getId()), forum);
  200. // Remove the forum in the position occupied
  201. // by the changed forum before its modification,
  202. // so then we can add the another forum into 
  203. // its position
  204. if (currentAtOrder != null) {
  205. tmpSet.remove(current);
  206. currentAtOrder.setOrder(current.getOrder());
  207. tmpSet.add(currentAtOrder);
  208. this.forumsIdMap.put(new Integer(currentAtOrder.getId()), currentAtOrder);
  209. }
  210. this.forums = tmpSet;
  211. }
  212. private Forum findByOrder(int order)
  213. {
  214. for (Iterator iter = this.forums.iterator(); iter.hasNext(); ) {
  215. Forum f = (Forum)iter.next();
  216. if (f.getOrder() == order) {
  217. return f;
  218. }
  219. }
  220. return null;
  221. }
  222. /**
  223.  * Removes a forum from the list.
  224.  * @param forumId int
  225.  */
  226. public void removeForum(int forumId) {
  227. this.forums.remove(this.getForum(forumId));
  228. this.forumsIdMap.remove(new Integer(forumId));
  229. }
  230. /**
  231.  * Gets a forum.
  232.  * 
  233.  * @param userId The user's id who is trying to see the forum
  234.  * @param forumId The id of the forum to get
  235.  * @return The <code>Forum</code> instance if found, or <code>null</code>
  236.  * otherwhise.
  237.  * @see #getForum(int)
  238.  */
  239. public Forum getForum(int userId, int forumId)
  240. {
  241. PermissionControl pc = SecurityRepository.get(userId);
  242. if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(forumId))) {
  243. return (Forum)this.forumsIdMap.get(new Integer(forumId));
  244. }
  245. return null;
  246. }
  247. /**
  248.  * Gets a forum.
  249.  * 
  250.  * @param forumId The forum's id 
  251.  * @return The requested forum, if found, or <code>null</code> if
  252.  * the forum does not exists or access to it is denied.
  253.  * @see #getForum(int, int)
  254.  */
  255. public Forum getForum(int forumId)
  256. {
  257. return this.getForum(SessionFacade.getUserSession().getUserId(), forumId);
  258. }
  259. /**
  260.  * Get all forums from this category.
  261.  * 
  262.  * @return All forums, regardless it is accessible 
  263.  * to the user or not.
  264.  */
  265. public Collection getForums()
  266. {
  267. if (this.forums.size() == 0) {
  268. return this.forums;
  269. }
  270. return this.getForums(SessionFacade.getUserSession().getUserId());
  271. }
  272. /**
  273.  * Gets all forums from this category.
  274.  * 
  275.  * @return The forums available to the user who make the call
  276.  * @see #getForums()
  277.      * @param userId int
  278.  */
  279. public Collection getForums(int userId) 
  280. {
  281. PermissionControl pc = SecurityRepository.get(userId);
  282. List forums = new ArrayList();
  283. for (Iterator iter = this.forums.iterator(); iter.hasNext(); ) {
  284. Forum f = (Forum)iter.next();
  285. if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(f.getId()))) {
  286. forums.add(f);
  287. }
  288. }
  289. return forums;
  290. }
  291. /** 
  292.  * @see java.lang.Object#hashCode()
  293.  */
  294. public int hashCode() 
  295. {
  296. return this.id;
  297. }
  298. /** 
  299.  * @see java.lang.Object#equals(java.lang.Object)
  300.  */
  301. public boolean equals(Object o) 
  302. {
  303. return ((o instanceof Category) && (((Category)o).getId() == this.id));
  304. }
  305. /** 
  306.  * @see java.lang.Object#toString()
  307.  */
  308. public String toString() {
  309. return "[" + this.name + ", id=" + this.id + ", order=" + this.order + "]"; 
  310. }
  311. }