001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/MemberForumWebHandler.java,v 1.56 2007/12/17 09:09:41 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.56 $
005: * $Date: 2007/12/17 09:09:41 $
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:
042: package com.mvnforum.admin;
043:
044: import java.util.*;
045:
046: import com.mvnforum.*;
047: import com.mvnforum.auth.*;
048: import com.mvnforum.categorytree.*;
049: import com.mvnforum.db.*;
050: import com.mvnforum.service.MvnForumServiceFactory;
051:
052: import com.mvnforum.service.CategoryService;
053: import net.myvietnam.mvncore.exception.*;
054: import net.myvietnam.mvncore.security.SecurityUtil;
055: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
056: import net.myvietnam.mvncore.service.EventLogService;
057: import net.myvietnam.mvncore.util.*;
058: import net.myvietnam.mvncore.web.GenericRequest;
059: import net.myvietnam.mvncore.web.GenericResponse;
060:
061: import org.apache.commons.logging.Log;
062: import org.apache.commons.logging.LogFactory;
063:
064: public class MemberForumWebHandler {
065: private OnlineUserManager onlineUserManager = OnlineUserManager
066: .getInstance();
067:
068: private static Log log = LogFactory
069: .getLog(MemberForumWebHandler.class);
070:
071: private static CategoryService categoryService = MvnForumServiceFactory
072: .getMvnForumService().getCategoryService();
073:
074: private static EventLogService eventLogService = MvnCoreServiceFactory
075: .getMvnCoreService().getEventLogService();
076:
077: public MemberForumWebHandler() {
078: }
079:
080: public void prepareAssignForumToMember(GenericRequest request,
081: GenericResponse response) throws BadInputException,
082: DatabaseException, ObjectNotFoundException,
083: AuthenticationException {
084:
085: OnlineUser onlineUser = onlineUserManager
086: .getOnlineUser(request);
087: MVNForumPermission permission = onlineUser.getPermission();
088: permission.ensureCanAdminSystem();
089:
090: Locale locale = I18nUtil.getLocaleInRequest(request);
091: int memberID = GenericParamUtil.getParameterInt(request,
092: "memberid");
093: MemberBean memberBean = null;
094: try {
095: memberBean = DAOFactory.getMemberDAO().getMember(memberID);
096: } catch (ObjectNotFoundException e) {
097: String localizedMessage = MVNForumResourceBundle
098: .getString(
099: locale,
100: "mvncore.exception.ObjectNotFoundException.memberid_not_exists",
101: new Object[] { new Integer(memberID) });
102: throw new ObjectNotFoundException(localizedMessage);
103: }
104:
105: Collection memberForumBeans = DAOFactory.getMemberForumDAO()
106: .getBeans_inMember(memberID);
107:
108: request.setAttribute("MemberBean", memberBean);
109: request.setAttribute("MemberForumBeans", memberForumBeans);
110:
111: CategoryBuilder builder = new DefaultCategoryBuilder();
112: CategoryTree tree = new CategoryTree(builder);
113: CategoryTreeListener listener = categoryService
114: .getManagementCategorySelector(request, response,
115: "assignforumtogroup");
116: tree.addCategeoryTreeListener(listener);
117: request.setAttribute("Result", tree.build());
118:
119: }
120:
121: public void prepareAssignMemberToForum(GenericRequest request)
122: throws BadInputException, DatabaseException,
123: ObjectNotFoundException, AuthenticationException {
124:
125: OnlineUser onlineUser = onlineUserManager
126: .getOnlineUser(request);
127: MVNForumPermission permission = onlineUser.getPermission();
128:
129: int forumID = GenericParamUtil
130: .getParameterInt(request, "forum");
131:
132: permission.ensureCanAssignToForum(forumID);
133: Locale locale = I18nUtil.getLocaleInRequest(request);
134: ForumBean forumBean = null;
135:
136: try {
137: forumBean = ForumCache.getInstance().getBean(forumID);
138: } catch (ObjectNotFoundException e) {
139: String localizedMessage = MVNForumResourceBundle
140: .getString(
141: locale,
142: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
143: new Object[] { new Integer(forumID) });
144: throw new ObjectNotFoundException(localizedMessage);
145: }
146: Collection groupsBeans = DAOFactory.getGroupsDAO().getGroups();
147: Collection groupForumBeans = DAOFactory.getGroupForumDAO()
148: .getBeans_inForum(forumID);
149: Collection memberForumBeans = DAOFactory.getMemberForumDAO()
150: .getBeans_inForum(forumID);
151:
152: for (Iterator iter = groupForumBeans.iterator(); iter.hasNext();) {
153: GroupForumBean groupForumBean = (GroupForumBean) iter
154: .next();
155: groupForumBean.setGroupsBean(GroupsBean.getGroupsBean(
156: groupsBeans, groupForumBean.getGroupID()));
157: }
158: for (Iterator iter = memberForumBeans.iterator(); iter
159: .hasNext();) {
160: MemberForumBean memberForumBean = (MemberForumBean) iter
161: .next();
162: //@todo: Optimize (this method can be called many time for just one memberID)
163: MemberBean memberBean = null;
164: int memberID = memberForumBean.getMemberID();
165: try {
166: memberBean = DAOFactory.getMemberDAO().getMember(
167: memberID);
168: } catch (ObjectNotFoundException e) {
169: String localizedMessage = MVNForumResourceBundle
170: .getString(
171: locale,
172: "mvncore.exception.ObjectNotFoundException.memberid_not_exists",
173: new Object[] { new Integer(memberID) });
174: throw new ObjectNotFoundException(localizedMessage);
175: }
176: memberForumBean.setMemberBean(memberBean);
177: }
178:
179: HashMap groupPermissionMap = new HashMap();
180: for (Iterator iterator = groupForumBeans.iterator(); iterator
181: .hasNext();) {
182: GroupForumBean groupForumBean = (GroupForumBean) iterator
183: .next();
184: String groupName = groupForumBean.getGroupsBean()
185: .getGroupName();
186: int currentPermission = groupForumBean.getPermission();
187: ArrayList combinedPerms;
188: ArrayList individualPerms;
189:
190: if (groupPermissionMap.containsKey(groupName) == false) {
191: ArrayList[] perm = new ArrayList[2];
192: combinedPerms = new ArrayList();
193: individualPerms = new ArrayList();
194: perm[0] = combinedPerms;
195: perm[1] = individualPerms;
196: groupPermissionMap.put(groupName, perm);
197:
198: } else {//map does not have groupName
199: ArrayList[] perm = (ArrayList[]) groupPermissionMap
200: .get(groupName);
201: combinedPerms = perm[0];
202: individualPerms = perm[1];
203: }
204:
205: if (AbstractPermission.isCombinedPerm(currentPermission)) {
206: combinedPerms.add(AbstractPermission
207: .getDescription(currentPermission));
208: } else if (AbstractPermission
209: .isIndividualPerm(currentPermission)) {
210: individualPerms.add(AbstractPermission
211: .getDescription(currentPermission));
212: } else {
213: // should not be here
214: AssertionUtil.doAssert(false, "Serious error!!!");
215: }
216: }//end for
217:
218: HashMap memberPermissionMap = new HashMap();
219: for (Iterator iterator = memberForumBeans.iterator(); iterator
220: .hasNext();) {
221: MemberForumBean memberForumBean = (MemberForumBean) iterator
222: .next();
223: String memberName = memberForumBean.getMemberBean()
224: .getMemberName();
225: int currentPermission = memberForumBean.getPermission();
226: ArrayList combinedPerms;
227: ArrayList individualPerms;
228:
229: if (memberPermissionMap.containsKey(memberName) == false) {
230: ArrayList[] perm = new ArrayList[2];
231: combinedPerms = new ArrayList();
232: individualPerms = new ArrayList();
233: perm[0] = combinedPerms;
234: perm[1] = individualPerms;
235: memberPermissionMap.put(memberName, perm);
236:
237: } else {//map does not have memberName
238: ArrayList[] perm = (ArrayList[]) memberPermissionMap
239: .get(memberName);
240: combinedPerms = perm[0];
241: individualPerms = perm[1];
242: }
243:
244: if (AbstractPermission.isCombinedPerm(currentPermission)) {
245: combinedPerms.add(AbstractPermission
246: .getDescription(currentPermission));
247: } else if (AbstractPermission
248: .isIndividualPerm(currentPermission)) {
249: individualPerms.add(AbstractPermission
250: .getDescription(currentPermission));
251: } else {
252: // should not be here
253: AssertionUtil.doAssert(false, "Serious error!!!");
254: }
255: }//end for
256:
257: request.setAttribute("ForumBean", forumBean);
258: request.setAttribute("GroupForumBeans", groupForumBeans);
259: request.setAttribute("MemberForumBeans", memberForumBeans);
260: request
261: .setAttribute("GroupForumPermission",
262: groupPermissionMap);
263: request.setAttribute("MemberForumPermission",
264: memberPermissionMap);
265: }
266:
267: public void processUpdate(GenericRequest request)
268: throws CreateException, ObjectNotFoundException,
269: BadInputException, DatabaseException,
270: DuplicateKeyException, ForeignKeyNotFoundException,
271: AuthenticationException {
272:
273: SecurityUtil.checkHttpPostMethod(request);
274:
275: OnlineUser onlineUser = onlineUserManager
276: .getOnlineUser(request);
277: MVNForumPermission permission = onlineUser.getPermission();
278:
279: Locale locale = I18nUtil.getLocaleInRequest(request);
280:
281: String btnAction = GenericParamUtil.getParameter(request,
282: "btnAction");
283: boolean addAction = false;
284:
285: if (btnAction.equals("Add")) {
286: addAction = true;
287: } else if (btnAction.equals("Remove")) {
288: addAction = false;
289: } else {
290: String localizedMessage = MVNForumResourceBundle
291: .getString(
292: locale,
293: "mvncore.exception.BadInputException.cannot_process.no_add_or_remove_is_specified");
294: throw new BadInputException(localizedMessage);
295: }
296:
297: int memberID = GenericParamUtil.getParameterInt(request,
298: "memberid");
299: int forumID = GenericParamUtil
300: .getParameterInt(request, "forum");
301:
302: permission.ensureCanAssignToForum(forumID);
303:
304: if (addAction) {
305: log.debug("Add List");
306: String[] addList = request.getParameterValues("add");
307: for (int i = 0; (addList != null) && (i < addList.length); i++) {
308: int perm = Integer.parseInt(addList[i]);
309: log.debug("perm = " + perm);
310: DAOFactory.getMemberForumDAO().create(memberID,
311: forumID, perm);
312: }
313: } else {
314: log.debug("Remove List");
315: String[] removeList = request.getParameterValues("remove");
316: for (int i = 0; (removeList != null)
317: && (i < removeList.length); i++) {
318: int perm = Integer.parseInt(removeList[i]);
319: log.debug("perm = " + removeList[i]);
320: DAOFactory.getMemberForumDAO().delete(memberID,
321: forumID, perm);
322: }
323: } //else
324:
325: String actionDesc = MVNForumResourceBundle.getString(
326: MVNForumConfig.getEventLogLocale(),
327: "mvnforum.eventlog.desc.UpdateMemberForumPermission",
328: new Object[] { new Integer(memberID),
329: new Integer(forumID) });
330: eventLogService.logEvent(onlineUser.getMemberName(), request
331: .getRemoteAddr(),
332: MVNForumConstant.EVENT_LOG_MAIN_MODULE,
333: MVNForumConstant.EVENT_LOG_SUB_MODULE_ADMIN,
334: "update member forum permission", actionDesc,
335: EventLogService.MEDIUM);
336:
337: }
338:
339: public void prepareList(GenericRequest request)
340: throws DatabaseException, BadInputException,
341: ObjectNotFoundException, AuthenticationException {
342:
343: OnlineUser onlineUser = onlineUserManager
344: .getOnlineUser(request);
345: MVNForumPermission permission = onlineUser.getPermission();
346: Locale locale = I18nUtil.getLocaleInRequest(request);
347: int memberID = -1;
348: String memberName = GenericParamUtil.getParameterSafe(request,
349: "member", false);
350: if (memberName.length() > 0) {
351: StringUtil.checkGoodName(memberName);// check for better security
352: try {
353: memberID = MemberCache.getInstance()
354: .getMemberIDFromMemberName(memberName);
355: } catch (ObjectNotFoundException e) {
356: String localizedMessage = MVNForumResourceBundle
357: .getString(
358: locale,
359: "mvncore.exception.ObjectNotFoundException.membername_not_exists",
360: new Object[] { memberName });
361: throw new ObjectNotFoundException(localizedMessage);
362: }
363: } else {
364: memberID = GenericParamUtil.getParameterInt(request,
365: "memberid");
366: }
367: int forumID = GenericParamUtil
368: .getParameterInt(request, "forum");
369:
370: permission.ensureCanAssignToForum(forumID);
371:
372: MemberBean memberBean = null;
373: try {
374: memberBean = DAOFactory.getMemberDAO().getMember(memberID);
375: } catch (ObjectNotFoundException e) {
376: String localizedMessage = MVNForumResourceBundle
377: .getString(
378: locale,
379: "mvncore.exception.ObjectNotFoundException.memberid_not_exists",
380: new Object[] { new Integer(memberID) });
381: throw new ObjectNotFoundException(localizedMessage);
382: }
383: ForumBean forumBean = null;
384: try {
385: forumBean = ForumCache.getInstance().getBean(forumID);
386: } catch (ObjectNotFoundException e) {
387: String localizedMessage = MVNForumResourceBundle
388: .getString(
389: locale,
390: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
391: new Object[] { new Integer(forumID) });
392: throw new ObjectNotFoundException(localizedMessage);
393: }
394: ArrayList memberForumBeans = (ArrayList) DAOFactory
395: .getMemberForumDAO().getBeans_inMemberForum(memberID,
396: forumID);
397:
398: int currentSize = memberForumBeans.size();
399: int[] currentPermissions = new int[currentSize];
400: for (int i = 0; i < currentSize; i++) {
401: MemberForumBean memberForumBean = (MemberForumBean) memberForumBeans
402: .get(i);
403: currentPermissions[i] = memberForumBean.getPermission();
404: }
405:
406: request.setAttribute("MemberBean", memberBean);
407: request.setAttribute("ForumBean", forumBean);
408: request.setAttribute("CurrentPermissions", currentPermissions);
409: }
410: }
|