franta-hg@1: package de.laures.cewolf.dp; franta-hg@1: franta-hg@1: import java.io.Serializable; franta-hg@1: import java.sql.Connection; franta-hg@1: import java.sql.ResultSet; franta-hg@1: import java.util.Map; franta-hg@1: franta-hg@1: import javax.naming.Context; franta-hg@1: import javax.naming.InitialContext; franta-hg@1: import javax.naming.NamingException; franta-hg@1: import javax.sql.DataSource; franta-hg@1: franta-hg@1: import org.jfree.data.xy.XYSeries; franta-hg@1: franta-hg@1: import de.laures.cewolf.DatasetProduceException; franta-hg@1: franta-hg@1: /** franta-hg@1: * @author glaures franta-hg@1: * franta-hg@1: * To change this generated comment edit the template variable "typecomment": franta-hg@1: * Window>Preferences>Java>Templates. franta-hg@1: * To enable and disable the creation of type comments go to franta-hg@1: * Window>Preferences>Java>Code Generation. franta-hg@1: */ franta-hg@1: public class DataSourceXYSeries implements Serializable { franta-hg@1: franta-hg@1: private String dataSourceName; franta-hg@1: private String query; franta-hg@1: private String xCol = "x"; franta-hg@1: private String yCol = "y"; franta-hg@1: private String seriesName = "name"; franta-hg@1: franta-hg@1: /** franta-hg@1: * Constructor for DataSourceXYSeries. franta-hg@1: */ franta-hg@1: public DataSourceXYSeries(String dataSourceName, String query) { franta-hg@1: this.dataSourceName = dataSourceName; franta-hg@1: this.query = query; franta-hg@1: } franta-hg@1: franta-hg@1: protected DataSource getDataSource() throws NamingException { franta-hg@1: Context initCtx = new InitialContext(); franta-hg@1: Context envCtx = (Context) initCtx.lookup("java:comp/env"); franta-hg@1: return (DataSource) envCtx.lookup(dataSourceName); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.DatasetProducer#produceDataset(Map) franta-hg@1: */ franta-hg@1: public XYSeries produceXYSeries() throws DatasetProduceException { franta-hg@1: XYSeries series = new XYSeries(seriesName); franta-hg@1: try { franta-hg@1: DataSource ds = getDataSource(); franta-hg@1: Connection con = ds.getConnection(); franta-hg@1: ResultSet rs = con.createStatement().executeQuery(query); franta-hg@1: int xColIndex = rs.findColumn(xCol); franta-hg@1: int yColIndex = rs.findColumn(yCol); franta-hg@1: while(rs.next()){ franta-hg@1: series.add((Number)rs.getObject(xColIndex), (Number)rs.getObject(yColIndex)); franta-hg@1: } franta-hg@1: } catch (Exception namingEx) { franta-hg@1: namingEx.printStackTrace(); franta-hg@1: throw new DatasetProduceException(namingEx.getMessage()); franta-hg@1: } franta-hg@1: return series; franta-hg@1: } franta-hg@1: }