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

📄 instancedaoimpl.java

📁 这是一个工作流管理的后端EJB实现
💻 JAVA
字号:
package com.coshare.joyteam.projectMgr.dao.mssql;

import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.coshare.joyteam.projectMgr.dao.DAOException;
import com.coshare.joyteam.projectMgr.dao.InstanceDAO;
import com.coshare.joyteam.projectMgr.dto.InstanceDTO;
import com.coshare.joyteam.projectMgr.dto.TemplateDTO;
import com.coshare.joyteam.util.ID;

public class InstanceDAOImpl extends AbstractMssqlDAO implements InstanceDAO {

	static String SQL_INSERT = "insert into [WfInstance] ([InstanceName], [TemplateID], [StartTime], [EndTime], [ExpireTime], [StatusKey], [ManagerId], [ManagerType], [RequesterId], [RequesterType], [theDescription]) values (?,?,?,?,?,?,?,?,?,?,?)";
	static String SQL_UPDATE = "update [WfInstance] set [InstanceName] = ?, [TemplateID] = ?, [StartTime] = ?, [EndTime] = ?, [ExpireTime] = ?, [StatusKey] = ?, [ManagerId] = ?, [ManagerType] = ?, [RequesterId] = ?, [RequesterType] = ?, [theDescription] = ? where [InstanceId] = ?";
	static String SQL_DELETE = "delete from [WfInstance] where [InstanceId] = ?";
	static String SQL_SELECT_ByInstanceId = "select * from [WfInstance] where [InstanceId] = ?";
	static String SQL_SELECT_ByInstanceName = "select * from [WfInstance] where [InstanceName] = ?";
	static String SQL_SELECT_ALL = "select * from [WfInstance]";

	public boolean AddNew(InstanceDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_INSERT);		
		boolean rt = false;
		try {
			ps.setString(1, dtoObj.getInstanceName());
			ps.setInt(2, dtoObj.getTemplateID().getId());
			ps.setTimestamp(3, new Timestamp(dtoObj.getStartTime().getTime()));
			ps.setTimestamp(4, new Timestamp(dtoObj.getEndTime().getTime()));
			ps.setInt(5, dtoObj.getExpireTime());
			ps.setInt(6, dtoObj.getStatusKey());
			ps.setInt(7, dtoObj.getManagerId());
			ps.setInt(8, dtoObj.getManagerType());
			ps.setString(9, dtoObj.getRequesterId());
			ps.setInt(10, dtoObj.getRequesterType());
			ps.setCharacterStream(11, new StringReader(dtoObj.getTheDescription()), dtoObj.getTheDescription().length());
			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(InstanceDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_UPDATE);		
		boolean rt = false;
		try {
			ps.setString(1, dtoObj.getInstanceName());
			ps.setInt(2, dtoObj.getTemplateID().getId());
			ps.setTimestamp(3, new Timestamp(dtoObj.getStartTime().getTime()));
			ps.setTimestamp(4, new Timestamp(dtoObj.getEndTime().getTime()));
			ps.setInt(5, dtoObj.getExpireTime());
			ps.setInt(6, dtoObj.getStatusKey());
			ps.setInt(7, dtoObj.getManagerId());
			ps.setInt(8, dtoObj.getManagerType());
			ps.setString(9, dtoObj.getRequesterId());
			ps.setInt(10, dtoObj.getRequesterType());
			ps.setCharacterStream(11, new StringReader(dtoObj.getTheDescription()), dtoObj.getTheDescription().length());
			ps.setInt(12, dtoObj.getInstanceId().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(InstanceDTO dtoObj) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_DELETE);		
		boolean rt = false;
		try {
			ps.setInt(1, dtoObj.getInstanceId().getId());
			rt = ps.executeUpdate() > 0;
		} catch (SQLException e) {
			throw new DAOException("delete failed.", e);
		}
		finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
		return rt;
	}

	public InstanceDTO getInstance(ID instanceId) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ByInstanceId);
		InstanceDTO rt = null;
		try {
			ps.setInt(1, instanceId.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 InstanceDTO getInstance(String instanceName) throws DAOException {
		PreparedStatement ps = da.prepareStatement(SQL_SELECT_ByInstanceName);
		InstanceDTO rt = null;
		try {
			ps.setString(1, instanceName);
			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 getInstances() 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 InstanceDTO getDTOFromRs(ResultSet rs) throws SQLException {
		InstanceDTO dto = new InstanceDTO();
		dto.setInstanceId(rs.getInt("InstanceId"));
		dto.setInstanceName(rs.getString("InstanceName"));
		dto.setTemplateID(TemplateDTO.intToID(rs.getInt("TemplateID")));
		dto.setStartTime(rs.getTimestamp("StartTime"));
		dto.setEndTime(rs.getTimestamp("EndTime"));
		dto.setExpireTime(rs.getInt("ExpireTime"));
		dto.setStatusKey(rs.getInt("StatusKey"));
		dto.setManagerId(rs.getInt("ManagerId"));
		dto.setManagerType(rs.getInt("ManagerType"));
		dto.setRequesterId(rs.getString("RequesterId"));
		dto.setRequesterType(rs.getInt("RequesterType"));
		dto.setTheDescription(rs.getString("theDescription"));
		return dto;
	}

}

⌨️ 快捷键说明

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