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

📄 usermgrimpl.java

📁 基于Sturts+Spring+Hibernate的一个高级销售管理系统。内容丰富
💻 JAVA
字号:
package com.yuanchung.sales.service.user.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.yuanchung.sales.util.KeyBean;
import com.yuanchung.organize.treeview.TreeviewDAO;
import com.yuanchung.sales.dao.user.UserDAO;
import com.yuanchung.sales.exception.ApplicationException;
import com.yuanchung.sales.exception.SystemException;
import com.yuanchung.sales.model.Position;
import com.yuanchung.sales.model.Treeview;
import com.yuanchung.sales.model.user.User;
import com.yuanchung.sales.model.userDefined.UserDefined;
import com.yuanchung.sales.model.userDefined.UserField;
import com.yuanchung.sales.service.user.UserMgr;
import com.yuanchung.sales.struts.user.form.UserForm;
import com.yuanchung.sales.util.Constants;
import com.yuanchung.sales.util.DateTimeTool;
import com.yuanchung.sales.util.StringTool;
import com.yuanchung.sales.vo.user.UserVo;

public class UserMgrImpl implements UserMgr {
	private static Logger logger = Logger.getLogger(UserMgrImpl.class);
	private static UserDAO userDao;

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	private TreeviewDAO treeviewDao;

	public void setTreeviewDao(TreeviewDAO treeviewDao) {
		this.treeviewDao = treeviewDao;
	}

	// 根据用户名和密码取得用户实体
	public User getUserByNameAndPass(String userName, String password)
			throws ApplicationException {
		try {
			return userDao.getUser(userName, password);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(Constants.EXCEPTION);
		}
	}

	// 查找所有的用户
	public List<UserVo> getAll() throws ApplicationException {
		List users = userDao.getAll();
		List<UserVo> usersVo = new ArrayList<UserVo>();
		for (Object o : users) {// 此处无需判断
			User user = (User) o;
			usersVo.add(new UserVo(user.getId(), user.getUserName(), user
					.getPassword(), user.getFamilyName(), user.getPosition(),
					user.getEmail(), user.getPhone(), user.getType()));
		}
		return usersVo;
	}

