1 43 package net.jforum.view.admin; 44 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 import net.jforum.dao.CategoryDAO; 49 import net.jforum.dao.DataAccessDriver; 50 import net.jforum.dao.security.GroupSecurityDAO; 51 import net.jforum.entities.Category; 52 import net.jforum.repository.ForumRepository; 53 import net.jforum.repository.SecurityRepository; 54 import net.jforum.repository.RolesRepository; 55 import net.jforum.security.PermissionControl; 56 import net.jforum.security.Role; 57 import net.jforum.security.RoleValue; 58 import net.jforum.security.RoleValueCollection; 59 import net.jforum.security.SecurityConstants; 60 import net.jforum.util.I18n; 61 import net.jforum.util.TreeGroup; 62 import net.jforum.util.preferences.TemplateKeys; 63 import net.jforum.view.admin.common.ModerationCommon; 64 65 71 public class CategoryAction extends AdminCommand 72 { 73 private CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO(); 74 75 public void list() throws Exception 77 { 78 this.context.put("categories", DataAccessDriver.getInstance().newCategoryDAO().selectAll()); 79 this.context.put("repository", new ForumRepository()); 80 this.setTemplateName(TemplateKeys.CATEGORY_LIST); 81 } 82 83 public void insert() throws Exception 85 { 86 this.context.put("groups", new TreeGroup().getNodes()); 87 this.context.put("selectedList", new ArrayList ()); 88 this.setTemplateName(TemplateKeys.CATEGORY_INSERT); 89 this.context.put("action", "insertSave"); 90 } 91 92 public void edit() throws Exception 94 { 95 this.context.put("category", this.cm.selectById(this.request.getIntParameter("category_id"))); 96 this.setTemplateName(TemplateKeys.CATEGORY_EDIT); 97 this.context.put("action", "editSave"); 98 } 99 100 public void editSave() throws Exception 102 { 103 Category c = new Category(ForumRepository.getCategory( 104 this.request.getIntParameter("categories_id"))); 105 c.setName(this.request.getParameter("category_name")); 106 c.setModerated("1".equals(this.request.getParameter("moderate"))); 107 108 this.cm.update(c); 109 ForumRepository.reloadCategory(c); 110 111 new ModerationCommon().setForumsModerationStatus(c, c.isModerated()); 112 113 this.list(); 114 } 115 116 public void delete() throws Exception 118 { 119 String ids[] = this.request.getParameterValues("categories_id"); 120 List errors = new ArrayList (); 121 122 if (ids != null) { 123 for (int i = 0; i < ids.length; i++){ 124 if (this.cm.canDelete(Integer.parseInt(ids[i]))) { 125 int id = Integer.parseInt(ids[i]); 126 Category c = this.cm.selectById(id); 127 this.cm.delete(id); 128 129 ForumRepository.removeCategory(c); 130 } 131 else { 132 errors.add(I18n.getMessage(I18n.CANNOT_DELETE_CATEGORY, new Object [] { new Integer (ids[i]) })); 133 } 134 } 135 } 136 137 if (errors.size() > 0) { 138 this.context.put("errorMessage", errors); 139 } 140 141 this.list(); 142 } 143 144 public void insertSave() throws Exception 146 { 147 Category c = new Category(); 148 c.setName(this.request.getParameter("category_name")); 149 c.setModerated("1".equals(this.request.getParameter("moderated"))); 150 151 int categoryId = this.cm.addNew(c); 152 c.setId(categoryId); 153 154 ForumRepository.addCategory(c); 155 156 String [] groups = this.request.getParameterValues("groups"); 157 if (groups != null) { 158 GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO(); 159 PermissionControl pc = new PermissionControl(); 160 pc.setSecurityModel(gmodel); 161 162 Role role = new Role(); 163 role.setName(SecurityConstants.PERM_CATEGORY); 164 165 for (int i = 0; i < groups.length; i++) { 166 int groupId = Integer.parseInt(groups[i]); 167 RoleValueCollection roleValues = new RoleValueCollection(); 168 169 RoleValue rv = new RoleValue(); 170 rv.setType(PermissionControl.ROLE_ALLOW); 171 rv.setValue(Integer.toString(categoryId)); 172 173 roleValues.add(rv); 174 175 pc.addRoleValue(groupId, role, roleValues); 176 } 177 178 SecurityRepository.clean(); 179 RolesRepository.clear(); 180 } 181 182 this.list(); 183 } 184 185 public void up() throws Exception 186 { 187 this.processOrdering(true); 188 } 189 190 public void down() throws Exception 191 { 192 this.processOrdering(false); 193 } 194 195 private void processOrdering(boolean up) throws Exception 196 { 197 Category toChange = new Category(ForumRepository.getCategory(Integer.parseInt( 198 this.request.getParameter("category_id")))); 199 200 List categories = ForumRepository.getAllCategories(); 201 202 int index = categories.indexOf(toChange); 203 if (index == -1 || (up && index == 0) || (!up && index + 1 == categories.size())) { 204 this.list(); 205 return; 206 } 207 208 if (up) { 209 Category otherCategory = new Category((Category)categories.get(index - 1)); 211 this.cm.setOrderUp(toChange, otherCategory); 212 } 213 else { 214 Category otherCategory = new Category((Category)categories.get(index + 1)); 216 this.cm.setOrderDown(toChange, otherCategory); 217 } 218 219 ForumRepository.reloadCategory(toChange); 220 this.list(); 221 } 222 } 223 | Popular Tags |