@WebService
public
class
UserHelper
implements
IUserHelper {
private
IUserDao userDao =
null
;
private
IDepartmentDao departDao =
null
;
private
IFunctionHelper functionHelper =
null
;
private
IRoleHelper roleHelper =
null
;
public
void
setUserDao(IUserDao userDao) {
this
.userDao = userDao;
}
public
void
setFunctionHelper(IFunctionHelper functionHelper) {
this
.functionHelper = functionHelper;
}
public
void
setDepartDao(IDepartmentDao departDao) {
this
.departDao = departDao;
}
public
void
setRoleHelper(IRoleHelper roleHelper) {
this
.roleHelper = roleHelper;
}
/**
* 通过id查询权限,没有则返回null
*
* @param id
* @return
*/
public
UserVO queryUserById(Long id)
throws
GeneralException {
User user =
null
;
try
{
user = userDao.queryUserById(id);
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userDeatil"
,
"通过id查询权限时出错!"
);
}
UserVO userVO = userPoToVo(user);
return
userVO;
}
/**
* 得到所有权限的集合,没有则返回 null
*
* @return
*/
public
List<UserVO> queryAllUser()
throws
GeneralException {
List<UserVO> allFuncVOs =
new
ArrayList<UserVO>();
List<User> allFuncs =
null
;
try
{
allFuncs = userDao.queryAllUser();
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userList"
,
"得到所有权限的集合时出错!"
);
}
for
(Iterator<?> iterator = allFuncs.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
UserVO userVO = userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
* 权限的PO 到 VO 转换的方法
*
* @param user
* @return
*/
public
UserVO userPoToVo(User user)
throws
GeneralException {
UserVO userVO =
new
UserVO();
BeanUtil.copyProperties(userVO, user);
try
{
userVO.setDepartName(departDao.queryNameById(user.getDepartid()));
}
catch
(Exception e) {
throw
new
GeneralException(
"error.user"
,
" 权限的PO 到 VO 转换时出错!"
);
}
if
(userVO.getStatus().equals(
"1"
)){
userVO.setStatus(
"可用"
);
}
else
{
userVO.setStatus(
"不可用"
);
}
userVO.setRegisterName(
"ZHANG"
);
userVO.setChangerName(
"ZHANG"
);
return
userVO;
}
/**
* 通过分页查询权限信息集合
*
* @param hql
* @param currentPage
* @param pageSize
* @return
* @throws GeneralException
*/
public
List<UserVO> queryUserByPage(String hql,
int
currentPage,
int
pageSize)
throws
GeneralException {
List<UserVO> allFuncVOs =
new
ArrayList<UserVO>();
List<User> allFuncs =
null
;
try
{
allFuncs = userDao.queryAllUser(hql, currentPage, pageSize);
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userList"
,
"分页得到权限的集合时出错!"
);
}
for
(Iterator<?> iterator = allFuncs.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
UserVO userVO = userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
* 返回User分页对象
*
* @param currentPage
* @return
*/
public
Pagination getPagination(
int
currentPage, String hql) {
return
new
Pagination(currentPage,
DisplayRecordCount.DISPLAY_IN_USER_LIST, userDao
.queryAllUserNubmer(hql));
}
/**
* 查到用户的所有角色ID
*
* @param userId
* @return
* @throws GeneralException
*/
public
List<Long> queryAllRoleidsOfUser(Long userId)
throws
GeneralException {
List<Long> rolesOfUser =
new
ArrayList<Long>();
List<UserRole> userRoles =
null
;
try
{
userRoles = userDao.queryRoleOnlyByUserId(userId);
}
catch
(Exception e) {
throw
new
GeneralException(
"error.userRoleidsList"
,
"得到用户的角色ID集合出错!"
);
}
for
(Iterator<?> iterator = userRoles.iterator(); iterator.hasNext();) {
UserRole userRole = (UserRole) iterator.next();
Long roleid = userRole.getRoleid();
rolesOfUser.add(roleid);
}
return
rolesOfUser;
}
/**
* 查到用户的所有角色
*
* @param userId
* @return
* @throws GeneralException
*/
public
List<RoleVO> queryAllRoleOfUser(Long userId)
throws
GeneralException {
List<Long> rolesOfUser =
new
ArrayList<Long>();
List<RoleVO> userRoles =
new
ArrayList<RoleVO>();
try
{
rolesOfUser = queryAllRoleidsOfUser(userId);
for
(Iterator<?> iterator = rolesOfUser.iterator(); iterator
.hasNext();) {
Long roleid = (Long) iterator.next();
RoleVO roleVO = roleHelper.queryRoleById(roleid);
userRoles.add(roleVO);
}
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userRoleList"
,
"得到用户的角色集合出错!"
);
}
return
userRoles;
}
/**
* 查询用户的所有权限 1.查询所有用户的权限 2.查询所有用户的角色 3.查询所有用户的权限+角色的权限-共同的权限
*
* @param userId
* @return
*/
public
List<FunctionVO> queryAllFunctionOfUser(Long userId,String system)
throws
GeneralException {
Set<FunctionVO> userAllFuncs =
new
HashSet<FunctionVO>();
List<FunctionVO> userAllFuncsList =
new
ArrayList<FunctionVO>();
try
{
List<UserAuth> userFuncs = userDao
.queryFunctionOnlyByUserId(userId);
if
(userFuncs !=
null
) {
for
(Iterator<?> iterator = userFuncs.iterator(); iterator
.hasNext();) {
UserAuth userFunc = (UserAuth) iterator.next();
Long funcId = userFunc.getFuncid();
FunctionVO funcVO = functionHelper
.queryFunctionById(funcId);
userAllFuncs.add(funcVO);
}
}
List<UserRole> userRoles = userDao.queryRoleOnlyByUserId(userId);
if
(userRoles !=
null
) {
for
(Iterator<?> iterator = userRoles.iterator(); iterator
.hasNext();) {
UserRole userRole = (UserRole) iterator.next();
Long roleId = userRole.getRoleid();
List<FunctionVO> funcVOs = roleHelper
.queryFunctionOfRole(roleId);
for
(Iterator<?> iterator2 = funcVOs.iterator(); iterator2
.hasNext();) {
FunctionVO functionVO = (FunctionVO) iterator2.next();
userAllFuncs.add(functionVO);
}
}
}
for
(Iterator<?> iterator = userAllFuncs.iterator(); iterator
.hasNext();) {
FunctionVO userAllFun = (FunctionVO) iterator.next();
if
(system.equals(
"crm"
)){
if
(userAllFun.getFuncid()>=20000000l){
userAllFuncsList.add(userAllFun);
}
}
else
if
(system.equals(
"hr"
)){
if
(userAllFun.getFuncid()<20000000l){
userAllFuncsList.add(userAllFun);
}
}
}
FunctionComparator fc =
new
FunctionComparator();
Collections.sort(userAllFuncsList, fc);
}
catch
(Exception e) {
e.printStackTrace();
throw
new
GeneralException(
"error.userAllFuncsList"
,
"得到用户的权限集合出错!"
);
}
return
userAllFuncsList;
}
}