sysroleservice.java

来自「管理公司合同」· Java 代码 · 共 207 行

JAVA
207
字号
/*
 * Created on 2006-10-18 20:00:04
 *
 * By Yehailong
 * Copyright juneng.com.cn, 2005-2006, All rights reserved.
 */
package cn.com.juneng.system.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.com.juneng.system.common.COMMON;
import cn.com.juneng.system.common.SpringBeanFactory;
import cn.com.juneng.system.dao.SysRoleDAO;
import cn.com.juneng.system.vo.SysMenuAuthVOImpl;
import cn.com.juneng.system.vo.SysRoleVOImpl;
import cn.com.juneng.system.vo.SysUserRoleVOImpl;

/**
 * service类,由Spring容器注入DAO实现,并管理事务性
 * 
 * @author yehailong
 */
public class SysRoleService {
	private SysRoleDAO sysRoleDAO;

	/**
	 * spring注入dao实现
	 * 
	 * @param sysRoleDAO
	 */
	public void setSysRoleDAO(SysRoleDAO sysRoleDAO) {
		this.sysRoleDAO = sysRoleDAO;
	}

	public SysRoleDAO getSysRoleDAO() {
		return this.sysRoleDAO;
	}

	public void remove(Serializable[] key) throws Exception {
		if (key != null) {
			for (int i = 0; i < key.length; i++) {
				sysRoleDAO.remove(key[i]);
			}
		}
	}

	public SysRoleVOImpl findByPrimaryKey(String key) throws Exception {
		return sysRoleDAO.findByPrimaryKey(key);
	}

	public void create(Object obj) throws Exception {
		sysRoleDAO.create(obj);
	}

	public void update(Object obj) throws Exception {
		sysRoleDAO.update(obj);
	}

	public List getList(String queryHql, String orderHql, int start,
			int maxResults) throws Exception {
		String hql = "from SysRoleVOImpl ";
		if (!COMMON.isEmpty(queryHql)) {
			hql += "where " + queryHql;
		}
		if (COMMON.isEmpty(orderHql)) {
			orderHql = " order by roleId desc";
		}
		hql += " " + orderHql;

		return sysRoleDAO.find(hql, start, maxResults);
	}

	public int getRowCount(String queryHql) {
		String hql = "from SysRoleVOImpl ";
		if (!COMMON.isEmpty(queryHql)) {
			hql += "where " + queryHql;
		}
		return sysRoleDAO.getRowCount(hql, null);
	}

	/**
	 * 添加角色用户
	 * 
	 * @param roleId
	 *            角色ID
	 * @param userIds
	 *            用户ID,“,”分隔多个
	 * @throws Exception
	 */
	public void addRoleUser(String roleId, String userIds) throws Exception {
		String[] userIdArr = userIds.split(",");
		for (int i = 0; i < userIdArr.length; i++) {
			SysUserRoleVOImpl userRole = new SysUserRoleVOImpl();
			userRole.setRoleId(roleId);
			userRole.setUserId(userIdArr[i]);
			SpringBeanFactory.getCommonService().getCommonDAO()
					.create(userRole);
		}
	}

	/**
	 * 删除角色用户
	 * 
	 * @param roleId
	 *            角色ID
	 * @param userIdArr
	 *            用户ID数组
	 * @throws Exception
	 */
	public void deleteRoleUser(String roleId, String[] userIdArr)
			throws Exception {
		StringBuffer userIds = new StringBuffer();
		for (int i = 0; i < userIdArr.length; i++) {
			if (i != 0) {
				userIds.append(",");
			}
			userIds.append("'" + userIdArr[i] + "'");
		}
		String hql = "delete from SysUserRoleVOImpl where roleId=? and userId in ("
				+ userIds + ")";
		SpringBeanFactory.getCommonService().getCommonDAO().update(hql,
				new Object[] { roleId });
	}

	/**
	 * 获取用户所属的角色列表
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public List findUserRole(String userId) throws Exception {
		String hql = "roleId in (select roleId from SysUserRoleVOImpl where userId = '"
				+ userId + "')";
		return this.getList(hql, "", 0, Integer.MAX_VALUE);
	}

	/**
	 * 角色授权的菜单列表
	 * 
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public List findRoleMenu(String roleId) throws Exception {
		String hql = "from SysMenuVOImpl where menuId in (select menuId from SysMenuAuthVOImpl where roleId = '"
				+ roleId + "')";
		return SpringBeanFactory.getCommonService().getHibernateTemplate()
				.find(hql);
	}

	/**
	 * 保存角色授权的菜单
	 * 
	 * @param roleId
	 *            角色ID
	 * @param menuIds
	 *            菜单ID ","分隔多个
	 * @return
	 */
	public void saveMenu(String roleId, String menuIds) throws Exception {
		// 删除之前所有授权
		String hql = "delete from SysMenuAuthVOImpl where roleId = '" + roleId
				+ "'";
		SpringBeanFactory.getCommonService().getCommonDAO().update(hql, null);

		if (COMMON.isEmpty(menuIds)) {
			return;
		}
		String[] menuIdArr = menuIds.split(",");
		List list = new ArrayList();
		for (int i = 0; i < menuIdArr.length; i++) {
			SysMenuAuthVOImpl menuAuth = new SysMenuAuthVOImpl();
			menuAuth.setRoleId(roleId);
			menuAuth.setMenuId(menuIdArr[i]);
			list.add(menuAuth);
		}
		SpringBeanFactory.getCommonService().getCommonDAO().batchSave(list);
	}

	/**
	 * 角色拥有的菜单集合
	 * 
	 * @param sysRoles
	 * @return
	 * @throws Exception
	 */
	public List findAuthMenu(List sysRoles) throws Exception {
		String roleIds = COMMON.getPropValueFromList(sysRoles, "roleId", "'");
		if (COMMON.isEmpty(roleIds)) {
			return null;
		}
		String roleFlags = COMMON.getPropValueFromList(sysRoles,"roleFlag","");
		StringBuffer sql = new StringBuffer();
		sql.append("from SysMenuVOImpl ");
		if(roleFlags.indexOf("Administrators")==-1){
			sql.append("where menuId in (select menuId from SysMenuAuthVOImpl where roleId in ("
				+ roleIds + ")) ");
		}
		sql.append("order by parentId,priority,menuId");
		return SpringBeanFactory.getCommonService().getHibernateTemplate()
				.find(sql.toString());
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?