SQLDataProvider.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:9k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SQLDataProvider.java,v 1.9 2005/10/15 11:43:20 pdoubleya Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset.provider.sql;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.ResultSetMetaData;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.jdesktop.dataset.DataColumn;
  30. import org.jdesktop.dataset.DataProvider;
  31. import org.jdesktop.dataset.DataRow;
  32. import org.jdesktop.dataset.DataTable;
  33. import static org.jdesktop.dataset.DataRow.DataRowStatus.DELETED;
  34. import static org.jdesktop.dataset.DataRow.DataRowStatus.INSERTED;
  35. import static org.jdesktop.dataset.DataRow.DataRowStatus.UPDATED;
  36. import org.jdesktop.dataset.event.TableChangeEvent;
  37. import org.jdesktop.dataset.provider.LoadTask;
  38. import org.jdesktop.dataset.provider.SaveTask;
  39. import org.jdesktop.dataset.provider.LoadTask.LoadItem;
  40. /**
  41.  * SQL based DataProvider for a JDNC DataSet. This implementation handles
  42.  * retrieving values from a database table, and persisting changes back
  43.  * to the table.
  44.  * 
  45.  * @author rbair
  46.  */
  47. public class SQLDataProvider extends DataProvider {
  48.     /** 
  49.      * Creates a new instance of SQLDataProvider 
  50.      */
  51.     public SQLDataProvider() {
  52.     }
  53.     
  54.     public SQLDataProvider(String tableName) {
  55.         TableCommand tableCommand = new TableCommand(tableName);
  56.         setCommand(tableCommand);
  57.     }
  58.     
  59.     public SQLDataProvider(String tableName, String whereClause) {
  60.         TableCommand tableCommand = new TableCommand(tableName, whereClause);
  61.         setCommand(tableCommand);
  62.     }
  63.     
  64.     /**
  65.      * @inheritDoc
  66.      */
  67.     protected LoadTask createLoadTask(DataTable[] tables) {
  68.         return new LoadTask(tables) {
  69.             protected void readData(DataTable[] tables) throws Exception {
  70.                 JDBCDataConnection conn = (JDBCDataConnection)getConnection();
  71.                 if (conn == null) {
  72.                     //no connection, short circuit
  73.                     return;
  74.                 }
  75.                 if (getCommand() == null) {
  76.                     //there isn't any command to run, so short circuit the method
  77.                     return;
  78.                 }
  79.                 //TODO when selectCommand exists, add it to the check here
  80.                 
  81.                 //set the progess count
  82.                 setMinimum(0);
  83.                 setMaximum(tables.length);
  84.                 //construct and execute a resultset for each table in turn.
  85.                 //as each table is finished, call scheduleLoad.
  86.                 for (DataTable table : tables) {
  87.                     try {
  88.                         PreparedStatement stmt = ((AbstractSqlCommand)getCommand()).getSelectStatement(conn);
  89.                         ResultSet rs = stmt.executeQuery();
  90.                         //collect the column names from the result set so that
  91.                         //I can retrieve the data from the result set into the
  92.                         //column based on matching column names
  93.                         ResultSetMetaData md = rs.getMetaData();
  94.                         List<String> names = new ArrayList<String>();
  95.                         List<DataColumn> columns = table.getColumns();
  96.                         for (int i=0; i<columns.size(); i++) {
  97.                             String name = columns.get(i).getName();
  98.                             for (int j=0; j<md.getColumnCount(); j++) {
  99.                                 if (name.equalsIgnoreCase(md.getColumnName(j+1))) {
  100.                                     names.add(name);
  101.                                 }
  102.                             }
  103.                         }
  104.                         
  105.                         //iterate over the result set. Every 50 items, schedule a load
  106.                         List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>(60);
  107.                         while (rs.next()) {
  108.                             if (rows.size() >= 50) {
  109.                                 LoadItem item = new LoadItem<List<Map<String,Object>>>(table, rows);
  110.                                 scheduleLoad(item);
  111.                                 rows = new ArrayList<Map<String,Object>>(60);
  112.                             }
  113.                             //create a row
  114.                             Map<String,Object> row = new HashMap<String,Object>();
  115.                             for (String name : names) {
  116.                                 row.put(name, rs.getObject(name));
  117.                             }
  118.                             rows.add(row);
  119.                         }
  120.                         //close the result set
  121.              rs.close();
  122.                         //load the remaining items
  123.                         LoadItem item = new LoadItem<List<Map<String,Object>>>(table, rows);
  124.                         scheduleLoad(item);
  125.                     } catch (Exception e) {
  126.                         e.printStackTrace();
  127.                     }
  128.                     setProgress(getProgress() + 1);
  129.                 }
  130.                 setProgress(getMaximum());
  131.             }
  132.             
  133.             /**
  134.              * @inheritDoc
  135.              */
  136.             protected void loadData(LoadItem[] items) {
  137.                 for (LoadItem<List<Map<String,Object>>> item : items) {
  138.                     for (Map<String,Object> row : item.data) {
  139.                         DataRow r  = item.table.appendRowNoEvent();
  140.                         for (String col : row.keySet()) {
  141.                             r.setValue(col, row.get(col));
  142.                         }
  143.                         r.setStatus(DataRow.DataRowStatus.UNCHANGED);
  144.                         
  145.                         // CLEAN
  146.                         item.table.fireDataTableChanged(TableChangeEvent.newRowAddedEvent(item.table, r));
  147.                     }
  148.                     item.table.fireDataTableChanged(TableChangeEvent.newLoadCompleteEvent(item.table));                    
  149.                 }
  150.             }
  151.         };
  152.     }
  153.     /**
  154.      * @inheritDoc
  155.      */
  156.     protected SaveTask createSaveTask(DataTable[] tables) {
  157.         return new SaveTask(tables) {
  158.             protected void saveData(DataTable[] tables) throws Exception {
  159.                 JDBCDataConnection conn = (JDBCDataConnection)getConnection();
  160.                 if (conn == null) {
  161.                     //no connection, short circuit
  162.                     return;
  163.                 }
  164.                 if (getCommand() == null) {
  165.                     //there isn't any command to run, so short circuit the method
  166.                     return;
  167.                 }
  168.                 //TODO when selectCommand exists, add it to the check here
  169.                 //set the progess count
  170.                 setMinimum(0);
  171.                 setMaximum(tables.length);
  172.                 for (DataTable table : tables) {
  173.                     //fetch the set of rows from the table
  174.                     List<DataRow> rows = table.getRows();
  175.                     //for each row, either insert it, update it, delete it, or
  176.                     //ignore it, depending on the row flag
  177.                     for (DataRow row : rows) {
  178.                         PreparedStatement stmt = null;
  179.                         switch (row.getStatus()) {
  180.                             case UPDATED:
  181.                                 stmt = ((AbstractSqlCommand)getCommand()).getUpdateStatement(conn, row);
  182.                                 conn.executeUpdate(stmt);
  183.                                 row.setStatus(DataRow.DataRowStatus.UNCHANGED);
  184.                                 break;
  185.                             case INSERTED:
  186.                                 stmt = ((AbstractSqlCommand)getCommand()).getInsertStatement(conn, row);
  187.                                 conn.executeUpdate(stmt);
  188.                                 row.setStatus(DataRow.DataRowStatus.UNCHANGED);
  189.                                 break;
  190.                             case DELETED:
  191.                                 stmt = ((AbstractSqlCommand)getCommand()).getDeleteStatement(conn, row);
  192.                                 conn.executeUpdate(stmt);
  193.                                 table.discardRow(row);
  194.                                 break;
  195.                             default:
  196.                                 //do nothing
  197.                                 break;
  198.                         }
  199.                     }
  200.                     table.fireDataTableChanged(TableChangeEvent.newSaveCompleteEvent(table));
  201.                     setProgress(getProgress() + 1);
  202.                 }
  203.                 setProgress(getMaximum());
  204.                 conn.commit();
  205.             }
  206.         };
  207.     }    
  208. }