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.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; import java.util.Calendar; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; 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.IsbTransactionBean; import com.sfs.whichdoctor.beans.SpecialtyBean; /** * The Class SpecialtyDAOImpl. */ public class SpecialtyDAOImpl extends WhichDoctorBaseDAOImpl implements SpecialtyDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(SpecialtyDAOImpl.class); /** The isb transaction dao. */ @Resource private IsbTransactionDAO isbTransactionDAO; /** The default training organisation. */ private String defaultTrainingOrganisation; /** The default training program. */ private String defaultTrainingProgram; /** The default status. */ private String defaultStatus; /** * Sets the default training organisation. * * @param organisation the new default training organisation */ public final void setDefaultTrainingOrganisation(final String organisation) { this.defaultTrainingOrganisation = organisation; } /** * Sets the default training program. * * @param program the new default training program */ public final void setDefaultTrainingProgram(final String program) { this.defaultTrainingProgram = program; } /** * Sets the default status. * * @param status the new default status */ public final void setDefaultStatus(final String status) { this.defaultStatus = status; } /** * Used to get an ArrayList of SpecialtyBeans for a specified GUID. * * @param guid the guid * @param fullResults the full results * @return the collection * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final Collection<SpecialtyBean> load(final int guid, final boolean fullResults) throws WhichDoctorDaoException { dataLogger.info("Specialties for GUID: " + guid + " requested"); final String loadSpecialties = getSQL().getValue("specialty/load") + " WHERE specialty.Active = true AND specialty.ReferenceGUID = ?" + " ORDER BY trainingprogram.Class, trainingprogram.Name, guid.CreatedDate"; Collection<SpecialtyBean> specialties = new ArrayList<SpecialtyBean>(); try { specialties = this.getJdbcTemplateReader().query(loadSpecialties, new Object[] { guid }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadSpecialty(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return specialties; } /** * Used to get an ArrayList of SpecialtyBeans for a specified GUID. * * @param specialtyId the specialty id * @return the specialty bean * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final SpecialtyBean load(final int specialtyId) throws WhichDoctorDaoException { dataLogger.info("SpecialtyId: " + specialtyId + " requested"); final String loadSpecialtyId = getSQL().getValue("specialty/load") + " WHERE specialty.SpecialtyId = ?"; SpecialtyBean specialty = null; try { specialty = (SpecialtyBean) this.getJdbcTemplateReader().queryForObject(loadSpecialtyId, new Object[] { specialtyId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadSpecialty(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for this search: " + ie.getMessage()); } return specialty; } /** * Creates the specialty bean. * * @param specialty the specialty * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int create(final SpecialtyBean specialty, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { specialty.setActive(true); return save(specialty, checkUser, privileges, "create"); } /** * Modify the specialty bean. * * @param specialty the specialty * @param checkUser the check user * @param privileges the privileges * @return the int * @throws WhichDoctorDaoException the which doctor dao exception */ public final int modify(final SpecialtyBean specialty, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { specialty.setActive(true); return save(specialty, checkUser, privileges, "modify"); } /** * Delete the specialty bean. * * @param specialty the specialty * @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 SpecialtyBean specialty, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { boolean success = false; specialty.setActive(false); int deleteId = save(specialty, checkUser, privileges, "delete"); if (deleteId > 0) { success = true; } return success; } /** * Gets a new specialty bean with the default settings. * * @return the default specialty bean */ public final SpecialtyBean getDefaultBean() { SpecialtyBean specialty = new SpecialtyBean(); specialty.setTrainingOrganisation(defaultTrainingOrganisation); specialty.setTrainingProgram(defaultTrainingProgram); specialty.setStatus(defaultStatus); return specialty; } /** * Save the specialty bean. * * @param specialty the specialty * @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 SpecialtyBean specialty, final UserBean checkUser, final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException { /* Check required information within Specialty bean is present */ if (StringUtils.isBlank(specialty.getTrainingOrganisation())) { throw new WhichDoctorDaoException( "The training organisation for this " + "specialty cannot be an empty string"); } if (StringUtils.isBlank(specialty.getTrainingProgram())) { throw new WhichDoctorDaoException( "The training program for this " + "specialty cannot be an empty string"); } if (specialty.getReferenceGUID() == 0) { throw new WhichDoctorDaoException("Specialty requires a valid Reference GUID number"); } if (!privileges.getPrivilege(checkUser, "specialties", action)) { throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " specialty entry"); } int trainingProgramId = 0; int statusId = 0; try { ObjectTypeBean object = this.getObjectTypeDAO().load("Training Program", specialty.getTrainingProgram(), specialty.getTrainingOrganisation()); trainingProgramId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for training program: " + e.getMessage()); throw new WhichDoctorDaoException("Specialty requires a valid training program to be defined"); } try { ObjectTypeBean object = this.getObjectTypeDAO().load("Specialty Status", "", specialty.getStatus()); statusId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for specialty status: " + e.getMessage()); throw new WhichDoctorDaoException("Specialty requires a valid status to be defined"); } int specialtyId = 0; Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis()); ArrayList<Object> parameters = new ArrayList<Object>(); parameters.add(specialty.getReferenceGUID()); parameters.add(trainingProgramId); parameters.add(specialty.getTrainingProgramYear()); parameters.add(statusId); parameters.add(specialty.getMemo()); parameters.add(specialty.getActive()); parameters.add(sqlTimeStamp); parameters.add(checkUser.getDN()); parameters.add(specialty.getLogMessage(action)); /* Begin the ISB transaction */ IsbTransactionBean isbTransaction = this.isbTransactionDAO.begin(specialty.getReferenceGUID()); try { Integer[] result = this.performUpdate("specialty", specialty.getGUID(), parameters, "Specialty", checkUser, action); /* Set the returned guid and id values */ specialty.setGUID(result[0]); specialtyId = result[1]; } catch (Exception e) { dataLogger.error("Error processing specialty record: " + e.getMessage()); throw new WhichDoctorDaoException("Error processing specialty record: " + e.getMessage()); } if (specialtyId > 0) { /* Commit the ISB transaction */ this.isbTransactionDAO.commit(isbTransaction); } return specialtyId; } /** * Load the specialty bean from the resultset. * * @param rs the rs * @return the specialty bean * @throws SQLException the sQL exception */ private SpecialtyBean loadSpecialty(final ResultSet rs) throws SQLException { SpecialtyBean specialty = new SpecialtyBean(); specialty.setId(rs.getInt("SpecialtyId")); specialty.setGUID(rs.getInt("GUID")); specialty.setReferenceGUID(rs.getInt("ReferenceGUID")); specialty.setTrainingOrganisation(rs.getString("TrainingOrganisation")); specialty.setTrainingProgram(rs.getString("TrainingProgram")); specialty.setTrainingProgramYear(rs.getInt("TrainingProgramYear")); specialty.setMemo(rs.getString("Memo")); specialty.setStatus(rs.getString("SpecialtyStatus")); specialty.setIsbRole(1, rs.getString("ISBRole")); specialty.setIsbRole(2, rs.getString("SecondISBRole")); specialty.setIsbRole(3, rs.getString("ThirdISBRole")); specialty.setActive(rs.getBoolean("Active")); try { specialty.setCreatedDate(rs.getTimestamp("CreatedDate")); } catch (SQLException sqe) { dataLogger.debug("Error parsing CreatedDate: " + sqe.getMessage()); } specialty.setCreatedBy(rs.getString("CreatedBy")); try { specialty.setModifiedDate(rs.getTimestamp("ModifiedDate")); } catch (SQLException sqe) { dataLogger.debug("Error parsing ModifiedDate: " + sqe.getMessage()); } specialty.setModifiedBy(rs.getString("ModifiedBy")); return specialty; } }