⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userprocessbean.java

📁 OBPM是一个开源
💻 JAVA
字号:
package cn.myapps.core.user.ejb;

import java.util.Collection;
import java.util.HashMap;

import org.apache.commons.beanutils.PropertyUtils;

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.constans.Framework;
import cn.myapps.core.permission.ejb.PermissionPackage;
import cn.myapps.core.user.action.WebUser;
import cn.myapps.core.user.dao.UserDAO;
import cn.myapps.util.Security;
import cn.myapps.util.StringUtil;
import cn.myapps.util.sequence.Sequence;

public class UserProcessBean extends BaseProcessBean implements UserProcess {

	public static HashMap _cache = new HashMap();

	public void doCreate(ValueObject vo) throws Exception {
		UserVO tmp = null;
		tmp = ((UserDAO) getDAO()).login(((UserVO) vo).getLoginno(), vo
				.getApplicationid());
		if (tmp != null) {
			throw new ExistNameException(
					"Exist same loginno,please choose another!");
		}
		String id = Sequence.getSequence();
		vo.setId(id);

		UserVO user = (UserVO) vo;

		if (user.getLoginpwd() == null) {
			user.setLoginpwd("");
		} else {
			user.setLoginpwd(encrypt(user.getLoginpwd()));
		}

		super.doCreate(user);
		PermissionPackage.clearCache();
	}

	public void doRemove(String pk) throws Exception {
		try {
			// 检查是否是根部门
			if (pk.equals(Framework.ADMINISTRATOR)) {
				throw new Exception("不能删除超级管理员");
			}

			PersistenceUtils.beginTransaction();
			// UserVO user = (UserVO)getDAO().find(pk);
			// if (user!=null) {
			// Collection roles = user.getRoles();
			// if (roles!=null) {
			// for (Iterator iter = roles.iterator(); iter.hasNext();) {
			// RoleVO role = (RoleVO) iter.next();
			// role.getUsers().remove(user);
			// user.getRoles().remove(role);
			// }
			// }
			// }

			getDAO().remove(pk);

			PersistenceUtils.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			PersistenceUtils.rollbackTransaction();
		}

		super.doRemove(pk);
		PermissionPackage.clearCache();
	}

	public void doUpdate(ValueObject vo) throws Exception {
		UserVO user = (UserVO) vo;
		UserVO tmp = null;
		tmp = ((UserDAO) getDAO()).login(user.getLoginno(),vo
				.getApplicationid());
		if (tmp != null && !tmp.getId().equals(vo.getId())) {
			throw new ExistNameException("same loginno ,please choose anther!");
			// throw new Exception("已有相同登录名用户,请重新选择登录名!");
		}
		if (user.getLoginpwd() != null
				&& user.getLoginpwd().trim().length() != 0) {
			user.setLoginpwd(encrypt(user.getLoginpwd()));
		} else {
			UserVO oldValue = (UserVO) ((UserDAO) getDAO()).find(user.getId());
			user.setLoginpwd(oldValue.getLoginpwd());
		}

		try {
			PersistenceUtils.beginTransaction();
			UserVO po = (UserVO) getDAO().find(vo.getId());
			if (po != null) {
				PropertyUtils.copyProperties(po, vo);
				getDAO().update(po);
			} else {
				getDAO().update(vo);
			}

			PersistenceUtils.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			PersistenceUtils.rollbackTransaction();
		}
		PermissionPackage.clearCache();
		// super.doUpdate(user);
	}

	public void changePwd(String id, String oldPwd, String newPwd)
			throws Exception {
		UserVO vo = (UserVO) getDAO().find(id);
		if (!encrypt(oldPwd).equals(vo.getLoginpwd())) {
			throw new Exception("旧密码不正确");
		}
		vo.setLoginpwd(encrypt(newPwd));
		super.doUpdate(vo);
	}

	public UserVO login(String no, String password) throws Exception {
		UserVO vo = null;
		try {
			vo = ((UserDAO) getDAO()).login(no);
		} catch (Exception ex) {
			throw ex;
		}

		if (vo == null) {
			throw new Exception("User does not exist!");
		}
		_cache.remove(vo.getId());

		if (vo.getLoginpwd().equals(encrypt(password))) {
			if (vo.getStatus() == 1) {
				return vo;
			} else {
				throw new Exception("Sorry! User does not effectived!");
			}

		} else {
			throw new Exception("Password Error!");
		}
	}

	public UserVO login(String no, String password, String applicationid)
			throws Exception {
		UserVO vo = null;
		try {
			vo = ((UserDAO) getDAO()).login(no, applicationid);
		} catch (Exception ex) {
			throw ex;
		}

		if (vo == null) {
			throw new Exception("User does not exist!");
		}
		_cache.remove(vo.getId());

		if (vo.getLoginpwd().equals(encrypt(password))) {
			if (vo.getStatus() == 1) {
				return vo;
			} else {
				throw new Exception("Sorry! User does not effectived!");
			}

		} else {
			throw new Exception("Password Error!");
		}
	}

	public WebUser getWebUserInstance(String userid) throws Exception {
		WebUser tmp = (WebUser) _cache.get(userid);
		if (tmp != null) {
			return tmp;
		}

		UserVO user = (UserVO) doView(userid);
		if (user != null) {
			tmp = new WebUser(user);
			_cache.put(userid, tmp);
			return tmp;
		}
		return null;
	}

	public UserVO login(String no) throws Exception {
		UserVO vo = null;
		try {
			vo = ((UserDAO) getDAO()).login(no);
		} catch (Exception ex) {
			if (ex.getMessage() != null
					&& ex.getMessage().equals("Row does not exist")) {
				throw new Exception("没有这个用户");
			} else {
				throw ex;
			}
		}
		_cache.remove(vo.getId());
		return vo;
	}

	public Collection getDatasByDept(String parent, String application)
			throws Exception {
		if (parent == null) {
			parent = "";
		}
		return ((UserDAO) getDAO()).getDatasByDept(parent, application);
	}

	public Collection getDatasByGroup(String parent, String application)
			throws Exception {
		getDAO();
		if (parent == null) {
			parent = "";
		}
		return ((UserDAO) getDAO()).getDatasByRoleid(parent, application);
	}

	private String encrypt(String s) throws Exception {
		return StringUtil.left(Security.encodeToMD5(s),
				Framework.PASSWORD_LENGTH);
	}

	protected IBaseDAO getDAO() throws Exception {
		return DAOFactory.getDefaultDAO(UserVO.class.getName());
	}

	public void doPersonalUpdate(ValueObject vo) throws Exception {

		UserVO user = (UserVO) vo;
		UserVO tmp = null;
		UserVO oldValue = (UserVO) ((UserDAO) getDAO()).find(user.getId());
		if (user.getLoginpwd() != null
				&& user.getLoginpwd().trim().length() != 0) {
			oldValue.setLoginpwd(encrypt(user.getLoginpwd()));
		}
		oldValue.setName(user.getName());
		oldValue.setEmail(user.getEmail());
		oldValue.setLanguageType(user.getLanguageType());

		try {
			PersistenceUtils.beginTransaction();
			UserVO po = (UserVO) getDAO().find(vo.getId());
			if (po != null) {
				getDAO().update(oldValue);
			} else {
				getDAO().update(oldValue);
			}

			PersistenceUtils.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			PersistenceUtils.rollbackTransaction();
		}
		PermissionPackage.clearCache();

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -