001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.chart;
021:
022: import java.util.List;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.util.ArrayList;
029: import java.util.Vector;
030:
031: import javax.portlet.PortletException;
032:
033: import org.jfree.data.CategoryDataset;
034: import org.jfree.data.DefaultKeyedValues2D;
035:
036: import com.nabhinc.util.db.DBUtil;
037:
038: /**
039: *
040: *
041: * @author Padmanabh Dabke
042: * (c) 2002, 2003 Nabh Information Systems, Inc. All Rights Reserved.
043: */
044: public class JDBCCategoryDataset extends BaseJDBCDataset implements
045: CategoryDataset {
046:
047: private static final long serialVersionUID = -7998356694642329093L;
048:
049: private String[] jcdsCategoryArray = null;
050: private String[] jcdsSeriesArray = null;
051: private DefaultKeyedValues2D jcds2DData = null;
052:
053: /**
054: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#executeQuery()
055: */
056: public void executeQuery() throws PortletException {
057: Object xObject = null;
058: int column = 0;
059: int currentColumn = 0;
060: int numberOfColumns = 0;
061: int numberOfValidColumns = 0;
062: int columnTypes[] = null;
063: List categoryNames = new ArrayList();
064: Vector rows = new Vector();
065: Number[] newRow;
066:
067: jcds2DData = new DefaultKeyedValues2D();
068: Connection connection = null;
069: ResultSet resultSet = null;
070: PreparedStatement statement = null;
071: ResultSetMetaData metaData = null;
072:
073: try {
074: connection = DBUtil.getConnection(bjdsDataSource);
075: statement = connection.prepareStatement(bjdsSQL);
076: resultSet = statement.executeQuery();
077: metaData = resultSet.getMetaData();
078:
079: numberOfColumns = metaData.getColumnCount();
080: int startColumn = 1;
081: boolean preConfiguredCats = false;
082: if (jcdsCategoryArray != null) {
083: preConfiguredCats = true;
084: startColumn = 0;
085: }
086:
087: if (jcdsSeriesArray == null) {
088: jcdsSeriesArray = new String[numberOfColumns
089: - startColumn];
090:
091: int colCount = 0;
092: for (column = startColumn; column < numberOfColumns; column++) {
093: jcdsSeriesArray[colCount] = metaData
094: .getColumnLabel(column + 1);
095: colCount++;
096: }
097: }
098:
099: int catNum = 0;
100: while (resultSet.next()) {
101:
102: if (preConfiguredCats) {
103:
104: for (int i = 0; i < numberOfColumns; i++) {
105: jcds2DData.setValue((Number) resultSet
106: .getObject(i + 1), jcdsSeriesArray[i],
107: jcdsCategoryArray[catNum]);
108: }
109: catNum++;
110: } else {
111: /// First column is a category name
112: String cat = resultSet.getString(1);
113: for (int i = 1; i < numberOfColumns; i++) {
114: jcds2DData.setValue((Number) resultSet
115: .getObject(i + 1),
116: jcdsSeriesArray[i - 1], cat);
117: }
118: }
119: }
120:
121: } catch (Exception ex) {
122: throw new PortletException(
123: "Encountered database exception.", ex);
124: } finally {
125: DBUtil.close(resultSet);
126: DBUtil.close(statement);
127: DBUtil.close(connection);
128: }
129: }
130:
131: /**
132: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#createDummyData()
133: */
134: public void createDummyData() {
135:
136: if (jcds2DData == null) {
137: jcds2DData = new DefaultKeyedValues2D();
138: jcdsCategoryArray = new String[] { "mon", "tue", "wed",
139: "thu", "fri", "sat", "sun" };
140: jcdsSeriesArray = new String[] { "test1.jsp", "test2.jsp",
141: "test3.jsp", "test4.jsp" };
142:
143: for (int i = 0; i < jcdsCategoryArray.length; i++) {
144: int lastY = (int) (Math.random() * 1000 + 1000);
145: for (int j = 0; j < jcdsSeriesArray.length; j++) {
146: final int y = lastY
147: + (int) (Math.random() * 200 - 100);
148: lastY = y;
149: jcds2DData.setValue(new Integer(y),
150: jcdsSeriesArray[j], jcdsCategoryArray[i]);
151: }
152: }
153: }
154:
155: }
156:
157: /* (non-Javadoc)
158: * @see org.jfree.data.KeyedValues2D#getRowKey(int)
159: */
160: public Comparable getRowKey(int arg0) {
161: return jcds2DData.getRowKey(arg0);
162: }
163:
164: /* (non-Javadoc)
165: * @see org.jfree.data.KeyedValues2D#getRowIndex(java.lang.Comparable)
166: */
167: public int getRowIndex(Comparable arg0) {
168: return jcds2DData.getRowIndex(arg0);
169: }
170:
171: /* (non-Javadoc)
172: * @see org.jfree.data.KeyedValues2D#getRowKeys()
173: */
174: public List getRowKeys() {
175: return jcds2DData.getRowKeys();
176: }
177:
178: /* (non-Javadoc)
179: * @see org.jfree.data.KeyedValues2D#getColumnKey(int)
180: */
181: public Comparable getColumnKey(int arg0) {
182: return jcds2DData.getColumnKey(arg0);
183: }
184:
185: /* (non-Javadoc)
186: * @see org.jfree.data.KeyedValues2D#getColumnIndex(java.lang.Comparable)
187: */
188: public int getColumnIndex(Comparable arg0) {
189: return jcds2DData.getColumnIndex(arg0);
190: }
191:
192: /* (non-Javadoc)
193: * @see org.jfree.data.KeyedValues2D#getColumnKeys()
194: */
195: public List getColumnKeys() {
196: return jcds2DData.getColumnKeys();
197: }
198:
199: /* (non-Javadoc)
200: * @see org.jfree.data.KeyedValues2D#getValue(java.lang.Comparable, java.lang.Comparable)
201: */
202: public Number getValue(Comparable arg0, Comparable arg1) {
203: return jcds2DData.getValue(arg0, arg1);
204: }
205:
206: /* (non-Javadoc)
207: * @see org.jfree.data.Values2D#getRowCount()
208: */
209: public int getRowCount() {
210: return jcds2DData.getRowCount();
211: }
212:
213: /* (non-Javadoc)
214: * @see org.jfree.data.Values2D#getColumnCount()
215: */
216: public int getColumnCount() {
217: return jcds2DData.getColumnCount();
218: }
219:
220: /* (non-Javadoc)
221: * @see org.jfree.data.Values2D#getValue(int, int)
222: */
223: public Number getValue(int arg0, int arg1) {
224: return jcds2DData.getValue(arg0, arg1);
225: }
226:
227: }
|