Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.dao; import java.sql.Date; import java.sql.ResultSet; import java.sql.Timestamp; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.RowMapper; import com.sfs.beans.ObjectTypeBean; import com.sfs.beans.PrivilegesBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.ProjectBean; /** * The Class ProjectDAOImpl. */ public class ProjectDAOImpl extends WhichDoctorBaseDAOImpl implements ProjectDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(ProjectDAOImpl.class); /** The person dao. */ @Resource private PersonDAO personDAO; /** The training status dao. */ @Resource private TrainingStatusDAO trainingStatusDAO; /** * Used to get an Collection of ProjectBeans for a specified GUID number. * * @param guid the guid * @param fullResults the full results * @return the collection * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final Collection<ProjectBean> load(final int guid, final boolean fullResults) throws WhichDoctorDaoException { dataLogger.info("Projects for GUID: " + guid + " requested"); final String loadProject = getSQL().getValue("project/load") + " WHERE project.Active = true AND project.ReferenceGUID = ?" + " ORDER BY project.Year, guid.CreatedDate"; Collection<ProjectBean> projects = new ArrayList<ProjectBean>(); try { projects = this.getJdbcTemplateReader().query(loadProject, new Object[] { guid }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadProject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return projects; } /** * Used to get a ProjectBean for a specified ProjectId. * * @param projectId the project id * @return the project bean * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final ProjectBean load(final int projectId) throws WhichDoctorDaoException { dataLogger.info("ProjectId: " + projectId + " requested"); final String loadId = getSQL().getValue("project/load") + " WHERE project.ProjectId = ?"; ProjectBean project = null; try { project = (ProjectBean) this.getJdbcTemplateReader().queryForObject(loadId, new Object[] { projectId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadProject(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return project; } /** * Creates the project bean. * * @param project the project * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int create(final ProjectBean project, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { project.setActive(true); return save(project, checkUser, privileges, "create"); } /** * Modify the project bean. * * @param project the project * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int modify(final ProjectBean project, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { project.setActive(true); return save(project, checkUser, privileges, "modify"); } /** * Delete the project bean. * * @param project the project * @param checkUser the check user * @param privileges the privileges * @return true, if successful * @throws WhichDoctorDaoException the which doctor dao exception */ public final boolean delete(final ProjectBean project, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { boolean success = false; project.setActive(false); int deleteId = save(project, checkUser, privileges, "delete"); if (deleteId > 0) { success = true; } return success; } /** * Save the project bean. * * @param project the project * @param checkUser the check user * @param privileges the privileges * @param action the action * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ private int save(final ProjectBean project, final UserBean checkUser, final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException { /* Check required information within Projectbean is present */ if (project.getReferenceGUID() == 0) { throw new WhichDoctorDaoException("Project requires a valid Reference GUID number"); } if (!privileges.getPrivilege(checkUser, "projects", action)) { throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " project entry"); } int assessmentTypeId = 0, projectTypeId = 0, projectStatusId = 0, specialtyTypeId = 0; try { ObjectTypeBean object = this.getObjectTypeDAO().load("Project Assessment Type", "", project.getAssessmentType()); assessmentTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for assessment type: " + e.getMessage()); throw new WhichDoctorDaoException("The project requires a valid assessment type"); } try { ObjectTypeBean object = this.getObjectTypeDAO().load("Project Type", "", project.getProjectType()); projectTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.info("Error loading objecttype for project type: " + e.getMessage()); } try { ObjectTypeBean object = this.getObjectTypeDAO().load("Project Status", "", project.getStatus()); projectStatusId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for project status: " + e.getMessage()); throw new WhichDoctorDaoException("The project requires a valid status"); } try { ObjectTypeBean object = this.getObjectTypeDAO().load("Training Program", project.getTrainingProgram(), project.getTrainingOrganisation()); specialtyTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.info("Error loading objecttype for specialty: " + e.getMessage()); } int projectId = 0; Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis()); Date submitted = null; Date resubmitted = null; if (project.getSubmitted() != null) { submitted = new Date(project.getSubmitted().getTime()); } if (project.getResubmitted() != null) { resubmitted = new Date(project.getResubmitted().getTime()); } int assessorId1 = 0, assessorId2 = 0, assessorId3 = 0; if (project.getAssessor1() != null) { PersonBean person = project.getAssessor1(); assessorId1 = person.getGUID(); } if (project.getAssessor2() != null) { PersonBean person = project.getAssessor2(); assessorId2 = person.getGUID(); } if (project.getAssessor3() != null) { PersonBean person = project.getAssessor3(); assessorId3 = person.getGUID(); } ArrayList<Object> parameters = new ArrayList<Object>(); parameters.add(project.getReferenceGUID()); parameters.add(assessmentTypeId); parameters.add(projectTypeId); parameters.add(projectStatusId); parameters.add(specialtyTypeId); parameters.add(project.getTitle()); parameters.add(project.getMemo()); parameters.add(submitted); parameters.add(resubmitted); parameters.add(project.getYear()); parameters.add(project.getAllOfProgram()); parameters.add(assessorId1); parameters.add(assessorId2); parameters.add(assessorId3); parameters.add(project.getActive()); parameters.add(sqlTimeStamp); parameters.add(checkUser.getDN()); parameters.add(project.getLogMessage(action)); try { Integer[] result = this.performUpdate("project", project.getGUID(), parameters, "Project", checkUser, action); /* Set the returned guid and id values */ project.setGUID(result[0]); projectId = result[1]; if (projectId > 0) { // Rebuild the training status for this person this.trainingStatusDAO.calculate(project.getReferenceGUID()); } } catch (Exception e) { dataLogger.error("Error processing project record: " + e.getMessage()); throw new WhichDoctorDaoException("Error processing project record: " + e.getMessage()); } return projectId; } /** * Load the project bean from the result set. * * @param rs the rs * @return the project bean * @throws SQLException the sQL exception */ private ProjectBean loadProject(final ResultSet rs) throws SQLException { ProjectBean project = new ProjectBean(); project.setId(rs.getInt("ProjectId")); project.setGUID(rs.getInt("GUID")); project.setReferenceGUID(rs.getInt("ReferenceGUID")); project.setAssessmentType(rs.getString("AssessmentType")); project.setAssessmentTypeAbbr(rs.getString("AssessmentTypeAbbr")); project.setProjectType(rs.getString("ProjectType")); project.setProjectTypeAbbr(rs.getString("ProjectTypeAbbr")); project.setStatus(rs.getString("Status")); project.setYear(rs.getInt("Year")); project.setAllOfProgram(rs.getBoolean("AllOfProgram")); project.setTitle(rs.getString("Title")); project.setMemo(rs.getString("Memo")); try { project.setSubmitted(rs.getDate("Submitted")); } catch (SQLException sqe) { dataLogger.debug("Error reading Submitted: " + sqe.getMessage()); } try { project.setResubmitted(rs.getDate("Resubmitted")); } catch (SQLException sqe) { dataLogger.debug("Error reading Resubmitted: " + sqe.getMessage()); } int assessorId1 = rs.getInt("AssessorId1"); if (assessorId1 > 0) { try { /* Load person related to assessorId 1 */ PersonBean person = this.personDAO.loadGUID(assessorId1); project.setAssessor1(person); } catch (Exception e) { dataLogger.error("Could not load PersonBean for Project"); } } int assessorId2 = rs.getInt("AssessorId2"); if (assessorId2 > 0) { try { /* Load person related to assessorId 2 */ PersonBean person = this.personDAO.loadGUID(assessorId2); project.setAssessor2(person); } catch (Exception e) { dataLogger.error("Could not load PersonBean for Project"); } } int assessorId3 = rs.getInt("AssessorId3"); if (assessorId3 > 0) { try { /* Load person related to assessorId 3 */ PersonBean person = this.personDAO.loadGUID(assessorId3); project.setAssessor3(person); } catch (Exception e) { dataLogger.error("Could not load PersonBean for Project"); } } project.setTrainingOrganisation(rs.getString("TrainingOrganisation")); project.setTrainingProgram(rs.getString("TrainingProgram")); project.setTrainingProgramISBMapping(rs.getString("TrainingProgramISBMapping")); project.setActive(rs.getBoolean("Active")); try { project.setCreatedDate(rs.getTimestamp("CreatedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading CreatedDate: " + sqe.getMessage()); } project.setCreatedBy(rs.getString("CreatedBy")); try { project.setModifiedDate(rs.getTimestamp("ModifiedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ModifiedDate: " + sqe.getMessage()); } project.setModifiedBy(rs.getString("ModifiedBy")); try { project.setExportedDate(rs.getTimestamp("ExportedDate")); } catch (SQLException sqe) { dataLogger.debug("Error reading ExportedDate: " + sqe.getMessage()); } project.setExportedBy(rs.getString("ExportedBy")); // Load user details from DB UserBean user = new UserBean(); user.setDN(rs.getString("CreatedBy")); user.setPreferredName(rs.getString("CreatedFirstName")); user.setLastName(rs.getString("CreatedLastName")); project.setCreatedUser(user); UserBean modified = new UserBean(); modified.setDN(rs.getString("ModifiedBy")); modified.setPreferredName(rs.getString("ModifiedFirstName")); modified.setLastName(rs.getString("ModifiedLastName")); project.setModifiedUser(modified); UserBean export = new UserBean(); export.setDN(rs.getString("ExportedBy")); export.setPreferredName(rs.getString("ExportedFirstName")); export.setLastName(rs.getString("ExportedLastName")); project.setExportedUser(export); return project; } }