📄 orgunithibernate.java
字号:
package com.hongsoft.res.database;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.LongType;
import com.hongsoft.res.pojo.OrgUnit;
import com.hongsoft.res.pojo.OrgUnitElement;
import com.hongsoft.res.pojo.ResException;
import com.hongsoft.res.pojo.ResManager;
import com.hongsoft.res.pojo.ResObject;
import com.hongsoft.res.pojo.ResObjectElement;
import com.hongsoft.res.pojo.ResObjectRe;
import com.hongsoft.res.pojo.ResObjectRela;
import com.hongsoft.res.pojo.ResType;
import com.hongsoft.res.util.IDGenerator;
public class OrgUnitHibernate {
// private static OrgUnitHibernate dao = null;
static OrgUnitHibernate getInstance() {
return new OrgUnitHibernate();
}
private OrgUnitHibernate() {
}
/**
* 通过orgID来获取对象的类型
*
* @param ou
* @return true:是 false:否
* @throws ResException
*/
private int getObjectType(Session session, long orgID) throws ResException {
int errorCode = ResException.OTHER_DATABASE_ERROR;
int type = -1;
try {
SQLQuery query = session.createSQLQuery(ResQuery.GET_OBJECT_TYPE_BY_ID);
query.setParameter(1, new Long(orgID), new LongType());
List userList = query.list();
Iterator itr = userList.iterator();
if (itr.hasNext())
type = ((ResObject) itr.next()).getType();
} catch (HibernateException e) {
throw new ResException(errorCode);
}
return type;
}
/**
* 判断一个父ID下是否已经包含了某个名字的机构
*
* @param parentID
* @param name
* @return
* @throws ResException
*/
private boolean orgUnitNameExist(Session session, long parentID, String name) throws ResException {
boolean isThere = false;
int errorCode = ResException.OTHER_DATABASE_ERROR;
// User user = null;
try {
Query q = session.createQuery(ResQuery.CHECK_ORG_NAME_UNIQUE);
q.setLong("parentID", parentID);
q.setString("name", name);
Iterator itr = q.iterate();
if (itr.hasNext())
isThere = true;
} catch (HibernateException e) {
throw new ResException(errorCode);
}
return isThere;
}
/**
* 在数据库写入组织信息,同时也要写入资源对象表,ou结构中没有ID
*
* @param ou OrgUnit
* @param parentOUID 该组织的父组织的资源ID
* @return 组织对应的资源ID
* @throws ResException
*/
public synchronized long createOrgUnit(Session session, OrgUnit ou, long parentOUID, int is_extend)
throws ResException {
// 下面判断父组织是否是组织
if (getObjectType(session, parentOUID) != ResType.ORG_UNIT) {
throw new ResException(ResException.OU_PARENTID_ISNOT_OU_ERROR);
}
// 然后判断该父组织下层的组织名是否重复,如果重复要抛出ResException异常
if (orgUnitNameExist(session, parentOUID, ou.getName())) {
throw new ResException(ResException.OU_NAME_UNIQUE_ERROR);
}
// 产生新的ID,生成新的组织
int errorCode = ResException.OTHER_DATABASE_ERROR;
long value = 0;
try {
value = IDGenerator.newID(session, ResObjectName.RES_OBJECT);
ResObject resObject = new ResObject();
resObject.setId(value);
resObject.setType(ResType.ORG_UNIT);
OrgUnit orgUnit = new OrgUnit();
orgUnit.setId(value);
orgUnit.setName(ou.getName());
orgUnit.setDescription(ou.getDescription());
resObject.setOrgUnit(orgUnit);
session.save(resObject);
ResObjectRela resOR = new ResObjectRela();
ResObjectRe resObjectRe = new ResObjectRe();
resObjectRe.setChild_id(value);
resObjectRe.setParent_id(parentOUID);
resOR.setId(resObjectRe);
resOR.setIs_extend(is_extend);
session.save(resOR);
} catch (HibernateException e) {
throw new ResException(errorCode);
}
return value;
}
/**
* 从数据库获取该组织及其下属所有组织的信息,并保存层次关系。参见<code>OrgUnitElement</code> 和<code>ResObjectElement</code>
*
* @param rootOrgUnitID 根目录的组织
* @return OrgUnitElement
*/
public OrgUnitElement getOUElement(Session session, long rootOrgUnitID) throws ResException {
ResObjectElement element = ResHibernateFactory.createResObjectHibernate().getResObjectElement(session,
rootOrgUnitID, false, true, ResType.ORG_UNIT);
return new OrgUnitElement(element);
}
/**
* 从数据库获取组织信息对象
*
* @param id 组织对应的资源ID
* @return OrgUnit,如果搜索不到返回null
*/
public OrgUnit getOrgUnit(Session session, long id) throws ResException {
int errorCode = ResException.OTHER_DATABASE_ERROR;
OrgUnit orgUnit = null;
try {
SQLQuery query = session.createSQLQuery(ResQuery.LOAD_ORG_BY_ID);
query.setParameter(1, new Long(id), new LongType());
List userList = query.list();
Iterator itr = userList.iterator();
if (itr.hasNext())
orgUnit = (OrgUnit) itr.next();
} catch (HibernateException e) {
throw new ResException(errorCode);
}
return orgUnit;
}
/**
* 修改数据库组织信息,不能改关联和ID
*
* @param ou OrgUnit
* @throws ResException
*/
public void modifyOrgUnit(Session session, OrgUnit ou) throws ResException {
try {
session.update(ou);
} catch (HibernateException e) {
throw new ResException(ResException.OTHER_DATABASE_ERROR);
}
}
/**
* 判断一个父ID下是否已经包含机构
*
* @param parentID
* @param name
* @return
* @throws ResException
*/
public boolean thereIsChildOrg(Session session, long parentID) throws ResException {
boolean isThere = false;
int errorCode = ResException.OTHER_DATABASE_ERROR;
// User user = null;
try {
Query q = session.createQuery(ResQuery.THERE_IS_CHILD_ORG);
q.setLong("parentID", parentID);
q.setInteger("type", ResType.ORG_UNIT);
Iterator itr = q.iterate();
if (itr.hasNext())
isThere = true;
} catch (HibernateException e) {
e.printStackTrace();
throw new ResException(errorCode);
}
return isThere;
}
/**
* 判断一个父ID下是否包含子部门或子用户,如果有,则该部门不能删除
*
* @param parentID
* @param name
* @return
* @throws ResException
*/
public boolean thereIsChild(Session session, long parentID) throws Exception {
boolean isThere = false;
// int errorCode = ResException.OTHER_DATABASE_ERROR;
List list = ResManager.searchUser(session, (int) parentID, "", "", true, true, 0, 1);
if (list == null || list.isEmpty()) {
OrgUnitElement ou = ResManager.getOrgUnitElement(session, parentID);
List children = ou.getChildren();
if (children != null && !children.isEmpty()) {
int size = children.size();
for (int i = 0; i < size; i++) {
OrgUnitElement child = new OrgUnitElement((ResObjectElement) children.get(i));
if (child.getOrgUnit(session).getIs_deleted() == 0) {
isThere = true;
break;
}
}
}
} else {
isThere = true;
}
return isThere;
}
/**
* 从数据库删除组织信息,关联信息删掉,本组织信息
*
* @param id 组织对应的资源ID
*/
public void deleteOrgUnit(Session session, long id) throws ResException {
OrgUnit ou = getOrgUnit(session, id);
ou.setIs_deleted(1);
modifyOrgUnit(session, ou);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -