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

📄 userservicehibernateimpl.java

📁 用JSF.Spring和Hibernate开发的一个办公用品管理系统的WEB应用。
💻 JAVA
字号:
package com.jsfabc.jsh.model.service.impl;

import com.jsfabc.jsh.model.service.UserService;
import com.jsfabc.jsh.model.exception.UserException;
import com.jsfabc.jsh.model.exception.UserNotExistException;
import com.jsfabc.jsh.model.exception.DataNotFoundException;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.model.exception.DuplicateUserException;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.jsfabc.jsh.model.bo.Department;
import com.jsfabc.jsh.model.bo.Person;
import com.jsfabc.jsh.model.bo.PersonRole;
import com.jsfabc.jsh.model.bo.Role;
import com.jsfabc.jsh.model.dao.DepartmentDao;
import com.jsfabc.jsh.model.dao.PersonRoleDao;
import com.jsfabc.jsh.model.dao.RoleDao;
import com.jsfabc.jsh.model.dao.PermissionDao;
import com.jsfabc.jsh.model.dao.UserDao;
import java.util.ArrayList;
import java.util.List;

public class UserServiceHibernateImpl implements UserService{
    //事务管理器
   	private PlatformTransactionManager transactionManager;
	
	//数据访问对象
    private UserDao userDao;
    private DepartmentDao departmentDao;
    private RoleDao roleDao;
    private PersonRoleDao personRoleDao;
    private PermissionDao permissionDao;
    
    
    
    //依赖注入
    public void setTransactionManager
                  (PlatformTransactionManager transactionManager){
    	this.transactionManager=transactionManager;
    }
    
    
    public void setUserDao(UserDao newValue){
    	this.userDao=newValue;
    }        

    public void setDepartmentDao(DepartmentDao newValue){
    	this.departmentDao=newValue;
    }

    public void setRoleDao(RoleDao newValue){
    	this.roleDao=newValue;
    }

    public void setPersonRoleDao(PersonRoleDao newValue){
    	this.personRoleDao=newValue;
    }    
    
    public void setPermissionDao(PermissionDao newValue){
    	this.permissionDao=newValue;
    }

    //构造函数
    public UserServiceHibernateImpl() {   	

    }
    
    //查找所有部门
    public List findAllDepartments() throws UserException {
        try {
        	
            List departments=(List)departmentDao.findAllDepartments();

            return departments;   
           
        }
        catch(DataAccessException daoe){
           daoe.printStackTrace();           
           throw new DbException(daoe.getMessage());
        }      
    }
    
    //查找一个用户的权限
    public List findPermission(String userId) throws UserException {
 
        try {
            List permissions=(List)permissionDao.findUserPermission(userId);

            return permissions;   
           
        }
        catch(DataAccessException daoe){
           daoe.printStackTrace();           
           throw new DbException(daoe.getMessage());
        }      
     }    
    
    //处理用户登录
    public Person login(String personId, String password) throws UserException {
    	//构建一个新用户
        Person person=new Person();
      
        try {
            //UserDao dao=new UserDaoHibernateImpl();
            
            person=userDao.find(personId);
            if(person!=null){
               //判断密码	
               if(password.equals(person.getPassword())){            	             
                   //加入所有部门列表
                   List departments=findAllDepartments();
                   person.setDepartments(departments);
                   
                  
                   //加入登录用户的权限
                   List permissions=findPermission(personId);
                   person.setPermissions(permissions);
                }
                else{
                   //表示用户名和密码合乎要求的用户不存在
                   person=null;
                }
            }    
            else{
                throw new DataNotFoundException(personId);
            }
            
            
            return person;   
           
        }
        catch(DataNotFoundException ue){	
           ue.printStackTrace();
           throw new UserNotExistException(personId);
        }
        catch(DataAccessException daoe){
           daoe.printStackTrace();           
           throw new DbException(daoe.getMessage());
        }      
     } 
    
