📄 editgroupaction.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2004 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. */package com.cyberdemia.cybertester.admin.user;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.ActionError;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.cyberdemia.cybertester.Constants;import com.cyberdemia.cybertester.CyberTesterUtils;import com.cyberdemia.cybertester.admin.AbstractAdminAction;import com.cyberdemia.cybertester.admin.AdminConstants;import com.cyberdemia.user.GroupData;import com.cyberdemia.user.GroupManager;import com.cyberdemia.user.GroupManagerHome;import com.cyberdemia.user.UserData;/** * * EditGroupAction is a Struts action to load the group editor page, * search for a list of users to be assigned to the group and adds users to the group. * * @version $Revision: 1.5 $ at $Date: 2004/06/17 15:46:22 $ by $Author: alexycyap $ * @author Alexander Yap */public final class EditGroupAction extends AbstractAdminAction { protected ActionForward doPerform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { Logger logger = CyberTesterUtils.getLogger(); String mode = request.getParameter(AdminConstants.MODE_KEY); if (mode==null) mode = AdminConstants.EDITOR_NEW_MODE; if (form==null) { form = new GroupForm(); session.setAttribute(mapping.getAttribute(), form); } GroupForm groupForm = (GroupForm)form; // mode may be one of the following: // new // edit // searchUsers // addUsers // For convenience, just check for new and treat edit, searchUsers and addUsers // mode collectively. // populate the form with data if editing existing test. if (AdminConstants.EDITOR_NEW_MODE.equalsIgnoreCase(mode)) { String hierId = (String)session.getAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY); if (hierId==null) { ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.hierarchyNode.unselected")); saveErrors(request, errors); return mapping.findForward(Constants.ERROR); } groupForm.reset(mapping,request); logger.fine("Adding new Group to hierarchy node "+hierId); } else { Integer groupId = CyberTesterUtils.getIntegerParameterOrRequestAttribute(request,AdminConstants.GROUP_ID_KEY); if (groupId==null) { throw new ServletException("Missing parameter "+AdminConstants.GROUP_ID_KEY); } String userSearchTarget = CyberTesterUtils.getParameterOrRequestAttribute(request, AdminConstants.SEARCH_TARGET_KEY); logger.fine("Editing Group "+groupId); Set searchResultsUserSet = new HashSet(); try { GroupManagerHome gMgrHome = CyberTesterUtils.getGroupManagerHome(); GroupManager groupMgr = (GroupManager)gMgrHome.create(); if ("addUsers".equalsIgnoreCase(mode)) { // If submitted out of order, do nothing. if (isTokenValid(request)) { Enumeration pEnum = request.getParameterNames(); List userIdToAddList = new ArrayList(); while (pEnum.hasMoreElements()) { String pName = (String)pEnum.nextElement(); if (pName.startsWith(AdminConstants.ADD_USER_PARAM_PREFIX) ) { if ( Boolean.valueOf(request.getParameter(pName)).booleanValue() ) { Integer userId = CyberTesterUtils.convertToInteger(pName.substring(AdminConstants.ADD_USER_PARAM_PREFIX.length())); if (userId!=null) { userIdToAddList.add(userId); } } } } groupMgr.assignUsersToGroup(userIdToAddList, groupId); } } GroupData groupData = groupMgr.getGroupData(groupId, true, false); groupForm.setId( groupId ); groupForm.setMode(mode); groupForm.setName( groupData.getName() ); UserData[] usersInGroup = groupData.getUsers(); groupForm.setUsers( usersInGroup ); if ((userSearchTarget!=null) && (userSearchTarget.trim().length()>0)) { groupForm.setSearchTarget(userSearchTarget); logger.log( Level.FINE, "Searching users for target "+userSearchTarget); try { GetUsersAction.searchForUsers( userSearchTarget, searchResultsUserSet); } catch (Exception ex) { logger.log( Level.WARNING, "Error searching users.", ex); } // We must remove users currently assigned to this group from searchResultsUserSet // because these users should not be displayed as available for addition to this group. for (int u=0; u<usersInGroup.length; u++) { searchResultsUserSet.remove( usersInGroup[u] ); } } else { groupForm.setSearchTarget(""); } } catch (Exception ex) { logger.log( Level.SEVERE, "Error transferring Group data to form.", ex); throw ex; } request.setAttribute( AdminConstants.USER_LIST_KEY, searchResultsUserSet ); request.setAttribute(AdminConstants.ADD_USER_PARAM_PREFIX_KEY, AdminConstants.ADD_USER_PARAM_PREFIX); } request.setAttribute(AdminConstants.DISASSOC_PARAM_PREFIX_KEY, AdminConstants.DISASSOC_PARAM_PREFIX); // Set a transactional control token to prevent double posting saveToken(request); return (mapping.findForward(Constants.SUCCESS)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -