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

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.dao.ExpenseClaimDAOImpl.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.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.ExpenseClaimBean;

/**
 * The Class ExpenseClaimDAOImpl.
 */
public class ExpenseClaimDAOImpl extends WhichDoctorBaseDAOImpl implements ExpenseClaimDAO {

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

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

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

        final String loadSQL = getSQL().getValue("expenseclaim/load")
                + " WHERE expenseclaim.Active = true AND expenseclaim.ReferenceGUID = ?"
                + " ORDER BY expenseclaim.ExpenseDate";

        Collection<ExpenseClaimBean> expenseClaims = new ArrayList<ExpenseClaimBean>();

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

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

    /**
     * Load the expense claim bean.
     *
     * @param expenseClaimId the expense claim id
     * @return the expense claim bean
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final ExpenseClaimBean load(final int expenseClaimId) throws WhichDoctorDaoException {

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

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

        ExpenseClaimBean expenseClaim = null;
        try {
            expenseClaim = (ExpenseClaimBean) this.getJdbcTemplateReader().queryForObject(loadId,
                    new Object[] { expenseClaimId }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadExpenseClaim(rs);
                        }
                    });

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

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

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

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

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

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

        boolean success = false;

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

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

        /* Create expense claim requires all the essential information */
        if (expenseClaim.getReferenceGUID() == 0) {
            throw new WhichDoctorDaoException("Expense claim requires a valid Reference GUID");
        }
        if (!privileges.getPrivilege(checkUser, "expenseclaims", action)) {
            throw new WhichDoctorDaoException("Insufficient user credentials to " + action + " expense claim");
        }

        int expenseClaimTypeId = 0;
        try {
            ObjectTypeBean object = this.getObjectTypeDAO().load("Expense Claim", expenseClaim.getExpenseType(),
                    expenseClaim.getExpenseClass());
            expenseClaimTypeId = object.getObjectTypeId();
        } catch (Exception e) {
            dataLogger.error("Error loading objecttype for expense claim: " + e.getMessage());
            throw new WhichDoctorDaoException("Expense claim requires a valid expense claim type");
        }

        int expenseClaimId = 0;

        Timestamp sqlTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        Date expenseDate = new Date(Calendar.getInstance().getTimeInMillis());
        if (expenseClaim.getExpenseDate() != null) {
            expenseDate = new Date(expenseClaim.getExpenseDate().getTime());
        }
        ArrayList<Object> parameters = new ArrayList<Object>();
        parameters.add(expenseClaim.getReferenceGUID());
        parameters.add(expenseClaimTypeId);
        parameters.add(expenseClaim.getCurrencyAbbreviation());
        parameters.add(expenseDate);
        parameters.add(expenseClaim.getExchangeRate());
        parameters.add(expenseClaim.getDescription());
        parameters.add(expenseClaim.getRate());
        parameters.add(expenseClaim.getQuantity());
        parameters.add(expenseClaim.getGSTRate());
        parameters.add(expenseClaim.getValue());
        parameters.add(expenseClaim.getNetValue());
        parameters.add(expenseClaim.getActive());
        parameters.add(sqlTimeStamp);
        parameters.add(checkUser.getDN());
        parameters.add(expenseClaim.getLogMessage(action));

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

        return expenseClaimId;
    }

    /**
     * Load expense claim.
     *
     * @param rs the rs
     * @return the expense claim bean
     * @throws SQLException the sQL exception
     */
    private ExpenseClaimBean loadExpenseClaim(final ResultSet rs) throws SQLException {

        ExpenseClaimBean expenseClaim = new ExpenseClaimBean();

        expenseClaim.setId(rs.getInt("ExpenseClaimId"));
        expenseClaim.setGUID(rs.getInt("GUID"));
        expenseClaim.setReferenceGUID(rs.getInt("ReferenceGUID"));
        expenseClaim.setExpenseClass(rs.getString("ExpenseClass"));
        expenseClaim.setExpenseType(rs.getString("ExpenseType"));
        expenseClaim.setCurrencyAbbreviation(rs.getString("CurrencyAbbreviation"));
        try {
            expenseClaim.setExpenseDate(rs.getDate("ExpenseDate"));
        } catch (SQLException sqe) {
            dataLogger.debug("Error loading ExpenseDate: " + sqe.getMessage());
        }
        expenseClaim.setExchangeRate(rs.getDouble("ExchangeRate"));
        expenseClaim.setDescription(rs.getString("Description"));
        expenseClaim.setRate(rs.getDouble("Rate"));
        expenseClaim.setQuantity(rs.getInt("Quantity"));
        expenseClaim.setGSTRate(rs.getDouble("GSTRate"));
        expenseClaim.setValue(rs.getDouble("Value"));
        expenseClaim.setNetValue(rs.getDouble("NetValue"));

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

        return expenseClaim;
    }
}