📄 roledao.java
字号:
package com.ufmobile.common.role.dao;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.ufmobile.common.dao.UFMobileDAO;
import com.ufmobile.common.exception.RoleException;
import com.ufmobile.common.login.entity.UserInfo;
import com.ufmobile.common.role.bo.Role;
import com.ufmobile.common.role.entity.MemberroleEntity;
import com.ufmobile.common.role.entity.MenuEntity;
import com.ufmobile.common.role.entity.RoleAgentEntity;
import com.ufmobile.common.role.entity.RoleEntity;
import com.ufmobile.common.role.entity.RoleusrEntity;
import com.ufmobile.platform.log.RunTimeLogger;
/**
* @date 2006/12/08
* @author Janet
* @since v3.0
*/
public class RoleDAO extends UFMobileDAO {
// 角色编码格式
String sCodeFormate = "0000";
public RoleDAO() {
super();
}
public RoleDAO(EntityManager manager) {
super(manager);
}
/**
* @function 新建角色
* @param roleEntity
* @throws RoleException
*/
public void addRole(RoleEntity roleEntity) throws RoleException {
if (roleEntity == null) {
roleEntity = new RoleEntity();
}
// 设置自动编号
roleEntity.setCode(getNextRoleCode(roleEntity.getSys()));
this.getManager().persist(roleEntity);
}
/**
* @function 删除角色
* @param roleId
* @throws RoleException
*/
public void deleteRole(Long roleId) throws RoleException {
if (roleId == null) {
throw new RoleException("Role ID is required!");
}
if (roleId == 0L)
throw new RoleException(getClass(), "预制系统管理员不能删除!");
List<RoleusrEntity> entityRoleUsr = getManager().createQuery(
"from RoleusrEntity where roleid=?").setParameter(1, roleId).getResultList();
if (entityRoleUsr != null && entityRoleUsr.size() > 0) {
throw new RoleException(getClass(), "该角色已分给操作员不能删除!");
}
RoleEntity role = this.getManager().find(RoleEntity.class, roleId);
if (role != null) {
this.getManager().remove(role);
this.deleteAssign(role);
}
}
/**
* @function 更新角色
* @param roleEntity
* @throws RoleException
*/
public void updateRole(RoleEntity roleEntity) throws RoleException {
if (roleEntity == null)
throw new RoleException("Role ID is required!");
this.getManager().merge(roleEntity);
}
/**
* @function 查询所有角色
* @return
* @throws RoleException
*/
public List<RoleEntity> getAllRoles() throws RoleException {
StringBuffer buf = new StringBuffer();
buf.append("select * from tb_common_role");
List roles = this.getManager().createNativeQuery(buf.toString(), RoleEntity.class)
.getResultList();
return roles;
}
/**
* @function 查询指定角色
* @param roleId
* @return
* @throws RoleException
*/
public RoleEntity getRoleEntity(Long roleId) throws RoleException {
if (roleId == null) {
throw new RoleException("Role ID is required!");
}
RoleEntity role = this.getManager().find(RoleEntity.class, roleId);
return role;
}
/**
* @param roleType
* @return
* @throws RoleException
*/
public String getNextRoleCode(String roleType) throws RoleException {
String sql = "select max(code) from tb_common_role where sys='" + roleType + "'";
List s = getManager().createNativeQuery(sql).getResultList();
if (s == null || s.size() == 0 || s.get(0) == null) {
return sCodeFormate.substring(0, sCodeFormate.length() - 1) + 1;
} else {
String scode = s.get(0).toString();
int istart = 0;
for (; istart < scode.length(); istart++) {
if (scode.charAt(istart) > '0')
break;
}
int number = 0;
try {
number = Integer.parseInt(scode.substring(istart));
} catch (NumberFormatException e) {
;
}
int x = number + 1;
return sCodeFormate.substring(0, sCodeFormate.length() - ("" + x).length()) + x;
}
}
/**
* @function 添加角色功能
*
* @throws RoleException
*/
public void addMenu( MenuEntity menuEntity)
throws RoleException {
// 只保存二级菜单
this.getManager().persist(menuEntity);
}
/**
* @funtion 删除角色分配
* @param roleEntity
* @throws RoleException
*/
public void deleteAssign(RoleEntity roleEntity) throws RoleException {
if (roleEntity == null) {
return;
}
// 查询改角色所关联的角色功能
String sSQL2 = "delete from MenuEntity where roleid=:id";
getManager().createQuery(sSQL2).setParameter("id", roleEntity.getId()).executeUpdate();
}
/**
* @function 查询编码所对应的菜单实体,注意!编码不是主健
* @param code 编码,例如0001,00010001,0002
* @return
* @throws RoleException
*/
public List getRoleMenuItem(String code, String sys) throws RoleException {
if (code == null || code.equals(""))
return null;
StringBuffer buf = new StringBuffer();
if ("0".equals(sys)) {
buf.append(" from MenuEntity where roleid=").append(Role.OWNER)
.append(" and code in (").append(code).append(")");
} else if ("1".equals(sys)) {
buf.append(" from MenuEntity where roleid=").append(Role.ADMIN)
.append(" and code in (").append(code).append(")");
;
} else if ("2".equals(sys)) {
buf.append(" from MenuEntity where roleid=").append(Role.AGENT)
.append(" and code in (").append(code).append(")");
;
}
List<MenuEntity> menuItems = this.getManager().createQuery(buf.toString()).getResultList();
return menuItems;
}
/**
* @function 查询某一个编码的子才单项
* @param code 编码,例如0001,00010001,0002
* @return 子菜单列表
* @throws RoleException
*/
public List<MenuEntity> getRoleMenuSubItem(String code) throws RoleException {
if ("".equals(code))
return null;
// roleId=0为原始的菜单项
StringBuffer buf = new StringBuffer();
buf.append("select * from tb_common_menu where roleid=0 and substr(code,0,");
buf.append(code.length());
buf.append(")='" + code + "' and length(code)=");
buf.append(code.length() + 3);
RunTimeLogger.info(RoleDAO.class, "SQL = " + buf.toString());
// 返回查询结果
List<MenuEntity> menuItems = this.getManager().createNativeQuery(buf.toString(),
MenuEntity.class).getResultList();
if (menuItems != null && menuItems.size() == 0)
return null;
else
return menuItems;
}
/**
* @functino 获得根节点菜单项
* @return
* @throws RoleException
*/
public List<MenuEntity> getRoleMenuRoot() throws RoleException {
StringBuffer buf = new StringBuffer();
// 三位表示一个级次001,002
buf.append("select * from tb_common_menu where roleid=0 and length(code)=3");
RunTimeLogger.info(RoleDAO.class, "SQL = " + buf.toString());
List<MenuEntity> menuItems = this.getManager().createNativeQuery(buf.toString(),
MenuEntity.class).getResultList();
if (menuItems != null && menuItems.size() == 0)
return null;
else
return menuItems;
}
/**
* 作者:msf <br>
* 日期:Dec 13, 2006 为用户分配角色
*
* @see com.ufmobile.common.role.bo.Role#assignRole(java.lang.Long,
* java.lang.Long, java.lang.Long, java.lang.Integer)
*/
public void assignRole(Long ursid, Long sysid, Long roleid, Integer itype) throws RoleException {
switch (itype.intValue()) {
case 0:
MemberroleEntity mEntity = new MemberroleEntity();
mEntity.setMemberid(ursid);
mEntity.setRoleid(roleid);
mEntity.setShopid(sysid);
getManager().persist(mEntity);
break;
case 1:
RoleusrEntity uEntity = new RoleusrEntity();
uEntity.setOperateid(ursid);
uEntity.setRoleid(roleid);
uEntity.setStreetid(sysid);
getManager().persist(uEntity);
break;
case 2:
RoleAgentEntity aEntity = new RoleAgentEntity();
aEntity.setagentid(ursid);
aEntity.setRoleid(roleid);
aEntity.setStreetid(sysid);
getManager().persist(aEntity);
break;
}
}
public int revokeRole(Long ursid, Long sysid, Integer itype) throws RoleException {
StringBuffer sSQL = new StringBuffer("delete from ");
switch (itype.intValue()) {
case 0:
sSQL.append("MemberroleEntity where shopid=? and memberid=?");
break;
case 1:
sSQL.append("RoleusrEntity where streetid=? and operateid=?");
break;
case 2:
sSQL.append("RoleAgentEntity where streetid=? and agentid=?");
break;
}
return getManager().createQuery(sSQL.toString()).setParameter(1, sysid).setParameter(2,
ursid).executeUpdate();
}
/**
* 作者:msf <br>
* 日期:Dec 13, 2006
*
* @see com.ufmobile.common.role.bo.Role#getRoles(java.lang.Integer)
*/
public List<RoleEntity> getRoles(Integer itype) throws RoleException {
StringBuffer buf = new StringBuffer();
buf.append("from RoleEntity where sys=:Sys");
List<RoleEntity> roleList = this.getManager().createQuery(buf.toString()).setParameter(
"Sys", itype.toString()).getResultList();
return roleList;
}
/**
* <p>
* 分页查询角色
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 18, 2006
*
* @param beginIndex
* @param maxIndex
* @return
* @throws RoleException
*/
public List<RoleEntity> queryRoles(int beginIndex, int maxIndex) throws RoleException {
StringBuffer buf = new StringBuffer();
buf.append(" from RoleEntity ");
return getManager().createQuery(buf.toString()).setFirstResult(beginIndex).setMaxResults(
maxIndex).getResultList();
}
public List<RoleEntity> getAssignRoles(Integer itype, Long systemid, Long usrid)
throws RoleException {
StringBuffer sSQL = new StringBuffer("select c from RoleEntity c,");
switch (itype.intValue()) {
case 0:
sSQL.append("MemberroleEntity where shopid=? and memberid=?");
break;
case 1:
sSQL.append("RoleusrEntity where streetid=? and operateid=?");
break;
case 2:
sSQL.append("RoleAgentEntity where streetid=? and agentid=?");
break;
}
sSQL.append(" and roleid =c.id");
return getManager().createQuery(sSQL.toString()).setParameter(1, systemid).setParameter(2,
usrid).getResultList();
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 23, 2006
*
* @param usrInfo
* @return
* @throws RoleException
*/
public List<RoleEntity> getAssignRoles(UserInfo usrInfo) throws RoleException {
StringBuffer sSQL = new StringBuffer("select c from RoleEntity c,");
switch (usrInfo.getType().intValue()) {
case 0:
sSQL.append("MemberroleEntity where shopid=? and memberid=?");
break;
case 1:
sSQL.append("RoleusrEntity where streetid=? and operateid=?");
break;
case 2:
sSQL.append("RoleAgentEntity where streetid=? and agentid=?");
break;
}
sSQL.append(" and roleid =c.id");
return getManager().createQuery(sSQL.toString()).setParameter(1,
usrInfo.getType().intValue() == 0 ? usrInfo.getShopid() : usrInfo.getStreetid())
.setParameter(2, usrInfo.getUsrid()).getResultList();
}
/**
* <p>
* 将菜单读入缓存
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 22, 2006
*
* @return
* @throws RoleException
*/
public List getRoleMenuFromDB(Long roleId) throws RoleException {
StringBuffer buf = new StringBuffer();
RoleEntity roleEntity = getManager().find(RoleEntity.class, roleId);
if (roleEntity != null) {
String type = roleEntity.getSys();
if ("0".equals(type)) {
buf.append(" from MenuEntity where roleid=").append(Role.OWNER).append(
"and functype=0");
} else if ("1".equals(type)) {
buf.append(" from MenuEntity where roleid=").append(Role.ADMIN).append(
" and functype=1");
} else if ("2".equals(type)) {
buf.append(" from MenuEntity where roleid=").append(Role.AGENT).append(
" and functype=2");
}
return this.getManager().createQuery(buf.toString()).getResultList();
}
return null;
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 23, 2006
*
* @param roleid
* @return
* @throws RoleException
*/
public List getAssignMenu(Long roleid) throws RoleException {
// 查询改角色所关联的角色功能
String sSQL = " from MenuEntity where roleid=:id order by menuorder";
return getManager().createQuery(sSQL).setParameter("id", roleid).getResultList();
}
public List getAssignMenu(String roleids) throws RoleException {
// 查询改角色所关联的角色功能
String sSQL = " from MenuEntity where roleid in " + roleids;
return getManager().createQuery(sSQL).getResultList();
}
/**
* <p>
* lsRole中 取出所有角色的菜单
* <p>
* 作者:msf <br>
* 日期:Dec 23, 2006
*
* @param lsRole
* @return
*/
public List<List> getMenuByRoles(List<RoleEntity> lsRole) {
if (lsRole == null || lsRole.size() == 0)
return null;
List<List> lsRet = new ArrayList<List>();
for (int i = 0; i < lsRole.size(); i++) {
List l = getManager().createQuery("from MenuEntity where roleid=? order by code")
.setParameter(1, lsRole.get(i).getId()).getResultList();
if (l != null)
lsRet.add(l);
}
return lsRet;
}
/**
* <p>
* 按页取出某一个类型的角色
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 25, 2006
*
* @param beginIndex
* @param maxIndex
* @param sys
* @return
* @throws RoleException
*/
public List<RoleEntity> queryRolesBySysPage(int beginIndex, int maxIndex, int sys)
throws RoleException {
StringBuffer buf = new StringBuffer();
buf.append("select * from tb_common_role where sys='").append(sys).append("' order by id");
Query query = this.getManager().createNativeQuery(buf.toString(), RoleEntity.class);
query.setFirstResult(beginIndex);
query.setMaxResults(maxIndex);
return query.getResultList();
}
public Long queryRolesBySysTotal(int sys) throws RoleException {
StringBuffer buf = new StringBuffer();
buf.append(" select count(*) from tb_common_role where sys='").append(sys).append("'");
return ((BigDecimal) getManager().createNativeQuery(buf.toString()).getSingleResult())
.longValue();
}
public List<MemberroleEntity> getRolebyMemberID(Long memberid) {
Query query = getManager().createQuery("from MemberroleEntity where memberid=?1");
query.setParameter(1, memberid);
return query.getResultList();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -