📄 customermanagesessionbean.java
字号:
package custom_management;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import java.util.Collection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
public class CustomerManageSessionBean
implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
/**
* 由sn获得用户
* @param sn Long
* @return EmployeeObject
*/
public EmployeeObject getWbEployeeBySn(Long sn) {
try {
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
WbEmployee employee = employeeH.findByPrimaryKey(sn);
return new EmployeeObject(employee.getSn().toString(),employee.getName(),employee.getSex(),employee.getAge().intValue(),employee.getAddress(),employee.getPw(),employee.getNote());
}
catch (NamingException ex) {
ex.printStackTrace();
}catch (FinderException ex1) {
ex1.printStackTrace();
}
return null;
}
/**
* 由id获得角色
* @param id Long
* @return RoleObject
*/
public RoleObject getWbRoleById(Long id) {
try {
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
WbRole role = roleH.findByPrimaryKey(id);
return new RoleObject(role.getObjUid().longValue(),role.getName() ,role.getDescription());
}
catch (NamingException ex) {
ex.printStackTrace();
}catch (FinderException ex1) {
ex1.printStackTrace();
}
return null;
}
/**
* 获得所有的用户
* @return Collection
*/
public Collection getWbEmployeeBeans() {
try {
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
return WrapperUtil.wrapperToEmployeeObject(employeeH.findAllEmployees());
}
catch (NamingException ex) {
ex.printStackTrace();
}
catch (FinderException ex1) {
ex1.printStackTrace();
}
return null;
}
/**
* 获得所有的角色
* @return Collection
*/
public Collection getWbRoleBeans() {
try {
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
return WrapperUtil.wrapperToRoleObject(roleH.findAllRoles());
}
catch (NamingException ex) {
ex.printStackTrace();
}catch (FinderException ex1) {
ex1.printStackTrace();
}
return null;
}
/**
* 保存新的用户和该用户对应的角色
* @param newEmployee EmployeeObject
* @param roleMapped Collection 存放Role对象的主键 id
* @return String
*/
public String saveNewEmployee(EmployeeObject newEmployee,
Collection roleMapped) {
try {
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
//
saveNewEmployee(ctx,employeeH,newEmployee,roleMapped);
} catch (NamingException ex) {
ex.printStackTrace();
} catch(CreateException ex) {
ex.printStackTrace();
} catch(FinderException ex) {
ex.printStackTrace();
}
return "";
}
public void saveNewEmployee(Context ctx ,WbEmployeeHome employeeH,EmployeeObject newEmployee,
Collection roleMapped)throws CreateException,NamingException,FinderException
{
//保存新的用户
WbEmployee emp = employeeH.create(new Long(newEmployee.getSn()),
newEmployee.getName(),
newEmployee.getSex(),
new Integer(newEmployee.getAge()),
newEmployee.getAddress(),
newEmployee.getPw(), newEmployee.getNote());
//保存用户对应的角色
setEmployeeToRoleMapping(emp,roleMapped,ctx);
}
/**
* 保存新的角色和该角色对应的用户
* @param newRole RoleObject
* @param employeeMapped Collection 存放Employee对象的主键sn
* @return String
*/
public String saveNewRole(RoleObject newRole, Collection employeeMapped) {
try {
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
//
saveNewRole(ctx,roleH,newRole,employeeMapped);
} catch (NamingException ex) {
ex.printStackTrace();
} catch(CreateException ex) {
ex.printStackTrace();
} catch(FinderException ex) {
ex.printStackTrace();
}
return "";
}
public void saveNewRole(Context ctx, WbRoleHome roleH, RoleObject newRole,
Collection employeeMapped) throws CreateException,
NamingException, FinderException {
//保存新的角色
WbRole role = roleH.create(new Long(newRole.getObjUid()), newRole.getName(),
newRole.getDescription());
//保存角色对应的用户
setRoleToEmployeeMapping(role, employeeMapped, ctx);
}
/**
* 删除sns所指定的多个用户
* @param sns Long[]
* @return String
*/
public String deleteEmployeeBySns(Long[] sns) {
if (sns != null)
for (int i = 0, len = sns.length; i < len; i++)
deleteEmployeeBySn(sns[i]);
return "";
}
/**
* 删除ids指定的多个角色
* @param ids Long[]
* @return String
*/
public String deleteRoleByIds(Long[] ids) {
if (ids != null)
for(int i=0, len= ids.length; i<len; i++)
deleteRoleById(ids[i]);
return "";
}
/**
* 删除sn所指定的一个用户
* @param sn Long
*/
public void deleteEmployeeBySn(Long sn) {
try {
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
employeeH.remove(sn);
//对应角色的删除
}
catch (NamingException ex) {
ex.printStackTrace();
}
catch (RemoveException ex) {
ex.printStackTrace();
}
}
/**
* 删除id指定的角色
* @param id Long
*/
public void deleteRoleById(Long id) {
try {
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
roleH.remove(id);
}
catch (NamingException ex) {
ex.printStackTrace();
}
catch (RemoveException ex) {
ex.printStackTrace();
}
}
/**
* 更新用户
* @param sn Long
* @param updateEmployee EmployeeObject
* @param roleMapped Collection
* @return String
*/
public String updateEmployeeBySn(Long oldsn, EmployeeObject updateEmployee , Collection roleMapped) {
try {
//删掉原来的用户,重新创建一个新的用户
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
WbEmployee emp = employeeH.findByPrimaryKey(oldsn);
emp.remove();
saveNewEmployee(ctx,employeeH,updateEmployee,roleMapped);
}
catch (NamingException ex) {
}
catch(FinderException ex) {
ex.printStackTrace();
}catch(RemoveException ex) {
ex.printStackTrace();
}catch(CreateException es) {
es.printStackTrace();
}
return "";
}
/**
* 更新角色的用户映射
* @param id Long
* @param updateRole RoleObject
* @param employeeMapped Collection
* @return String
*/
public String updateRoleById(Long oldid, RoleObject updateRole , Collection employeeMapped) {
try {
//删掉原来的角色,重新创建一个新的角色
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
WbRole role = roleH.findByPrimaryKey(oldid);
role.remove();
saveNewRole(ctx,roleH,updateRole,employeeMapped);
}
catch (NamingException ex) {
}
catch (FinderException ex) {
ex.printStackTrace();
}catch(RemoveException ex) {
ex.printStackTrace();
}catch(CreateException ex) {
ex.printStackTrace();
}
return "";
}
//获得roleId所对应的用户
public Collection getEmployeesByRoleId(Long roleId) {
try {
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
WbRole role = roleH.findByPrimaryKey(roleId);
return WrapperUtil.wrapperToEmployeeObject(role.getWbEmployee());
}
catch (NamingException ex) {
}
catch (FinderException ex) {
ex.printStackTrace();
}
return null;
}
//获得employeeId所对应的角色
public Collection getRolesByEmployeeId(Long employeeId) {
try {
Context ctx = new InitialContext();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
WbEmployee emp = employeeH.findByPrimaryKey(employeeId);
return WrapperUtil.wrapperToRoleObject(emp.getWbRole());
}
catch (NamingException ex) {
}
catch (FinderException ex) {
ex.printStackTrace();
}
return null;
}
//保存用户对应的角色
public void setEmployeeToRoleMapping(WbEmployee emp, Collection roleMapped ,Context ctx) throws NamingException,FinderException{
if (roleMapped != null && roleMapped.size() > 0)
{
Collection roles = new ArrayList();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
for (Iterator iter = roleMapped.iterator(); iter.hasNext(); ) {
RoleObject item = (RoleObject) iter.next();
roles.add(roleH.findByPrimaryKey(new Long(item.getObjUid())));
}
emp.setWbRole(roles);
}
}
//保存角色对应的用户
public void setRoleToEmployeeMapping(WbRole role, Collection employeeMapped , Context ctx) throws NamingException,FinderException{
if (employeeMapped != null && employeeMapped.size() > 0)
{
Collection emps = new ArrayList();
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
for (Iterator iter = employeeMapped.iterator(); iter.hasNext(); ) {
EmployeeObject item = (EmployeeObject) iter.next();
emps.add(employeeH.findByPrimaryKey(new Long(item.getSn())));
}
role.setWbEmployee(emps);
}
}
//返回未映射的角色
public Collection[] getRolesMappedAndNotMappedByEmpId(Long employeeId) {
try {
Context ctx = new InitialContext();
//获得Bean的本地接口
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
//获得该用户已经映射的角色
WbEmployee employee = employeeH.findByPrimaryKey(employeeId);
Collection roleMapped = employee.getWbRole();
Collection roleAll = roleH.findAllRoles();
Collection roleNotMapped = WrapperUtil.wrapperRoleNotMapped(roleMapped,roleAll);
roleMapped = WrapperUtil.wrapperToRoleObject(roleMapped);
return new Collection[]{roleMapped ,roleNotMapped};
}
catch (NamingException ex) {
}
catch(FinderException ex){
ex.printStackTrace();
}
return null;
}
//返回未映射的用户
public Collection[] getEmployeesMappedAndNotMappedByRoleId(Long roleId) {
try {
//获得Bean的本地接口
Context ctx = new InitialContext();
WbRoleHome roleH = (WbRoleHome) ctx.lookup("WbRole");
WbEmployeeHome employeeH = (WbEmployeeHome) ctx.lookup("WbEmployee");
//获得该角色已经映射的用户
WbRole role = roleH.findByPrimaryKey(roleId);
Collection empMapped = role.getWbEmployee();
Collection empAll = employeeH.findAllEmployees();
Collection empNotMapped = WrapperUtil.wrapperEmployeeNotMapped(empMapped,empAll);
empMapped = WrapperUtil.wrapperToEmployeeObject(empMapped);
return new Collection[]{empMapped ,empNotMapped};
}
catch (NamingException ex) {
}
catch (FinderException ex) {
ex.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -