001: /*
002: * Created on 11/04/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: package br.com.igor.beans.table;
025:
026: import java.math.BigDecimal;
027: import java.sql.Date;
028: import java.sql.ResultSet;
029: import java.sql.ResultSetMetaData;
030: import java.sql.SQLException;
031: import java.sql.Timestamp;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import javax.swing.table.AbstractTableModel;
036:
037: import br.com.igor.beans.DBCollectionModel;
038: import br.com.igor.db.Controller;
039: import br.com.igor.db.ControllerCreationException;
040: import br.com.igor.db.ControllerProvider;
041: import br.com.igor.db.PersistentObject;
042: import br.com.igor.plugin.core.DynamicClassLoader;
043:
044: /**
045: * Modelo de dados que deverão ser representados através de um DBTable
046: *
047: * @author Igor Regis da Silva Simões
048: */
049: public class DBTableModel extends AbstractTableModel implements
050: DBCollectionModel {
051: /**
052: * Dados que compoem o modelo
053: */
054: private ResultSet dados = null;
055:
056: /**
057: * Tipo de dados carregados no modelo
058: */
059: private PersistentObject dataType = null;
060:
061: /**
062: * Creates a new instance of DataBaseTableModel
063: *
064: * @param dataType Tipo de dados do modelo
065: */
066: public DBTableModel(PersistentObject dataType) {
067: setDataType(dataType);
068: }
069:
070: /**
071: * @see br.com.igor.beans.DBCollectionModel#loadDados()
072: */
073: public void loadDados() throws ControllerCreationException,
074: SQLException {
075: if (getDataType() != null) {
076: String classe = dataType.getClass().getName();
077: Controller controller = ControllerProvider
078: .getControllerProvider()
079: .getController(
080: classe
081: .substring(classe.lastIndexOf('.') + 1));
082: dados = controller.getAll(dados, dataType);
083: fireTableDataChanged();
084: }
085: }
086:
087: /**
088: * @see br.com.igor.beans.DBCollectionModel#getDados()
089: */
090: public ResultSet getDados() {
091: try {
092: if (dados != null)
093: dados.isFirst();
094: } catch (SQLException e) {
095: //Fazemos o tratamento do erro pos do contrário a tela pode
096: //tentar exibir o conteúdo de um ResultSet que esteja fechado
097: if (e.getErrorCode() == -158)//ResultSet fechado
098: this .dados = null;
099: try {
100: loadDados();
101: } catch (ControllerCreationException e1) {
102: // //TODO Auto-generated catch block
103: e1.printStackTrace();
104: } catch (SQLException e1) {
105: // //TODO Auto-generated catch block
106: e1.printStackTrace();
107: }
108: }
109: return this .dados;
110: }
111:
112: /**
113: * @see br.com.igor.beans.DBCollectionModel#getDataAt(int)
114: */
115: public Map<String, Object> getDataAt(int index) throws SQLException {
116: if (getRowCount() == 0)
117: return null;
118: HashMap<String, Object> data = new HashMap<String, Object>(1);
119:
120: ResultSetMetaData metaData = dados.getMetaData();
121:
122: int coluna;
123: for (int i = 0; i < metaData.getColumnCount(); i++)
124: if ((coluna = findColumn(metaData.getColumnName(i + 1)) - 1) != -2)
125: data.put(metaData.getColumnName(i + 1), getValueAt(
126: index, coluna) == null ? null : getValueAt(
127: index, coluna));
128: return data;
129: }
130:
131: /**
132: * @see br.com.igor.beans.DBCollectionModel#getPersistentObject(int)
133: */
134: public PersistentObject getPersistentObject(int index)
135: throws SQLException {
136: if (getRowCount() == 0)
137: return null;
138: PersistentObject dado = null;
139:
140: try {
141: dado = (PersistentObject) getDataType().getClass()
142: .newInstance();
143: } catch (InstantiationException ie) {
144: //TODO Excecao
145: ie.printStackTrace();
146: } catch (IllegalAccessException iae) {
147: //TODO Excecao
148: iae.printStackTrace();
149: }
150:
151: dado.setDados(getDataAt(index));
152: return dado;
153: }
154:
155: /**
156: * @see javax.swing.table.TableModel#getColumnCount()
157: */
158: public int getColumnCount() {
159: if (getDados() == null)
160: return 0;
161:
162: try {
163: return dados.getMetaData().getColumnCount();
164: } catch (SQLException sqle) {
165: //TODO Exceção
166: sqle.printStackTrace();
167: }
168: return -1;
169: }
170:
171: /**
172: * @see javax.swing.table.TableModel#getRowCount()
173: */
174: public int getRowCount() {
175: if (getDados() == null)
176: return 0;
177:
178: try {
179: dados.last();
180: return dados.getRow();
181: } catch (SQLException sqle) {
182: //TODO Exceção
183: }
184: return -1;
185: }
186:
187: /**
188: * @see javax.swing.table.TableModel#getValueAt(int, int)
189: */
190: public Object getValueAt(int rowIndex, int columnIndex) {
191: int x = columnIndex;
192: if (getDados() == null || getRowCount() == 0)
193: return null;
194:
195: try {
196: dados.first();
197: for (int i = 0; i < rowIndex; i++) {
198: dados.next();
199: }
200: Object dado = dados.getObject(++columnIndex);
201:
202: if (dado instanceof Date)
203: return new java.util.Date(((Date) dado).getTime());
204: else if (dado instanceof Timestamp)
205: return new java.util.Date(((Timestamp) dado).getTime());
206: else if (dado instanceof BigDecimal)
207: return new Double(((BigDecimal) dado).doubleValue());
208:
209: return dado;
210: } catch (SQLException sqle) {
211: //TODO Exceção
212: sqle.printStackTrace();
213: return null;
214: }
215: }
216:
217: /**
218: * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
219: */
220: @Override
221: public Class getColumnClass(int columnIndex) {
222: int x = columnIndex;
223:
224: if (getDados() == null)
225: return null;
226:
227: try {
228: dados.first();
229: Class classe = DynamicClassLoader.getClassLoader()
230: .loadClass(
231: dados.getMetaData().getColumnClassName(
232: ++columnIndex));
233:
234: if (classe.getName().equals(Date.class.getName())
235: || classe.getName().equals(
236: Timestamp.class.getName()))
237: return java.util.Date.class;
238: else if (classe.getName()
239: .equals(BigDecimal.class.getName()))
240: return Double.class;
241:
242: return classe;
243: } catch (SQLException sqle) {
244: //TODO Exceção
245: sqle.printStackTrace();
246: } catch (ClassNotFoundException cnfe) {
247: //TODO Exceção
248: cnfe.printStackTrace();
249: }
250:
251: return null;
252: }
253:
254: /**
255: * @see javax.swing.table.AbstractTableModel#getColumnName(int)
256: */
257: @Override
258: public String getColumnName(int columnIndex) {
259: int x = columnIndex;
260:
261: try {
262: dados.first();
263: return dados.getMetaData().getColumnName(++columnIndex);
264: } catch (SQLException sqle) {
265: //TODO Exceção
266: sqle.printStackTrace();
267: }
268: return null;
269: }
270:
271: /**
272: * @see javax.swing.table.AbstractTableModel#findColumn(java.lang.String)
273: */
274: @Override
275: public int findColumn(String columnName) {
276: if (getDados() == null)
277: return -1;
278:
279: try {
280: dados.first();
281: for (int i = 1; i <= dados.getMetaData().getColumnCount(); i++) {
282: if (dados.getMetaData().getColumnName(i).equals(
283: columnName))
284: return i;
285: }
286: } catch (SQLException sqle) {
287: //TODO Exceção
288: sqle.printStackTrace();
289: }
290: return -1;
291: }
292:
293: /**
294: * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
295: */
296: @Override
297: public boolean isCellEditable(int rowIndex, int columnIndex) {
298: return false;
299: }
300:
301: /**
302: * @see br.com.igor.beans.DBCollectionModel#setDataType(br.com.igor.db.PersistentObject)
303: */
304: public void setDataType(PersistentObject dataType) {
305: this .dataType = dataType;
306: }
307:
308: /**
309: * @see br.com.igor.beans.DBCollectionModel#getDataType()
310: */
311: public PersistentObject getDataType() {
312: return dataType;
313: }
314:
315: /**
316: * @see br.com.igor.beans.DBCollectionModel#filter()
317: */
318: public void filter() throws ControllerCreationException,
319: SQLException {
320: if (getDataType() != null) {
321: String classe = dataType.getClass().getName();
322: Controller controller = ControllerProvider
323: .getControllerProvider()
324: .getController(
325: classe
326: .substring(classe.lastIndexOf('.') + 1));
327: dados = controller.filterBy(dados, dataType);
328: fireTableDataChanged();
329: }
330: }
331:
332: /**
333: * @see java.lang.Object#finalize()
334: */
335: @Override
336: protected void finalize() throws Throwable {
337: super .finalize();
338: fecharConexaoDB();
339: }
340:
341: /**
342: *
343: *
344: */
345: private void fecharConexaoDB() {
346: if (getDados() != null) {
347: String classe = dataType.getClass().getName();
348: Controller controller;
349: try {
350: controller = ControllerProvider.getControllerProvider()
351: .getController(
352: classe.substring(classe
353: .lastIndexOf('.') + 1));
354: controller.freeResource(dados);
355: } catch (ControllerCreationException e) {
356: // //TODO Auto-generated catch block
357: e.printStackTrace();
358: } catch (SQLException e1) {
359: // //TODO Auto-generated catch block
360: e1.printStackTrace();
361: }
362: }
363: }
364: }
|