com.sfs.dao.FinancialTypeDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.dao.FinancialTypeDAOImpl.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.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 javax.annotation.Resource;

import com.sfs.beans.FinancialTypeBean;
import com.sfs.beans.PrivilegesBean;
import com.sfs.beans.UserBean;

import org.apache.log4j.Logger;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;

/**
 * The Class FinancialTypeDAOImpl.
 */
public class FinancialTypeDAOImpl extends BaseDAOImpl implements FinancialTypeDAO {

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

    /** The user dao. */
    @Resource
    private UserDAO userDAO;

    /**
     * Load a collection of FinancialTypeBeans.
     *
     * @param type the type
     *
     * @return the collection< financial type bean>
     *
     * @throws SFSDaoException the SFS dao exception
     */
    @SuppressWarnings("unchecked")
    public final Collection<FinancialTypeBean> load(final String type) throws SFSDaoException {
        if (type == null) {
            throw new SFSDaoException("Error: type cannot be null");
        }
        if (type.compareTo("") == 0) {
            throw new SFSDaoException("Error: type cannot be an empty string");
        }

        Collection<FinancialTypeBean> listItems = new ArrayList<FinancialTypeBean>();

        try {
            listItems = this.getJdbcTemplateReader().query(this.getSQL().getValue("financialType/load"),
                    new Object[] { type }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadDetails(rs);
                        }
                    });

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

