01: package de.laures.cewolf.dp;
02:
03: import java.io.Serializable;
04: import java.sql.Connection;
05: import java.sql.ResultSet;
06: import java.util.Map;
07:
08: import javax.naming.Context;
09: import javax.naming.InitialContext;
10: import javax.naming.NamingException;
11: import javax.sql.DataSource;
12:
13: import org.jfree.data.xy.XYSeries;
14:
15: import de.laures.cewolf.DatasetProduceException;
16:
17: /**
18: * @author glaures
19: *
20: * To change this generated comment edit the template variable "typecomment":
21: * Window>Preferences>Java>Templates.
22: * To enable and disable the creation of type comments go to
23: * Window>Preferences>Java>Code Generation.
24: */
25: public class DataSourceXYSeries implements Serializable {
26:
27: private String dataSourceName;
28: private String query;
29: private String xCol = "x";
30: private String yCol = "y";
31: private String seriesName = "name";
32:
33: /**
34: * Constructor for DataSourceXYSeries.
35: */
36: public DataSourceXYSeries(String dataSourceName, String query) {
37: this .dataSourceName = dataSourceName;
38: this .query = query;
39: }
40:
41: protected DataSource getDataSource() throws NamingException {
42: Context initCtx = new InitialContext();
43: Context envCtx = (Context) initCtx.lookup("java:comp/env");
44: return (DataSource) envCtx.lookup(dataSourceName);
45: }
46:
47: /**
48: * @see de.laures.cewolf.DatasetProducer#produceDataset(Map)
49: */
50: public XYSeries produceXYSeries() throws DatasetProduceException {
51: XYSeries series = new XYSeries(seriesName);
52: try {
53: DataSource ds = getDataSource();
54: Connection con = ds.getConnection();
55: ResultSet rs = con.createStatement().executeQuery(query);
56: int xColIndex = rs.findColumn(xCol);
57: int yColIndex = rs.findColumn(yCol);
58: while (rs.next()) {
59: series.add((Number) rs.getObject(xColIndex),
60: (Number) rs.getObject(yColIndex));
61: }
62: } catch (Exception namingEx) {
63: namingEx.printStackTrace();
64: throw new DatasetProduceException(namingEx.getMessage());
65: }
66: return series;
67: }
68: }
|