com.sfs.whichdoctor.dao.QualificationDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.dao.QualificationDAOImpl.java

Source

/*******************************************************************************
 * 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.Calendar;
import java.util.Collection;

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.QualificationBean;

/**
 * The Class QualificationDAOImpl.
 */
public class QualificationDAOImpl extends WhichDoctorBaseDAOImpl implements QualificationDAO {

    /** The data logger. */
    private static Logger dataLogger = Logger.getLogger(QualificationDAOImpl.class);

    /** The default qualification type. */
    private String defaultQualificationType;

    /** The default qualification sub type. */
    private String defaultQualificationSubType;

    /** The default country. */
    private String defaultCountry;

    /**
     * Sets the default qualification type.
     *
     * @param qualificationType the new default qualification type
     */
    public final void setDefaultQualificationType(final String qualificationType) {
        this.defaultQualificationType = qualificationType;
    }

    /**
     * Sets the default qualification sub type.
     *
     * @param qualificationSubType the new default qualification sub type
     */
    public final void setDefaultQualificationSubType(final String qualificationSubType) {
        this.defaultQualificationSubType = qualificationSubType;
    }

    /**
     * Sets the default country.
     *
     * @param country the new default country
     */
    public final void setDefaultCountry(final String country) {
        this.defaultCountry = country;
    }

    /**
     * Used to ge an Collection of QualificationBean details 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<QualificationBean> load(final int guid, final boolean fullResults)
            throws WhichDoctorDaoException {

        dataLogger.info("Qualifications for GUID: " + guid + " requested");

        final String loadQualifications = getSQL().getValue("qualification/load")
                + " WHERE qualification.Active = true AND qualification.ReferenceGUID = ?"
                + " ORDER BY qualification.Year ASC";

        Collection<QualificationBean> qualifications = new ArrayList<QualificationBean>();

        try {
            qualifications = this.getJdbcTemplateReader().query(loadQualifications, new Object[] { guid },
                    new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadQualification(rs);
                        }
                    });

        } catch (IncorrectResultSizeDataAccessException ie) {
            dataLogger.debug("No results found for this search: " + ie.getMessage());
        }
        return qualifications;
    }

    /**
     * Load the qualification bean.
     *
     * @param qualificationId the qualification id
     * @return the qualification bean
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final QualificationBean load(final int qualificationId) throws WhichDoctorDaoException {

        dataLogger.info("Getting qualificationId:" + qualificationId);

        final String loadQualificationId = getSQL().getValue("qualification/load")
                + " WHERE qualification.QualificationId = ?";

        QualificationBean qualification = null;

        try {
            qualification = (QualificationBean) this.getJdbcTemplateReader().queryForObject(loadQualificationId,
                    new Object[] { qualificationId }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadQualification(rs);
                        }
                    });

        } catch (IncorrectResultSizeDataAccessException ie) {
            dataLogger.debug("No results found for this search: " + ie.getMessage());
        }
        return qualification;
    }

    /**
     * Creates the qualification bean.
     *
     * @param qualification the qualification
     * @param checkUser the check user
     * @param privileges the privileges
     * @return the int
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final int create(final QualificationBean qualification, final UserBean checkUser,
            final PrivilegesBean privileges) throws WhichDoctorDaoException {

        qualification.setActive(true);
        return save(qualification, checkUser, privileges, "create");
    }

    /**
     * Modify the qualification bean.
     *
     * @param qualification the qualification
     * @param checkUser the check user
     * @param privileges the privileges
     * @return the int
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final int modify(final QualificationBean qualification, final UserBean checkUser,
            final PrivilegesBean privileges) throws WhichDoctorDaoException {

        qualification.setActive(true);
        return save(qualification, checkUser, privileges, "modify");
    }

    /**
     * Delete the qualification bean.
     *
     * @param qualification the qualification
     * @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 QualificationBean qualification, final UserBean checkUser,
            final PrivilegesBean privileges) throws WhichDoctorDaoException {

        boolean success = false;

        qualification.setActive(false);
        int deleteId = save(qualification, checkUser, privileges, "delete");
        if (deleteId > 0) {
            success = true;
        }
        return success;
    }

    /**
     * Gets a new qualification bean with the default settings.
     *
     * @return the default qualification bean
     */
    public final QualificationBean getDefaultBean() {

        QualificationBean qualification = new QualificationBean();

        qualification.setQualificationType(this.defaultQualificationType);
        qualification.setQualificationSubType(this.defaultQualificationSubType);
        qualification.setCountry(this.defaultCountry);

        return qualification;
    }

