dao.DirBlobTitleQuery.java Source code

Java tutorial

Introduction

Here is the source code for dao.DirBlobTitleQuery.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 dao.BaseDaoException;
import util.DbUtils;
import util.DbConstants;
import model.Directory;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.core.SqlParameter;

/**
  * This class is used by spring to populate a <code>Photo</code> bean.
  * This class implements CarryonQuery
  *
  * @author Smitha Gudur (smitha@redbasin.com)
  * @version $Revision: 1.1 $
  */
class DirBlobTitleQuery extends BasicQuery {

    private volatile Vector columnNames = null;

    /**
     * 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 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).
     *
     */
    DirBlobTitleQuery(DataSource ds) {
        super(ds, "select 1 from dirblob where directoryid=? and btitle=?");
        declareParameter(new SqlParameter("directoryid", Types.BIGINT));
        declareParameter(new SqlParameter("btitle", Types.VARCHAR));
        compile();
    }

    /**
     *  Spring calls this method for each record found in the resultset.
     *  @param rs result set
     *  @param rowNum number of rows
     *  @returns Object
     *  @throws SQLException
     */
    protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {

        if (rowNum == 0) {
            if (columnNames == null) {
                columnNames = dbutils.getColumnNames(rs);
            }
        }

        Directory directory = (Directory) eop.newObject(DbConstants.DIRECTORY);
        for (int j = 0; j < columnNames.size(); j++) {
            directory.setValue((String) columnNames.elementAt(j),
                    (String) rs.getString((String) columnNames.elementAt(j)));
        }
        return directory; // we return becos spring requires it. 
    }
}