java/cewolf-1.0/src/main/java/de/laures/cewolf/dp/DataSourceXYSeries.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
     1 package de.laures.cewolf.dp;
     2 
     3 import java.io.Serializable;
     4 import java.sql.Connection;
     5 import java.sql.ResultSet;
     6 import java.util.Map;
     7 
     8 import javax.naming.Context;
     9 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), (Number)rs.getObject(yColIndex));
    60 			}
    61 		} catch (Exception namingEx) {
    62 			namingEx.printStackTrace();
    63 			throw new DatasetProduceException(namingEx.getMessage());
    64 		}
    65 		return series;
    66 	}
    67 }