dao.DiaryPhotoAddQuery.java Source code

Java tutorial

Introduction

Here is the source code for dao.DiaryPhotoAddQuery.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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class implements CarryonBlobUpdate
 * This is used to add blob streams (photos/documents/music ..) for a given user
 * This class is used to populate and share information for <code>Userpage</code> bean
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class DiaryPhotoAddQuery {

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

    public DiaryPhotoAddQuery(DataSource ds) {
        dataSource = ds;
    }

    /**
    * This method is used to add blobstreams for a user. <code>Userpage</code>
    * @param blob - the blob stream
    * @param category - the blob type (1 - photos, 2-music, 3-video 4 - documents, 5 - archives)
    * @param mimeType - the mime type (image/jpeg, application/octet-stream)
    * @param btitle - the title for this blob
    * @param bsize - the size of the blob
    * @param zoom - the zoom for the blob (used for displaying for image/jpeg)
    * @param caption - caption
    * @throws Dao Exception - when an error or exception occurs while inserting this blob in DB.
    *
     **/
    public void addBlob(byte[] blob, int category, String mimeType, String btitle, long bsize, int zoom,
            String caption) throws BaseDaoException {
        long dt = System.currentTimeMillis();
        Connection conn = null;
        String stmt = "insert into diaryphotos values(?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement s = null;
        try {
            conn = dataSource.getConnection();
            s = conn.prepareStatement(stmt);
            s.setLong(1, 0);
            s.setBytes(2, blob);
            s.setInt(3, new Integer(category));
            s.setString(4, mimeType);
            s.setString(5, btitle);
            s.setLong(6, bsize);
            s.setInt(7, new Integer(zoom));
            s.setString(8, caption);
            s.executeUpdate();
        } catch (SQLException e) {
            logger.error("Error adding a blob in diaryphotos ", e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    throw new BaseDaoException("Could not close connection " + stmt, e);
                }
            }
        }
    }
}