java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/DataContainer.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
     1 /*
     2  * Created on 13.04.2003
     3  *
     4  * To change the template for this generated file go to
     5  * Window>Preferences>Java>Code Generation>Code and Comments
     6  */
     7 package de.laures.cewolf.taglib;
     8 
     9 import java.io.Serializable;
    10 import java.util.Date;
    11 import java.util.Map;
    12 
    13 import org.apache.commons.logging.Log;
    14 import org.apache.commons.logging.LogFactory;
    15 import org.jfree.data.general.Dataset;
    16 
    17 import de.laures.cewolf.DatasetProduceException;
    18 import de.laures.cewolf.DatasetProducer;
    19 import de.laures.cewolf.taglib.util.DatasetProductionTimeStore;
    20 import de.laures.cewolf.taglib.util.KeyGenerator;
    21 import de.laures.cewolf.util.Assert;
    22 
    23 /**
    24  * @author guido
    25  *
    26  * To change the template for this generated type comment go to
    27  * Window>Preferences>Java>Code Generation>Code and Comments
    28  */
    29 public class DataContainer implements DataAware, Serializable {
    30 
    31     private static transient final Log log = LogFactory.getLog(ChartImageDefinition.class);
    32 
    33     private transient Dataset data;
    34     private transient DatasetProducer producer;
    35 
    36     private Map datasetProductionParams;
    37     private long datasetProduceTime;
    38     private boolean useCache = true;
    39 
    40     public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) {
    41         log.debug("setDataProductionConfig");
    42         producer = dsp;
    43         datasetProductionParams = params;
    44         this.useCache = useCache;
    45         checkDataProductionNeed();
    46     }
    47 
    48     public Object getDataset() throws DatasetProduceException {
    49         Assert.check(producer != null, "you need to specifiy a producer for the data of the chart.");
    50         log.debug("getting data..");
    51         if (data == null) {
    52             log.debug("producing new dataset for " + producer.getProducerId());
    53             data = (Dataset) producer.produceDataset(datasetProductionParams);
    54             DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
    55             dataCache.addEntry(producer.getProducerId(), datasetProductionParams, new Date(datasetProduceTime));
    56         }
    57         Assert.check(data != null, "your producer of type " + producer.getClass().getName() + " produced a null dataset.");
    58         return data;
    59     }
    60 
    61     /**
    62      * This method checks if there has been a dataset production 
    63      * for the same DatasetProvider and parameters. If so the DatasetProducer
    64      * is consulted to check the expiry of this data.
    65      * If the data has expired the retrieval of a cached image of this
    66      * ChartDefinition is avoided by setting the datasetProduceTime to the
    67      * actual time. After this the hash code of this object can not be
    68      * present in the image cache and so a new image with new data will
    69      * be rendered.
    70      * If the data did not expire the last dataset production time is stored
    71      * as a memeber to reach the same hash code for this object as the one
    72      * before if possible.
    73      * This method is called during serialization to ensure the same serialized
    74      * representation of this and a eventually formally stored object.
    75      */
    76     private void checkDataProductionNeed() {
    77         log.debug("checking data actuality..." + producer + ", " + datasetProductionParams);
    78         final String prodId = producer.getProducerId();
    79         log.debug(prodId + ", " + KeyGenerator.generateKey((Serializable)datasetProductionParams));
    80         log.debug("useCache = " + useCache);
    81         DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
    82         if (useCache && dataCache.containsEntry(prodId, datasetProductionParams)) {
    83             Date produceTime = dataCache.getProductionTime(prodId, datasetProductionParams);
    84             log.debug("cached data available.");
    85             // cached data available
    86             if (!producer.hasExpired(datasetProductionParams, produceTime)) {
    87                 log.debug("cached data is still valid.");
    88                 this.datasetProduceTime = produceTime.getTime();
    89                 return;
    90             }
    91             dataCache.removeEntry(prodId, datasetProductionParams);
    92         }
    93         datasetProduceTime = System.currentTimeMillis();
    94     }
    95 
    96 }