franta-hg@1: franta-hg@1: franta-hg@1: franta-hg@1: Cewolf Tutorial(2): Provide a DatasetProducer franta-hg@1: franta-hg@1: franta-hg@1: franta-hg@1: franta-hg@1:

Tutorial: Step 2

franta-hg@1:

Provide a DatasetProducer

franta-hg@1:

As Cewolf uses a MVC (Model-View-Control) approach the data which franta-hg@1: are shown in your chart are separated from the view defined in the JSP franta-hg@1: page. So you can change them separately. To provide the chart with the franta-hg@1: correct data you must provide an object which implements the interfacede.laures.cewolf.DatasetProducer. franta-hg@1: This object is asked to produce data every time a new chart must be franta-hg@1: rendered. Below you can see an example implementation of a franta-hg@1: DatasetProducer which could be used to provide data needed for our franta-hg@1: example scenario.

franta-hg@1:

franta-hg@1:
package de.laures.cewolf.example;
franta-hg@1: 
franta-hg@1: import java.io.Serializable;
franta-hg@1: import java.util.Date;
franta-hg@1: import java.util.Map;
franta-hg@1: 
franta-hg@1: import org.apache.commons.logging.Log;
franta-hg@1: import org.apache.commons.logging.LogFactory;
franta-hg@1: import org.jfree.data.category.CategoryDataset;
franta-hg@1: import org.jfree.data.category.DefaultCategoryDataset;
franta-hg@1: 
franta-hg@1: import de.laures.cewolf.DatasetProduceException;
franta-hg@1: import de.laures.cewolf.DatasetProducer;
franta-hg@1: import de.laures.cewolf.links.CategoryItemLinkGenerator;
franta-hg@1: import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
franta-hg@1: 
franta-hg@1: /** 
franta-hg@1:  * An example data producer.
franta-hg@1:  * @author  Guido Laures 
franta-hg@1:  */
franta-hg@1: public class PageViewCountData implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
franta-hg@1: 
franta-hg@1:     private static final Log log = LogFactory.getLog(PageViewCountData.class);
franta-hg@1: 
franta-hg@1:     // These values would normally not be hard coded but produced by
franta-hg@1:     // some kind of data source like a database or a file
franta-hg@1:     private final String[] categories =    {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
franta-hg@1:     private final String[] seriesNames =    {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};
franta-hg@1: 
franta-hg@1: 	/**
franta-hg@1: 	 *  Produces some random data.
franta-hg@1: 	 */
franta-hg@1:     public Object produceDataset(Map params) throws DatasetProduceException {
franta-hg@1:     	log.debug("producing data.");
franta-hg@1:         DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
franta-hg@1: 			/**
franta-hg@1: 			 * @see java.lang.Object#finalize()
franta-hg@1: 			 */
franta-hg@1: 			protected void finalize() throws Throwable {
franta-hg@1: 				super.finalize();
franta-hg@1: 				log.debug(this +" finalized.");
franta-hg@1: 			}
franta-hg@1:         };
franta-hg@1:         for (int series = 0; series < seriesNames.length; series ++) {
franta-hg@1:             int lastY = (int)(Math.random() * 1000 + 1000);
franta-hg@1:             for (int i = 0; i < categories.length; i++) {
franta-hg@1:                 final int y = lastY + (int)(Math.random() * 200 - 100);
franta-hg@1:                 lastY = y;
franta-hg@1:                 dataset.addValue(y, seriesNames[series], categories[i]);
franta-hg@1:             }
franta-hg@1:         }
franta-hg@1:         return dataset;
franta-hg@1:     }
franta-hg@1: 
franta-hg@1:     /**
franta-hg@1:      * This producer's data is invalidated after 5 seconds. By this method the
franta-hg@1:      * producer can influence Cewolf's caching behaviour the way it wants to.
franta-hg@1:      */
franta-hg@1: 	public boolean hasExpired(Map params, Date since) {		
franta-hg@1:         log.debug(getClass().getName() + "hasExpired()");
franta-hg@1: 		return (System.currentTimeMillis() - since.getTime())  > 5000;
franta-hg@1: 	}
franta-hg@1: 
franta-hg@1: 	/**
franta-hg@1: 	 * Returns a unique ID for this DatasetProducer
franta-hg@1: 	 */
franta-hg@1: 	public String getProducerId() {
franta-hg@1: 		return "PageViewCountData DatasetProducer";
franta-hg@1: 	}
franta-hg@1: 
franta-hg@1:     /**
franta-hg@1:      * Returns a link target for a special data item.
franta-hg@1:      */
franta-hg@1:     public String generateLink(Object data, int series, Object category) {
franta-hg@1:         return seriesNames[series];
franta-hg@1:     }
franta-hg@1:     
franta-hg@1: 	/**
franta-hg@1: 	 * @see java.lang.Object#finalize()
franta-hg@1: 	 */
franta-hg@1: 	protected void finalize() throws Throwable {
franta-hg@1: 		super.finalize();
franta-hg@1: 		log.debug(this + " finalized.");
franta-hg@1: 	}
franta-hg@1: 
franta-hg@1: 	/**
franta-hg@1: 	 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
franta-hg@1: 	 */
franta-hg@1: 	public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
franta-hg@1: 		return seriesNames[series];
franta-hg@1: 	}
franta-hg@1: 
franta-hg@1: }
franta-hg@1: 
franta-hg@1:

As you can see this datasetproducer is not very useful. Normally franta-hg@1: this class would try to access a datasource (e.g. a database) to access franta-hg@1: the needed information. But to serve as an example it should do.

franta-hg@1:

A DatasetProducer needs to implement three methods. The most franta-hg@1: important one is the produceDataset() method which actually franta-hg@1: produces the data which should be used to render a chart. This method franta-hg@1: takes a parameter map which is filled by some special tags of the JSP franta-hg@1: which will be explained later on.

franta-hg@1:

The hasExpired() method is called by the Cewolf framework franta-hg@1: if there already exits a data object produced by this producer in franta-hg@1: Cewolf's data cache. When returning true the producer franta-hg@1: signalizes that the data formerly used has expired.

franta-hg@1:

By providing an unique ID via the getProducerId() method franta-hg@1: the Cewolf framework identifies a producer type. Two producer instances franta-hg@1: with the same ID are supposed to produce the same data.

franta-hg@1:

Compile the class and put it in the correct folder structure under franta-hg@1: your web application's /WEB-INF/classes directory to make it franta-hg@1: available for your application.

franta-hg@1:

Step 3: Install the Cewolf Servlet in your Web franta-hg@1: Application>>

franta-hg@1: SourceForge Logo franta-hg@1: franta-hg@1: