package connect;
import content.DBConnectionManager;
import content.FileUtils;
import content.EIEException;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
public class DataSetBean{
        private DBConnectionManager DBcnnManager;
        private FileUtils fmt            =      new FileUtils();
        private FileUtils dbutils        =      new FileUtils();        private Connection conn          =      null;
        private Statement  stmt          =      null;
        private ResultSet  rset          =      null;
        private HashMap    set_hm        =      null;        private String            type          =       "";
        private String            filter        =       "";
        private String            sorter        =       "";
        private String            dataSource    =       "";        //#############################################################################################################
        //public  DataSetBean()
        //功  能:构造函数--声明对象并建立数据库连接
        //返回值:出错抛出EIEException异常
        //#############################################################################################################
        public DataSetBean() throws EIEException
        {                DBcnnManager=DBConnectionManager.getInstance();
                conn = DBcnnManager.getConnection("sand");
                if(conn == null)
                        throw new EIEException("DB001");
                fmt.setFile("format.txt");
                if(fmt.openFile())
                        fmt.loadFormat();
        }        //#############################################################################################################
        //public  void  init ( String Type, String DataSource, String Filter, String Sorter)
        //功  能:Bean的初始化。
        //参数一:Type 类型的值为table、storeproc、view。
        //参数二:DataSource为table、storeproc、view名称(即表名或存储过程名称或视的名称;
        //       对于storeproc和view,其实现语句以文本形式存放在\Resource\DBApp\下,
        //       DataSource就是相应的文本文件的文件名).
        //参数三:Filter为过滤条件(对table和view有效);
        //参数四:Sorter为排序字段(对table和view有效);对于storeproc ,
        //       Sorter是相应存储过程的参数(形式为para1&para2&para3&………)
        //返回值:出错抛出EIEException异常 
        //#############################################################################################################
        public void init (String Type,String Datasource,String Filter,String Sorter) throws EIEException
        {
                this.type       =   Type.trim().toUpperCase();
                this.filter     =   Filter.trim();
                this.dataSource =   Datasource.trim();
                this.sorter     =   Sorter.trim();
                //check the privilege of user
                //       throw new IEIException("IEI0001");                if(this.type.equals(""))
                        throw new EIEException("DB002");
                if(!this.type.equals("TABLE") && !this.type.equals("VIEW") && !this.type.equals("STOREPROC"))
                        throw new EIEException("DB002");
                if(this.dataSource.equals(""))
                        throw new EIEException("DB002");        }        //#############################################################################################################
        //public  int  execute()
        //功  能:执行Bean的初始化的操作。
        //无参数:
        //返回值:如果正确,返回值为正整数,表示记录集的记录总数;否则抛出EIEException异常
        //#############################################################################################################
        public int execute() throws EIEException
        {
                int success = 0;
                if(!this.type.equals("") && !this.dataSource.equals(""))
                {
                        if(this.type.equals("STOREPROC"))
                                success=executeProcedure(this.dataSource,this.sorter);
                        if(this.type.equals("TABLE") || this.type.equals("VIEW"))
                                success=executeQuery(this.getSqlString());                }                return success;
        }