?? usermgrimpl.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 + -