📄 groupmanagerbean.java
字号:
/* * UserEJB - CyberDemia's User management library implemented using EJBs. * Copyright (C) 2004 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; 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 library for complete text of license. */package com.cyberdemia.user;import javax.ejb.*;import javax.naming.*;import java.util.*;//import java.util.logging.*;import com.cyberdemia.ejb.BaseSessionBean;/** * GroupManagerBean is a facade that provides * useful methods for managing user groups. * * @ejb.bean name="GroupManagerBean" * type="Stateless" * view-type="remote" * jndi-name="ejb/GroupManager" * * @ejb.util generate="physical" * @ejb.interface remote-class="com.cyberdemia.user.GroupManager" extends="javax.ejb.EJBObject" * @ejb.home remote-class="com.cyberdemia.user.GroupManagerHome" extends="javax.ejb.EJBHome" * @ejb.ejb-ref ejb-name="UserBean" ref-name="ejb/LocalUser" view-type="local" * @ejb.ejb-ref ejb-name="GroupBean" ref-name="ejb/LocalGroup" view-type="local" * * @author Alexander Yap * @version $Revision: 1.4 $ at $Date: 2004/06/05 14:55:23 $ by $Author: alexycyap $ * */public class GroupManagerBean extends BaseSessionBean{ /** * Adds a new group to the database using the data passed in * by a GroupData instance. Some fields in GroupData are ignored, such * as id and createdMillis. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param data GroupData instance storing data for the new group. * @param userIdCollection IDs of existing users to add to this new group, or null to not add any user. * @return Unique ID (primary key) of the new group. * @throws UserException if an error occurs. */ public Integer addGroup(GroupData data, Collection userIdCollection) throws UserException { Integer gid = null; try { LocalGroup group = s_groupHome.create(data.getName(), data.getParentId()); if (userIdCollection!=null) { Collection usersInGroupCollection = group.getUsers(); Iterator userIdIter = userIdCollection.iterator(); while (userIdIter.hasNext()) { LocalUser user = UserManagerBean.doFindUserById((Integer)userIdIter.next()); usersInGroupCollection.add(user); } } gid = group.getGeneratedPrimaryKey(); } catch (CreateException ex) { getSessionContext().setRollbackOnly(); throw new UserException("Error adding group "+data.getName(), ex); } return gid; } /** * Removed an existing group from the database. * Associated users are NOT removed. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id Unique ID of group to remove. * @throws UserException if there is an error. */ public void removeGroup(Integer id) throws UserException { LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+id, fex); } try { group.remove(); } catch (Exception ex) { getSessionContext().setRollbackOnly(); throw new UserException("Error removing Group with ID "+id, ex); } } /** * Updates an existing group. Only a few fields in group may be updated. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id ID of group to update * @param name New group name * @throws UserException if an error occurs such as the id is invalid. */ public void setGroup(Integer id, String name) throws UserException { LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+id, fex); } group.setName(name); } /** * Gets the data of a group identified by its unique ID. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id Group ID. * @param includeUsers true to retrieve associated users' data, otherwisse false. * @param includeUserRoles if includeUsers is true, then setting includeUserRoles to true will also retrieve the users' roles, otherwise roles are not retrieved. * @return GroupData instance storing the group's data, or null if the group doesn't exist. * @throws UserException if there is an error. */ public GroupData getGroupData(Integer id, boolean includeUsers, boolean includeUserRoles) throws UserException { LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+id, fex); } GroupData data = null; try { data = createGroupData(group, includeUsers, includeUserRoles); } catch (Exception ex) { throw new UserException( "Error getting data for Group "+group.getName(), ex); } return data; } /** * Gets the data of all groups with the specified parent ID. * Optionally return associated users' data. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param parentId Parent ID to search for. * @param includeUsers true to retrieve associated users' data, otherwisse false. * @param includeUserRoles if includeUsers is true, then setting includeUserRoles to true will also retrieve the users' roles, otherwise roles are not retrieved. * @return Array of GroupData instances for found groups, or empty array if no group found. */ public GroupData[] getGroupsByParentId(String parentId, boolean includeUsers, boolean includeUserRoles) { GroupData[] groupDataArr = null; try { Collection groupCollection = s_groupHome.findByParentId(parentId); groupDataArr = convertGroupCollectionToDataArray(groupCollection, includeUsers, includeUserRoles); } catch (FinderException fex) { groupDataArr = new GroupData[0]; } return groupDataArr; } /** * Gets the data of all groups containing the specified user. * This method will <b>not</b> retrieve any user or role related data, * since the caller of this method probably already has the details of the user * in question. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param userId User ID to match. * @return Array of GroupData instances for found groups, or empty array if no group found. */ public GroupData[] getGroupsByUser(Integer userId) throws UserException { LocalUser user = UserManagerBean.doFindUserById(userId); Collection groupCollection = user.getGroups(); return convertGroupCollectionToDataArray(groupCollection, false, false); } /** * Assigns a list of users to a group. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param userIdList List of user IDs. * @param groupId Group ID * @throws UserException if there is an error. */ public void assignUsersToGroup(List userIdList, Integer groupId) throws UserException { LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(groupId); } catch (FinderException fex) { throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+groupId, fex); } Collection usersInGroupCollection = group.getUsers(); Iterator userIdIter = userIdList.iterator(); while (userIdIter.hasNext()) { Integer userId = (Integer)userIdIter.next(); try { LocalUser user = UserManagerBean.doFindUserById(userId); usersInGroupCollection.add(user); } catch (UserException uex) { getSessionContext().setRollbackOnly(); throw uex; } } } /** * Assigns a user to a list of groups. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param userId User Id * @param groupIdList List of group IDs to assign user to. * @throws UserException if there is an error. */ public void assignUserToGroups(Integer userId, List groupIdList) throws UserException { LocalUser user = UserManagerBean.doFindUserById(userId); Iterator groupIdIter = groupIdList.iterator(); while (groupIdIter.hasNext()) { Integer groupId = (Integer)groupIdIter.next(); LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(groupId); Collection usersInGroupCollection = group.getUsers(); usersInGroupCollection.add(user); } catch (FinderException fex) { getSessionContext().setRollbackOnly(); throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+groupId, fex); } } } /** * Removes a list of users from a group. * * @ejb.interface-method * @ejb.transaction type="Required" * * @param id ID of group to remove users from. * @param userIdToRemoveList List of user IDs to remove. * @throws UserException if there is an error. */ public void removeUsersFromGroup(Integer id, List userIdToRemoveList) throws UserException { LocalGroup group = null; try { group = s_groupHome.findByPrimaryKey(id); } catch (FinderException fex) { throw new UserException(UserException.GROUP_NOT_FOUND, "Cannot find Group with ID "+id, fex); } Collection usersInGroupCollection = group.getUsers(); Iterator userToRemoveIdIter = userIdToRemoveList.iterator(); while (userToRemoveIdIter.hasNext()) { try { LocalUser user = UserManagerBean.doFindUserById((Integer)userToRemoveIdIter.next()); usersInGroupCollection.remove(user); } catch (UserException uex) { getSessionContext().setRollbackOnly(); throw uex; } } } /** * Dummy create method. * * @ejb.create-method view-type="remote" */ public void ejbCreate() { // Nothing here } private static GroupData createGroupData(LocalGroup group, boolean includeUsers, boolean includeUserRoles) { GroupData data = new GroupData(group.getId(), group.getParentId(), group.getName(), group.getCreatedMillis()); if (includeUsers) { UserData[] users = UserManagerBean.convertUserCollectionToDataArray(group.getUsers(), includeUserRoles); data.setUsers(users); } return data; } private static GroupData[] convertGroupCollectionToDataArray(Collection groupCollection, boolean includeUsers, boolean includeUserRoles) { GroupData[] groupDataArr = new GroupData[groupCollection.size()]; Iterator groupIter = groupCollection.iterator(); for (int gidx=0; groupIter.hasNext(); gidx++) { LocalGroup group = (LocalGroup)groupIter.next(); groupDataArr[gidx] = createGroupData(group, includeUsers, includeUserRoles); } return groupDataArr; } private static LocalGroupHome s_groupHome = null; static { try { Context ctx = new InitialContext(); try { s_groupHome = (LocalGroupHome)ctx.lookup(JNDI_PREFIX+LocalGroupHome.JNDI_NAME); } finally { ctx.close(); } } catch (NamingException nex) { throw new EJBException("Error retrieving homes", nex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -