Package realcix20.guis.components

Source Code of realcix20.guis.components.CacheTableModel

/*���ܼ�飄1�7
*XTableʹ�ã���4��bmodel��
*/
package realcix20.guis.components;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.table.AbstractTableModel;

import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Column;
import realcix20.guis.utils.I18NManager;
import realcix20.guis.utils.Item;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.ObjectUtil;

public class CacheTableModel extends AbstractTableModel {
   
    private int rowCount;
    private ArrayList columns;
    private ArrayList tables;
    private ResultSet rs;
    private String select_sql;
    private String select_count_sql;
    private BaseClass object;
    private boolean noRow;
    private String stat;//report or object
    //fix count sum
  HashMap<Integer, Double> sum=new HashMap<Integer, Double>();
  HashMap<Integer, Integer> count=new HashMap<Integer, Integer>();   
   
    public CacheTableModel(BaseClass object, String select_sql, String stat) {
        this.object = object;
        this.select_sql = select_sql;
        this.stat = stat;
        createScrollEnabledResultSet();
        initColumns();
        initRowCount();
    }
   
    private void initColumns() {             
        try {                  
            tables = new ArrayList();
            columns = new ArrayList();
//            Statement stmt = DAO.getInstance().getConnection().createStatement();
//            System.err.println(">>>>>>>"+select_sql);
//            ResultSet rs = stmt.executeQuery(select_sql);
//           createScrollEnabledResultSet()
            ResultSetMetaData rsmd = rs.getMetaData();      
            if (stat.equals("object")) {
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    Column column = ObjectUtil.findColumn(object, rsmd.getTableName(i), rsmd.getColumnName(i));
                    if ( (column.isIndexField()) ||
                            ( (column.isPrimaryKey()) && (column.getTableName().equals(object.getMainTableName())) ) ) {
                        getTables().add(rsmd.getTableName(i));
                        getColumns().add(rsmd.getColumnName(i));
                    }
                }
            } else if (stat.equals("report")) {
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    getTables().add(rsmd.getTableName(i));
                    getColumns().add(rsmd.getColumnName(i));
          if(rsmd.getColumnName(i).startsWith("SUM")){
            sum.put(i, 0.0);
          }else if(rsmd.getColumnName(i).startsWith("COUNT")){
            count.put(i, 0);
          }                   
                }
        while (rs.next()) {
          for (Integer sk: sum.keySet()) {
            sum.put(sk, rs.getDouble(sk)+sum.get(sk));           
          }
          for (Integer ck : count.keySet()) {
            count.put(ck, rs.getInt(ck)+count.get(ck));
          }
        }               
            }                       
//            stmt.close();
        } catch (SQLException sqle) {
//            System.err.println("initColumns() Error!");
            sqle.printStackTrace();
        }    
    }
   
    private void initRowCount() {
        try {
            getRs().last();
      if(is_sum_or_count()){
        rowCount=rs.getRow()+1;
      }else{
        if (getRs().getRow() == 0) {
                  noRow = true;
                  rowCount = 0;
              } else {
                  noRow = false;
                  rowCount = getRs().getRow();
              }       
      }
        } catch (SQLException sqle) {
//            System.err.println("initRowCount() Error!");
            sqle.printStackTrace();
        }
    }
   
    private void createScrollEnabledResultSet() {
        try {
            rs = DAO.getInstance().getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(select_sql);           
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
   
    public Column getColumn(int columnIndex) {
        if (columnIndex == 0)
            return null;
        else
            return ObjectUtil.findColumn(object, (String)getTables().get(columnIndex-1), (String)getColumns().get(columnIndex-1));
    }      
   
    public String getColumnName(int columnIndex) {
        if (columnIndex == 0 )
            return "";
        else {
            if (stat.equals("object")) {
                Column column = ObjectUtil.findColumn(object, (String)getTables().get(columnIndex-1), (String)getColumns().get(columnIndex-1));
                return column.toString();
            } else if (stat.equals("report")) {
                if ( (tables.get(columnIndex - 1) != null) && (((String)tables.get(columnIndex - 1)).trim().length() > 0) ) {
                    Column column = ObjectUtil.findColumn(object, (String)getTables().get(columnIndex-1), (String)getColumns().get(columnIndex-1));
                    return column.toString();
                } else {
                    String name = getTables().get(columnIndex - 1) + "." + getColumns().get(columnIndex - 1);
                    return name;
                }
            } else {
                return null;
            }
        }
    }
   
    public Object getDataAt(int rowIndex, int columnIndex) {
        Object obj = null;
        if (!isNoRow()) {
            try {
                getRs().absolute(rowIndex + 1);
                if (columnIndex == 0) {
                    obj = rowIndex + 1;
                    return obj;
                } else {
                    obj = getRs().getObject((String)getColumns().get(columnIndex-1));
                    return obj;
                }
            } catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
        return obj;
    }
   
    public Object getValueAt(int rowIndex, int columnIndex) {
     
          Object obj = null;
          if (!isNoRow()) {
              try {               
                getRs().last();
              if(getRs().getRow()==rowIndex&&is_sum_or_count()&&columnIndex==0){
                return TxtManager.getTxt("REPORT_TOTAL");
              }else if(getRs().getRow()==rowIndex&&sum.keySet().contains(columnIndex)){
                return sum.get(columnIndex);
              }else if(getRs().getRow()==rowIndex&&count.keySet().contains(columnIndex)){
                return count.get(columnIndex);
              }else if(getRs().getRow()==rowIndex){
                return " ";
              }
             
                  getRs().absolute(rowIndex + 1);
                  if (columnIndex == 0) {
                      obj = rowIndex+1;
                      return obj;
                  }
                  else {
                      obj = getRs().getObject((String)getColumns().get(columnIndex-1));
                      Column column = getColumn(columnIndex);
                      if (column != null) {
                          if (column.isI18N()) {
                              String i18nString = I18NManager.getI18NString(column.getI18nPrefix(), obj);
                              if (GlobalValueManager.getI18nStringDisplayStat().equals("key")) {
                                  Item item = new Item(obj, realcix20.guis.utils.Formatter.formatObject(obj, column));
                                  return item;
                              } else if (GlobalValueManager.getI18nStringDisplayStat().equals("text")) {
                                  Item item = new Item(obj, i18nString);
                                  return item;
                              } else if (GlobalValueManager.getI18nStringDisplayStat().equals("key-text")) {
                                  Item item = new Item(obj, realcix20.guis.utils.Formatter.formatObject(obj, column) + " - " + i18nString);
                                  return item;
                              }                           
                          } else {                         
                        rs.absolute(rowIndex + 1);
//                        return rs.getObject(columnIndex + 1);
                                Item item = new Item(obj, realcix20.guis.utils.Formatter.formatObject(obj, column));
                                  return item;
                          }
                      } else {
                          return obj;
                      }
                  }
              } catch (SQLException sqle) {
//                  System.err.println("getValueAt(" + rowIndex + "," + columnIndex + ") Error!");
                  sqle.printStackTrace();
              }
          }
          return "";     
    }              
   

  public boolean is_sum_or_count() {
    return sum.size()>0||count.size()>0;
  }
 
    public int getColumnCount() {
        return getColumns().size() + 1;       
    }
   
    public int getRowCount() {
        return rowCount;
    }

    public boolean isNoRow() {
        return noRow;
    }

    public ResultSet getRs() {
        return rs;
    }

    public ArrayList getTables() {
        return tables;
    }

    public ArrayList getColumns() {
        return columns;
    }
   
}

TOP

Related Classes of realcix20.guis.components.CacheTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.