sysdaoimpl.java

来自「本源码为教学管理信息系统」· Java 代码 · 共 415 行

JAVA
415
字号
package com.wygl.xtgl.service;

import java.io.File;
import java.sql.Types;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.hibernate.HibernateException;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
import com.wygl.dbdao.DbDaoException;
import com.wygl.page.HQuery;
import com.wygl.page.Paras;
import com.wygl.page.ParasList;
import com.wygl.service.AbstractService;
import com.wygl.xtgl.condition.PersonCondition;
import com.wygl.xtgl.domain.Employee;
import com.wygl.xtgl.domain.Privilege;
import com.wygl.xtgl.domain.Role;
import com.wygl.xtgl.domain.RolePrivilege;
import com.wygl.xtgl.domain.User;
import com.wygl.xtgl.domain.UserRole;

/**
 * 针对hibernate的通用数据库访问接口实现
 * 
 * @spring.bean id ="***erviceTarget"
 * @spring.property name="sessionFactory" ref="sessionFactory"
 */
public class SysDaoImpl extends AbstractService implements SysDao {
	private PersonCondition condition;//基类条件

	public PersonCondition getCondition() {
		return condition;
	}
	public void setCondition(PersonCondition condition) {
		this.condition = condition;
	}
	/**构建hquery*/
	public void creatHquery() throws Exception{	
		hquery = new HQuery();
		hquery.setQueryString(condition.getHqlString());
		
		//实例化参数,本例为两个参数	
		
		Paras paras1=new Paras();
		paras1.setPName(condition.getDwdm()+"%");//现任职部门
		paras1.setTypeNo(Types.VARCHAR);
		
		Paras paras2=new Paras();
		paras2.setPName("%"+condition.getName()+"%");//姓名
		paras2.setTypeNo(Types.VARCHAR);
		

		
		ParasList paraslist=new ParasList();
		
		paraslist.add(paras1);
		paraslist.add(paras2);
		
		hquery.setParaslist(paraslist);
		this.setPageInfo();	//设置分页信息
	}
	/**设置页面信息*/
	public void setPageInfo()throws Exception {
		//查询记录总数
		int pagecount = 0;
		pagecount = dbDao.count(hquery);
		condition.setRowCounts(pagecount);
		condition.setCrossPages(Math.round(Math.ceil(pagecount*1.0/condition.getRowsPerpage())));  
		if(condition.getPageNo() > condition.getCrossPages()){
		    condition.setPageNo(condition.getCrossPages());
		}
		//设置当前行
		long currentRow = (condition.getPageNo() - 1)*condition.getRowsPerpage();
		if(currentRow < 0){
			currentRow = 0;
			condition.setPageNo(1);
		}
		condition.setCurrentRow(currentRow + 1);	
		
		//调用查寻方法得到第一页结果
		hquery.setPageStartNo(condition.getPageNo());
		hquery.setOrderby("order by empl.department.dwdm asc,empl.name asc");
		
	}
	/**查询列表*/
	public List queryList() throws Exception{
   		return dbDao.queryObjectsToPages(hquery);		
	}
	
	
	
