Role

/**
 * 
 */
package com.etong.common.system.domain;


import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;


import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.Where;


import com.etong.common.system.domain.base.StringIdEntity;


/**
 * @version 1.0
 */
@Entity
@Table(name="SYS_ROLES"/*, schema="UAWP"*/)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role extends StringIdEntity {


private static final long serialVersionUID = 6696840255623377526L;


/** 角色编号  */
private String code;

/** 角色名 */
private String name;


/** 角色描述 */
private String description;

/** 归属机构 */
private Organization organization;

/** 该角色的用户 */
private Set<User> users;


/** 该角色的授权信息 */
private Set<Permission> permissions;

/** 是否为系统内置的角色 */
private boolean buildin;


/** 是否禁用 */
protected boolean disabled;

/** 用户数量 */
private transient int userCount = -1;

/**
* @return the code
*/
@Column(length=8, nullable=false, unique=true)
public String getCode() {
return code;
}


/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}


/**
* @return the name
*/
@Column(length=16, nullable=false)
public String getName() {
return name;
}


/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}


/**
* @return the description
*/
@Column(length=127)
public String getDescription() {
return description;
}


/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}


/**
* @return the organization
*/
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="org_id", nullable=true)
public Organization getOrganization() {
return organization;
}


/**
* @param organization the organization to set
*/
public void setOrganization(Organization organization) {
this.organization = organization;
}


/**
* @return the users
*/
    @ManyToMany(mappedBy="roles", fetch=FetchType.LAZY)
    @Where(clause="f_disabled='F' and f_removed='F'")
@OrderBy("order")
public Set<User> getUsers() {
if(users == null) {
users = new HashSet<User>();
}
return users;
}


/**
* @return the authorities
*/
@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
@JoinTable(name="SYS_ROLE_PERMISSIONS"/*, schema="UAWP"*/,
joinColumns= {@JoinColumn(name="role_id")}, 
inverseJoinColumns= {@JoinColumn(name="perm_id")})
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public Set<Permission> getPermissions() {
if(permissions == null) {
permissions = new LinkedHashSet<Permission>();
}
return permissions;
}


/**
* @param permissions the permissions to set
*/
public void setPermissions(Set<Permission> permissions) {
this.permissions = permissions;
}


/**
* @param users the users to set
*/
public void setUsers(Set<User> users) {
this.users = users;
}


/**
* @return the buildin
*/
@Type(type="true_false")
public boolean isBuildin() {
return buildin;
}


/**
* @param buildin the buildin to set
*/
public void setBuildin(boolean buildin) {
this.buildin = buildin;
}


/**
* @return the disabled
*/
@Type(type="true_false")
public boolean isDisabled() {
return disabled;
}


/**
* @param disabled the disabled to set
*/
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}


/**
* @return the userCount
*/
@Transient
public int getUserCount() {
return userCount;
}


/**
* @param userCount the userCount to set
*/
public void setUserCount(int userCount) {
this.userCount = userCount;
}

/**
* 为当前角色增加用户。
* @param user
*/
public void addUser(User user) {
this.getUsers().add(user);
user.getRoles().add(this);
}

/**
* 移除当前角色的特定用户。
* @param user
*/
public void removeUser(User user) {
this.getUsers().remove(user);
user.getRoles().remove(this);
}

/**
* 为当前角色增加授权。
* @param auth
*/
public void addPermission(Permission permission) {
this.getPermissions().add(permission);
permission.getRoles().add(this);
}

/**
* 移除当前角色的特定授权。
* @param auth
*/
public void removePermission(Permission permission) {
this.getPermissions().remove(permission);
permission.getRoles().remove(this);
}

/**
* 通过比较两个实体的ID是否相等,来判断两个实体是否相等。
* @param entity 进行比较的另一个实体。
* @return 相等返回 true ,不相等返回 false 。
*/
@Override
public boolean equals(Object entity) {
if(entity == this) {
return true;
}
if(entity instanceof Role
/*|| !this.getClass().equals(entity.getClass())*/) {
Role that = (Role)entity;
return (id == null ? that.id == null : id.equals(that.id));
}
return false;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值