📄 privilegehibernate.java
字号:
package com.hongsoft.res.database;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.LongType;
import com.hongsoft.res.pojo.ResException;
import com.hongsoft.res.pojo.ResManager;
import com.hongsoft.res.pojo.ResObjectElement;
import com.hongsoft.res.pojo.ResPrivilege;
import com.hongsoft.res.util.IDGenerator;
public class PrivilegeHibernate {
// private static PrivilegeHibernate dao = null;
public static PrivilegeHibernate getInstance() {
return new PrivilegeHibernate();
}
private PrivilegeHibernate() {
}
/**
* 修改资源对象拥有的权限,首先删除原来拥有的权限,再赋予指定的权限
*
* @param resID 资源对象ID
* @param privs 权限对象(Privilege)List
* @throws ResException
*/
public void updatePrivilege(Session session, long resID, List privs) throws ResException {
deleteAllPrivilege(session, resID);
try {
int size = privs.size();
for (int i = 0; i < size; i++) {
ResPrivilege resPriv = new ResPrivilege();
resPriv.setType(Integer.parseInt((String) privs.get(i)));
resPriv.setResID(resID);
createPrivilege(session, resPriv);
}
} catch (ResException hs) {
}
}
/**
* 为用户赋予权限,同时也要写入资源对象表
*
* @param session Session对象
* @param priv ResPrivilege对象
* @return 资源权限对象ID
* @throws ResException
*/
public synchronized long createPrivilege(Session session, ResPrivilege priv) throws ResException {
long id = IDGenerator.newID(session, ResObjectName.RES_OBJECT_PRIV);
priv.setId(id);
try {
session.save(priv);
} catch (HibernateException he) {
throw new ResException(ResException.OTHER_DATABASE_ERROR, he);
}
return id;
}
/**
* 从数据库读资源对象(resID)对应的权限对象String集合
*
* @param session Session对象
* @param resID 资源对象ID
* @return 权限对象String集合
* @throws ResException
*/
public Set getPrivileges(Session session, long resID) throws Exception {
Set privSet = new HashSet();
SQLQuery query = session.createSQLQuery(ResQuery.LOAD_PRIVILIGE);
query.setParameter(1, new Long(resID), new LongType());
List list = query.list();
Iterator ite = list.iterator();
while (ite.hasNext()) {
ResPrivilege res = (ResPrivilege) ite.next();
privSet.add(String.valueOf(res.getType()));
}
return privSet;
}
/**
* 获取某资源下属权限以及该对象下属对象拥有的所有权限的集合
*
* @param session Session对象
* @param resID 资源对象ID
* @return Privilege set
* @throws ResException
*/
public Set getResAllPrivileges(Session session, long resID) throws Exception {
Set set = new HashSet();
try {
ResObjectElement element = ResManager.getResObjectElment(session, resID);
long id = element.getID();
set.addAll(this.getPrivileges(session, id));
List children = element.getChildren();
if (children != null) {
for (int j = 0; j < children.size(); j++) {
long childID = ((ResObjectElement) children.get(j)).getID();
set.addAll(getResAllPrivileges(session, childID));
}
}
} catch (ResException re) {
re.printStackTrace();
}
return set;
}
/**
* 删除资源对象有关的所有权限
*
* @param session Session对象
* @param resID 资源对象ID
* @throws ResException
*/
public void deleteAllPrivilege(Session session, long resID) throws ResException {
try {
SQLQuery query = session.createSQLQuery(ResQuery.LOAD_PRIVILIGE);
query.setParameter(1, new Long(resID), new LongType());
query.executeUpdate();
} catch (HibernateException he) {
throw new ResException(ResException.OTHER_DATABASE_ERROR, he);
}
}
/**
* 删除资源对象有关的所有权限realation
*
* @param session Session对象
* @param resID 资源对象ID
* @throws ResException
*/
public void deletePrivilegeRelation(Session session, long resID) throws ResException {
try {
SQLQuery query = session.createSQLQuery(ResQuery.LOAD_PRIVILIGE);
query.setParameter(1, new Long(resID), new LongType());
query.executeUpdate();
} catch (HibernateException he) {
throw new ResException(ResException.OTHER_DATABASE_ERROR, he);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -