instresourceorgdaoimpl.java

来自「这是一个工作流管理的后端EJB实现」· Java 代码 · 共 214 行

JAVA
214
字号
package com.coshare.joyteam.projectMgr.dao.mssql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.coshare.joyteam.projectMgr.dao.DAOException;
import com.coshare.joyteam.projectMgr.dao.InstResourceOrgDAO;
import com.coshare.joyteam.projectMgr.dto.InstResourceOrgDTO;
import com.coshare.joyteam.projectMgr.dto.InstanceDTO;
import com.coshare.joyteam.projectMgr.dto.TaskDTO;
import com.coshare.joyteam.util.ID;

public class InstResourceOrgDAOImpl extends AbstractMssqlDAO implements
		InstResourceOrgDAO {

	static String SQL_INSERT = "insert into [WfInstResourceOrg] ([InstanceId], [TaskId], [ResId], [Type]) values (?,?,?,?)";
	static String SQL_UPDATE = "update [WfInstResourceOrg] set [InstanceId] = ?, [TaskId] = ?, [ResId] = ?, [Type] = ? where [InstResOrgId] = ?";
	static String SQL_DELETE = "delete from [WfInstResourceOrg] where [InstResOrgId] = ?";
	static String SQL_SELECT_ByInstResOrgId = "select * from [WfInstResourceOrg] where [InstResOrgId] = ?";
	static String SQL_SELECT_BytaskId = "select * from [WfInstResourceOrg] where [TaskId] = ?";
	static String SQL_SELECT_ByinstId = "select * from [WfInstResourceOrg] where [InstanceId] = ?";
	static String SQL_SELECT_ALL = "select * from [WfInstResourceOrg]";
	static String SQL_SELECT_ByPRIMARYKEY = "select * from [WfInstResourceOrg] where [InstanceId] = ? and [TaskId] = ? and [ResId] = ? and [Type] = ?";

	public boolean AddNew(InstResourceOrgDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_INSERT);		
		boolean rt = false;
		try {
			ps.setInt(1, dtoObj.getInstanceId().getId());
			ps.setInt(2, dtoObj.getTaskId().getId());
			ps.setString(3, dtoObj.getResId());
			ps.setInt(4, dtoObj.getType());
			rt = ps.executeUpdate() > 0;
		} catch (SQLException e) {
			throw new DAOException("insert failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public boolean Update(InstResourceOrgDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_UPDATE);		
		boolean rt = false;
		try {
			ps.setInt(1, dtoObj.getInstanceId().getId());
			ps.setInt(2, dtoObj.getTaskId().getId());
			ps.setString(3, dtoObj.getResId());
			ps.setInt(4, dtoObj.getType());
			ps.setInt(5, dtoObj.getInstResOrgId().getId());
			rt = ps.executeUpdate() > 0;
		} catch (SQLException e) {
			throw new DAOException("update failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public boolean Delete(InstResourceOrgDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_DELETE);		
		boolean rt = false;
		try {
			ps.setInt(1, dtoObj.getInstResOrgId().getId());
			rt = ps.executeUpdate() > 0;
		} catch (SQLException e) {
			throw new DAOException("delete failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public InstResourceOrgDTO getInstResourceOrg(ID instResOrgId)
			throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ByInstResOrgId);
		InstResourceOrgDTO rt = null;
		try {
			ps.setInt(1, instResOrgId.getId());
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				rt = getDTOFromRs(rs);
			}
			else {
				throw new DAOException("not found.");
			}
		} catch (SQLException e) {
			throw new DAOException("select failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public List getTaskResourceOrgs(ID taskId) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_BytaskId);
		ArrayList rt = new ArrayList();
		try {
			ps.setInt(1, taskId.getId());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				rt.add(getDTOFromRs(rs));
			}
		} catch (SQLException e) {
			throw new DAOException("select failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public List getInstResourceOrgs(ID instanceId) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ByinstId);
		ArrayList rt = new ArrayList();
		try {
			ps.setInt(1, instanceId.getId());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				rt.add(getDTOFromRs(rs));
			}
		} catch (SQLException e) {
			throw new DAOException("select failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public List getInstResourceOrgs() throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ALL);
		ArrayList rt = new ArrayList();
		try {
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				rt.add(getDTOFromRs(rs));
			}
		} catch (SQLException e) {
			throw new DAOException("select failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public InstResourceOrgDTO getDTOFromRs(ResultSet rs) throws SQLException {
		InstResourceOrgDTO dto = new InstResourceOrgDTO();
		dto.setInstResOrgId(rs.getInt("InstResOrgId"));
		dto.setInstanceId(InstanceDTO.intToID(rs.getInt("InstanceId")));
		dto.setTaskId(TaskDTO.intToID(rs.getInt("TaskId")));
		dto.setResId(rs.getString("ResId"));
		dto.setType(rs.getInt("Type"));
		return dto;
	}

	public InstResourceOrgDTO getInstResourceOrg(ID instanceId, ID taskId, String resId, int type) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ByPRIMARYKEY);
		InstResourceOrgDTO rt = null;
		try {
			ps.setInt(1, instanceId.getId());
			ps.setInt(2, taskId.getId());
			ps.setString(3, resId);
			ps.setInt(4, type);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				rt = getDTOFromRs(rs);
			}
			else {
				throw new DAOException("not found.");
			}
		} catch (SQLException e) {
			throw new DAOException("select failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

}

⌨️ 快捷键说明

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