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.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.TreeMap; 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.BuilderBean; import com.sfs.beans.ObjectTypeBean; import com.sfs.beans.PrivilegesBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.AccreditationBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.RotationBean; /** * The Class AccreditationDAOImpl. */ public class AccreditationDAOImpl extends WhichDoctorBaseDAOImpl implements AccreditationDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(AccreditationDAOImpl.class); /** The assessment dao. */ @Resource private AssessmentDAO assessmentDAO; /** The membership dao. */ @Resource private MembershipDAO membershipDAO; /** The person dao. */ @Resource private PersonDAO personDAO; /** The rotation dao. */ @Resource private RotationDAO rotationDAO; /** * Load a collection of AccreditationBeans. * * @param guid the guid * @param fullResults the full results * * @return the collection< accreditation bean> * * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final Collection<AccreditationBean> load(final int guid, final boolean fullResults) throws WhichDoctorDaoException { dataLogger.info("Accreditations for GUID: " + guid + " requested"); final String loadAccreditation = getSQL().getValue("accreditation/load") + " WHERE accreditation.Active = true" + " AND accreditation.ReferenceGUID = ?" + " ORDER BY accreditation.Core DESC," + " specialtytype.Class ASC, specialtytype.Name ASC"; Collection<AccreditationBean> accreditations = new ArrayList<AccreditationBean>(); try { accreditations = this.getJdbcTemplateReader().query(loadAccreditation, new Object[] { guid }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadAccreditation(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } return accreditations; } /** * Load the AccreditationBean. * * @param accreditationId the accreditation id * * @return the accreditation bean * * @throws WhichDoctorDaoException the which doctor dao exception */ public final AccreditationBean load(final int accreditationId) throws WhichDoctorDaoException { dataLogger.info("Getting accreditationId:" + accreditationId); final String loadAccreditationId = getSQL().getValue("accreditation/load") + " WHERE accreditation.AccreditationId = ?"; AccreditationBean accreditation = null; try { accreditation = (AccreditationBean) this.getJdbcTemplateReader().queryForObject(loadAccreditationId, new Object[] { accreditationId }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { return loadAccreditation(rs); } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } return accreditation; } /** * Creates the AccreditationBean. * * @param accreditation the accreditation * @param checkUser the check user * @param privileges the privileges * * @return the int * * @throws WhichDoctorDaoException the which doctor dao exception */ public final int create(final AccreditationBean accreditation, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { accreditation.setActive(true); return save(accreditation, checkUser, privileges, "create"); } /** * Modify the AccreditationBean. * * @param accreditation the accreditation * @param checkUser the check user * @param privileges the privileges * * @return the int * * @throws WhichDoctorDaoException the which doctor dao exception */ public final int modify(final AccreditationBean accreditation, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { accreditation.setActive(true); return save(accreditation, checkUser, privileges, "modify"); } /** * Delete the AccreditationBean. * * @param accreditation the accreditation * @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 AccreditationBean accreditation, final UserBean checkUser, final PrivilegesBean privileges) throws WhichDoctorDaoException { boolean success = false; accreditation.setActive(false); int deleted = save(accreditation, checkUser, privileges, "delete"); if (deleted > 0) { success = true; } return success; } /** * Save. * * @param accreditation the accreditation * @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 AccreditationBean accreditation, final UserBean checkUser, final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException { // Check required information within accreditation bean is present if (accreditation.getReferenceGUID() == 0) { throw new WhichDoctorDaoException("Accreditation requires a valid Reference GUID"); } if (!privileges.getPrivilege(checkUser, "accreditations", action)) { throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " accreditation"); } // Variable for holding result int accreditationId = 0; int accreditationTypeId = 0; int specialtyTypeId = 0; try { ObjectTypeBean object = this.getObjectTypeDAO().load("Accreditation Type", accreditation.getAccreditationType(), accreditation.getAccreditationClass()); accreditationTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for accreditation type: " + e.getMessage()); } try { ObjectTypeBean object = this.getObjectTypeDAO().load("Accredited Specialty", accreditation.getSpecialtySubType(), accreditation.getSpecialtyType()); specialtyTypeId = object.getObjectTypeId(); } catch (Exception e) { dataLogger.error("Error loading objecttype for accredited specialty: " + e.getMessage()); } // Get accreditation SQL Date object and timestamp Date accreditationDate = null; if (accreditation.getAccreditationDate() != null) { accreditationDate = new Date(accreditation.getAccreditationDate().getTime()); } Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis()); ArrayList<Object> parameters = new ArrayList<Object>(); parameters.add(accreditation.getReferenceGUID()); parameters.add(accreditation.getCore()); parameters.add(accreditationTypeId); parameters.add(specialtyTypeId); parameters.add(accreditation.getWeeksApproved()); parameters.add(accreditation.getWeeksCertified()); parameters.add(accreditation.getNote()); parameters.add(accreditationDate); parameters.add(accreditation.getActive()); parameters.add(sqlTimeStamp); parameters.add(checkUser.getDN()); parameters.add(accreditation.getLogMessage(action)); try { Integer[] result = this.performUpdate("accreditation", accreditation.getGUID(), parameters, "Accreditation", checkUser, action); /* Set the returned guid and id values */ accreditation.setGUID(result[0]); accreditationId = result[1]; } catch (Exception e) { dataLogger.error("Error processing accreditation record: " + e.getMessage()); throw new WhichDoctorDaoException("Error processing accreditation record: " + e.getMessage()); } if (accreditationId > 0) { updateIndeces(accreditation.getReferenceGUID(), checkUser, privileges); } return accreditationId; } /** * Gets the training summary. * * @param guid the guid * @param type the type * * @return the training summary * * @throws WhichDoctorDaoException the which doctor dao exception */ @SuppressWarnings("unchecked") public final TreeMap<String, AccreditationBean[]> getTrainingSummary(final int guid, final String type) throws WhichDoctorDaoException { if (type == null) { throw new NullPointerException("Training type cannot be null"); } dataLogger.info("Getting " + type + " Training Summary for Member GUID: " + guid); TreeMap<String, AccreditationBean[]> summary = new TreeMap<String, AccreditationBean[]>(); Collection<AccreditationBean> accreditations = new ArrayList<AccreditationBean>(); try { accreditations = this.getJdbcTemplateReader().query(this.getSQL().getValue("accreditation/loadSummary"), new Object[] { guid, type }, new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { AccreditationBean accreditation = new AccreditationBean(); accreditation.setAbbreviation(rs.getString("AccreditationTypeAbbreviation")); accreditation.setAccreditationType(rs.getString("AccreditationType")); accreditation.setSpecialtyType(rs.getString("SpecialtyTypeClass")); accreditation.setSpecialtySubType(rs.getString("SpecialtyTypeName")); accreditation.setSpecialtyTypeAbbreviation(rs.getString("SpecialtyTypeAbbreviation")); accreditation.setCore(rs.getBoolean("Core")); accreditation.setWeeksApproved(rs.getInt("WeeksApproved")); accreditation.setWeeksCertified(rs.getInt("WeeksCertified")); // The active flag holds whether the accreditation is excess boolean active = true; String trainingClass = rs.getString("TrainingClass"); if (StringUtils.contains(trainingClass, "nterrupted") || StringUtils.contains(trainingClass, "ontinuing")) { active = false; } accreditation.setActive(active); return accreditation; } }); } catch (IncorrectResultSizeDataAccessException ie) { dataLogger.debug("No results found for search: " + ie.getMessage()); } for (AccreditationBean acrd : accreditations) { if (acrd.getActive()) { // Generate index key String specialtyAbbreviation = acrd.getAccreditationType(); String specialtyTypeName = acrd.getSpecialtyType(); if (StringUtils.isNotBlank(acrd.getAbbreviation())) { specialtyAbbreviation = acrd.getAbbreviation(); } if (StringUtils.isNotBlank(acrd.getSpecialtySubType())) { specialtyTypeName = acrd.getSpecialtyType() + " - " + acrd.getSpecialtySubType(); } String specialtyKey = specialtyAbbreviation + ": " + specialtyTypeName; AccreditationBean core = new AccreditationBean(); core.setAbbreviation(acrd.getAbbreviation()); core.setAccreditationType(acrd.getAccreditationType()); core.setCore(true); core.setSpecialtyType(acrd.getSpecialtyType()); core.setSpecialtySubType(acrd.getSpecialtySubType()); core.setSpecialtyTypeAbbreviation(acrd.getSpecialtyTypeAbbreviation()); AccreditationBean nonCore = new AccreditationBean(); nonCore.setAbbreviation(acrd.getAbbreviation()); nonCore.setAccreditationType(acrd.getAccreditationType()); nonCore.setCore(false); nonCore.setSpecialtyType(acrd.getSpecialtyType()); nonCore.setSpecialtySubType(acrd.getSpecialtySubType()); nonCore.setSpecialtyTypeAbbreviation(acrd.getSpecialtyTypeAbbreviation()); if (summary.containsKey(specialtyKey)) { // Specialty exists in TreeMap -> Get array and modify try { AccreditationBean[] existing = summary.get(specialtyKey); core = existing[0]; nonCore = existing[1]; } catch (Exception e) { dataLogger.error("Error loading existing training summary item: " + e.getMessage()); } } // Add to the relevant core/nonCore running totals if (acrd.getCore()) { core.setWeeksApproved(core.getWeeksApproved() + acrd.getWeeksApproved()); core.setWeeksCertified(core.getWeeksCertified() + acrd.getWeeksCertified()); } else { nonCore.setWeeksApproved(nonCore.getWeeksApproved() + acrd.getWeeksApproved()); nonCore.setWeeksCertified(nonCore.getWeeksCertified() + acrd.getWeeksCertified()); } // Set accreditation details AccreditationBean[] details = new AccreditationBean[] { core, nonCore }; // Add accreditation to map summary.put(specialtyKey, details); } } return summary; } /** * Update the accreditation summary, training status and training year indeces. * * @param rotationGUID the rotation guid */ private void updateIndeces(final int rotationGUID, final UserBean checkUser, final PrivilegesBean privileges) { RotationBean rotation = null; try { rotation = this.rotationDAO.loadGUID(rotationGUID); } catch (Exception e) { dataLogger.error("Error loading person related to accreditation: " + e.getMessage()); } if (rotation != null) { // Update the relevant indeces try { this.rotationDAO.recalculateAccreditationForPerson(rotation.getPersonId()); } catch (Exception e) { dataLogger.error("Error updating accreditation summary: " + e.getMessage()); } try { this.membershipDAO.updateTrainingStatus(rotation.getPersonId(), checkUser, privileges); } catch (Exception e) { dataLogger.error("Error updating training status: " + e.getMessage()); } PersonBean person = null; BuilderBean loadDetails = new BuilderBean(); loadDetails.setParameter("TRAINING_ROTATIONS", true); try { person = this.personDAO.loadGUID(rotation.getPersonId(), loadDetails); } catch (WhichDoctorDaoException wde) { dataLogger.error("Error loading person: " + wde.getMessage()); } if (person != null && person.getRotations() != null) { for (RotationBean rtn : person.getRotations()) { try { this.assessmentDAO.recalculateYearsOfTraining(rtn); } catch (Exception e) { dataLogger.error("Error updating year of training for" + " rotation GUID " + rtn.getGUID() + ": " + e.getMessage()); } } } } } /** * Load accreditation. * * @param rs the rs * * @return the accreditation bean * * @throws SQLException the SQL exception */ private AccreditationBean loadAccreditation(final ResultSet rs) throws SQLException { AccreditationBean accreditation = new AccreditationBean(); accreditation.setId(rs.getInt("AccreditationId")); accreditation.setGUID(rs.getInt("GUID")); accreditation.setReferenceGUID(rs.getInt("ReferenceGUID")); accreditation.setCore(rs.getBoolean("Core")); accreditation.setAccreditationClass(rs.getString("AccreditationClass")); accreditation.setAccreditationType(rs.getString("AccreditationType")); accreditation.setAbbreviation(rs.getString("AccreditationTypeAbbreviation")); accreditation.setSpecialtyType(rs.getString("SpecialtyTypeClass")); accreditation.setSpecialtyTypeAbbreviation(rs.getString("SpecialtyTypeAbbreviation")); accreditation.setSpecialtySubType(rs.getString("SpecialtyTypeName")); accreditation.setWeeksApproved(rs.getInt("WeeksApproved")); accreditation.setWeeksCertified(rs.getInt("WeeksCertified")); accreditation.setNote(rs.getString("Note")); try { accreditation.setAccreditationDate(rs.getDate("AccreditationDate")); } catch (Exception e) { dataLogger.debug("Error parsing AccreditationDate: " + e.getMessage()); } accreditation.setActive(rs.getBoolean("Active")); try { accreditation.setCreatedDate(rs.getTimestamp("CreatedDate")); } catch (SQLException e) { dataLogger.debug("Error parsing CreatedDate: " + e.getMessage()); } accreditation.setCreatedBy(rs.getString("CreatedBy")); try { accreditation.setModifiedDate(rs.getTimestamp("ModifiedDate")); } catch (SQLException e) { dataLogger.debug("Error parsing ModifiedDate: " + e.getMessage()); } accreditation.setModifiedBy(rs.getString("ModifiedBy")); try { accreditation.setExportedDate(rs.getTimestamp("ExportedDate")); } catch (SQLException e) { dataLogger.debug("Error parsing ExportedDate: " + e.getMessage()); } accreditation.setExportedBy(rs.getString("ExportedBy")); return accreditation; } }