java/cewolf-1.0/src/site/tutorial/step2.html
author František Kučera <franta-hg@frantovo.cz>
Sun Mar 01 11:06:49 2009 +0100 (2009-03-01)
changeset 4 249560bcc466
permissions -rw-r--r--
Doplnění serializovatelnosti
     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
     2 <html>
     3 <head>
     4   <title>Cewolf Tutorial(2): Provide a DatasetProducer</title>
     5   <meta name="author" content="Guido Laures">
     6   <link href="../cewolf.css" rel="stylesheet" type="text/css">
     7 </head>
     8 <body>
     9 <h1>Tutorial: Step 2</h1>
    10 <h2>Provide a DatasetProducer</h2>
    11 <p> As Cewolf uses a MVC (Model-View-Control) approach the data which
    12 are shown in your chart are separated from the view defined in the JSP
    13 page. So you can change them separately. To provide the chart with the
    14 correct data you must provide an object which implements the interface<tt>de.laures.cewolf.DatasetProducer</tt>.
    15 This object is asked to produce data every time a new chart must be
    16 rendered. Below you can see an example implementation of a
    17 DatasetProducer which could be used to provide data needed for our
    18 example scenario. </p>
    19 <p> </p>
    20 <pre>package de.laures.cewolf.example;
    21 
    22 import java.io.Serializable;
    23 import java.util.Date;
    24 import java.util.Map;
    25 
    26 import org.apache.commons.logging.Log;
    27 import org.apache.commons.logging.LogFactory;
    28 import org.jfree.data.category.CategoryDataset;
    29 import org.jfree.data.category.DefaultCategoryDataset;
    30 
    31 import de.laures.cewolf.DatasetProduceException;
    32 import de.laures.cewolf.DatasetProducer;
    33 import de.laures.cewolf.links.CategoryItemLinkGenerator;
    34 import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
    35 
    36 /** 
    37  * An example data producer.
    38  * @author  Guido Laures 
    39  */
    40 public class PageViewCountData implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
    41 
    42     private static final Log log = LogFactory.getLog(PageViewCountData.class);
    43 
    44     // These values would normally not be hard coded but produced by
    45     // some kind of data source like a database or a file
    46     private final String[] categories =    {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
    47     private final String[] seriesNames =    {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};
    48 
    49 	/**
    50 	 *  Produces some random data.
    51 	 */
    52     public Object produceDataset(Map params) throws DatasetProduceException {
    53     	log.debug("producing data.");
    54         DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
    55 			/**
    56 			 * @see java.lang.Object#finalize()
    57 			 */
    58 			protected void finalize() throws Throwable {
    59 				super.finalize();
    60 				log.debug(this +" finalized.");
    61 			}
    62         };
    63         for (int series = 0; series < seriesNames.length; series ++) {
    64             int lastY = (int)(Math.random() * 1000 + 1000);
    65             for (int i = 0; i < categories.length; i++) {
    66                 final int y = lastY + (int)(Math.random() * 200 - 100);
    67                 lastY = y;
    68                 dataset.addValue(y, seriesNames[series], categories[i]);
    69             }
    70         }
    71         return dataset;
    72     }
    73 
    74     /**
    75      * This producer's data is invalidated after 5 seconds. By this method the
    76      * producer can influence Cewolf's caching behaviour the way it wants to.
    77      */
    78 	public boolean hasExpired(Map params, Date since) {		
    79         log.debug(getClass().getName() + "hasExpired()");
    80 		return (System.currentTimeMillis() - since.getTime())  > 5000;
    81 	}
    82 
    83 	/**
    84 	 * Returns a unique ID for this DatasetProducer
    85 	 */
    86 	public String getProducerId() {
    87 		return "PageViewCountData DatasetProducer";
    88 	}
    89 
    90     /**
    91      * Returns a link target for a special data item.
    92      */
    93     public String generateLink(Object data, int series, Object category) {
    94         return seriesNames[series];
    95     }
    96     
    97 	/**
    98 	 * @see java.lang.Object#finalize()
    99 	 */
   100 	protected void finalize() throws Throwable {
   101 		super.finalize();
   102 		log.debug(this + " finalized.");
   103 	}
   104 
   105 	/**
   106 	 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
   107 	 */
   108 	public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
   109 		return seriesNames[series];
   110 	}
   111 
   112 }
   113 </pre>
   114 <p> As you can see this datasetproducer is not very useful. Normally
   115 this class would try to access a datasource (e.g. a database) to access
   116 the needed information. But to serve as an example it should do. </p>
   117 <p> A DatasetProducer needs to implement three methods. The most
   118 important one is the <tt>produceDataset()</tt> method which actually
   119 produces the data which should be used to render a chart. This method
   120 takes a parameter map which is filled by some special tags of the JSP
   121 which will be explained later on. </p>
   122 <p> The <tt>hasExpired()</tt> method is called by the Cewolf framework
   123 if there already exits a data object produced by this producer in
   124 Cewolf's data cache. When returning <tt>true</tt> the producer
   125 signalizes that the data formerly used has expired. </p>
   126 <p> By providing an unique ID via the <tt>getProducerId()</tt> method
   127 the Cewolf framework identifies a producer type. Two producer instances
   128 with the same ID are supposed to produce the same data. </p>
   129 <p> Compile the class and put it in the correct folder structure under
   130 your web application's <tt>/WEB-INF/classes</tt> directory to make it
   131 available for your application. </p>
   132 <p> <a href="step3.html">Step 3: Install the Cewolf Servlet in your Web
   133 Application&gt;&gt;</a> </p>
   134 <a height="30" target="new" href="http://sourceforge.net"><img
   135  alt="SourceForge Logo" border="0" height="30"
   136  src="http://sourceforge.net/sflogo.php?group_id=57282&amp;type=5"></a>
   137 </body>
   138 </html>