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

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.dao.WorkshopDAOImpl.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.Date;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Calendar;

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

/**
 * The Class WorkshopDAOImpl.
 */
public class WorkshopDAOImpl extends WhichDoctorBaseDAOImpl implements WorkshopDAO {

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

    /**
     * Used to get a Collection of WorkshopBeans 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<WorkshopBean> load(final int guid, final boolean fullResults)
            throws WhichDoctorDaoException {

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

        final String loadWorkshops = getSQL().getValue("workshop/load")
                + " WHERE workshop.Active = true AND workshop.ReferenceGUID = ?" + " ORDER BY workshop.Date";

        Collection<WorkshopBean> workshops = new ArrayList<WorkshopBean>();

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

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

    /**
     * Used to get a WorkshopBean for a workshop Id.
     *
     * @param workshopId the workshop id
     * @return the workshop bean
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final WorkshopBean load(final int workshopId) throws WhichDoctorDaoException {

        dataLogger.info("WorkshopId: " + workshopId + " requested");

        final String loadId = getSQL().getValue("workshop/load") + " WHERE workshop.WorkshopId = ?";

        WorkshopBean workshop = null;

        try {
            workshop = (WorkshopBean) this.getJdbcTemplateReader().queryForObject(loadId,
                    new Object[] { workshopId }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadWorkshop(rs, true);
                        }
                    });

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

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

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

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

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

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

        boolean success = false;

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

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

        /* Check required information within Memo bean is present */
        if (workshop.getWorkshopDate() == null) {
            throw new WhichDoctorDaoException("Workshop requires a valid date");
        }
        if (workshop.getType() == null) {
            throw new WhichDoctorDaoException("Workshop type cannot be null");
        }
        if (workshop.getType().compareTo("") == 0) {
            throw new WhichDoctorDaoException("Workshop type cannot be an empty string");
        }
        if (workshop.getReferenceGUID() == 0) {
            throw new WhichDoctorDaoException("Workshop requires a valid Reference GUID number");
        }
        if (!privileges.getPrivilege(checkUser, "workshops", action)) {
            throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " workshop entry");
        }

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

        int workshopId = 0;

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

        ArrayList<Object> parameters = new ArrayList<Object>();
        Date workshopDate = null;
        if (workshop.getWorkshopDate() != null) {
            workshopDate = new Date(workshop.getWorkshopDate().getTime());
        }

        parameters.add(workshop.getReferenceGUID());
        parameters.add(workshopTypeId);
        parameters.add(workshopDate);
        parameters.add(workshop.getMemo());
        parameters.add(workshop.getActive());
        parameters.add(sqlTimeStamp);
        parameters.add(checkUser.getDN());
        parameters.add(workshop.getLogMessage(action));

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

        return workshopId;
    }

    /**
     * Load workshop.
     *
     * @param rs the rs
     * @param fullResults the full results
     * @return the workshop bean
     * @throws SQLException the SQL exception
     */
    private WorkshopBean loadWorkshop(final ResultSet rs, final boolean fullResults) throws SQLException {

        WorkshopBean workshop = new WorkshopBean();

        workshop.setId(rs.getInt("WorkshopId"));
        workshop.setGUID(rs.getInt("GUID"));
        workshop.setReferenceGUID(rs.getInt("ReferenceGUID"));
        workshop.setType(rs.getString("Type"));
        try {
            workshop.setWorkshopDate(rs.getDate("Date"));
        } catch (SQLException e) {
            workshop.setWorkshopDate(null);
        }
        workshop.setMemo(rs.getString("Memo"));

        workshop.setActive(rs.getBoolean("Active"));

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

        return workshop;
    }
}