        return listItems;
    }

    /**
     * Load a single FinancialTypeBean for the supplied id.
     *
     * @param financialTypeId the financial type id
     *
     * @return the financial type bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final FinancialTypeBean load(final int financialTypeId) throws SFSDaoException {
        if (financialTypeId == 0) {
            throw new SFSDaoException("FinancialTypeId value cannot be 0");
        }

        FinancialTypeBean financialTypeBean = null;

        try {
            financialTypeBean = (FinancialTypeBean) this.getJdbcTemplateReader().queryForObject(
                    this.getSQL().getValue("financialType/loadId"), new Object[] { financialTypeId },
                    new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadDetails(rs);
                        }
                    });

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

        return financialTypeBean;
    }

    /**
     * Load a FinancialTypeBean for the supplied parameters.
     *
     * @param object the object
     * @param financialType the financial type
     * @param objectClass the object class
     *
     * @return the financial type bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final FinancialTypeBean load(final String object, final String financialType, final String objectClass)
            throws SFSDaoException {
        if (object == null) {
            throw new SFSDaoException("Finance Object definition cannot be null");
        }
        if (financialType == null) {
            throw new SFSDaoException("Finance Type cannot be null");
        }
        if (objectClass == null) {
            throw new SFSDaoException("Finance Class canot be null");
        }

        FinancialTypeBean financialTypeBean = null;

        try {
            financialTypeBean = (FinancialTypeBean) this.getJdbcTemplateReader().queryForObject(
                    this.getSQL().getValue("financialType/loadType"),
                    new Object[] { object, financialType, objectClass }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadDetails(rs);
                        }
                    });

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

    /**
     * Creates a new financial type.
     *
     * @param financialTypeBean the financial type bean
     * @param checkUser the check user
     * @param privileges the privileges
     *
     * @return true, if successful
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final boolean create(final FinancialTypeBean financialTypeBean, final UserBean checkUser,
            final PrivilegesBean privileges) throws SFSDaoException {
        boolean success = false;

        if (financialTypeBean.getObject() == null) {
            throw new SFSDaoException("FinancialType cannot have a null object type");
        }
        if (financialTypeBean.getName() == null) {
            throw new SFSDaoException("FinancialType cannot have a null name");
        }
        if (financialTypeBean.getClassName() == null) {
            throw new SFSDaoException("FinancialType cannot have a null class");
        }
        if (financialTypeBean.getObject().compareTo("") == 0) {
            throw new SFSDaoException("FinancialType cannot have an empty string for its object type");
        }
        if (financialTypeBean.getName().compareTo("") == 0) {
            throw new SFSDaoException("FinancialType cannot have an empty string for a name");
        }
        if (financialTypeBean.getClassName().compareTo("") == 0) {
            throw new SFSDaoException("FinancialType cannot have an empty string for a class");
        }
        if (!privileges.getPrivilege(checkUser, "objectType", "create")) {
            throw new SFSDaoException("Insufficient user credentials to create financial type");
        }

        Calendar calendar = Calendar.getInstance();
        Timestamp currentTime = new Timestamp(calendar.getTimeInMillis());

        final int insertCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("financialType/create"),
                new Object[] { financialTypeBean.getObject(), financialTypeBean.getName(),
                        financialTypeBean.getClassName(), financialTypeBean.getAbbreviation(),
                        financialTypeBean.getDescription(), financialTypeBean.getValue(),
                        financialTypeBean.getSecurity(), financialTypeBean.getLdapMapping(1),
                        financialTypeBean.getLdapMapping(2), financialTypeBean.getLdapMapping(3), currentTime,
                        checkUser.getDN() });

        if (insertCount > 0) {
            dataLogger.info(checkUser.getDN() + " successfully created financial type");
            success = true;
        }
        return success;
    }

    /**
     * Modify an existing financial type.
     *
     * @param financialTypeBean the financial type bean
     * @param checkUser the check user
     * @param privileges the privileges
     *
     * @return true, if successful
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final boolean modify(final FinancialTypeBean financialTypeBean, final UserBean checkUser,
            final PrivilegesBean privileges) throws SFSDaoException {
        boolean success = false;

        if (financialTypeBean.getFinancialTypeId() == 0) {
            throw new SFSDaoException("FinancialTypeId cannot be 0");
        }
        if (financialTypeBean.getName() == null) {
            throw new SFSDaoException("FinancialType cannot have a null name");
        }
        if (financialTypeBean.getClassName() == null) {
            throw new SFSDaoException("FinancialType cannot have a null class");
        }
        if (financialTypeBean.getName().compareTo("") == 0) {
            throw new SFSDaoException("FinancialType cannot have an empty string for a name");
        }
        if (financialTypeBean.getClassName().compareTo("") == 0) {
            throw new SFSDaoException("FinancialType cannot have an empty string for a class");
        }
        if (!privileges.getPrivilege(checkUser, "objectType", "modify")) {
            throw new SFSDaoException("Insufficient user credentials to edit financial type");
        }

        Calendar calendar = Calendar.getInstance();
        Timestamp currentTime = new Timestamp(calendar.getTimeInMillis());

        final int updateCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("financialType/modify"),
                new Object[] { financialTypeBean.getName(), financialTypeBean.getClassName(),
                        financialTypeBean.getAbbreviation(), financialTypeBean.getDescription(),
                        financialTypeBean.getValue(), financialTypeBean.getSecurity(),
                        financialTypeBean.getLdapMapping(1), financialTypeBean.getLdapMapping(2),
                        financialTypeBean.getLdapMapping(3), currentTime, checkUser.getDN(),
                        financialTypeBean.getFinancialTypeId() });

        if (updateCount > 0) {
            dataLogger.info(checkUser.getDN() + " successfully modified financial type");
            success = true;
        }
        return success;
    }

    /**
     * Delete a financial type.
     *
     * @param financialTypeBean the financial type bean
     * @param checkUser the check user
     * @param privileges the privileges
     *
     * @return true, if successful
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final boolean delete(final FinancialTypeBean financialTypeBean, final UserBean checkUser,
            final PrivilegesBean privileges) throws SFSDaoException {
        boolean success = false;

        if (financialTypeBean.getFinancialTypeId() == 0) {
            throw new SFSDaoException("FinancialType requires a vaild FinancialTypeId number");
        }
        if (!privileges.getPrivilege(checkUser, "objectType", "delete")) {
            throw new SFSDaoException("Insufficient user credentials to delete financial type");
        }

        final int deleteCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("financialType/delete"),
                new Object[] { financialTypeBean.getFinancialTypeId() });

        if (deleteCount > 0) {
            dataLogger.info(checkUser.getDN() + " successfully deleted financial type");
            success = true;
        }
        return success;
    }

    /**
     * Load details.
     *
     * @param rs the rs
     *
     * @return the financial type bean
     *
     * @throws SQLException the SQL exception
     */
    private FinancialTypeBean loadDetails(final ResultSet rs) throws SQLException {
        FinancialTypeBean financialType = new FinancialTypeBean();

        financialType.setFinancialTypeId(rs.getInt("FinancialTypeId"));
        financialType.setObject(rs.getString("Object"));
        financialType.setName(rs.getString("Name"));
        financialType.setClassName(rs.getString("Class"));
        financialType.setAbbreviation(rs.getString("Abbreviation"));
        financialType.setSecurity(rs.getString("Security"));
        financialType.setLdapMapping(1, rs.getString("LdapMapping"));
        financialType.setLdapMapping(2, rs.getString("SecondLdapMapping"));
        financialType.setLdapMapping(3, rs.getString("ThirdLdapMapping"));
        financialType.setValue(rs.getDouble("Value"));
        financialType.setDescription(rs.getString("Description"));
        financialType.setCreated(rs.getTimestamp("Created"));
        financialType.setCreatedBy(rs.getString("CreatedBy"));
        financialType.setModified(rs.getTimestamp("Modified"));
        financialType.setModifiedBy(rs.getString("ModifiedBy"));

        if (financialType.getCreatedBy() != null && this.userDAO != null) {
            try {
                financialType.setCreatedByUser(this.userDAO.loadCached(financialType.getCreatedBy()));
            } catch (Exception e) {
                dataLogger.info("Could not load CreatedBy UserBean for FinancialTypeId = "
                        + financialType.getFinancialTypeId());
            }
        }
        if (financialType.getModifiedBy() != null && this.userDAO != null) {
            try {
                financialType.setModifiedByUser(this.userDAO.loadCached(financialType.getModifiedBy()));
            } catch (Exception e) {
                dataLogger.info("Could not load ModifiedBy UserBean for FinancialTypeId = "
                        + financialType.getFinancialTypeId());
            }
        }
        return financialType;
    }
}