// 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:   DAO.java

package com.keyshop.pub.dao;

import com.keyshop.pub.util.CacheManager;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.*;
import javax.sql.DataSource;
import org.apache.commons.beanutils.BeanUtils;

// Referenced classes of package com.keyshop.pub.dao:
//            DAOCacheManager

public abstract class DAO
{

    protected DataSource ds;
    protected Connection dbCon;

    protected void populate(Object bean, ResultSet rs)
        throws SQLException
    {
        ResultSetMetaData metaData = rs.getMetaData();
        int ncolumns = metaData.getColumnCount();
        HashMap properties = new HashMap();
        for(int i = 1; i <= ncolumns; i++)
            properties.put(sql2javaName(metaData.getColumnName(i)), rs.getString(i));

        try
        {
            BeanUtils.populate(bean, properties);
        }
        catch(InvocationTargetException ite)
        {
            throw new SQLException("BeanUtils.populate threw " + ite.toString());
        }
        catch(IllegalAccessException iae)
        {
            throw new SQLException("BeanUtils.populate threw " + iae.toString());
        }
    }

    public int getSize(String tableName, String condition)
        throws SQLException
    {
        Connection conn;
        PreparedStatement pstmt;
        ResultSet rs;
        conn = null;
        pstmt = null;
        rs = null;
        SQLException sqle;
        int i;
        try
        {
            String sql = "SELECT count(*) FROM " + tableName + " " + condition;
            conn = ds.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            rs.next();
            int size = rs.getInt(1);
            close(rs);
            close(pstmt);
            i = size;
        }
        finally
        {
            close(conn);
        }
        return i;
        sqle;
        close(rs);
        close(pstmt);
        rollback(conn);
        sqle.printStackTrace();
        throw sqle;
    }

    public DAO(DataSource ds)
    {
        this.ds = ds;
    }

    public DAO(Connection con)
    {
        dbCon = con;
    }

    public DAO(DataSource ds, Connection con)
    {
        this.ds = ds;
        dbCon = con;
    }

    public void setDataSource(DataSource ds)
    {
        this.ds = ds;
    }

    protected void close(ResultSet rs)
    {
        if(rs != null)
        {
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            rs = null;
        }
    }

    protected void close(PreparedStatement pstmt)
    {
        if(pstmt != null)
        {
            try
            {
                pstmt.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            pstmt = null;
        }
    }

    protected void close(Connection conn)
    {
        if(conn != null)
        {
            try
            {
                conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    protected void rollback(Connection conn)
    {
        if(conn != null)
        {
            try
            {
                conn.rollback();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    protected static String java2sqlName(String name)
    {
        String column = "";
        for(int i = 0; i < name.length(); i++)
            if(i < name.length() - 1 && name.charAt(i) >= 'a' && name.charAt(i) <= 'z' && name.charAt(i + 1) >= 'A' && name.charAt(i + 1) <= 'Z')
                column = column + name.charAt(i) + "_";
            else
                column = column + name.charAt(i);

        return column.toLowerCase();
    }

    protected static String sql2javaName(String name)
    {
        String column = "";
        for(int i = 0; i < name.length(); i++)
            if(name.charAt(i) == '_')
                column = column + (++i >= name.length() ? "" : String.valueOf(name.charAt(i)).toUpperCase());
            else
                column = column + name.charAt(i);

        return column;
    }

    public List list(String model, String sql)
        throws SQLException
    {
        String objKey;
        ArrayList list;
        String objKeys[] = {
            sql
        };
        objKey = CacheManager.createKey(objKeys);
        list = (ArrayList)DAOCacheManager.getCache(objKey);
        if(list != null)
            return list;
        list = new ArrayList();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            pstmt = conn.prepareStatement(sql);
            Object bean;
            for(rs = pstmt.executeQuery(); rs.next(); list.add(bean))
            {
                bean = Class.forName(model).newInstance();
                populate(bean, rs);
            }

            close(rs);
            close(pstmt);
        }
        catch(Exception e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        DAOCacheManager.putCache(list, objKey, 1);
        return list;
    }

    public ResultSet list(String sql)
        throws SQLException
    {
        return null;
    }

    public List search(String model, String sql, List condition)
        throws SQLException
    {
        List list;
        System.out.println("==sql:" + sql);
        list = new ArrayList();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            pstmt = conn.prepareStatement(sql);
            for(int i = 0; i < condition.size(); i++)
                pstmt.setString(i + 1, (String)condition.get(i));

            Object bean;
            for(rs = pstmt.executeQuery(); rs.next(); list.add(bean))
            {
                bean = Class.forName(model).newInstance();
                populate(bean, rs);
            }

            close(rs);
            close(pstmt);
        }
        catch(Exception e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return list;
    }
}
