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

package com.keyshop.pub.dao;

import com.keyshop.pub.model.BaseInfo;
import com.keyshop.pub.util.StringUtil;
import java.sql.*;
import java.util.*;
import javax.sql.DataSource;

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

public class BaseInfoDAO extends DAO
{

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

    public void insert(String tableName, String titles[], String values[])
        throws SQLException
    {
        if(StringUtil.isEmpty(tableName) || titles == null || titles.length <= 0 || values == null || values.length <= 0 || titles.length != values.length)
            throw new SQLException("PARAMETERS ERROR:(TABLE NAME | TITLE NAME | VALUE)");
        String sql = "INSERT INTO " + tableName + "(";
        for(int i = 0; i < titles.length; i++)
            if(i == titles.length - 1)
                sql = sql + titles[i];
            else
                sql = sql + titles[i] + ",";

        sql = sql + ") values(";
        for(int i = 0; i < values.length; i++)
            if(i == values.length - 1)
                sql = sql + "'" + values[i] + "'";
            else
                sql = sql + "'" + values[i] + "',";

        sql = sql + ")";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate();
            pstmt.close();
            try
            {
                conn.commit();
            }
            catch(SQLException sqlexception) { }
        }
        finally
        {
            close(conn);
        }
        return;
    }

    public void update(String tableName, String titles[], String values[])
        throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try
        {
            conn = ds.getConnection();
            String sql = "UPDATE " + tableName + " SET ";
            for(int i = 1; i < titles.length; i++)
                if(i == titles.length - 1)
                    sql = sql + titles[i] + "='" + values[i] + "'";
                else
                    sql = sql + titles[i] + "='" + values[i] + "',";

            sql = sql + " WHERE id='" + values[0] + "'";
            pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate();
            close(pstmt);
            try
            {
                conn.commit();
            }
            catch(Exception exception) { }
        }
        finally
        {
            close(conn);
        }
        DAOCacheManager.clearCache();
        return;
    }

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

    public List listAll(String tableName, String columNames[])
        throws SQLException
    {
        Connection conn;
        PreparedStatement pstmt;
        ResultSet rs;
        List list;
        conn = null;
        pstmt = null;
        rs = null;
        list = new ArrayList();
        SQLException e;
        List list1;
        try
        {
            if(ds != null)
            {
                conn = ds.getConnection();
                break MISSING_BLOCK_LABEL_46;
            }
            list1 = null;
        }
        finally
        {
            close(conn);
        }
        return list1;
        String sql = "select * from " + tableName;
        pstmt = conn.prepareStatement(sql);
        HashMap map;
        for(rs = pstmt.executeQuery(); rs.next(); list.add(map))
        {
            map = new HashMap();
            for(int i = 0; i < columNames.length; i++)
                map.put(columNames[i], rs.getString(columNames[i]));

        }

        close(rs);
        close(pstmt);
          goto _L1
        e;
        close(rs);
        close(pstmt);
        rollback(conn);
        e.printStackTrace();
_L1:
        return list;
    }

    public void putColumParamters(String tableName, BaseInfo baseInfo)
        throws SQLException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List list = new ArrayList();
        ArrayList columNames = new ArrayList();
        columNames.add("id");
        ArrayList dispalyNames = new ArrayList();
        dispalyNames.add("id");
        ArrayList cloumTypes = new ArrayList();
        cloumTypes.add("id");
        ArrayList maxLengths = new ArrayList();
        maxLengths.add("0");
        try
        {
            conn = ds.getConnection();
            String sql = "select * from base_table_config where table_name='" + tableName + "'";
            pstmt = conn.prepareStatement(sql);
            for(rs = pstmt.executeQuery(); rs.next(); maxLengths.add((new StringBuffer(String.valueOf(rs.getInt("maxlength")))).toString()))
            {
                columNames.add(rs.getString("colum_name"));
                dispalyNames.add(rs.getString("display_name"));
                cloumTypes.add(rs.getString("type"));
            }

            baseInfo.setColumNames(listToArray(columNames));
            baseInfo.setDisplayNames(listToArray(dispalyNames));
            baseInfo.setCloumTypes(listToArray(cloumTypes));
            baseInfo.setMaxlength(listToArray(maxLengths));
            close(rs);
            close(pstmt);
        }
        catch(SQLException e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return;
    }

    public String generateUUID(String tableName)
    {
        String maxRowId;
        String sql = "select max(id) from " + tableName;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        maxRowId = "0";
        try
        {
            conn = ds.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next())
            {
                maxRowId = rs.getString(1);
                maxRowId = (new StringBuffer(String.valueOf(Integer.parseInt(maxRowId) + 1))).toString();
            }
        }
        catch(SQLException e)
        {
            close(rs);
            close(pstmt);
            rollback(conn);
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
        return (new StringBuffer(String.valueOf(maxRowId))).toString();
    }

    public String[] listToArray(ArrayList list)
    {
        if(list == null || list.size() <= 0)
            return null;
        String baseInfos[] = new String[list.size()];
        Iterator it = list.iterator();
        int i = 0;
        while(it.hasNext()) 
            baseInfos[i++] = (String)it.next();
        return baseInfos;
    }
}
