001: /*
002: * Copyright 2006 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. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * This TableModel is used to wrap a PentahoResultSet object into a TableModel that
014: * can be used as input to a JFreeReport. It could also be used as input to a
015: * swing JTable.
016: *
017: * Created Sep 8, 2005
018: * @author mbatchel
019: */
020: package org.pentaho.plugin.jfreereport.helper;
021:
022: import javax.swing.table.AbstractTableModel;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.jfree.report.util.CloseableTableModel;
027: import org.pentaho.commons.connection.IPentahoResultSet;
028: import org.pentaho.core.system.PentahoSystem;
029: import org.pentaho.data.connection.mdx.MDXMetaData;
030: import org.pentaho.data.connection.mdx.MDXResultSet;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.util.IVersionHelper;
033:
034: // import org.jfree.report.modules.misc.tablemodel.TypeMapper;
035:
036: public class PentahoTableModel extends AbstractTableModel implements
037: CloseableTableModel {
038: private static final long serialVersionUID = 696878055074045444L;
039:
040: private IPentahoResultSet resultSet;
041:
042: private transient Object[][] rowHeaders;
043:
044: public PentahoTableModel(IPentahoResultSet rs) {
045: super ();
046: resultSet = rs;
047: }
048:
049: /**
050: * returns the logger object
051: *
052: * @return log
053: */
054: public Log getLogger() {
055: return LogFactory.getLog(getClass());
056: }
057:
058: public int getColumnCount() {
059: if (resultSet == null) {
060: return 0;
061: }
062:
063: if (rowHeaders == null) {
064: rowHeaders = resultSet.getMetaData().getRowHeaders();
065: }
066:
067: if (rowHeaders != null && rowHeaders.length > 0) {
068: return rowHeaders[0].length + resultSet.getColumnCount();
069: } else {
070: return resultSet.getColumnCount();
071: }
072: }
073:
074: public Object getValueAt(int rowIndex, int columnIndex) {
075: if (resultSet == null) {
076: return null;
077: }
078:
079: if (rowHeaders == null) {
080: rowHeaders = resultSet.getMetaData().getRowHeaders();
081: }
082:
083: if (rowHeaders != null) {
084: if (columnIndex < rowHeaders[0].length) {
085: return rowHeaders[rowIndex][columnIndex];
086: } else {
087: columnIndex -= rowHeaders[0].length;
088: }
089: }
090:
091: // catch any exceptions so we don't blow up the entire jfreereport
092: Object val = null;
093: try {
094: val = resultSet.getValueAt(rowIndex, columnIndex);
095: } catch (IndexOutOfBoundsException e1) {
096: //
097: // MB - This isn't an error condition. Indeed, it will happen when there are zero rows of
098: // data. So, log an info message and be done with it. We also don't want an if-check to be
099: // done on every cell for this boundary case.
100: //
101: getLogger()
102: .info(
103: Messages
104: .getErrorString("PentahoTableModel.ERROR_0001_GET_VALUE_AT")); //$NON-NLS-1$
105: } catch (Throwable t) {
106: IVersionHelper versionHelper = PentahoSystem
107: .getVersionHelper(null);
108: getLogger()
109: .error(
110: "Error Start: Pentaho " + versionHelper.getVersionInformation(this .getClass())); //$NON-NLS-1$
111: getLogger()
112: .error(
113: Messages
114: .getErrorString("PentahoTableModel.ERROR_0001_GET_VALUE_AT"), t); //$NON-NLS-1$
115: getLogger().error("Error end:"); //$NON-NLS-1$
116: }
117:
118: return val;
119: }
120:
121: public int getRowCount() {
122: if (resultSet != null) {
123: return resultSet.getRowCount();
124: }
125: return 0;
126: }
127:
128: public String getColumnName(int columnNumber) {
129: if (resultSet == null) {
130: return null;
131: }
132:
133: // Flatten out the column headers into one column-name
134: Object[][] columnHeaders = resultSet.getMetaData()
135: .getColumnHeaders();
136: if (rowHeaders == null) {
137: rowHeaders = resultSet.getMetaData().getRowHeaders();
138: }
139:
140: if (rowHeaders != null) {
141: if (columnNumber < rowHeaders[0].length) {
142: if (resultSet instanceof MDXResultSet) {
143: return ((MDXMetaData) resultSet.getMetaData())
144: .getColumnName(columnNumber);
145: }
146: } else {
147: columnNumber -= rowHeaders[0].length;
148: }
149: }
150: StringBuffer buf = new StringBuffer();
151: for (int i = 0; i < columnHeaders.length; i++) {
152: if (i > 0) {
153: buf.append("/"); //$NON-NLS-1$
154: }
155: buf.append(columnHeaders[i][columnNumber].toString());
156: }
157: return buf.toString();
158: }
159:
160: public void close() {
161: // Close the old result set if needed.
162: if (resultSet != null) {
163: resultSet.close();
164: }
165: resultSet = null;
166: // JFreeReport wont listen, but it is always good style to comply to
167: // the contract ..
168: fireTableStructureChanged();
169: }
170: }
|