franta-hg@1: /* franta-hg@1: * Created on 13.04.2003 franta-hg@1: * franta-hg@1: * To change the template for this generated file go to franta-hg@1: * Window>Preferences>Java>Code Generation>Code and Comments franta-hg@1: */ franta-hg@1: package de.laures.cewolf.taglib; 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.general.Dataset; 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.taglib.util.DatasetProductionTimeStore; franta-hg@1: import de.laures.cewolf.taglib.util.KeyGenerator; franta-hg@1: import de.laures.cewolf.util.Assert; franta-hg@1: franta-hg@1: /** franta-hg@1: * @author guido franta-hg@1: * franta-hg@1: * To change the template for this generated type comment go to franta-hg@1: * Window>Preferences>Java>Code Generation>Code and Comments franta-hg@1: */ franta-hg@1: public class DataContainer implements DataAware, Serializable { franta-hg@1: franta-hg@1: private static transient final Log log = LogFactory.getLog(ChartImageDefinition.class); franta-hg@1: franta-hg@1: private transient Dataset data; franta-hg@1: private transient DatasetProducer producer; franta-hg@1: franta-hg@1: private Map datasetProductionParams; franta-hg@1: private long datasetProduceTime; franta-hg@1: private boolean useCache = true; franta-hg@1: franta-hg@1: public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) { franta-hg@1: log.debug("setDataProductionConfig"); franta-hg@1: producer = dsp; franta-hg@1: datasetProductionParams = params; franta-hg@1: this.useCache = useCache; franta-hg@1: checkDataProductionNeed(); franta-hg@1: } franta-hg@1: franta-hg@1: public Object getDataset() throws DatasetProduceException { franta-hg@1: Assert.check(producer != null, "you need to specifiy a producer for the data of the chart."); franta-hg@1: log.debug("getting data.."); franta-hg@1: if (data == null) { franta-hg@1: log.debug("producing new dataset for " + producer.getProducerId()); franta-hg@1: data = (Dataset) producer.produceDataset(datasetProductionParams); franta-hg@1: DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance(); franta-hg@1: dataCache.addEntry(producer.getProducerId(), datasetProductionParams, new Date(datasetProduceTime)); franta-hg@1: } franta-hg@1: Assert.check(data != null, "your producer of type " + producer.getClass().getName() + " produced a null dataset."); franta-hg@1: return data; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * This method checks if there has been a dataset production franta-hg@1: * for the same DatasetProvider and parameters. If so the DatasetProducer franta-hg@1: * is consulted to check the expiry of this data. franta-hg@1: * If the data has expired the retrieval of a cached image of this franta-hg@1: * ChartDefinition is avoided by setting the datasetProduceTime to the franta-hg@1: * actual time. After this the hash code of this object can not be franta-hg@1: * present in the image cache and so a new image with new data will franta-hg@1: * be rendered. franta-hg@1: * If the data did not expire the last dataset production time is stored franta-hg@1: * as a memeber to reach the same hash code for this object as the one franta-hg@1: * before if possible. franta-hg@1: * This method is called during serialization to ensure the same serialized franta-hg@1: * representation of this and a eventually formally stored object. franta-hg@1: */ franta-hg@1: private void checkDataProductionNeed() { franta-hg@1: log.debug("checking data actuality..." + producer + ", " + datasetProductionParams); franta-hg@1: final String prodId = producer.getProducerId(); franta-hg@1: log.debug(prodId + ", " + KeyGenerator.generateKey((Serializable)datasetProductionParams)); franta-hg@1: log.debug("useCache = " + useCache); franta-hg@1: DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance(); franta-hg@1: if (useCache && dataCache.containsEntry(prodId, datasetProductionParams)) { franta-hg@1: Date produceTime = dataCache.getProductionTime(prodId, datasetProductionParams); franta-hg@1: log.debug("cached data available."); franta-hg@1: // cached data available franta-hg@1: if (!producer.hasExpired(datasetProductionParams, produceTime)) { franta-hg@1: log.debug("cached data is still valid."); franta-hg@1: this.datasetProduceTime = produceTime.getTime(); franta-hg@1: return; franta-hg@1: } franta-hg@1: dataCache.removeEntry(prodId, datasetProductionParams); franta-hg@1: } franta-hg@1: datasetProduceTime = System.currentTimeMillis(); franta-hg@1: } franta-hg@1: franta-hg@1: }