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

Java tutorial

Introduction

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

import javax.annotation.Resource;

import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;

import com.sfs.dao.FinancialTypeDAO;

/**
 * The Class WhichDoctorFinancialObjectDAOImpl.
 *
 * @author David Harrison
 */
public class WhichDoctorFinancialObjectDAOImpl extends WhichDoctorCoreObjectDAOImpl {

    /** The financial type dao. */
    @Resource
    protected FinancialTypeDAO financialTypeDAO;

    /**
     * Check number.
     *
     * @param type the type
     * @param existingNumber the existing number
     * @param issuedDate the issued date
     * @return the string
     */
    protected String checkNumber(final String type, final String existingNumber, final Date issuedDate) {

        String number = "";

        /* Does a receipt number need to be generated */
        Date issued = Calendar.getInstance().getTime();
        if (issuedDate != null) {
            issued = issuedDate;
        }

        boolean generateNumber = true;
        if (existingNumber != null) {
            if (existingNumber.compareTo("") != 0) {
                generateNumber = false;
            }
        }

        if (generateNumber) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy");
            int intNumber = 0;
            String strNumber = "";

            try {
                strNumber = (String) this.getJdbcTemplateReader().queryForObject(
                        this.getSQL().getValue(type + "/findMaxNumber"),
                        new Object[] { simpleDateFormat.format(issued) + "%" }, new RowMapper() {
                            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                                return rs.getString(1);
                            }
                        });

            } catch (IncorrectResultSizeDataAccessException ie) {
                // No results found for this search
            }

            if (strNumber != null) {
                strNumber = strNumber.substring(2);
                intNumber = Integer.parseInt(strNumber);
            } else {
                intNumber = 0;
            }

            strNumber = String.valueOf(intNumber + 1);
            while (strNumber.length() < 4) {
                strNumber = "0" + strNumber;
            }
            number = simpleDateFormat.format(issued) + strNumber;

        } else {
            number = existingNumber;
        }

        return number;
    }

}