java/cewolf-1.0/src/site/tutorial/step2.html
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/site/tutorial/step2.html	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Cewolf Tutorial(2): Provide a DatasetProducer</title>
     1.8 +  <meta name="author" content="Guido Laures">
     1.9 +  <link href="../cewolf.css" rel="stylesheet" type="text/css">
    1.10 +</head>
    1.11 +<body>
    1.12 +<h1>Tutorial: Step 2</h1>
    1.13 +<h2>Provide a DatasetProducer</h2>
    1.14 +<p> As Cewolf uses a MVC (Model-View-Control) approach the data which
    1.15 +are shown in your chart are separated from the view defined in the JSP
    1.16 +page. So you can change them separately. To provide the chart with the
    1.17 +correct data you must provide an object which implements the interface<tt>de.laures.cewolf.DatasetProducer</tt>.
    1.18 +This object is asked to produce data every time a new chart must be
    1.19 +rendered. Below you can see an example implementation of a
    1.20 +DatasetProducer which could be used to provide data needed for our
    1.21 +example scenario. </p>
    1.22 +<p> </p>
    1.23 +<pre>package de.laures.cewolf.example;
    1.24 +
    1.25 +import java.io.Serializable;
    1.26 +import java.util.Date;
    1.27 +import java.util.Map;
    1.28 +
    1.29 +import org.apache.commons.logging.Log;
    1.30 +import org.apache.commons.logging.LogFactory;
    1.31 +import org.jfree.data.category.CategoryDataset;
    1.32 +import org.jfree.data.category.DefaultCategoryDataset;
    1.33 +
    1.34 +import de.laures.cewolf.DatasetProduceException;
    1.35 +import de.laures.cewolf.DatasetProducer;
    1.36 +import de.laures.cewolf.links.CategoryItemLinkGenerator;
    1.37 +import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
    1.38 +
    1.39 +/** 
    1.40 + * An example data producer.
    1.41 + * @author  Guido Laures 
    1.42 + */
    1.43 +public class PageViewCountData implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
    1.44 +
    1.45 +    private static final Log log = LogFactory.getLog(PageViewCountData.class);
    1.46 +
    1.47 +    // These values would normally not be hard coded but produced by
    1.48 +    // some kind of data source like a database or a file
    1.49 +    private final String[] categories =    {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
    1.50 +    private final String[] seriesNames =    {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};
    1.51 +
    1.52 +	/**
    1.53 +	 *  Produces some random data.
    1.54 +	 */
    1.55 +    public Object produceDataset(Map params) throws DatasetProduceException {
    1.56 +    	log.debug("producing data.");
    1.57 +        DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
    1.58 +			/**
    1.59 +			 * @see java.lang.Object#finalize()
    1.60 +			 */
    1.61 +			protected void finalize() throws Throwable {
    1.62 +				super.finalize();
    1.63 +				log.debug(this +" finalized.");
    1.64 +			}
    1.65 +        };
    1.66 +        for (int series = 0; series < seriesNames.length; series ++) {
    1.67 +            int lastY = (int)(Math.random() * 1000 + 1000);
    1.68 +            for (int i = 0; i < categories.length; i++) {
    1.69 +                final int y = lastY + (int)(Math.random() * 200 - 100);
    1.70 +                lastY = y;
    1.71 +                dataset.addValue(y, seriesNames[series], categories[i]);
    1.72 +            }
    1.73 +        }
    1.74 +        return dataset;
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * This producer's data is invalidated after 5 seconds. By this method the
    1.79 +     * producer can influence Cewolf's caching behaviour the way it wants to.
    1.80 +     */
    1.81 +	public boolean hasExpired(Map params, Date since) {		
    1.82 +        log.debug(getClass().getName() + "hasExpired()");
    1.83 +		return (System.currentTimeMillis() - since.getTime())  > 5000;
    1.84 +	}
    1.85 +
    1.86 +	/**
    1.87 +	 * Returns a unique ID for this DatasetProducer
    1.88 +	 */
    1.89 +	public String getProducerId() {
    1.90 +		return "PageViewCountData DatasetProducer";
    1.91 +	}
    1.92 +
    1.93 +    /**
    1.94 +     * Returns a link target for a special data item.
    1.95 +     */
    1.96 +    public String generateLink(Object data, int series, Object category) {
    1.97 +        return seriesNames[series];
    1.98 +    }
    1.99 +    
   1.100 +	/**
   1.101 +	 * @see java.lang.Object#finalize()
   1.102 +	 */
   1.103 +	protected void finalize() throws Throwable {
   1.104 +		super.finalize();
   1.105 +		log.debug(this + " finalized.");
   1.106 +	}
   1.107 +
   1.108 +	/**
   1.109 +	 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
   1.110 +	 */
   1.111 +	public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
   1.112 +		return seriesNames[series];
   1.113 +	}
   1.114 +
   1.115 +}
   1.116 +</pre>
   1.117 +<p> As you can see this datasetproducer is not very useful. Normally
   1.118 +this class would try to access a datasource (e.g. a database) to access
   1.119 +the needed information. But to serve as an example it should do. </p>
   1.120 +<p> A DatasetProducer needs to implement three methods. The most
   1.121 +important one is the <tt>produceDataset()</tt> method which actually
   1.122 +produces the data which should be used to render a chart. This method
   1.123 +takes a parameter map which is filled by some special tags of the JSP
   1.124 +which will be explained later on. </p>
   1.125 +<p> The <tt>hasExpired()</tt> method is called by the Cewolf framework
   1.126 +if there already exits a data object produced by this producer in
   1.127 +Cewolf's data cache. When returning <tt>true</tt> the producer
   1.128 +signalizes that the data formerly used has expired. </p>
   1.129 +<p> By providing an unique ID via the <tt>getProducerId()</tt> method
   1.130 +the Cewolf framework identifies a producer type. Two producer instances
   1.131 +with the same ID are supposed to produce the same data. </p>
   1.132 +<p> Compile the class and put it in the correct folder structure under
   1.133 +your web application's <tt>/WEB-INF/classes</tt> directory to make it
   1.134 +available for your application. </p>
   1.135 +<p> <a href="step3.html">Step 3: Install the Cewolf Servlet in your Web
   1.136 +Application&gt;&gt;</a> </p>
   1.137 +<a height="30" target="new" href="http://sourceforge.net"><img
   1.138 + alt="SourceForge Logo" border="0" height="30"
   1.139 + src="http://sourceforge.net/sflogo.php?group_id=57282&amp;type=5"></a>
   1.140 +</body>
   1.141 +</html>