001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GroupForumWebHandler.java,v 1.55 2007/12/17 09:09:40 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.55 $
005: * $Date: 2007/12/17 09:09:40 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.admin;
042:
043: import java.util.*;
044:
045: import com.mvnforum.*;
046: import com.mvnforum.auth.*;
047: import com.mvnforum.categorytree.*;
048: import com.mvnforum.db.*;
049: import com.mvnforum.service.CategoryService;
050: import com.mvnforum.service.MvnForumServiceFactory;
051:
052: import net.myvietnam.mvncore.exception.*;
053: import net.myvietnam.mvncore.security.SecurityUtil;
054: import net.myvietnam.mvncore.service.EventLogService;
055: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
056: import net.myvietnam.mvncore.util.AssertionUtil;
057: import net.myvietnam.mvncore.util.GenericParamUtil;
058: import net.myvietnam.mvncore.util.I18nUtil;
059: import net.myvietnam.mvncore.web.GenericRequest;
060: import net.myvietnam.mvncore.web.GenericResponse;
061:
062: import org.apache.commons.logging.Log;
063: import org.apache.commons.logging.LogFactory;
064:
065: public class GroupForumWebHandler {
066:
067: private static Log log = LogFactory
068: .getLog(GroupForumWebHandler.class);
069:
070: private OnlineUserManager onlineUserManager = OnlineUserManager
071: .getInstance();
072:
073: private static CategoryService categoryService = MvnForumServiceFactory
074: .getMvnForumService().getCategoryService();
075:
076: private static EventLogService eventLogService = MvnCoreServiceFactory
077: .getMvnCoreService().getEventLogService();
078:
079: public GroupForumWebHandler() {
080: }
081:
082: public void prepareList(GenericRequest request)
083: throws DatabaseException, BadInputException,
084: ObjectNotFoundException, AuthenticationException {
085:
086: OnlineUser onlineUser = onlineUserManager
087: .getOnlineUser(request);
088: MVNForumPermission permission = onlineUser.getPermission();
089: int groupID = GenericParamUtil
090: .getParameterInt(request, "group");
091: int forumID = GenericParamUtil
092: .getParameterInt(request, "forum");
093:
094: permission.ensureCanAssignToForum(forumID);
095: Locale locale = I18nUtil.getLocaleInRequest(request);
096:
097: GroupsBean groupsBean = DAOFactory.getGroupsDAO().getGroup(
098: groupID);
099: ForumBean forumBean = null;
100: try {
101: forumBean = ForumCache.getInstance().getBean(forumID);
102: } catch (ObjectNotFoundException e) {
103: String localizedMessage = MVNForumResourceBundle
104: .getString(
105: locale,
106: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
107: new Object[] { new Integer(forumID) });
108: throw new ObjectNotFoundException(localizedMessage);
109: }
110:
111: ArrayList groupForumBeans = (ArrayList) DAOFactory
112: .getGroupForumDAO().getBeans_inGroupForum(groupID,
113: forumID);
114: int currentSize = groupForumBeans.size();
115: int[] currentPermissions = new int[currentSize];
116: for (int i = 0; i < currentSize; i++) {
117: GroupForumBean groupForumBean = (GroupForumBean) groupForumBeans
118: .get(i);
119: currentPermissions[i] = groupForumBean.getPermission();
120: }
121:
122: request.setAttribute("GroupsBean", groupsBean);
123: request.setAttribute("ForumBean", forumBean);
124: request.setAttribute("CurrentPermissions", currentPermissions);
125: }
126:
127: public void processUpdate(GenericRequest request)
128: throws CreateException, ObjectNotFoundException,
129: BadInputException, DatabaseException,
130: DuplicateKeyException, ForeignKeyNotFoundException,
131: AuthenticationException {
132:
133: SecurityUtil.checkHttpPostMethod(request);
134:
135: OnlineUser onlineUser = onlineUserManager
136: .getOnlineUser(request);
137: MVNForumPermission permission = onlineUser.getPermission();
138:
139: Locale locale = I18nUtil.getLocaleInRequest(request);
140:
141: String btnAction = GenericParamUtil.getParameter(request,
142: "btnAction");
143: boolean addAction = false;
144:
145: if (btnAction.equals("Add")) {
146: addAction = true;
147: } else if (btnAction.equals("Remove")) {
148: addAction = false;
149: } else {
150: String localizedMessage = MVNForumResourceBundle
151: .getString(
152: locale,
153: "mvncore.exception.BadInputException.cannot_process.no_add_or_remove_is_specified");
154: throw new BadInputException(localizedMessage);
155: }
156:
157: int groupID = GenericParamUtil
158: .getParameterInt(request, "group");
159: int forumID = GenericParamUtil
160: .getParameterInt(request, "forum");
161:
162: permission.ensureCanAssignToForum(forumID);
163:
164: if (addAction) {
165: log.debug("Add List");
166: String[] addList = request.getParameterValues("add");
167: for (int i = 0; (addList != null) && (i < addList.length); i++) {
168: int perm = Integer.parseInt(addList[i]);
169: log.debug("perm = " + perm);
170: DAOFactory.getGroupForumDAO().create(groupID, forumID,
171: perm);
172: }
173: } else {
174: log.debug("Remove List");
175: String[] removeList = request.getParameterValues("remove");
176: for (int i = 0; (removeList != null)
177: && (i < removeList.length); i++) {
178: int perm = Integer.parseInt(removeList[i]);
179: log.debug("perm = " + removeList[i]);
180: DAOFactory.getGroupForumDAO().delete(groupID, forumID,
181: perm);
182: }
183: }//else
184:
185: String actionDesc = MVNForumResourceBundle.getString(
186: MVNForumConfig.getEventLogLocale(),
187: "mvnforum.eventlog.desc.UpdateGroupForumPermission",
188: new Object[] { new Integer(groupID) });
189: eventLogService.logEvent(onlineUser.getMemberName(), request
190: .getRemoteAddr(),
191: MVNForumConstant.EVENT_LOG_MAIN_MODULE,
192: MVNForumConstant.EVENT_LOG_SUB_MODULE_ADMIN,
193: "update group forum permission", actionDesc,
194: EventLogService.MEDIUM);
195:
196: }
197:
198: public void prepareAssignForumToGroup(GenericRequest request,
199: GenericResponse response) throws BadInputException,
200: DatabaseException, ObjectNotFoundException,
201: AuthenticationException {
202:
203: // In this function, we will show the current permission of this group
204: OnlineUser onlineUser = onlineUserManager
205: .getOnlineUser(request);
206: MVNForumPermission permission = onlineUser.getPermission();
207: permission.ensureCanAdminSystem();
208:
209: int groupID = GenericParamUtil
210: .getParameterInt(request, "group");
211:
212: GroupsBean groupsBean = DAOFactory.getGroupsDAO().getGroup(
213: groupID);
214: Collection groupForumBeans = DAOFactory.getGroupForumDAO()
215: .getBeans_inGroup(groupID);
216:
217: request.setAttribute("GroupsBean", groupsBean);
218: request.setAttribute("GroupForumBeans", groupForumBeans);
219:
220: CategoryBuilder builder = new DefaultCategoryBuilder();
221: CategoryTree tree = new CategoryTree(builder);
222: CategoryTreeListener listener = categoryService
223: .getManagementCategorySelector(request, response,
224: "assignforumtogroup");
225: tree.addCategeoryTreeListener(listener);
226: request.setAttribute("Result", tree.build());
227: }
228:
229: public void prepareAssignGroupToForum(GenericRequest request)
230: throws BadInputException, DatabaseException,
231: ObjectNotFoundException, AuthenticationException {
232:
233: // In this function, we will show the current permission of this forum
234: OnlineUser onlineUser = onlineUserManager
235: .getOnlineUser(request);
236: MVNForumPermission permission = onlineUser.getPermission();
237:
238: int forumID = GenericParamUtil
239: .getParameterInt(request, "forum");
240: Locale locale = I18nUtil.getLocaleInRequest(request);
241: permission.ensureCanAssignToForum(forumID);
242: ForumBean forumBean = null;
243: try {
244: forumBean = ForumCache.getInstance().getBean(forumID);
245: } catch (ObjectNotFoundException e) {
246: String localizedMessage = MVNForumResourceBundle
247: .getString(
248: locale,
249: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
250: new Object[] { new Integer(forumID) });
251: throw new ObjectNotFoundException(localizedMessage);
252: }
253: Collection groupsBeans = DAOFactory.getGroupsDAO().getGroups();
254: Collection groupForumBeans = DAOFactory.getGroupForumDAO()
255: .getBeans_inForum(forumID);
256: Collection memberForumBeans = DAOFactory.getMemberForumDAO()
257: .getBeans_inForum(forumID);
258:
259: for (Iterator iter = groupForumBeans.iterator(); iter.hasNext();) {
260: GroupForumBean groupForumBean = (GroupForumBean) iter
261: .next();
262: groupForumBean.setGroupsBean(GroupsBean.getGroupsBean(
263: groupsBeans, groupForumBean.getGroupID()));
264: }
265: int memberID = 0;
266: try {
267: for (Iterator iter = memberForumBeans.iterator(); iter
268: .hasNext();) {
269: MemberForumBean memberForumBean = (MemberForumBean) iter
270: .next();
271: //@todo: Optimize (this method can be called many time for just one memberID)
272: memberID = memberForumBean.getMemberID();
273: MemberBean memberBean = DAOFactory.getMemberDAO()
274: .getMember(memberID);
275: memberForumBean.setMemberBean(memberBean);
276: }
277: } catch (ObjectNotFoundException e) {
278: String localizedMessage = MVNForumResourceBundle
279: .getString(
280: locale,
281: "mvncore.exception.ObjectNotFoundException.memberid_not_exists",
282: new Object[] { new Integer(memberID) });
283: throw new ObjectNotFoundException(localizedMessage);
284: }
285:
286: HashMap groupPermissionMap = new HashMap();
287: for (Iterator iterator = groupForumBeans.iterator(); iterator
288: .hasNext();) {
289: GroupForumBean groupForumBean = (GroupForumBean) iterator
290: .next();
291: String groupName = groupForumBean.getGroupsBean()
292: .getGroupName();
293: int currentPermission = groupForumBean.getPermission();
294: ArrayList combinedPerms;
295: ArrayList individualPerms;
296:
297: if (groupPermissionMap.containsKey(groupName) == false) {
298: ArrayList[] perm = new ArrayList[2];
299: combinedPerms = new ArrayList();
300: individualPerms = new ArrayList();
301: perm[0] = combinedPerms;
302: perm[1] = individualPerms;
303: groupPermissionMap.put(groupName, perm);
304:
305: } else {//map does not have groupName
306: ArrayList[] perm = (ArrayList[]) groupPermissionMap
307: .get(groupName);
308: combinedPerms = perm[0];
309: individualPerms = perm[1];
310: }
311:
312: if (AbstractPermission.isCombinedPerm(currentPermission)) {
313: combinedPerms.add(AbstractPermission
314: .getDescription(currentPermission));
315: } else if (AbstractPermission
316: .isIndividualPerm(currentPermission)) {
317: individualPerms.add(AbstractPermission
318: .getDescription(currentPermission));
319: } else {
320: // should not be here
321: AssertionUtil.doAssert(false, "Serious error!!!");
322: }
323: }//end for
324:
325: HashMap memberPermissionMap = new HashMap();
326: for (Iterator iterator = memberForumBeans.iterator(); iterator
327: .hasNext();) {
328: MemberForumBean memberForumBean = (MemberForumBean) iterator
329: .next();
330: String memberName = memberForumBean.getMemberBean()
331: .getMemberName();
332: int currentPermission = memberForumBean.getPermission();
333: ArrayList combinedPerms;
334: ArrayList individualPerms;
335:
336: if (memberPermissionMap.containsKey(memberName) == false) {
337: ArrayList[] perm = new ArrayList[2];
338: combinedPerms = new ArrayList();
339: individualPerms = new ArrayList();
340: perm[0] = combinedPerms;
341: perm[1] = individualPerms;
342: memberPermissionMap.put(memberName, perm);
343:
344: } else {//map does not have memberName
345: ArrayList[] perm = (ArrayList[]) memberPermissionMap
346: .get(memberName);
347: combinedPerms = perm[0];
348: individualPerms = perm[1];
349: }
350:
351: if (AbstractPermission.isCombinedPerm(currentPermission)) {
352: combinedPerms.add(AbstractPermission
353: .getDescription(currentPermission));
354: } else if (AbstractPermission
355: .isIndividualPerm(currentPermission)) {
356: individualPerms.add(AbstractPermission
357: .getDescription(currentPermission));
358: } else {
359: // should not be here
360: AssertionUtil.doAssert(false, "Serious error!!!");
361: }
362: }//end for
363:
364: request.setAttribute("ForumBean", forumBean);
365: request.setAttribute("GroupsBeans", groupsBeans);
366: request.setAttribute("GroupForumBeans", groupForumBeans);
367: request.setAttribute("MemberForumBeans", memberForumBeans);
368: request
369: .setAttribute("GroupForumPermission",
370: groupPermissionMap);
371: request.setAttribute("MemberForumPermission",
372: memberPermissionMap);
373: }
374: }
|