dao.LogtabDaoJdbc.java Source code

Java tutorial

Introduction

Here is the source code for dao.LogtabDaoJdbc.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Vector;
import javax.sql.DataSource;
import model.Logtab;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import util.DbUtils;

/**
  * This object implements LogtabDao
  *
  * @author Smitha Gudur (smitha@redbasin.com)
  * @version $Revision: 1.1 $
  */
public class LogtabDaoJdbc implements LogtabDao {

    private DataSource ds;
    protected final Log logger = LogFactory.getLog(getClass());

    /**
      *  This method returns logtab bean for a given hotdiary login
      *  @param login
      *  @return Logtab
      *  @throws BaseDaoException
      */
    public Logtab getLogtab(String login) throws BaseDaoException {

        logger.info("LogtabDaoJdbc, getLogtab() login!" + login);

        LogtabQuery query = new LogtabQuery(ds);
        Object[] params = new Object[1];
        params[0] = login;
        List result = query.execute(params);
        // for guestbook entries multiple rows are returned.
        if (result.size() == 1) {
            return (Logtab) result.get(0);
        } else {
            // TODO: add a method to get toString() for List. 
            logger.error("Multiple logtab beans returned. Only one bean is expected to be returned!" + login
                    + "Logtab beans returned = " + result.toString());
            return null;
        }
    }

    /**
      *  This property is setby spring automatically at web.xml startup
      *  @param ds - this is JDBC datasource bean that is connection from the pool
      */
    public void setJdbcSource(DataSource ds) {
        this.ds = ds;
    }

    /**
      *  This inline class is used by spring to populate a <code>Logtab</code> bean.
      */
    class LogtabQuery extends MappingSqlQuery {

        Vector columnNames;
        Vector columnTypes;

        /**
         * This constructor is called when the Logtab's getLogtab() method makes a 
         * call to query.execute(). After the query is executed, the result set is
         * returned. For each row in the result set, the mapRow method is called by
         * Spring. In the very first call to mapRow() for the first row in the result
         * set, we make a call to RSMD to get columnNames and columnTypes and cache
         * them to a local array in this object. This way, we can avoid multiple calls
         * to RSMD since, spring calls mapRow many times (one per row in result set).
         *
         */
        LogtabQuery(DataSource ds) {
            super(ds, "select * " + "from logtab " + "where login = ?");
            declareParameter(new SqlParameter("login", Types.VARCHAR));
            compile();
        }

        /**
         *  Spring calls this method for each record found in the resultset.
         *  This method is called only once becos there should be only one matching login.
         *  @param rs - result set
         *  @param rowNum - number of rows
         *  @returns Object
         *  @throws SQLException
         */
        protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {

            logger.info("mapRow rowNum = " + rowNum);

            if (rowNum == 0) {
                // get column names and types from rsmd and save to local array in object
                DbUtils dbutils = new DbUtils();
                columnNames = dbutils.getColumnNames(rs);
                // columnTypes = dbutils.getColumnTypes(rs); 
            }

            Logtab logtab = new Logtab();
            for (int j = 0; j < columnNames.size(); j++) {
                logger.info("columnNames = " + (String) columnNames.elementAt(j));
                logtab.setValue((String) columnNames.elementAt(j),
                        (String) rs.getString((String) columnNames.elementAt(j)));
            }
            return logtab; // we return becos spring requires it. 
        }
    }

}