001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.datasetplugin.multidataset;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.crm.report.connection.ColumnInfo;
020: import org.pentaho.reportdesigner.crm.report.connection.SQLUtil;
021: import org.pentaho.reportdesigner.crm.report.datasetplugin.MetadataTableModel;
022: import org.pentaho.reportdesigner.crm.report.datasetplugin.jdbc.JDBCClassLoader;
023:
024: import javax.swing.table.AbstractTableModel;
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.Statement;
029: import java.util.ArrayList;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: /**
034: * User: Martin Date: 27.07.2006 Time: 16:39:51
035: */
036: public class JDBCTableModel extends AbstractTableModel implements
037: MetadataTableModel {
038: @NonNls
039: @NotNull
040: private static final Logger LOG = Logger
041: .getLogger(JDBCTableModel.class.getName());
042:
043: @NotNull
044: private ArrayList<Object[]> data;
045: @NotNull
046: private ArrayList<ColumnInfo> columnInfos;
047:
048: public JDBCTableModel(@NotNull
049: JNDISource jndiSource, @NotNull
050: String sqlQuery, int maxRowsToProcess) throws Exception {
051: // noinspection ConstantConditions
052: if (jndiSource == null) {
053: throw new IllegalArgumentException(
054: "jndiSource must not be null");
055: }
056: // noinspection ConstantConditions
057: if (sqlQuery == null) {
058: throw new IllegalArgumentException(
059: "sqlQuery must not be null");
060: }
061:
062: Connection connection = null;
063: Statement statement = null;
064: ResultSet resultSet = null;
065: try {
066: // noinspection JDBCResourceOpenedButNotSafelyClosed
067: connection = JDBCClassLoader.getConnection(jndiSource
068: .getDriverClass(),
069: jndiSource.getConnectionString(), jndiSource
070: .getUsername(), jndiSource.getPassword());
071: // connection = DriverManager.getConnection(jndiSource.getConnectionString(), jndiSource.getUsername(), jndiSource.getPassword());
072: statement = connection.createStatement();
073: try {
074: if (maxRowsToProcess > 0) {
075: statement.setMaxRows(maxRowsToProcess);
076: }
077: } catch (Exception e) {
078: if (LOG.isLoggable(Level.FINE))
079: LOG.log(Level.FINE,
080: "JDBCTableModel.JDBCTableModel ", e);
081: // if this fails, we need not blow out, we just want to try to set this here
082: // since it is will do a significantly better job at limited the data
083: }
084: resultSet = statement.executeQuery(sqlQuery);
085: ResultSetMetaData metaData = resultSet.getMetaData();
086: int columnCount = metaData.getColumnCount();
087:
088: columnInfos = new ArrayList<ColumnInfo>();
089:
090: for (int i = 1; i <= columnCount; i++) {
091: String columnName = metaData.getColumnName(i);
092: Class<?> type = Class.forName(metaData
093: .getColumnClassName(i));
094: columnInfos.add(new ColumnInfo("", columnName, type));
095: }
096:
097: data = new ArrayList<Object[]>();
098:
099: while (resultSet.next()) {
100: Object[] rowData = new Object[columnCount];
101: for (int i = 1; i <= columnCount; i++) {
102: rowData[i - 1] = resultSet.getObject(i);
103: }
104: data.add(rowData);
105: if (data.size() >= maxRowsToProcess) {
106: break;
107: }
108: }
109: } finally {
110: SQLUtil.closeResultSet(resultSet);
111: SQLUtil.closeStatement(statement);
112: SQLUtil.closeConnection(connection);
113: }
114: }
115:
116: @NotNull
117: public String getColumnName(int column) {
118: return columnInfos.get(column).getColumnName();
119: }
120:
121: @NotNull
122: public Class<?> getColumnClass(int columnIndex) {
123: return columnInfos.get(columnIndex).getColumnClass();
124: }
125:
126: @NotNull
127: public ColumnInfo getColumnInfo(int column) {
128: return columnInfos.get(column);
129: }
130:
131: public int getColumnCount() {
132: return columnInfos.size();
133: }
134:
135: public int getRowCount() {
136: return data.size();
137: }
138:
139: @Nullable
140: public Object getValueAt(int rowIndex, int columnIndex) {
141: return data.get(rowIndex)[columnIndex];
142: }
143: }
|