⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolemanagerimpl.java

📁 基于java的组号查询模块
💻 JAVA
字号:
/**
 * 
 */
package com.lily.dap.service.right.impl;

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

import org.apache.commons.lang.StringUtils;

import com.lily.dap.Constants;
import com.lily.dap.model.QueryCondition;
import com.lily.dap.model.QueryExpression;
import com.lily.dap.model.right.Permission;
import com.lily.dap.model.right.RightObject;
import com.lily.dap.model.right.RightOperation;
import com.lily.dap.model.right.Role;
import com.lily.dap.model.right.RoleRole;
import com.lily.dap.service.core.BaseManager;
import com.lily.dap.service.core.VerifyUtil;
import com.lily.dap.service.core.exception.DataContentRepeatException;
import com.lily.dap.service.core.exception.DataHaveIncludeException;
import com.lily.dap.service.core.exception.DataHaveUsedException;
import com.lily.dap.service.core.exception.DataNotExistException;
import com.lily.dap.service.core.exception.DataNotIncludeException;
import com.lily.dap.service.core.exception.IllegalHierarchicalException;
import com.lily.dap.service.core.exception.NotSupportOperationException;
import com.lily.dap.service.right.RoleManager;
import com.lily.dap.util.RandomGUID;

/**
 * @author zouxuemo
 *
 */