    /**
     * Save the qualification bean.
     *
     * @param qualification the qualification
     * @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 QualificationBean qualification, final UserBean checkUser,
            final PrivilegesBean privileges, final String action) throws WhichDoctorDaoException {

        /* Check required information within qualification bean is present */
        if (qualification.getReferenceGUID() == 0) {
            throw new WhichDoctorDaoException("Qualification requires a valid Reference GUID");
        }
        if (!privileges.getPrivilege(checkUser, "qualifications", action)) {
            throw new WhichDoctorDaoException(
                    "Insufficient user credentials to " + action + " qualification entry");
        }

        int qualificationTypeId = 0;
        try {
            ObjectTypeBean object = this.getObjectTypeDAO().load("Qualification",
                    qualification.getQualificationSubType(), qualification.getQualificationType());
            qualificationTypeId = object.getObjectTypeId();
        } catch (Exception e) {
            dataLogger.error("Error loading objecttype for qualification type: " + e.getMessage());
            throw new WhichDoctorDaoException("Qualification requires a valid type");
        }

        int qualificationId = 0;

        Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis());

        ArrayList<Object> parameters = new ArrayList<Object>();
        parameters.add(qualification.getReferenceGUID());
        parameters.add(qualification.getQualification());
        parameters.add(qualification.getInstitution());
        parameters.add(qualification.getHonor());
        parameters.add(qualificationTypeId);
        parameters.add(qualification.getYear());
        parameters.add(qualification.getCountry());
        parameters.add(qualification.getActive());
        parameters.add(sqlTimeStamp);
        parameters.add(checkUser.getDN());
        parameters.add(qualification.getLogMessage(action));

        try {
            Integer[] result = this.performUpdate("qualification", qualification.getGUID(), parameters,
                    "Qualification", checkUser, action);
            /* Set the returned guid and id values */
            qualification.setGUID(result[0]);
            qualificationId = result[1];
        } catch (Exception e) {
            dataLogger.error("Error processing qualification record: " + e.getMessage());
            throw new WhichDoctorDaoException("Error processing qualification record: " + e.getMessage());
        }
        return qualificationId;
    }

    /**
     * Load the qualification bean from the result set.
     *
     * @param rs the rs
     * @return the qualification bean
     * @throws SQLException the sQL exception
     */
    private QualificationBean loadQualification(final ResultSet rs) throws SQLException {

        QualificationBean qualification = new QualificationBean();

        qualification.setId(rs.getInt("QualificationId"));
        qualification.setGUID(rs.getInt("GUID"));
        qualification.setReferenceGUID(rs.getInt("ReferenceGUID"));
        qualification.setQualification(rs.getString("Qualification"));
        qualification.setQualificationType(rs.getString("QualificationType"));
        qualification.setQualificationSubType(rs.getString("QualificationSubType"));
        qualification.setInstitution(rs.getString("Institution"));
        qualification.setHonor(rs.getString("Honor"));
        qualification.setYear(rs.getInt("Year"));
        qualification.setCountry(rs.getString("Country"));

        qualification.setActive(rs.getBoolean("Active"));
        try {
            qualification.setCreatedDate(rs.getTimestamp("CreatedDate"));
        } catch (SQLException sqe) {
            dataLogger.debug("Error loading CreatedDate: " + sqe.getMessage());
        }
        qualification.setCreatedBy(rs.getString("CreatedBy"));
        try {
            qualification.setModifiedDate(rs.getTimestamp("ModifiedDate"));
        } catch (SQLException sqe) {
            dataLogger.debug("Error loading ModifiedDate: " + sqe.getMessage());
        }
        qualification.setModifiedBy(rs.getString("ModifiedBy"));

        return qualification;
    }
}