View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/cewolf/CewolfHighLowSeriesDataTag.java,v 1.2 2006/03/16 14:23:02 hkollmann Exp $
3    * $Revision: 1.2 $
4    * $Date: 2006/03/16 14:23:02 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.taglib.cewolf;
25  
26  import java.util.ArrayList;
27  import java.util.Date;
28  import java.util.HashMap;
29  
30  import javax.servlet.jsp.JspException;
31  
32  import de.laures.cewolf.taglib.DataAware;
33  import de.laures.cewolf.DatasetProducer;
34  
35  import org.jfree.data.xy.AbstractXYDataset;
36  import org.jfree.data.xy.OHLCDataItem;
37  import org.jfree.data.xy.OHLCDataset;
38  
39  import org.dbforms.taglib.AbstractDbBaseHandlerTag;
40  import org.dbforms.config.ResultSetVector;
41  import org.dbforms.util.Util;
42  
43  /***
44   * 
45   * This tag defines a HighLow Series for use inside a cewolf timeseries
46   * 
47   * @author Henner Kollmann
48   * 
49   */
50  public class CewolfHighLowSeriesDataTag extends AbstractDbBaseHandlerTag {
51  
52  	private String timeField;
53  	private String openField;
54  	private String highField;
55  	private String lowField;
56  	private String closeField;
57  	private String title;
58  
59  	public int doEndTag() throws JspException {
60  		String s = getTitle();
61  		if (Util.isNull(s)) {
62  			s = getTimeField();
63  		}
64  		CewolfOHLCDataset ds = new CewolfOHLCDataset(s);
65  		ResultSetVector rsv = getParentForm().getResultSetVector();
66  
67  		for (int i = 0; i < rsv.size(); i++) {
68  			Date d = (Date) rsv.getFieldAsObject(i, getTimeField());
69  			Double dd = (Double) rsv.getFieldAsObject(i, getHighField());
70  			double h = (dd != null) ? dd.doubleValue() : 0;
71  			dd = (Double) rsv.getFieldAsObject(i, getOpenField());
72  			double o = (dd != null) ? dd.doubleValue() : h;
73  			dd = (Double) rsv.getFieldAsObject(i, getLowField());
74  			double l = (dd != null) ? dd.doubleValue() : 0;
75  			dd = (Double) rsv.getFieldAsObject(i, getCloseField());
76  			double c = (dd != null) ? dd.doubleValue() : l;
77  			ds.add(0, new OHLCDataItem(d, o, h, l, c, 0));
78  		}
79  
80  		DatasetProducer dataProducer = new DbFormsDatasetProducer(ds);
81  		DataAware dw = (DataAware) findAncestorWithClass(this, DataAware.class);
82  		dw.setDataProductionConfig(dataProducer, new HashMap(), false);
83  
84  		return SKIP_BODY;
85  	}
86  
87  	public void doFinally() {
88  		super.doFinally();
89  	}
90  
91  	public String getCloseField() {
92  		return closeField;
93  	}
94  
95  	public void setCloseField(String closeField) {
96  		this.closeField = closeField;
97  	}
98  
99  	public String getHighField() {
100 		return highField;
101 	}
102 
103 	public void setHighField(String highField) {
104 		this.highField = highField;
105 	}
106 
107 	public String getLowField() {
108 		return lowField;
109 	}
110 
111 	public void setLowField(String lowField) {
112 		this.lowField = lowField;
113 	}
114 
115 	public String getOpenField() {
116 		return openField;
117 	}
118 
119 	public void setOpenField(String openField) {
120 		this.openField = openField;
121 	}
122 
123 	public String getTimeField() {
124 		return timeField;
125 	}
126 
127 	public void setTimeField(String timeField) {
128 		this.timeField = timeField;
129 	}
130 
131 	public String getTitle() {
132 		return title;
133 	}
134 
135 	public void setTitle(String title) {
136 		this.title = title;
137 	}
138 
139 	private class CewolfOHLCDataset extends AbstractXYDataset implements
140 			OHLCDataset {
141 
142 		private ArrayList data = new ArrayList();
143 
144 		private String key;
145 
146 		/***
147 		 * Creates a new dataset.
148 		 * 
149 		 * @param key
150 		 *            the series key.
151 		 * @param data
152 		 *            the data items.
153 		 */
154 		public CewolfOHLCDataset(String key) {
155 			this.key = key;
156 		}
157 
158 		public void add(int series, OHLCDataItem item) {
159 			data.add(item);
160 		}
161 
162 		/***
163 		 * Returns the series key.
164 		 * 
165 		 * @param series
166 		 *            the series index (ignored).
167 		 * 
168 		 * @return The series key.
169 		 */
170 		public Comparable getSeriesKey(int series) {
171 			return key;
172 		}
173 
174 		/***
175 		 * Returns the x-value for a data item.
176 		 * 
177 		 * @param series
178 		 *            the series index (ignored).
179 		 * @param item
180 		 *            the item index (zero-based).
181 		 * 
182 		 * @return The x-value.
183 		 */
184 		public Number getX(int series, int item) {
185 			return new Long(getXDate(series, item).getTime());
186 		}
187 
188 		/***
189 		 * Returns the x-value for a data item as a date.
190 		 * 
191 		 * @param series
192 		 *            the series index (ignored).
193 		 * @param item
194 		 *            the item index (zero-based).
195 		 * 
196 		 * @return The x-value as a date.
197 		 */
198 		public Date getXDate(int series, int item) {
199 			return ((OHLCDataItem) data.get(item)).getDate();
200 		}
201 
202 		/***
203 		 * Returns the y-value.
204 		 * 
205 		 * @param series
206 		 *            the series index (ignored).
207 		 * @param item
208 		 *            the item index (zero-based).
209 		 * 
210 		 * @return The y value.
211 		 */
212 		public Number getY(int series, int item) {
213 			return getClose(series, item);
214 		}
215 
216 		/***
217 		 * Returns the high value.
218 		 * 
219 		 * @param series
220 		 *            the series index (ignored).
221 		 * @param item
222 		 *            the item index (zero-based).
223 		 * 
224 		 * @return The high value.
225 		 */
226 		public Number getHigh(int series, int item) {
227 			return ((OHLCDataItem) data.get(item)).getHigh();
228 		}
229 
230 		/***
231 		 * Returns the high-value (as a double primitive) for an item within a
232 		 * series.
233 		 * 
234 		 * @param series
235 		 *            the series (zero-based index).
236 		 * @param item
237 		 *            the item (zero-based index).
238 		 * 
239 		 * @return The high-value.
240 		 */
241 		public double getHighValue(int series, int item) {
242 			double result = Double.NaN;
243 			Number high = getHigh(series, item);
244 			if (high != null) {
245 				result = high.doubleValue();
246 			}
247 			return result;
248 		}
249 
250 		/***
251 		 * Returns the low value.
252 		 * 
253 		 * @param series
254 		 *            the series index (ignored).
255 		 * @param item
256 		 *            the item index (zero-based).
257 		 * 
258 		 * @return The low value.
259 		 */
260 		public Number getLow(int series, int item) {
261 			return ((OHLCDataItem) data.get(item)).getLow();
262 		}
263 
264 		/***
265 		 * Returns the low-value (as a double primitive) for an item within a
266 		 * series.
267 		 * 
268 		 * @param series
269 		 *            the series (zero-based index).
270 		 * @param item
271 		 *            the item (zero-based index).
272 		 * 
273 		 * @return The low-value.
274 		 */
275 		public double getLowValue(int series, int item) {
276 			double result = Double.NaN;
277 			Number low = getLow(series, item);
278 			if (low != null) {
279 				result = low.doubleValue();
280 			}
281 			return result;
282 		}
283 
284 		/***
285 		 * Returns the open value.
286 		 * 
287 		 * @param series
288 		 *            the series index (ignored).
289 		 * @param item
290 		 *            the item index (zero-based).
291 		 * 
292 		 * @return The open value.
293 		 */
294 		public Number getOpen(int series, int item) {
295 			return ((OHLCDataItem) data.get(item)).getOpen();
296 		}
297 
298 		/***
299 		 * Returns the open-value (as a double primitive) for an item within a
300 		 * series.
301 		 * 
302 		 * @param series
303 		 *            the series (zero-based index).
304 		 * @param item
305 		 *            the item (zero-based index).
306 		 * 
307 		 * @return The open-value.
308 		 */
309 		public double getOpenValue(int series, int item) {
310 			double result = Double.NaN;
311 			Number open = getOpen(series, item);
312 			if (open != null) {
313 				result = open.doubleValue();
314 			}
315 			return result;
316 		}
317 
318 		/***
319 		 * Returns the close value.
320 		 * 
321 		 * @param series
322 		 *            the series index (ignored).
323 		 * @param item
324 		 *            the item index (zero-based).
325 		 * 
326 		 * @return The close value.
327 		 */
328 		public Number getClose(int series, int item) {
329 			return ((OHLCDataItem) data.get(item)).getClose();
330 		}
331 
332 		/***
333 		 * Returns the close-value (as a double primitive) for an item within a
334 		 * series.
335 		 * 
336 		 * @param series
337 		 *            the series (zero-based index).
338 		 * @param item
339 		 *            the item (zero-based index).
340 		 * 
341 		 * @return The close-value.
342 		 */
343 		public double getCloseValue(int series, int item) {
344 			double result = Double.NaN;
345 			Number close = getClose(series, item);
346 			if (close != null) {
347 				result = close.doubleValue();
348 			}
349 			return result;
350 		}
351 
352 		/***
353 		 * Returns the trading volume.
354 		 * 
355 		 * @param series
356 		 *            the series index (ignored).
357 		 * @param item
358 		 *            the item index (zero-based).
359 		 * 
360 		 * @return The trading volume.
361 		 */
362 		public Number getVolume(int series, int item) {
363 			return ((OHLCDataItem) data.get(item)).getVolume();
364 		}
365 
366 		/***
367 		 * Returns the volume-value (as a double primitive) for an item within a
368 		 * series.
369 		 * 
370 		 * @param series
371 		 *            the series (zero-based index).
372 		 * @param item
373 		 *            the item (zero-based index).
374 		 * 
375 		 * @return The volume-value.
376 		 */
377 		public double getVolumeValue(int series, int item) {
378 			double result = Double.NaN;
379 			Number volume = getVolume(series, item);
380 			if (volume != null) {
381 				result = volume.doubleValue();
382 			}
383 			return result;
384 		}
385 
386 		/***
387 		 * Returns the series count.
388 		 * 
389 		 * @return 1.
390 		 */
391 		public int getSeriesCount() {
392 			return 1;
393 		}
394 
395 		/***
396 		 * Returns the item count for the specified series.
397 		 * 
398 		 * @param series
399 		 *            the series index (ignored).
400 		 * 
401 		 * @return The item count.
402 		 */
403 		public int getItemCount(int series) {
404 			return data.size();
405 		}
406 
407 		/***
408 		 * Tests this instance for equality with an arbitrary object.
409 		 * 
410 		 * @param obj
411 		 *            the object (<code>null</code> permitted).
412 		 * 
413 		 * @return A boolean.
414 		 */
415 		public boolean equals(Object obj) {
416 			if (this == obj) {
417 				return true;
418 			}
419 			if (!(obj instanceof CewolfOHLCDataset)) {
420 				return false;
421 			}
422 			return this.data.equals(((CewolfOHLCDataset) obj).data);
423 		}
424 
425 	}
426 
427 }