// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   AttachmentDAO.java

package com.keyshop.pub.upload.dao;

import com.keyshop.pub.dao.DAO;
import com.keyshop.pub.upload.model.Attachment;
import com.keyshop.pub.util.Sequence;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.*;
import javax.sql.DataSource;

public class AttachmentDAO extends DAO
{

    public AttachmentDAO(DataSource ds)
    {
        super(ds);
    }

    public int insert(Attachment file)
        throws SQLException, Exception
    {
        String sql = "INSERT INTO attachment (id,fileName,fileSize,fileType,note,fileContent,createDate) VALUES (?, ?, ?, ?,?,?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            Sequence sequence = Sequence.getInstance(conn);
            int id = sequence.getNextValue(3);
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.setString(2, file.getFileName());
            pstmt.setString(3, file.getFileSize());
            pstmt.setString(4, file.getFileType());
            pstmt.setString(5, file.getNote());
            System.out.println("======= size :" + file.getFileContent().available());
            pstmt.setBinaryStream(6, file.getFileContent(), file.getFileContent().available());
            pstmt.setString(7, file.getCreateDate());
            pstmt.executeUpdate();
            conn.commit();
            pstmt.close();
            close(conn);
            return id;
        }
        catch(SQLException sqle)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            sqle.printStackTrace();
            throw sqle;
        }
    }

    public void delete(String id)
        throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try
        {
            conn = ds.getConnection();
            String sql = "DELETE FROM attachment WHERE id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, Integer.parseInt(id));
            pstmt.executeUpdate();
            close(pstmt);
            conn.commit();
        }
        catch(SQLException e)
        {
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return;
    }

    public void retrieveBaseInfo(Attachment file)
        throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        if(file.getId() == null)
            return;
        try
        {
            conn = ds.getConnection();
            String sql = "SELECT id,FILE_NAME,FILE_SIZE,FILE_TYPE,NOTE,CREATE_DATE FROM attachment WHERE id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, Integer.parseInt(file.getId()));
            rs = pstmt.executeQuery();
            if(rs.next())
                populate(file, rs);
            close(rs);
            close(pstmt);
        }
        catch(SQLException e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return;
    }

    public void retrieve(Attachment file)
        throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            String sql = "SELECT * FROM attachment WHERE id=?";
            System.out.println("sql = " + sql + "'" + file.getId() + "'");
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, file.getId());
            rs = pstmt.executeQuery();
            if(rs.next())
            {
                file.setId(rs.getString("id"));
                file.setFileName(rs.getString("FILE_NAME"));
                file.setFileSize(rs.getString("FILE_SIZE"));
                file.setFileData(rs.getBytes("FILE_CONTENT"));
                file.setNote(rs.getString("NOTE"));
                file.setCreateDate(rs.getString("CREATE_DATE"));
            }
            close(rs);
            close(pstmt);
        }
        catch(SQLException e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return;
    }

    public static void main(String args[])
        throws Exception
    {
    }
}
