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 org.jfree.data.HighLowDataset;
023:
024: import com.nabhinc.util.db.DBUtil;
025:
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.util.Vector;
030:
031: import javax.portlet.PortletException;
032:
033: /**
034: *
035: *
036: * @author Padmanabh Dabke
037: * (c) 2002, 2003 Nabh Information Systems, Inc. All Rights Reserved.
038: */
039: public class JDBCHighLowDataset extends BaseJDBCDataset implements
040: HighLowDataset {
041:
042: private static final long serialVersionUID = -6654476935158347558L;
043:
044: public boolean jhldsDrawVolume = false;
045: private Vector jhldsDates = null;
046: private Vector jhldsHigh = null;
047: private Vector jhldsLow = null;
048: private Vector jhldsOpen = null;
049: private Vector jhldsClose = null;
050: private Vector jhldsVol = null;
051: private String jhldsSeriesName = 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 numberOfColumns = 0;
060: int numberOfValidColumns = 0;
061: int columnTypes[] = null;
062:
063: Connection connection = null;
064: ResultSet resultSet = null;
065: PreparedStatement statement = null;
066: jhldsDates = new Vector();
067: jhldsHigh = new Vector();
068: jhldsLow = new Vector();
069: jhldsOpen = new Vector();
070: jhldsClose = new Vector();
071: try {
072: connection = DBUtil.getConnection(bjdsDataSource);
073: statement = connection.prepareStatement(bjdsSQL);
074: resultSet = statement.executeQuery();
075: /// might need to add, to free memory from any previous result sets
076: Vector points = new Vector();
077: while (resultSet.next()) {
078: jhldsDates.addElement(resultSet.getDate(1));
079: jhldsHigh.addElement(resultSet.getObject(2));
080: jhldsLow.addElement(resultSet.getObject(3));
081: jhldsOpen.addElement(resultSet.getObject(4));
082: jhldsClose.addElement(resultSet.getObject(5));
083: if (jhldsDrawVolume) {
084: jhldsVol.addElement(resultSet.getObject(6));
085: }
086:
087: }
088:
089: fireDatasetChanged();
090:
091: } catch (Exception ex) {
092: throw new PortletException(
093: "Encountered database exception.", ex);
094: } finally {
095: DBUtil.close(resultSet);
096: DBUtil.close(statement);
097: DBUtil.close(connection);
098:
099: }
100: }
101:
102: /**
103: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#createDummyData()
104: */
105: public void createDummyData() {
106: jhldsDates = new Vector();
107: jhldsHigh = new Vector();
108: jhldsLow = new Vector();
109: jhldsOpen = new Vector();
110: jhldsClose = new Vector();
111: }
112:
113: /**
114: * @see org.jfree.data.HighLowDataset#getHighValue(int, int)
115: */
116: public Number getHighValue(int series, int item) {
117: if (jhldsHigh == null) {
118: return null;
119: } else {
120: return (Number) jhldsHigh.elementAt(item);
121: }
122: }
123:
124: /**
125: * @see org.jfree.data.HighLowDataset#getLowValue(int, int)
126: */
127: public Number getLowValue(int series, int item) {
128: if (jhldsLow == null) {
129: return null;
130: } else {
131: return (Number) jhldsLow.elementAt(item);
132: }
133: }
134:
135: /**
136: * @see org.jfree.data.HighLowDataset#getOpenValue(int, int)
137: */
138: public Number getOpenValue(int series, int item) {
139: if (jhldsOpen == null) {
140: return null;
141: } else {
142: return (Number) jhldsOpen.elementAt(item);
143: }
144: }
145:
146: /**
147: * @see org.jfree.data.HighLowDataset#getCloseValue(int, int)
148: */
149: public Number getCloseValue(int series, int item) {
150: if (jhldsClose == null) {
151: return null;
152: } else {
153: return (Number) jhldsClose.elementAt(item);
154: }
155: }
156:
157: /**
158: * @see org.jfree.data.HighLowDataset#getVolumeValue(int, int)
159: */
160: public Number getVolumeValue(int series, int item) {
161: if (jhldsVol == null) {
162: return null;
163: } else {
164: return (Number) jhldsVol.elementAt(item);
165: }
166: }
167:
168: /**
169: * @see org.jfree.data.XYDataset#getItemCount(int)
170: */
171: public int getItemCount(int series) {
172: return jhldsDates.size();
173: }
174:
175: /**
176: * Returns the x-value for one item in a series.
177: * <p>
178: * The value returned is a Long object generated from the underlying Date object.
179: *
180: * @param series the series (zero-based index).
181: * @param item the item (zero-based index).
182: *
183: * @return the x-value.
184: */
185: public Number getXValue(int series, int item) {
186: return new Long(((java.sql.Date) jhldsDates.elementAt(item))
187: .getTime());
188: }
189:
190: /**
191: * Returns the y-value for one item in a series.
192: * <p>
193: * This method (from the XYDataset interface) is mapped to the getCloseValue(...) method.
194: *
195: * @param series the series (zero-based index).
196: * @param item the item (zero-based index).
197: *
198: * @return the y-value.
199: */
200: public Number getYValue(int series, int item) {
201: return getCloseValue(series, item);
202: }
203:
204: /**
205: * @see org.jfree.data.SeriesDataset#getSeriesCount()
206: */
207: public int getSeriesCount() {
208: return 1;
209: }
210:
211: /**
212: * @see org.jfree.data.SeriesDataset#getSeriesName(int)
213: */
214: public String getSeriesName(int series) {
215: return jhldsSeriesName;
216: }
217: }
|