rolemanagerimpl.java

来自「利用STRUTS2+SPRING+HIBERNATE/IBATIS建立的基本开发」· Java 代码 · 共 120 行

JAVA
120
字号
/**
 * 
 */
package com.sunwah.baseapp.system.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.sunwah.baseapp.service.CommonStatusCodes;
import com.sunwah.baseapp.system.dao.FunctionDao;
import com.sunwah.baseapp.system.dao.RoleDao;
import com.sunwah.baseapp.system.dao.UserDao;
import com.sunwah.baseapp.system.model.Functions;
import com.sunwah.baseapp.system.model.RoleFunction;
import com.sunwah.baseapp.system.model.RoleFunctionId;
import com.sunwah.baseapp.system.model.Roles;

/**
 * @author MARK
 * 
 */
public class RoleManagerImpl implements RoleManager {
	private RoleDao roleDao;

	private FunctionDao functionDao;

	private UserDao userDao;

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setFunctionDao(FunctionDao functionDao) {
		this.functionDao = functionDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sunwah.baseapp.system.service.RoleManager#createRole(com.sunwah.baseapp
	 * .system.model.Roles, java.util.List)
	 */
	@Override
	public int createRole(Roles role, List<Long> functionIds) {
		// 判断角色名称是否已经存在
		if (this.roleDao.findRoleByRoleName(role.getRoleName()) != null)
			return CommonStatusCodes.NAME_EXIST;
		Set roleFunctionSet = makeRoleFunctionSet(role, functionIds);
		role.setRoleFunctions(roleFunctionSet);
		this.roleDao.save(role);
		return CommonStatusCodes.OK;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sunwah.baseapp.system.service.RoleManager#deleteRole(java.lang.Long)
	 */
	@Override
	public void deleteRole(Long roleId) {
		this.roleDao.remove(roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sunwah.baseapp.system.service.RoleManager#findRole(java.lang.Long)
	 */
	@Override
	public Roles findRole(Long roleId) {
		Roles role = this.roleDao.get(roleId);
		if (role != null) {
			role.setCreateUserName(userDao);
			role.setModiryUserName(userDao);

		}
		return role;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sunwah.baseapp.system.service.RoleManager#updateRole(com.sunwah.baseapp
	 * .system.model.Roles, java.util.List)
	 */
	@Override
	public int updateRole(Roles role, List<Long> functionIds) {
		// 判断角色名称是否已经存在
		Roles tmpRole = this.roleDao.findRoleByRoleName(role.getRoleName());
		if (tmpRole != null && !tmpRole.getRoleId().equals(role.getRoleId()))
			return CommonStatusCodes.NAME_EXIST;

		Set roleFunctionSet = makeRoleFunctionSet(role, functionIds);
		role.setRoleFunctions(roleFunctionSet);
		this.roleDao.merge(role);
		return CommonStatusCodes.OK;
	}

	private Set makeRoleFunctionSet(Roles role, List<Long> functionIds) {
		Set roleFunctionSet = new HashSet();
		if (functionIds != null) {
			for (Long functionId : functionIds) {
				Functions function = functionDao.get(functionId);
				roleFunctionSet.add(new RoleFunction(new RoleFunctionId(role,
						function), role, function));
			}
		}
		return roleFunctionSet;
	}
}

⌨️ 快捷键说明

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