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: import java.sql.Connection;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026:
027: import javax.portlet.PortletException;
028:
029: import org.jfree.chart.tooltips.PieToolTipGenerator;
030: import org.jfree.chart.urls.PieURLGenerator;
031: import org.jfree.data.DefaultKeyedValues;
032: import org.jfree.data.PieDataset;
033:
034: import com.nabhinc.util.db.DBUtil;
035:
036: /**
037: *
038: *
039: * @author Padmanabh Dabke
040: * (c) 2002, 2003 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class JDBCPieDataset extends BaseJDBCDataset implements
043: PieDataset, PieToolTipGenerator, PieURLGenerator {
044:
045: private static final long serialVersionUID = -2779799940607207488L;
046:
047: private DefaultKeyedValues jpdsPieData = null;
048:
049: /**
050: * Dummy data.
051: */
052: private String[] jpdsDummyData = { "mon", "tue", "wed", "thu",
053: "fri", "sat", "sun" };
054:
055: /**
056: * Categories specified in configuration.
057: * @see com.nabhinc.core.XMLInitable#init(Element)
058: */
059: private String[] jpdsConfiguredCategories = null;
060:
061: /**
062: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#createDummyData()
063: */
064: public void createDummyData() {
065: if (jpdsPieData == null) {
066: jpdsPieData = new DefaultKeyedValues();
067: for (int i = 0; i < jpdsDummyData.length; i++) {
068: jpdsPieData.setValue(jpdsDummyData[i], new Integer(i
069: * i - i + 1));
070: }
071: }
072: }
073:
074: /**
075: * @see com.nabhinc.portal.charting.AbstractDataSet#executeQuery()
076: */
077: public void executeQuery() throws PortletException {
078: Object xObject = null;
079: int column = 0;
080: int numberOfColumns = 0;
081: int numberOfValidColumns = 0;
082: int columnTypes[] = null;
083:
084: jpdsPieData = new DefaultKeyedValues();
085: Connection connection = null;
086: ResultSet resultSet = null;
087: PreparedStatement statement = null;
088: try {
089: connection = DBUtil.getConnection(bjdsDataSource);
090: statement = connection.prepareStatement(bjdsSQL);
091: resultSet = statement.executeQuery();
092: /// might need to add, to free memory from any previous result sets
093: int counter = 0;
094: while (resultSet.next()) {
095: Object category = null;
096: Number value = null;
097: if (jpdsConfiguredCategories == null) {
098: category = resultSet.getString(1);
099: //System.out.println("Category = " + category);
100: value = (Number) resultSet.getObject(2);
101: } else {
102: category = jpdsConfiguredCategories[counter];
103: counter++;
104: value = (Number) resultSet.getObject(1);
105: }
106: jpdsPieData.setValue((Comparable) category, value);
107:
108: }
109:
110: fireDatasetChanged();
111:
112: } catch (Exception ex) {
113: throw new PortletException("Database exception.", ex);
114: } finally {
115: DBUtil.close(resultSet);
116: DBUtil.close(statement);
117: DBUtil.close(connection);
118: }
119: }
120:
121: /**
122: * @see de.laures.cewolf.PieSectionLinkGenerator#generateLink(java.lang.Object, java.lang.Object)
123: */
124: public String generateLink(Object dataset, Object category) {
125: return "";
126: }
127:
128: /**
129: * @see org.jfree.chart.tooltips.PieToolTipGenerator#generateToolTip(com.jrefinery.data.PieDataset, java.lang.Object)
130: */
131: public String generateToolTip(PieDataset data, Object category) {
132: return category.toString();
133: }
134:
135: /**
136: * @see org.jfree.chart.urls.PieURLGenerator#generateURL(com.jrefinery.data.PieDataset, java.lang.Object)
137: */
138: public String generateURL(PieDataset data, Object category) {
139: return "http://java.sun.com?cat=" + category.toString();
140: }
141:
142: /* (non-Javadoc)
143: * @see org.jfree.data.KeyedValues#getKey(int)
144: */
145: public Comparable getKey(int arg0) {
146: return jpdsPieData.getKey(arg0);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.jfree.data.KeyedValues#getIndex(java.lang.Comparable)
151: */
152: public int getIndex(Comparable arg0) {
153: return jpdsPieData.getIndex(arg0);
154: }
155:
156: /* (non-Javadoc)
157: * @see org.jfree.data.KeyedValues#getValue(java.lang.Comparable)
158: */
159: public Number getValue(Comparable arg0) {
160: return jpdsPieData.getValue(arg0);
161: }
162:
163: /* (non-Javadoc)
164: * @see org.jfree.data.Values#getItemCount()
165: */
166: public int getItemCount() {
167: return jpdsPieData.getItemCount();
168:
169: }
170:
171: /* (non-Javadoc)
172: * @see org.jfree.chart.tooltips.PieToolTipGenerator#generateToolTip(org.jfree.data.PieDataset, java.lang.Comparable, int)
173: */
174: public String generateToolTip(PieDataset arg0, Comparable arg1,
175: int arg2) {
176: return "";
177: }
178:
179: /* (non-Javadoc)
180: * @see org.jfree.chart.urls.PieURLGenerator#generateURL(org.jfree.data.PieDataset, java.lang.Comparable, int)
181: */
182: public String generateURL(PieDataset arg0, Comparable arg1, int arg2) {
183: return "";
184: }
185:
186: /* (non-Javadoc)
187: * @see org.jfree.data.KeyedValues#getKeys()
188: */
189: public List getKeys() {
190: return jpdsPieData.getKeys();
191:
192: }
193:
194: /* (non-Javadoc)
195: * @see org.jfree.data.Values#getValue(int)
196: */
197: public Number getValue(int item) {
198: return jpdsPieData.getValue(item);
199: }
200:
201: }
|