Tutorial: Step 2

Provide a DatasetProducer

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

package de.laures.cewolf.example;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
import de.laures.cewolf.links.CategoryItemLinkGenerator;
import de.laures.cewolf.tooltips.CategoryToolTipGenerator;

/** 
 * An example data producer.
 * @author  Guido Laures 
 */
public class PageViewCountData implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {

    private static final Log log = LogFactory.getLog(PageViewCountData.class);

    // These values would normally not be hard coded but produced by
    // some kind of data source like a database or a file
    private final String[] categories =    {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
    private final String[] seriesNames =    {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};

	/**
	 *  Produces some random data.
	 */
    public Object produceDataset(Map params) throws DatasetProduceException {
    	log.debug("producing data.");
        DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
			/**
			 * @see java.lang.Object#finalize()
			 */
			protected void finalize() throws Throwable {
				super.finalize();
				log.debug(this +" finalized.");
			}
        };
        for (int series = 0; series < seriesNames.length; series ++) {
            int lastY = (int)(Math.random() * 1000 + 1000);
            for (int i = 0; i < categories.length; i++) {
                final int y = lastY + (int)(Math.random() * 200 - 100);
                lastY = y;
                dataset.addValue(y, seriesNames[series], categories[i]);
            }
        }
        return dataset;
    }

    /**
     * This producer's data is invalidated after 5 seconds. By this method the
     * producer can influence Cewolf's caching behaviour the way it wants to.
     */
	public boolean hasExpired(Map params, Date since) {		
        log.debug(getClass().getName() + "hasExpired()");
		return (System.currentTimeMillis() - since.getTime())  > 5000;
	}

	/**
	 * Returns a unique ID for this DatasetProducer
	 */
	public String getProducerId() {
		return "PageViewCountData DatasetProducer";
	}

    /**
     * Returns a link target for a special data item.
     */
    public String generateLink(Object data, int series, Object category) {
        return seriesNames[series];
    }
    
	/**
	 * @see java.lang.Object#finalize()
	 */
	protected void finalize() throws Throwable {
		super.finalize();
		log.debug(this + " finalized.");
	}

	/**
	 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
	 */
	public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
		return seriesNames[series];
	}

}

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

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

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

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

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

Step 3: Install the Cewolf Servlet in your Web Application>>

SourceForge Logo