public class RoleManagerImpl extends BaseManager implements RoleManager {
	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#addHavePermission(long, java.lang.String, java.lang.String[], java.lang.String)
	 */
	public Permission addPermission(long role_id, String object_code,
			String[] operation_code, String des) throws DataNotExistException {
		get(Role.class, role_id);
		
		Permission permission = new Permission();
		permission.setRole_id(role_id);
		permission.setRi_ob(object_code);
		permission.setRi_ops(StringUtils.join(operation_code, Constants.STRING_SEPARATOR3));
		permission.setDes(des);
		
		doSave(permission);
		return permission;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#addHaveRole(long, long)
	 */
	public void addHaveRole(long role_id, long have_role_id)
			throws DataNotExistException, IllegalHierarchicalException {
		get(Role.class, role_id);
		get(Role.class, have_role_id);

		assertRoleNotIncluded(have_role_id, role_id);
		
		RoleRole rr = new RoleRole();
		rr.setFk_role(role_id);
		rr.setFk_have_role(have_role_id);
		doSave(rr);
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#createPrivateRole(java.lang.String)
	 */
	public Role createPrivateRole(String des) {
		Role role = new Role();
		role.setFlag(Role.FLAG_PRIVATE_ROLE);
		role.setCode(new RandomGUID().toString().substring(0, 20));
		role.setName(new RandomGUID().toString());
		role.setDes(des);
		
		doSave(role);
		return role;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#createPublicRole(java.lang.String, java.lang.String)
	 */
	public Role createPublicRole(String code, String name, String des)
			throws DataContentRepeatException {
		VerifyUtil verifyUtil = new VerifyUtil(dao);
		verifyUtil.assertDataNotRepeat(Role.class, "code", code, "角色对象编号重复,请重新指定新的角色编号");
		
		Role role = new Role();
		role.setFlag(Role.FLAG_PUBLIC_ROLE);
		role.setCode(code);
		role.setName(name);
		role.setDes(des);
		
		doSave(role);
		return role;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#fillPermissionFullName(com.lily.dap.model.right.Permission)
	 */
	public Permission fillPermissionFullName(Permission permission) {
		RightObject rightObject = (RightObject)dao.gets(RightObject.class, new QueryCondition().putCondition("code", permission.getRi_ob())).get(0);
		List operationList = dao.gets(RightOperation.class, 
					new QueryCondition()
					.putCondition("objectCode", permission.getRi_ob())
					.putCondition("code", QueryExpression.OP_IN, permission.getRi_ops()));
		
		StringBuffer sb = new StringBuffer();
		sb.append(rightObject.getName()).append(":");
		
		Iterator it  = operationList.iterator();
		while (it.hasNext()) {
			RightOperation operation = (RightOperation)it.next();
			sb.append(operation.getName()).append(",");
		}
		
		permission.setFullName(sb.toString());
		return permission;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#fillPermissionFullName(java.util.Collection)
	 */
	public Collection fillPermissionFullName(Collection permissions) {
		Iterator it = permissions.iterator();
		
		while (it.hasNext())
			fillPermissionFullName((Permission)it.next());
		
		return permissions;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#getHaveAllPermissions(long)
	 */
	public Set getHaveAllPermissions(long role_id) {
		Role role = (Role)get(Role.class, role_id);
		
		Set roleSet = new HashSet();
		roleSet.add(role);
		
		addHaveRoles(role_id, roleSet);
		
		Set permissionSet = new HashSet();
		Iterator it = roleSet.iterator();
		while(it.hasNext()){
			Role r = (Role)it.next();
			permissionSet.addAll(r.getPermissions());
		}
		
		return permissionSet;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#getHaveAllRoles(long)
	 */
	public Set getHaveAllRoles(long role_id, boolean isOnlyPublicRole) {
		Role role = (Role)get(Role.class, role_id);
		
		Set set = new HashSet();
		set.add(role);
		
		addHaveRoles(role_id, set);
		
		if (isOnlyPublicRole) {
			Set publicRoles = new HashSet();
			
			Iterator it = set.iterator();
			while (it.hasNext()) {
				Role r = (Role)it.next();
				
				if (r.getFlag() == Role.FLAG_PUBLIC_ROLE)
					publicRoles.add(r);
			}
			
			return publicRoles;
		} else
			return set;
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#getHavePermissions(long)
	 */
	public List getHavePermissions(long role_id) {
		return dao.gets(Permission.class, new QueryCondition().putCondition("role_id", role_id));
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#getHavePublicRoles(long)
	 */
	public List getHavePublicRoles(long role_id) {
		return dao.gets(Role.class, new QueryCondition()
										.putCondition("flag", Role.FLAG_PUBLIC_ROLE)
										.putCondition("id", QueryExpression.OP_INQUERY, "select fk_have_role from RoleRole where fk_role = " + role_id));
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#getRole(java.lang.String)
	 */
	public Role getRole(String code) throws DataNotExistException {
		return (Role)get(Role.class, "code", code);
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#modifyHavePermission(long, java.lang.String, java.lang.String[], java.lang.String)
	 */
	public void modifyPermission(long permission_id, String object_code,
			String[] operation_code, String des) throws DataNotExistException {
		Permission permission = (Permission)get(Permission.class, permission_id);
		permission.setRi_ob(object_code);
		permission.setRi_ops(StringUtils.join(operation_code, Constants.STRING_SEPARATOR3));
		permission.setDes(des);
		
		doSave(permission);
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#modifyRole(long, java.lang.String, java.lang.String)
	 */
	public void modifyRole(long role_id, String name, String des)
			throws DataNotExistException {
		Role role = (Role)get(Role.class, role_id);
		role.setName(name);
		role.setDes(des);
		
		doSave(role);
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#removeRole(long)
	 */
	public void removeRole(long role_id) throws DataNotExistException, DataHaveUsedException {
		get(Role.class, role_id);
		
		List rrList = gets(RoleRole.class, new QueryCondition().putCondition("fk_have_role", role_id));
		if (rrList.size() > 0)
			throw new DataHaveUsedException("角色已经被其他角色包含,请先删除其他角色中包含的本角色再删除!");
		
		removeHaveRoles(role_id);
		
		doRemove(Role.class, new Long(role_id));
	}

	/* (non-Javadoc)
	 * @see com.lily.dap.service.right.RoleManager#removeHaveRole(long, long)
	 */
	public void removeHaveRole(long role_id, long have_role_id)
			throws DataNotExistException {
		get(Role.class, role_id);
		get(Role.class, have_role_id);
		
		RoleRole rr = null;
		try {
			rr = (RoleRole)get(RoleRole.class, new String[]{"fk_role", "fk_have_role"}, new Object[]{new Long(role_id), new Long(have_role_id)});
		} catch (DataNotExistException e) {
			throw new DataNotIncludeException("本角色不包含要去除的角色,不能进行删除");
		}
		
		doRemove(RoleRole.class, new Long(rr.getId()));
	}

    /**
     * 去除角色拥有的直接子角色
     * 
     * @param role_id 角色id
     */
	private void removeHaveRoles(long role_id) {
		List rrList = gets(RoleRole.class, new QueryCondition().putCondition("fk_role", role_id));
		
		Iterator it = rrList.iterator();
		while (it.hasNext()) {
			RoleRole rr = (RoleRole)it.next();
			
			doRemove(RoleRole.class, new Long(rr.getId()));
		}
	}
	
	/**
	 * @param role_id
	 * @param set
	 */
	private void addHaveRoles(long role_id, Set set) {
		List roleList = dao.gets(Role.class, new QueryCondition().putCondition("id", QueryExpression.OP_INQUERY, "select fk_have_role from RoleRole where fk_role = " + role_id));
		set.addAll(roleList);
		
		Iterator it = roleList.iterator();
		while (it.hasNext())
			addHaveRoles(((Role)it.next()).getId(), set);
	}
	
    /**
     * 检查给定角色是否不包含角色,如果包含,抛出数据已包含异常
     * 
     * @param role_id 给定角色
     * @param have_role_id 被包含角色
     */
    private void assertRoleNotIncluded(long role_id, long have_role_id){
		Set haveSet = new HashSet();
		addHaveRoles(role_id, haveSet);
		
		Iterator it = haveSet.iterator();
		while(it.hasNext()){
			Role role = (Role)it.next();
			if(role.getId() == have_role_id){
				throw new DataHaveIncludeException("要添加的角色已经拥有了本角色,不能进行循环包含!");
			}
		}
    }

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeAdd(java.lang.Object)
	 */
	protected Object onBeforeAdd(Object entity) {
		throw new NotSupportOperationException("接口不允许调用add(...)方法!");
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeModify(java.lang.Object)
	 */
	protected Object onBeforeModify(Object entity) {
		throw new NotSupportOperationException("接口不允许调用modify(...)方法!");
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeRemove(java.lang.Object)
	 */
	protected void onBeforeRemove(Object entity) {
		throw new NotSupportOperationException("接口不允许调用remove(...)方法!");
	}
}

⌨️ 快捷键说明

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