📄 departmentprocessbean.java
字号:
package cn.myapps.core.department.ejb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import cn.myapps.base.action.ParamsTable;
import cn.myapps.base.dao.DAOFactory;
import cn.myapps.base.dao.IBaseDAO;
import cn.myapps.base.dao.PersistenceUtils;
import cn.myapps.base.dao.ValueObject;
import cn.myapps.base.ejb.BaseProcessBean;
import cn.myapps.core.department.dao.DepartmentDAO;
import cn.myapps.core.permission.ejb.PermissionPackage;
import cn.myapps.core.permission.ejb.PermissionProcess;
import cn.myapps.util.ProcessFactory;
public class DepartmentProcessBean extends BaseProcessBean implements
DepartmentProcess {
public void doCreate(ValueObject vo) throws Exception {
super.doCreate(vo);
PermissionPackage.clearCache();
}
public void doRemove(String pk) throws Exception {
getDAO();
// 检查是否是根部门
// 检查是否有下级部门
DepartmentVO tempDepartment;
Collection subs = ((DepartmentDAO) getDAO()).getDatasByParent(pk);
if (subs != null && !subs.isEmpty()) {
throw new DepartmentException("Has subdepartment");
}
tempDepartment = (DepartmentVO)((DepartmentDAO) getDAO()).find(pk);
if(tempDepartment.getUsers().size()>0)
{
throw new DepartmentException("Has user");
}
if(tempDepartment.getRoles().size()>0)
{
throw new DepartmentException("Has Role");
}
// 检查是否有下属用户
/*
* Collection col = userDAO.getDatasByDept(pk); if (col != null &&
* !col.isEmpty()) { throw new Exception("该部门有下属用户, 不能删除"); }
*/
super.doRemove(pk);
PermissionPackage.clearCache();
}
public void doUpdate(ValueObject vo) throws Exception {
try {
PersistenceUtils.beginTransaction();
DepartmentVO po = (DepartmentVO) getDAO().find(vo.getId());
PermissionProcess pp = (PermissionProcess) ProcessFactory
.createProcess(PermissionProcess.class);
if (po != null) {
if (po.getSuperior() != null
&& ((DepartmentVO) vo).getSuperior() != null
&& !po.getSuperior().getId().equals(
(((DepartmentVO) vo).getSuperior()).getId())) {
changLevel((DepartmentVO) vo);
}
Collection roles=po.getRoles();
PropertyUtils.copyProperties(po, vo);
po.setRoles(roles);
getDAO().update(po);
} else {
getDAO().update(vo);
}
PersistenceUtils.commitTransaction();
} catch (Exception e) {
e.printStackTrace();
PersistenceUtils.rollbackTransaction();
}
PermissionPackage.clearCache();
}
public Collection doSimpleQuery(ParamsTable params) throws Exception {
getDAO();
return ((DepartmentDAO) getDAO()).simpleQuery(params);
}
public Collection getDatasByParent(String parent) throws Exception {
return ((DepartmentDAO) getDAO()).getDatasByParent(parent);
}
public String getIdByName(String deptname, String application) throws Exception {
return ((DepartmentDAO) getDAO()).getIdByName(deptname, application);
}
protected IBaseDAO getDAO() throws Exception {
IBaseDAO dao = (DepartmentDAO) DAOFactory
.getDefaultDAO(DepartmentVO.class.getName());
return dao;
}
public DepartmentVO getRootDepartmentByApplication(String application)throws Exception{
return ((DepartmentDAO) getDAO()).getRootDepartmentByApplication(application);
}
public Map deepSearchDepartmentTree(Collection cols,
DepartmentVO startNode, String excludeNodeId, int deep)
throws Exception {
Map list = new LinkedHashMap();
String prefix = "|------------------------------------------------";
if (startNode != null) {
list.put(startNode.getId(), prefix.substring(0, deep * 2)
+ startNode.getName());
}
Iterator iter = cols.iterator();
while (iter.hasNext()) {
DepartmentVO vo = (DepartmentVO) iter.next();
if (startNode == null) {
if (vo.getSuperior() == null) {
if (vo.getId() != null && !vo.getId().equals(excludeNodeId)) {
Map tmp = deepSearchDepartmentTree(cols, vo,
excludeNodeId, deep + 1);
list.putAll(tmp);
}
}
} else {
if (vo.getSuperior() != null
&& vo.getSuperior().getId().equals(startNode.getId())) {
if (vo.getId() != null && !vo.getId().equals(excludeNodeId)) {
Map tmp = deepSearchDepartmentTree(cols, vo,
excludeNodeId, deep + 1);
list.putAll(tmp);
}
}
}
}
return list;
}
public Collection getUnderDeptList(String deptid) throws Exception {
HashSet result = new HashSet();
Collection colls = new ArrayList();
Iterator itmp = (doSimpleQuery(null)).iterator();
while (itmp.hasNext()) {
DepartmentVO dept = (DepartmentVO) itmp.next();
if (dept.getSuperior() != null
&& dept.getSuperior().getId().equals(deptid)) {
colls.add(dept);
} else if (dept.getId() != null && dept.getId().equals(deptid)) {
colls.add(dept);
}
}
if (colls != null) {
Iterator iter = colls.iterator();
while (iter.hasNext()) {
DepartmentVO vo = (DepartmentVO) iter.next();
if (vo.getId() != null && !vo.getId().equals(deptid)) {
Collection sub = getUnderDeptList(vo.getId());
for (Iterator i = sub.iterator(); i.hasNext();) {
DepartmentVO d = (DepartmentVO) i.next();
result.add(d);
}
}
result.add(vo);
}
}
return result;
}
public Collection getSuperiorDeptList(String depid) throws Exception {
HashSet result = new HashSet();
DepartmentVO dep = (DepartmentVO) doView(depid);
if (dep != null) {
result.add(dep);
while (dep.getSuperior() != null) {
dep = dep.getSuperior();
result.add(dep);
}
}
return result;
}
public void changLevel(DepartmentVO vo) throws Exception {
Collection colls = new ArrayList();
// Collection departmentlist= doSimpleQuery(null);
Collection departmentlist = ((DepartmentDAO) getDAO())
.getAllDepartment(vo.getApplicationid());
Iterator itmp = departmentlist.iterator();
while (itmp.hasNext()) {
DepartmentVO dept = (DepartmentVO) itmp.next();
if (dept.getSuperior() != null
&& dept.getSuperior().getId().equals(vo.getId())) {
dept.setLevel(vo.getLevel() + 1);
getDAO().update(dept);
colls.add(dept);
}
}
if (colls != null) {
Iterator iter = colls.iterator();
while (iter.hasNext()) {
DepartmentVO dept = (DepartmentVO) iter.next();
if (dept != null) {
changLevel(dept);
}
}
}
}
public Collection getDepartmentByLevel(int level, String application) throws Exception {
return ((DepartmentDAO) getDAO()).getDepartmentByLevel(level, application);
}
public Collection getDepartmentByName(String byName, String application) throws Exception {
return ((DepartmentDAO)getDAO()).getDepartmentByName(byName, application);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -