java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/DataContainer.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/DataContainer.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/*
     1.5 + * Created on 13.04.2003
     1.6 + *
     1.7 + * To change the template for this generated file go to
     1.8 + * Window>Preferences>Java>Code Generation>Code and Comments
     1.9 + */
    1.10 +package de.laures.cewolf.taglib;
    1.11 +
    1.12 +import java.io.Serializable;
    1.13 +import java.util.Date;
    1.14 +import java.util.Map;
    1.15 +
    1.16 +import org.apache.commons.logging.Log;
    1.17 +import org.apache.commons.logging.LogFactory;
    1.18 +import org.jfree.data.general.Dataset;
    1.19 +
    1.20 +import de.laures.cewolf.DatasetProduceException;
    1.21 +import de.laures.cewolf.DatasetProducer;
    1.22 +import de.laures.cewolf.taglib.util.DatasetProductionTimeStore;
    1.23 +import de.laures.cewolf.taglib.util.KeyGenerator;
    1.24 +import de.laures.cewolf.util.Assert;
    1.25 +
    1.26 +/**
    1.27 + * @author guido
    1.28 + *
    1.29 + * To change the template for this generated type comment go to
    1.30 + * Window>Preferences>Java>Code Generation>Code and Comments
    1.31 + */
    1.32 +public class DataContainer implements DataAware, Serializable {
    1.33 +
    1.34 +    private static transient final Log log = LogFactory.getLog(ChartImageDefinition.class);
    1.35 +
    1.36 +    private transient Dataset data;
    1.37 +    private transient DatasetProducer producer;
    1.38 +
    1.39 +    private Map datasetProductionParams;
    1.40 +    private long datasetProduceTime;
    1.41 +    private boolean useCache = true;
    1.42 +
    1.43 +    public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) {
    1.44 +        log.debug("setDataProductionConfig");
    1.45 +        producer = dsp;
    1.46 +        datasetProductionParams = params;
    1.47 +        this.useCache = useCache;
    1.48 +        checkDataProductionNeed();
    1.49 +    }
    1.50 +
    1.51 +    public Object getDataset() throws DatasetProduceException {
    1.52 +        Assert.check(producer != null, "you need to specifiy a producer for the data of the chart.");
    1.53 +        log.debug("getting data..");
    1.54 +        if (data == null) {
    1.55 +            log.debug("producing new dataset for " + producer.getProducerId());
    1.56 +            data = (Dataset) producer.produceDataset(datasetProductionParams);
    1.57 +            DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
    1.58 +            dataCache.addEntry(producer.getProducerId(), datasetProductionParams, new Date(datasetProduceTime));
    1.59 +        }
    1.60 +        Assert.check(data != null, "your producer of type " + producer.getClass().getName() + " produced a null dataset.");
    1.61 +        return data;
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * This method checks if there has been a dataset production 
    1.66 +     * for the same DatasetProvider and parameters. If so the DatasetProducer
    1.67 +     * is consulted to check the expiry of this data.
    1.68 +     * If the data has expired the retrieval of a cached image of this
    1.69 +     * ChartDefinition is avoided by setting the datasetProduceTime to the
    1.70 +     * actual time. After this the hash code of this object can not be
    1.71 +     * present in the image cache and so a new image with new data will
    1.72 +     * be rendered.
    1.73 +     * If the data did not expire the last dataset production time is stored
    1.74 +     * as a memeber to reach the same hash code for this object as the one
    1.75 +     * before if possible.
    1.76 +     * This method is called during serialization to ensure the same serialized
    1.77 +     * representation of this and a eventually formally stored object.
    1.78 +     */
    1.79 +    private void checkDataProductionNeed() {
    1.80 +        log.debug("checking data actuality..." + producer + ", " + datasetProductionParams);
    1.81 +        final String prodId = producer.getProducerId();
    1.82 +        log.debug(prodId + ", " + KeyGenerator.generateKey((Serializable)datasetProductionParams));
    1.83 +        log.debug("useCache = " + useCache);
    1.84 +        DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
    1.85 +        if (useCache && dataCache.containsEntry(prodId, datasetProductionParams)) {
    1.86 +            Date produceTime = dataCache.getProductionTime(prodId, datasetProductionParams);
    1.87 +            log.debug("cached data available.");
    1.88 +            // cached data available
    1.89 +            if (!producer.hasExpired(datasetProductionParams, produceTime)) {
    1.90 +                log.debug("cached data is still valid.");
    1.91 +                this.datasetProduceTime = produceTime.getTime();
    1.92 +                return;
    1.93 +            }
    1.94 +            dataCache.removeEntry(prodId, datasetProductionParams);
    1.95 +        }
    1.96 +        datasetProduceTime = System.currentTimeMillis();
    1.97 +    }
    1.98 +
    1.99 +}