	//查找当前登录用户
	public User searchUser(final String loginName) throws Exception {	
		List list = dbDao.queryObjects("from User as user where user.name ='" + loginName +"'");
		if (list == null || list.size() == 0) return null;
		User u = (User) list.get(0);
		return u;
	}	
	//查找登录人的权限菜单
	public List searchPrivilegeByUser(User user) throws Exception {	
		String hql = "select distinct rolePrivilege.privilege from RolePrivilege as rolePrivilege where rolePrivilege.role in " +
				"  ( select distinct userRole.role from  UserRole as userRole " +
				"where   userRole.user.id = '" + user.getId() + "' )  order by  rolePrivilege.privilege.sequence";
		if(user.getName().equals("system")) hql = "from Privilege as pri";
		List list = dbDao.queryObjects(hql);
		return list;
	}
	//查找二级单位
	public List searchDw(String dwjb) throws Exception {
		return dbDao.queryObjects("from Department as dep where dep.dwjb='"+dwjb+"' order by dep.dwdm asc");
	}
	public List searchDw(String supdw,String dwjb) throws Exception {
		return dbDao.queryObjects("select distinct new com.olive.taglib.SelectOptions(dep.dwdm,dep.dwmc) from Department as dep where dep.dwdm like '"+supdw+"%'"+" and dep.dwjb='"+dwjb+"' order by dep.dwdm asc");
	}
	//查找单位下的人员
	public List searchEmployee(String dwdm) throws Exception {			
		return dbDao.queryObjects("from Employee as employee where employee.department.dwdm like '" + dwdm + "' order by employee.name asc");
	}
	/**查询对象*/
	public Object queryById(String id)throws Exception{
		try {
			return dbDao.queryObjectById(Employee.class,id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
   	/**保存干部竞聘信息*/
	public String save(Object obj)throws Exception{
		Employee emp=(Employee)obj;
		try{
			if (checkRepeat(emp) <= 0 ) {//校验录入内容是否重复
				if(emp.getEmployeeId()==null ||emp.getEmployeeId().equals("")){
					emp.setYgdm(this.getMaxCode(emp));
					dbDao.addObject(emp);
				}else{
					dbDao.updateObject(emp);
				}
			}else{
    			return "false";
    		}
	    }catch (HibernateException e) {
	        return "error";
	    }
	    return "true";
	}
	/**校验输入重复*/
	public int checkRepeat(Object obj) throws Exception {
		try {
			Employee emp=(Employee)obj;
			String str = "from Employee as emp where emp.name ='"+emp.getName()+"' and emp.bz ='"+emp.getSfsj()+"'";
			if(emp.getEmployeeId()==null ||emp.getEmployeeId().equals("")){
				return dbDao.queryObjectsCount(str);
			}else{
				return dbDao.queryObjectsCount(str+" and emp.EmployeeId <> '"+emp.getEmployeeId()+"'");    	
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} 
	}
	/**删除信息*/
	public String delete(List ids)throws Exception{
		try {
			if(ids==null||ids.size()==0)	return "false";			
			dbDao.deleteObjects(Employee.class,ids);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";		
		}
	}
	//查找是否注册
	public boolean registered(String username) throws Exception {
		boolean registered = false;
		List users = (List)dbDao.queryObjects("from User as user where user.name = '" + username + "'");
		if(users.size()>0) registered=true;
		return registered;
	}
	//查找用户组
	public List listCandiRoles(final User user) throws Exception {
		String hql ="from Role as role where role not in (select distinct userRole.role from UserRole as userRole where userRole.user.id = '"+ user.getId() + "')";
		return dbDao.queryObjects(hql);
	}
   	/**保存干部竞聘信息*/
	public String saveUser(Object obj)throws Exception{
		User user=(User)obj;
		try{
			if(user.getId()==null ||user.getId().equals("")){
				dbDao.addObject(user);
			}else{
				dbDao.updateObject(user);
			}
	    }catch (HibernateException e) {
	        return "error";
	    }
	    return "true";
	}
	public User loadUser(String userId) {
		try {
			return (User)dbDao.queryObjectById(User.class,userId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//查找role对象
	public Role loadRole(String roleId) throws Exception{
		return (Role)dbDao.queryObjectById(Role.class,roleId);
	}
	//查询所有的user
	public List searchAllUsers(String strDwdm,String strName) throws Exception{
		User user =(User)ActionContext.getContext().getSession().get("currentUser");
	    String username = user.getName();
	    String hql= null;
	    if(username.equals("system")){
	    	hql="from User as user where user.name not like 'system%' and user.name like'"+strName+"%'";
	    }else{
	    	hql="from User as user where user.employee.department.dwdm like '"+ strDwdm +"%' and user.name not like 'system%' and user.name like '"+strName+"%' order by user.employee.department.dwdm asc";
	    }
		List users = (List)dbDao.queryObjects(hql);
		return users;
	}
	//刷新user
	public User reloadUser(User user) throws Exception{
		dbDao.refresh(user);
		return user;		
	}
	//批量删除user
	public String deleteUsers(List ids) throws Exception {
		try {
			if(ids==null||ids.size()==0)	return "false";			
			dbDao.deleteObjects(User.class,ids);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";		
		}
	}	
	//查询权限组
	public List searchAllRoles() throws Exception {
		return dbDao.queryObjects("from Role as role");
	}
	//保存权限组
	public String saveRole(Role role) throws Exception {
		try{
			if(role.getId()==null || role.getId().equals("")){
				dbDao.addObject(role);
			}else{
				dbDao.updateObject(role);
			}
	    }catch (HibernateException e) {
	        return "error";
	    }
	    return "true";
	}
	//批量删除role
	public String deleteRole(List ids) throws Exception{
		try {
			if(ids==null||ids.size()==0)	return "false";			
			dbDao.deleteObjects(Role.class,ids);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";		
		}
	}	
	//查找所有模块
	public List searchAllModels() throws Exception {
		return dbDao.queryObjects("from Model as model");
	}
	//查找所有菜单
	public List searchAllPrivileges() throws Exception {
		return dbDao.queryObjects("from Privilege as privilege order by privilege.sequence asc");
	}
	//查找组拥有的权限
	public List searchPrivilegeByRole(String roleId) throws Exception {
		return dbDao.queryObjects("select distinct rolePrivilege.privilege from  RolePrivilege as rolePrivilege where rolePrivilege.role.id ='" + roleId+ "' order by  rolePrivilege.privilege.sequence");
	}
	//删除所有授权
	public void removeAllPrivilegeOfRole(String roleId) throws Exception{
		List priOfRole = (List)dbDao.queryObjects("from RolePrivilege as rp where rp.role.id ='" + roleId + "'");
		if (priOfRole.size() > 0) {
			for(int i=0;i<priOfRole.size();i++){
				RolePrivilege pri = (RolePrivilege)priOfRole.get(i);
				dbDao.deleteObject(RolePrivilege.class,pri.getId());
			}
		}	
	}
	//添加授权	
	public void addPrivilegeToRole(final String roleId, final List selectedPrivileges) throws Exception {
		try {
			Role role = (Role)loadRole(roleId);//提取当前权限组
			if (selectedPrivileges.size() > 0){
				for (Iterator it = selectedPrivileges.iterator(); it.hasNext();) {
					String menuNo = (String) it.next();
					if (menuNo != null && !menuNo.equals("")) {
						Privilege privilege = (Privilege)dbDao.queryObjectById(Privilege.class, menuNo);//提取当前菜单
						RolePrivilege rolePrivilege = new RolePrivilege();
						rolePrivilege.setRole(role);
						rolePrivilege.setPrivilege(privilege);
						role.getRolePrivileges().add(rolePrivilege);
						dbDao.addObject(rolePrivilege);	
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("权限组授权错误:" + e.getMessage());
		}			
	}

	//退出组
	public User removeRoleFromUser(User user, String userRoleId) throws Exception{
		for (Iterator it = user.getUserRoles().iterator();it.hasNext();){
			UserRole userRole = (UserRole)it.next();
			if (userRole.getId().equals(userRoleId)){
				it.remove();
				dbDao.deleteObject(userRole);
			}
		}		
		return user;
	}
	/**上传照片*/
   	public String uploadFj(String jbxxb_id) throws Exception{   
   		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		ServletConfig config = ServletActionContext.getServletConfig();
		com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
		mySmartUpload.initialize(config,request,response); 
	    mySmartUpload.upload();
	    Employee empl=(Employee)dbDao.queryObjectById(Employee.class,jbxxb_id);
		for (int i=0;i<mySmartUpload.getFiles().getCount();i++){		
			try {
				com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
	    	   	if (!myFile.isMissing()){
	    	    	String myFileName=myFile.getFileName();
	    	    	int pos =myFileName.indexOf(".");
	    			String ext =myFileName.substring(pos+1);
	    	    	String path=ServletActionContext.getServletContext().getRealPath("/")+"/jsp/fjgl/rszp//";
	    	    	if(empl.getZplj()!=null && !empl.getZplj().equals("")){
	    		   		String zpmc = empl.getZplj();
	    		   		File file=new File(path,zpmc);
	    		   		if(file.exists()){//判断文件是否存在!!如果存在就删除;
	    		   			file.delete();
	    		   		}
	    		   	}
	    	    	myFileName = empl.getSfsj()+"."+ext;
	    	    	String trace=path+myFileName;
	    	    	myFile.saveAs(trace);
	    	    	empl.setZplj(myFileName);
	    	    	dbDao.updateObject(empl);
		    	   	}
			} catch (DbDaoException e) {
				e.printStackTrace();
			}
		}
		return "true";
	}
   	/**删除照片*/
   	public void delZp(String jbxxb_id) throws Exception{		
	    try {   
	   	    Employee empl=(Employee)dbDao.queryObjectById(Employee.class,jbxxb_id);
	    	String path=ServletActionContext.getServletContext().getRealPath("/")+"/jsp/fjgl/rszp//";
	    	if(empl.getZplj()!=null && !empl.getZplj().equals("")){
		   		String zpmc = empl.getZplj();
		   		File file=new File(path,zpmc);
		   		if(file.exists()){//判断文件是否存在!!如果存在就删除;
		   			file.delete();
		   		}
		   	}
	    	empl.setZplj("");
	    	dbDao.updateObject(empl);
		} catch (DbDaoException e) {
			e.printStackTrace();
		}
	}
   	/*提取最大号*/
	public String getMaxCode(Employee emp) throws Exception{
		try{
			//找出当前最大号	
			List temp =dbDao.queryObjects("from Employee as emp where emp.department.dwdm='"+emp.getDepartment().getDwdm()+"' order by CONVERT(int, emp.ygdm)  desc");
			Employee empl = new Employee();
			if(temp.size()>0){
				empl = (Employee)dbDao.queryObjects("from Employee as emp where emp.department.dwdm='"+emp.getDepartment().getDwdm()+"' order by CONVERT(int, emp.ygdm)  desc").get(0);
			}
			String maxCode = empl.getYgdm();
			String curCode;
			if (maxCode==null || maxCode.equals("")) {
				curCode = "001";
			}else{
				maxCode = maxCode.substring(3,6);
				curCode = Integer.toString(Integer.valueOf(maxCode).intValue() + 1);
				if (curCode.length() == 1) {
					curCode = "00"+curCode;
				}else if (curCode.length() == 2) {
					curCode = "0"+curCode;
				}
			}
			return emp.getDepartment().getDwdm()+curCode;
	    }catch (HibernateException e) {
	        return "error";
	    }
	}
	
	public List getdbr(String dwdm)throws Exception{
		return dbDao.queryObjects("select new com.wygl.taglib.SelectOptions(employee.name,employee.name) from Employee as employee where employee.department.dwdm like '" + dwdm + "' order by employee.name asc");
	}
	
}

⌨️ 快捷键说明

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