CategoryAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 7k
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 creation date: Mar 10, 2003 / 8:49:51 PM
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.view.admin;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import net.jforum.dao.CategoryDAO;
  47. import net.jforum.dao.DataAccessDriver;
  48. import net.jforum.dao.GroupSecurityDAO;
  49. import net.jforum.entities.Category;
  50. import net.jforum.repository.ForumRepository;
  51. import net.jforum.repository.SecurityRepository;
  52. import net.jforum.repository.RolesRepository;
  53. import net.jforum.security.PermissionControl;
  54. import net.jforum.security.Role;
  55. import net.jforum.security.RoleValue;
  56. import net.jforum.security.RoleValueCollection;
  57. import net.jforum.security.SecurityConstants;
  58. import net.jforum.util.I18n;
  59. import net.jforum.util.TreeGroup;
  60. import net.jforum.util.preferences.TemplateKeys;
  61. import net.jforum.view.admin.common.ModerationCommon;
  62. /**
  63.  * ViewHelper for category administration.
  64.  * 
  65.  * @author Rafael Steil
  66.  * @version $Id: CategoryAction.java,v 1.25 2006/12/07 23:34:04 rafaelsteil Exp $
  67.  */
  68. public class CategoryAction extends AdminCommand 
  69. {
  70. private CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO();
  71. // Listing
  72. public void list()
  73. {
  74. this.context.put("categories", DataAccessDriver.getInstance().newCategoryDAO().selectAll());
  75. this.context.put("repository", new ForumRepository());
  76. this.setTemplateName(TemplateKeys.CATEGORY_LIST);
  77. }
  78. // One more, one more
  79. public void insert()
  80. {
  81. this.context.put("groups", new TreeGroup().getNodes());
  82. this.context.put("selectedList", new ArrayList());
  83. this.setTemplateName(TemplateKeys.CATEGORY_INSERT);
  84. this.context.put("action", "insertSave");
  85. }
  86. // Edit
  87. public void edit()
  88. {
  89. this.context.put("category", this.cm.selectById(this.request.getIntParameter("category_id")));
  90. this.setTemplateName(TemplateKeys.CATEGORY_EDIT);
  91. this.context.put("action", "editSave");
  92. }
  93. //  Save information
  94. public void editSave()
  95. {
  96. Category c = new Category(ForumRepository.getCategory(
  97. this.request.getIntParameter("categories_id")));
  98. c.setName(this.request.getParameter("category_name"));
  99. c.setModerated("1".equals(this.request.getParameter("moderate")));
  100. this.cm.update(c);
  101. ForumRepository.reloadCategory(c);
  102. new ModerationCommon().setForumsModerationStatus(c, c.isModerated());
  103. this.list();
  104. }
  105. // Delete
  106. public void delete()
  107. {
  108. String ids[] = this.request.getParameterValues("categories_id");
  109. List errors = new ArrayList();
  110. if (ids != null) {
  111. for (int i = 0; i < ids.length; i++){
  112. if (this.cm.canDelete(Integer.parseInt(ids[i]))) {
  113. int id = Integer.parseInt(ids[i]);
  114. Category c = this.cm.selectById(id);
  115. this.cm.delete(id);
  116. ForumRepository.removeCategory(c);
  117. }
  118. else {
  119. errors.add(I18n.getMessage(I18n.CANNOT_DELETE_CATEGORY, new Object[] { new Integer(ids[i]) }));
  120. }
  121. }
  122. }
  123. if (errors.size() > 0) {
  124. this.context.put("errorMessage", errors);
  125. }
  126. this.list();
  127. }
  128. // A new one
  129. public void insertSave()
  130. {
  131. Category c = new Category();
  132. c.setName(this.request.getParameter("category_name"));
  133. c.setModerated("1".equals(this.request.getParameter("moderated")));
  134. int categoryId = this.cm.addNew(c);
  135. c.setId(categoryId);
  136. ForumRepository.addCategory(c);
  137. String[] groups = this.request.getParameterValues("groups");
  138. if (groups != null) {
  139. GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO();
  140. PermissionControl pc = new PermissionControl();
  141. pc.setSecurityModel(gmodel);
  142. Role role = new Role();
  143. role.setName(SecurityConstants.PERM_CATEGORY);
  144. for (int i = 0; i < groups.length; i++) {
  145. int groupId = Integer.parseInt(groups[i]);
  146. RoleValueCollection roleValues = new RoleValueCollection();
  147. RoleValue rv = new RoleValue();
  148. rv.setValue(Integer.toString(categoryId));
  149. roleValues.add(rv);
  150. pc.addRoleValue(groupId, role, roleValues);
  151. }
  152. SecurityRepository.clean();
  153. RolesRepository.clear();
  154. }
  155. this.list();
  156. }
  157. public void up()
  158. {
  159. this.processOrdering(true);
  160. }
  161. public void down()
  162. {
  163. this.processOrdering(false);
  164. }
  165. private void processOrdering(boolean up) 
  166. {
  167. Category toChange = new Category(ForumRepository.getCategory(Integer.parseInt(
  168. this.request.getParameter("category_id"))));
  169. List categories = ForumRepository.getAllCategories();
  170. int index = categories.indexOf(toChange);
  171. if (index == -1 || (up && index == 0) || (!up && index + 1 == categories.size())) {
  172. this.list();
  173. return;
  174. }
  175. if (up) {
  176. // Get the category which comes *before* the category we want to change
  177. Category otherCategory = new Category((Category)categories.get(index - 1));
  178. this.cm.setOrderUp(toChange, otherCategory);
  179. }
  180. else {
  181. // Get the category which comes *after* the category we want to change
  182. Category otherCategory = new Category((Category)categories.get(index + 1));
  183. this.cm.setOrderDown(toChange, otherCategory);
  184. }
  185. ForumRepository.reloadCategory(toChange);
  186. this.list();
  187. }
  188. }