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 creation date: Mar 28, 2003 / 8:21:56 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: import net.jforum.dao.CategoryDAO;
049: import net.jforum.dao.DataAccessDriver;
050: import net.jforum.dao.ForumDAO;
051: import net.jforum.dao.GroupSecurityDAO;
052: import net.jforum.dao.MailIntegrationDAO;
053: import net.jforum.dao.TopicDAO;
054: import net.jforum.entities.Category;
055: import net.jforum.entities.Forum;
056: import net.jforum.entities.MailIntegration;
057: import net.jforum.repository.ForumRepository;
058: import net.jforum.repository.RolesRepository;
059: import net.jforum.repository.SecurityRepository;
060: import net.jforum.security.PermissionControl;
061: import net.jforum.security.Role;
062: import net.jforum.security.RoleValue;
063: import net.jforum.security.RoleValueCollection;
064: import net.jforum.security.SecurityConstants;
065: import net.jforum.util.TreeGroup;
066: import net.jforum.util.preferences.TemplateKeys;
067: import net.jforum.view.admin.common.ModerationCommon;
068:
069: /**
070: * @author Rafael Steil
071: * @version $Id: ForumAction.java,v 1.34 2007/08/25 00:11:29 rafaelsteil Exp $
072: */
073: public class ForumAction extends AdminCommand {
074: // Listing
075: public void list() {
076: this .context.put("categories", DataAccessDriver.getInstance()
077: .newCategoryDAO().selectAll());
078: this .context.put("repository", new ForumRepository());
079: this .setTemplateName(TemplateKeys.FORUM_ADMIN_LIST);
080: }
081:
082: // One more, one more
083: public void insert() {
084: CategoryDAO cm = DataAccessDriver.getInstance()
085: .newCategoryDAO();
086:
087: this .context.put("groups", new TreeGroup().getNodes());
088: this .context.put("selectedList", new ArrayList());
089: this .setTemplateName(TemplateKeys.FORUM_ADMIN_INSERT);
090: this .context.put("categories", cm.selectAll());
091: this .context.put("action", "insertSave");
092: }
093:
094: // Edit
095: public void edit() {
096: int forumId = this .request.getIntParameter("forum_id");
097: ForumDAO forumDao = DataAccessDriver.getInstance()
098: .newForumDAO();
099:
100: CategoryDAO cm = DataAccessDriver.getInstance()
101: .newCategoryDAO();
102:
103: this .setTemplateName(TemplateKeys.FORUM_ADMIN_EDIT);
104: this .context.put("categories", cm.selectAll());
105: this .context.put("action", "editSave");
106: this .context.put("forum", forumDao.selectById(forumId));
107:
108: // Mail Integration
109: // MailIntegrationDAO integrationDao = DataAccessDriver.getInstance().newMailIntegrationDAO();
110: // this.context.put("mailIntegration", integrationDao.find(forumId));
111: }
112:
113: public void editSave() {
114: ForumDAO forumDao = DataAccessDriver.getInstance()
115: .newForumDAO();
116: Forum f = forumDao.selectById(this .request
117: .getIntParameter("forum_id"));
118:
119: boolean moderated = f.isModerated();
120: int categoryId = f.getCategoryId();
121:
122: f.setDescription(this .request.getParameter("description"));
123: f
124: .setIdCategories(this .request
125: .getIntParameter("categories_id"));
126: f.setName(this .request.getParameter("forum_name"));
127: f.setModerated("1"
128: .equals(this .request.getParameter("moderate")));
129:
130: forumDao.update(f);
131:
132: if (moderated != f.isModerated()) {
133: new ModerationCommon().setTopicModerationStatus(f.getId(),
134: f.isModerated());
135: }
136:
137: if (categoryId != f.getCategoryId()) {
138: f.setIdCategories(categoryId);
139: ForumRepository.removeForum(f);
140:
141: f.setIdCategories(this .request
142: .getIntParameter("categories_id"));
143: ForumRepository.addForum(f);
144: } else {
145: ForumRepository.reloadForum(f.getId());
146: }
147:
148: //this.handleMailIntegration();
149:
150: this .list();
151: }
152:
153: private void handleMailIntegration() {
154: int forumId = this .request.getIntParameter("forum_id");
155: MailIntegrationDAO dao = DataAccessDriver.getInstance()
156: .newMailIntegrationDAO();
157:
158: if (!"1".equals(this .request.getParameter("mail_integration"))) {
159: dao.delete(forumId);
160: } else {
161: boolean exists = dao.find(forumId) != null;
162:
163: MailIntegration m = this .fillMailIntegrationFromRequest();
164:
165: if (exists) {
166: dao.update(m);
167: } else {
168: dao.add(m);
169: }
170: }
171: }
172:
173: private MailIntegration fillMailIntegrationFromRequest() {
174: MailIntegration m = new MailIntegration();
175:
176: m.setForumId(this .request.getIntParameter("forum_id"));
177: m.setForumEmail(this .request.getParameter("forum_email"));
178: m.setPopHost(this .request.getParameter("pop_host"));
179: m.setPopUsername(this .request.getParameter("pop_username"));
180: m.setPopPassword(this .request.getParameter("pop_password"));
181: m.setPopPort(this .request.getIntParameter("pop_port"));
182: m.setSSL("1".equals(this .request.getParameter("requires_ssl")));
183:
184: return m;
185: }
186:
187: public void up() {
188: this .processOrdering(true);
189: }
190:
191: public void down() {
192: this .processOrdering(false);
193: }
194:
195: private void processOrdering(boolean up) {
196: Forum toChange = new Forum(ForumRepository.getForum(Integer
197: .parseInt(this .request.getParameter("forum_id"))));
198:
199: Category category = ForumRepository.getCategory(toChange
200: .getCategoryId());
201: List forums = new ArrayList(category.getForums());
202: int index = forums.indexOf(toChange);
203:
204: if (index == -1 || (up && index == 0)
205: || (!up && index + 1 == forums.size())) {
206: this .list();
207: return;
208: }
209:
210: ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
211:
212: if (up) {
213: // Get the forum which comes *before* the forum we're changing
214: Forum otherForum = new Forum((Forum) forums.get(index - 1));
215: fm.setOrderUp(toChange, otherForum);
216: } else {
217: // Get the forum which comes *after* the forum we're changing
218: Forum otherForum = new Forum((Forum) forums.get(index + 1));
219: fm.setOrderDown(toChange, otherForum);
220: }
221:
222: category.changeForumOrder(toChange);
223: ForumRepository.refreshCategory(category);
224:
225: this .list();
226: }
227:
228: // Delete
229: public void delete() {
230: String ids[] = this .request.getParameterValues("forum_id");
231:
232: ForumDAO forumDao = DataAccessDriver.getInstance()
233: .newForumDAO();
234: TopicDAO topicDao = DataAccessDriver.getInstance()
235: .newTopicDAO();
236:
237: if (ids != null) {
238: for (int i = 0; i < ids.length; i++) {
239: int forumId = Integer.parseInt(ids[i]);
240:
241: topicDao.deleteByForum(forumId);
242: forumDao.delete(forumId);
243:
244: Forum f = new Forum(ForumRepository.getForum(forumId));
245: ForumRepository.removeForum(f);
246: }
247:
248: SecurityRepository.clean();
249: RolesRepository.clear();
250: }
251:
252: this .list();
253: }
254:
255: // A new one
256: public void insertSave() {
257: Forum f = new Forum();
258: f.setDescription(this .request.getParameter("description"));
259: f
260: .setIdCategories(this .request
261: .getIntParameter("categories_id"));
262: f.setName(this .request.getParameter("forum_name"));
263: f.setModerated("1"
264: .equals(this .request.getParameter("moderate")));
265:
266: int forumId = DataAccessDriver.getInstance().newForumDAO()
267: .addNew(f);
268: f.setId(forumId);
269:
270: ForumRepository.addForum(f);
271:
272: GroupSecurityDAO gmodel = DataAccessDriver.getInstance()
273: .newGroupSecurityDAO();
274: PermissionControl pc = new PermissionControl();
275: pc.setSecurityModel(gmodel);
276:
277: String[] allGroups = this .request.getParameterValues("groups");
278:
279: // Access
280: String[] groups = this .request
281: .getParameterValues("groupsAccess");
282: if (groups != null) {
283: this .addRole(pc, SecurityConstants.PERM_FORUM, f.getId(),
284: groups);
285: } else {
286: this .addRole(pc, SecurityConstants.PERM_FORUM, f.getId(),
287: allGroups);
288: }
289:
290: // Anonymous posts
291: groups = this .request.getParameterValues("groupsAnonymous");
292: if (groups != null) {
293: this .addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f
294: .getId(), groups);
295: } else {
296: this .addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f
297: .getId(), allGroups);
298: }
299:
300: // Read-only
301: groups = this .request.getParameterValues("groupsReadOnly");
302: if (groups != null) {
303: this .addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f
304: .getId(), groups);
305: } else {
306: this .addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f
307: .getId(), allGroups);
308: }
309:
310: // Reply-only
311: this .addRole(pc, SecurityConstants.PERM_REPLY_ONLY, f.getId(),
312: allGroups);
313:
314: // HTML
315: groups = this .request.getParameterValues("groupsHtml");
316: if (groups != null) {
317: this .addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f
318: .getId(), groups);
319: } else {
320: this .addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f
321: .getId(), allGroups);
322: }
323:
324: SecurityRepository.clean();
325: RolesRepository.clear();
326:
327: //this.handleMailIntegration();
328:
329: this .list();
330: }
331:
332: private void addRole(PermissionControl pc, String roleName,
333: int forumId, String[] groups) {
334: Role role = new Role();
335: role.setName(roleName);
336:
337: for (int i = 0; i < groups.length; i++) {
338: int groupId = Integer.parseInt(groups[i]);
339: RoleValueCollection roleValues = new RoleValueCollection();
340:
341: RoleValue rv = new RoleValue();
342: rv.setValue(Integer.toString(forumId));
343: roleValues.add(rv);
344:
345: pc.addRoleValue(groupId, role, roleValues);
346: }
347: }
348: }
|