	// 初始化用户
	public User initUser(String userName, String password, String familyName,
			String position, String email, String phone, String type,
			Integer isDeptLead, Integer positionId) throws ApplicationException {
		try {
			// 根据岗位ID查找节点
			Treeview treeview = treeviewDao.findById(positionId);
			String positionName = "";
			if (StringTool.isNotBlank(position)) {// 若岗位名称为空
				positionName = position;
			} else {
				// 该节点的岗位
				positionName = treeview.getName();
			}
			return new User(treeview, userName, password, familyName,
					positionName, email, phone, type, isDeptLead);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 增加用户
	public void addUser(UserForm userForm, String password)
			throws SystemException {
		try {
			int positionId = userForm.getPositionId();

			// 初始化用户实体
			User user = initUser(userForm.getUserName(), password, userForm
					.getFamilyName(), userForm.getPosition(), userForm
					.getEmail(), userForm.getPhone(), userForm.getType(),
					userForm.getIsDeptLead(), positionId);
			// 保存用户
			userDao.saveUser(user);
			// 查找最新保存的用户
			User initUser = userDao.getUserLastest();
			// 保存用户自定义客户选项
			UserDefined userDefinedCust = new UserDefined(initUser,
					Constants.ALLCUSTOMER, Constants.ALLCUSTOMER_INT,
					new Date());
			userDao.saveUserDefined(userDefinedCust);
			// 查找最新保存的用户客户显示字段
			UserField userFieldCust = new UserField(userDao
					.getUserDefinedLastest(), Constants.CUSTOMER_NAME + ","
					+ Constants.INDUSTRYOf + "," + Constants.COMMUNADDR + ","
					+ Constants.PHONE);
			userDao.saveUserField(userFieldCust);

			// 保存用户自定义联系人选项
			UserDefined userDefinedContact = new UserDefined(initUser,
					Constants.ALLCONTACT, Constants.ALLCONTACT_INT,
					new Date());
			userDao.saveUserDefined(userDefinedContact);
			// 查找最新保存的用户选项
			UserField userFieldContact = new UserField(userDao
					.getUserDefinedLastest(), Constants.NAME + ","
					+ Constants.SATULATION + "," + Constants.CONTACTPHONE + ","
					+ Constants.CUSTOMERNAME);
			userDao.saveUserField(userFieldContact);

			// 保存用户自定义联系人选项
			UserDefined userDefinedBusiOpport = new UserDefined(initUser,
					Constants.ALLBUSIOPPORT, Constants.ALLBUSIOPPORT_INT,
					new Date());
			userDao.saveUserDefined(userDefinedBusiOpport);
			// 查找最新保存的用户选项
			UserField userFieldBusiOpport = new UserField(userDao
					.getUserDefinedLastest(), Constants.BUSIOPPORTNAME + ","
					+ Constants.INVOLVEPRODUCT + ","
					+ Constants.PRETRANSACTION_DATE + ","
					+ Constants.CUSTOMERNAME);
			userDao.saveUserField(userFieldBusiOpport);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Constants.SAVEUSEREXCEPTION);
			throw new SystemException(Constants.SAVEUSEREXCEPTION);
		}
	}

	// 根据id搜索用户
	public User getUserById(int id) throws ApplicationException {
		try {
			return userDao.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(Constants.FINDUSEREXCEPTION);
		}
	}

	// 将用户po转换成vo
	public UserVo TransfromUserPoToVo(User user) {
		return new UserVo(user.getId(), user.getUserName(), user.getPassword(),
				user.getFamilyName(), user.getPosition(), user.getEmail(), user
						.getPhone(), user.getType());
	}

	public void deleteUserAndRelation(User user) throws ApplicationException {
		try {

			// 删除该用户的角色;需要先罗列出该用户的角色
			List uRoleList = userDao.getUserRoleList(user.getId());
			userDao.deleteUserRole(uRoleList);

			// 删除该用户的查找视图
			List uDefinedList = userDao.getUserDefined(user);// 获取该用户的视图列表;
			if (uDefinedList != null) {
				for (Iterator iterator = uDefinedList.iterator(); iterator
						.hasNext();) {
					UserDefined uDefined = (UserDefined) iterator.next();// 获取某个视图;
					List uFieldList = userDao.getUserField(uDefined);// 获取该视图的显示字段列表;
					userDao.deleteUserField(uFieldList);// 删除该视图显示字段列表;
					List uFilterList = userDao.getUserFilter(uDefined);// 获取该视图的过滤字段;
					userDao.deleteUserFilter(uFilterList);// 删除该视图的过滤字段列表;
					userDao.deleteUserDefined(uDefined);// 删除该视图
				}
			}
			// 删除单个的用户;
			userDao.deleteUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(Constants.DELETEFIALURE);
		}
	}

	public void updateUser(User user) throws ApplicationException {
		try {
			userDao.editUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 根据父结点和姓名查找结点
	public Treeview getTreeviewByNameAndParentId(String name, int parentId)
			throws ApplicationException {
		try {
			return userDao.getTreeviewByFamilyNameAndParentId(name, parentId);
		} catch (Exception e) {
			throw new ApplicationException(Constants.FINDNODEEXCEPTION);
		}
	}

	// 根据岗位ID查找岗位
	public Position getPositionById(int positionId) throws ApplicationException {
		return userDao.getPositionById(positionId);
	}

	// 根据结点ID查找结点
	public Treeview findTreeviewById(int treeviewId)
			throws ApplicationException {
		try {
			return userDao.findTreeviewById(treeviewId);
		} catch (Exception e) {
			throw new ApplicationException(
					Constants.UPDATEORSAVEUSERPOSITONEXCEPTION);
		}
	}

	// 根据岗位ID获取用户信息
	public static List<User> findByTreeviewId(Integer id) {
		// TODO Auto-generated method stub
		List<User> users = userDao.findByTreeviewId(id);
		return users;
	}

	/**
	 * 根据角色ID查找用户信息
	 * 
	 * @param int
	 * @return List<User>
	 */
	public List<User> findUserByRoleId(int roleId, String familyName) {
		List<User> users = userDao.findByRoleId(roleId, familyName);
		return users;
	}

	public List<User> findAll(String familyName) {

		return userDao.findAll(familyName);
	}

	public User findById(int userId) {
		// TODO Auto-generated method stub
		try {
			return userDao.findById(userId);
		} catch (RuntimeException re) {
			logger.error(re);
			throw new SystemException(Constants.SYSTEMEXCEPTION);
		}
	}

	/**
	 * * 删除指定用户 说明:根据传进来的数组,分别取出userID,然后找出其对于的对象,遍历删除 author:张明强
	 * time:2008-12-26
	 */
	public String deleteUser(String[] userIds) {
		logger.debug("这一串值是什么1:" + userIds[0]);
		StringBuffer roleId_userRole = new StringBuffer();
		try {
			logger.debug("有到这边吗");
			for (int i = 0; i < userIds.length; i++) {
				// System.out.println("---测试测试--:"+userIds.length);
				// System.out.println("这个值是什么:"+userIds[i]);
				// 转换类型
				int ii = Integer.parseInt(userIds[i]);
				User user = userDao.findById(ii);
				logger.debug("查询对象成功!!!!!");
				// 改变用户的删除状态值,就是把deleteFlag质为1
				user.setDeleteFlag(1); // 把deleteFlag 置为1
				userDao.updateDeleteFlag(user);
				logger.debug("把用户的deleteFlag质为1成功");
			}
		} catch (RuntimeException re) {
			logger.error(re);
			throw new SystemException(Constants.SYSTEMEXCEPTION);
		}
		return roleId_userRole.toString();
	}

	public void updatePassword(String[] userIds) {

		try {
			for (int i = 0; i < userIds.length; i++) {

				System.out.println("userIds是什么:" + userIds[i]);
				// 转换类型
				int ii = Integer.parseInt(userIds[i]);
				User user = userDao.findById(ii);
				String password = Constants.SYSTEM_ADMIN_INIT_PASSWORD; // 初始化密码
				password = new KeyBean().getkeyBeanofStr(password); // 密码加密
				/*
				 * user.setPassword(password); userDao.initPassword(user);
				 */
				userDao.updatePassword(ii, password);
				logger.debug("更新成功");
			}
		} catch (RuntimeException re) {
			logger.error(re);
			throw new SystemException(Constants.SYSTEMEXCEPTION);
		}

	}
}

⌨️ 快捷键说明

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