KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > CachingOlapModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model;
14
15 import org.apache.log4j.Logger;
16
17 import com.tonbeller.jpivot.core.Model;
18 import com.tonbeller.jpivot.core.ModelChangeEvent;
19 import com.tonbeller.jpivot.core.ModelChangeListener;
20
21 /**
22  * An OlapModel decorator that caches the result
23  *
24  * @author av
25  */

26 public class CachingOlapModel extends OlapModelDecorator implements ModelChangeListener {
27   private static Logger logger = Logger.getLogger(CachingOlapModel.class);
28   Result result = null;
29
30   /**
31    * invalidates the current result
32    */

33   public void modelChanged(ModelChangeEvent e) {
34     result = null;
35   }
36
37     /**
38      * invalidates the current result
39      */

40     public void structureChanged(ModelChangeEvent e) {
41         result = null;
42     }
43
44   public Result getResult() throws OlapException {
45     logger.info("CachingOlapModel: " + ((result == null) ? "getting Result from OLAP Server" : "using cached result"));
46     if (result == null) {
47       long t1 = System.currentTimeMillis();
48       result = super.getResult();
49       long t2 = System.currentTimeMillis();
50       logger.info("Execute Query took " + (t2 - t1) + " millisec");
51     }
52     return result;
53   }
54
55
56   /**
57    * @see com.tonbeller.jpivot.core.Extension#decorate(Model)
58    */

59   public Model decorate(Model modelToDecorate) {
60     modelToDecorate.addModelChangeListener(this);
61     return super.decorate(modelToDecorate);
62   }
63
64 }
65
Popular Tags