    //处理用户注册
    public Person register(String personId, String password, 
    		String pwd,String personName,Department department,
    		String email,String telephone) 
                 throws UserException {
    	//构建一个新用户
        Person person=new Person();
        
        //创建一个新的数据访问对象	
        //UserDao dao=new UserDaoHibernateImpl();
              
        try {
            
            //判断密码	
            if(password.equals(pwd)){
                //设置用户属性
                person.setPersonId(personId);
                person.setPassword(password);
                person.setPersonName(personName);
                person.setDepartment(department);
                person.setEmail(email);
                person.setTelephone(telephone);
              
                //用dao在数据库表中创建一个用户记录
               userDao.create(person);
               
            }
            else{
                 
                //表示欲注册的用户名不合乎要求,不能返回用户对象
                person=null;    
                    
            }
            
            //返回用户对象
            return person;
        }

        //处理数据库访问异常
        catch(DataAccessException daoe){
        	
            //判断是否是因为该用户名已经注册过
            try{
                userDao.find(personId);
            }
            catch(DataAccessException daoe1){
            	          	
            	throw new DbException(daoe.getMessage());
            }
            	
           daoe.printStackTrace();                     
           //如果该用户名已经注册,则抛出重复的用户异常    
           throw new DuplicateUserException(personId);
        }
            
     }
    
    
    //返回一个部门的用户列表    
    public List depUsers(Department department) throws UserException{
    	List depUsers=new ArrayList();
    	try{
    		
    		depUsers=(ArrayList)userDao.findDepUsers(department);
    		return depUsers;
    	}
        catch(DataAccessException daoe){
            daoe.printStackTrace();           
            throw new DbException(daoe.getMessage());
         }      
    	
    }
    
    //查找所有的角色
    public List Roles() throws UserException {
    	List roles=new ArrayList();
        try{
    		
    		roles=(ArrayList)roleDao.findRoles();
    		return roles;
    	}
        catch(DataAccessException daoe){
            daoe.printStackTrace();           
            throw new DbException(daoe.getMessage());
         }      
    }
    
    //为一个用户指派角色
    public void assignRole(final String personId,final Integer [] roleIds)
                                  throws UserException{
         
    	TransactionTemplate transactionTemplate
    	                   =new TransactionTemplate(this.transactionManager);
    	transactionTemplate.setPropagationBehavior
    	                   (TransactionDefinition.PROPAGATION_REQUIRED);
    	transactionTemplate.execute(
    		new	TransactionCallbackWithoutResult(){
    			public void doInTransactionWithoutResult(TransactionStatus status){
    				 //查找用户原有角色,如果用户是系统设定的管理员即
    				 //用户帐号为”admin“的用户其角色不能删除
    				 List roleList=personRoleDao.findUserRolesForDel(personId);
    			        personRoleDao.deleteUserRoles(roleList);
    			        //计算新选择的用户角色
    					List newUserRoles=new ArrayList();
    			        for(int i=0;i<roleIds.length;i++){
    			        	PersonRole personRole=new PersonRole();
    			        	Person person=new Person();
    			        	person.setPersonId(personId);
    			        	Role role=new Role();
    			        	role.setRoleId(roleIds[i]);
    			        	
    			        	personRole.setPerson(person);
    			        	personRole.setRole(role);
    			        	newUserRoles.add(personRole);
    			        } 
                        //保存该用户新角色
    			        personRoleDao.saveUserRoles(newUserRoles);
    			}
    		}
    	);
    	 
    }

    //更改用户密码
    public void updatePassword(String personId,String password)
                                throws UserException{
        try{
        	Person person=userDao.find(personId);
        	person.setPassword(password);
    		userDao.updatePerson(person);    		
    	}
        catch(DataAccessException daoe){
            daoe.printStackTrace();           
            throw new DbException(daoe.getMessage());
         }      
    }
    
}

⌨️ 快捷键说明

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