1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/dp/DataSourceXYSeries.java Sat Feb 28 21:31:02 2009 +0100
1.3 @@ -0,0 +1,67 @@
1.4 +package de.laures.cewolf.dp;
1.5 +
1.6 +import java.io.Serializable;
1.7 +import java.sql.Connection;
1.8 +import java.sql.ResultSet;
1.9 +import java.util.Map;
1.10 +
1.11 +import javax.naming.Context;
1.12 +import javax.naming.InitialContext;
1.13 +import javax.naming.NamingException;
1.14 +import javax.sql.DataSource;
1.15 +
1.16 +import org.jfree.data.xy.XYSeries;
1.17 +
1.18 +import de.laures.cewolf.DatasetProduceException;
1.19 +
1.20 +/**
1.21 + * @author glaures
1.22 + *
1.23 + * To change this generated comment edit the template variable "typecomment":
1.24 + * Window>Preferences>Java>Templates.
1.25 + * To enable and disable the creation of type comments go to
1.26 + * Window>Preferences>Java>Code Generation.
1.27 + */
1.28 +public class DataSourceXYSeries implements Serializable {
1.29 +
1.30 + private String dataSourceName;
1.31 + private String query;
1.32 + private String xCol = "x";
1.33 + private String yCol = "y";
1.34 + private String seriesName = "name";
1.35 +
1.36 + /**
1.37 + * Constructor for DataSourceXYSeries.
1.38 + */
1.39 + public DataSourceXYSeries(String dataSourceName, String query) {
1.40 + this.dataSourceName = dataSourceName;
1.41 + this.query = query;
1.42 + }
1.43 +
1.44 + protected DataSource getDataSource() throws NamingException {
1.45 + Context initCtx = new InitialContext();
1.46 + Context envCtx = (Context) initCtx.lookup("java:comp/env");
1.47 + return (DataSource) envCtx.lookup(dataSourceName);
1.48 + }
1.49 +
1.50 + /**
1.51 + * @see de.laures.cewolf.DatasetProducer#produceDataset(Map)
1.52 + */
1.53 + public XYSeries produceXYSeries() throws DatasetProduceException {
1.54 + XYSeries series = new XYSeries(seriesName);
1.55 + try {
1.56 + DataSource ds = getDataSource();
1.57 + Connection con = ds.getConnection();
1.58 + ResultSet rs = con.createStatement().executeQuery(query);
1.59 + int xColIndex = rs.findColumn(xCol);
1.60 + int yColIndex = rs.findColumn(yCol);
1.61 + while(rs.next()){
1.62 + series.add((Number)rs.getObject(xColIndex), (Number)rs.getObject(yColIndex));
1.63 + }
1.64 + } catch (Exception namingEx) {
1.65 + namingEx.printStackTrace();
1.66 + throw new DatasetProduceException(namingEx.getMessage());
1.67 + }
1.68 + return series;
1.69 + }
1.70 +}