KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > tags > OlapModelProxy


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.tags;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import javax.servlet.http.HttpSession JavaDoc;
19 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
20 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 import com.tonbeller.jpivot.core.ModelChangeEvent;
25 import com.tonbeller.jpivot.core.ModelChangeListener;
26 import com.tonbeller.jpivot.olap.model.OlapException;
27 import com.tonbeller.jpivot.olap.model.OlapModel;
28 import com.tonbeller.jpivot.olap.model.OlapModelDecorator;
29 import com.tonbeller.jpivot.olap.model.impl.Empty;
30 import com.tonbeller.jpivot.tags.StateManager.State;
31 import com.tonbeller.tbutils.testenv.Environment;
32
33 /**
34  * proxy for OlapModel. There is one instance per session, the GUI
35  * components refer to this instance. When the underlying OlapModel (decoree)
36  * is exchanged, a structureChanged event is fired.
37  * <p />
38  * OlapModelProxy is responsible for calling initialize/destroy on its decoree(s).
39  * <p />
40  * When a new decoree is set, the previous one will be destroyed.
41  */

42
43 public class OlapModelProxy extends OlapModelDecorator implements HttpSessionBindingListener JavaDoc {
44   ArrayList JavaDoc listeners = new ArrayList JavaDoc();
45   public static final String JavaDoc DEFAULT_NAME = "default";
46
47   private static final Logger logger = Logger.getLogger(OlapModelProxy.class);
48
49   private ModelChangeListener modelChangeListener = new ModelChangeListener() {
50     public void modelChanged(ModelChangeEvent e) {
51       fireModelChanged();
52     }
53
54     public void structureChanged(ModelChangeEvent e) {
55       fireStructureChanged();
56     }
57   };
58
59   class MyState implements State {
60     String JavaDoc name;
61     OlapModel model;
62
63     MyState(OlapModel model) {
64       this.name = DEFAULT_NAME;
65       this.model = model;
66     }
67
68     MyState(String JavaDoc name, OlapModel model) {
69       this.name = name;
70       this.model = model;
71     }
72
73     public void initialize() throws Exception JavaDoc {
74       try {
75         if (logger.isInfoEnabled())
76           logger.info("initializing: " + model);
77         model.initialize();
78       } catch (OlapException e) {
79         logger.error(null, e);
80         throw e;
81       }
82     }
83
84     public void destroy() throws Exception JavaDoc {
85       if (logger.isInfoEnabled())
86         logger.info("destroying: " + model);
87       model.destroy();
88     }
89
90     public void show() throws OlapException {
91       if (logger.isInfoEnabled())
92         logger.info("activating: " + model);
93       model.addModelChangeListener(modelChangeListener);
94       setDelegate(model);
95       fireStructureChanged();
96     }
97
98     public void hide() throws OlapException {
99       if (logger.isInfoEnabled())
100         logger.info("deactivating: " + model);
101       model.removeModelChangeListener(modelChangeListener);
102       setDelegate(Empty.EMPTY_MODEL);
103       fireStructureChanged();
104     }
105
106     public String JavaDoc getName() {
107       return name;
108     }
109
110     public void setName(String JavaDoc name) {
111       this.name = name;
112     }
113   }
114
115   StateManager stateManager;
116
117   private OlapModelProxy(boolean stackMode) {
118     super(Empty.EMPTY_MODEL);
119     if (stackMode)
120       stateManager = new StackStateManager();
121     else
122       stateManager = new PageStateManager();
123     if (Environment.isTest())
124       stateManager.setLogger(new TestStateLogger());
125   }
126
127   public static OlapModelProxy instance(String JavaDoc id, HttpSession JavaDoc session) {
128     return instance(id, session, true);
129   }
130
131   /**
132    * retrieves the instance from the session. If the instance does not exist,
133    * a new instance is created and stored as a session attribute
134    *
135    * @param id the name of the session attribute that will be created. Use this name to reference
136    * from other tags like pivot table, chart etc.
137    *
138    * @param session the current session, will contain the new created attribute
139    *
140    * @param stackMode if true, queries with different names are stacked, i.e. a StackStateManager
141    * will be used. Otherwise queries with different names will exist parallel, i.e. a PageStackManager
142    * is used. This attribute is evaluated only when the session attribute is created, i.e. when
143    * this method is called for the first time.
144    */

145   public static OlapModelProxy instance(String JavaDoc id, HttpSession JavaDoc session, boolean stackMode) {
146     OlapModelProxy omp = (OlapModelProxy) session.getAttribute(id);
147     if (omp == null) {
148       omp = new OlapModelProxy(stackMode);
149       session.setAttribute(id, omp);
150     }
151     return omp;
152   }
153
154   /**
155    * destroys the current model, if present. Then the new model is initalized
156    * and shown.
157    */

158   public void initializeAndShow(OlapModel model) throws Exception JavaDoc {
159     State s = new MyState(model);
160     stateManager.initializeAndShow(s);
161   }
162
163   String JavaDoc nonEmptyQueryName(String JavaDoc queryName) {
164     if (queryName == null || queryName.length() == 0)
165       return DEFAULT_NAME;
166     return queryName;
167   }
168
169   /**
170    * destroys the current model with the given <code>queryName</code>, if present.
171    * Then the new model is initalized and shown. Depending on the StateManager type
172    * queries with other names may be popped off the stack and destroyed.
173    *
174    * @see #showByName(String)
175    */

176   public void initializeAndShow(String JavaDoc queryName, OlapModel model) throws Exception JavaDoc {
177     queryName = nonEmptyQueryName(queryName);
178     State s = new MyState(queryName, model);
179     stateManager.initializeAndShow(s);
180   }
181
182   /**
183    * shows the query that was initialized with <code>queryName</code>.
184    * @see #initializeAndShow(String, OlapModel)
185    */

186   public void showByName(String JavaDoc queryName) throws Exception JavaDoc {
187     queryName = nonEmptyQueryName(queryName);
188     stateManager.showByName(queryName);
189   }
190
191   public void destroyAll() throws Exception JavaDoc {
192     stateManager.destroyAll();
193   }
194
195   public void destroyQuery(String JavaDoc queryName) throws Exception JavaDoc {
196     queryName = nonEmptyQueryName(queryName);
197     stateManager.destroyByName(queryName);
198   }
199
200   private void fireModelChanged() {
201     ModelChangeEvent e = new ModelChangeEvent(this);
202     for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
203       ModelChangeListener l = (ModelChangeListener) iter.next();
204       l.modelChanged(e);
205     }
206   }
207
208   private void fireStructureChanged() {
209     ModelChangeEvent e = new ModelChangeEvent(this);
210     for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
211       ModelChangeListener l = (ModelChangeListener) iter.next();
212       l.structureChanged(e);
213     }
214   }
215
216   public void addModelChangeListener(ModelChangeListener l) {
217     listeners.add(l);
218   }
219
220   public void removeModelChangeListener(ModelChangeListener l) {
221     listeners.remove(l);
222   }
223
224   public void valueBound(HttpSessionBindingEvent JavaDoc ev) {
225   }
226
227   public void valueUnbound(HttpSessionBindingEvent JavaDoc ev) {
228     logger.info("session timeout");
229     try {
230       stateManager.destroyAll();
231     } catch (Exception JavaDoc e) {
232       logger.error(null, e);
233     }
234   }
235
236   /**
237    * OlapModelProxy is responsible for calling initialize/destroy
238    * @throws RuntimeExecption if called
239    */

240   public void destroy() {
241     throw new RuntimeException JavaDoc("must not be called");
242   }
243
244   /**
245    * OlapModelProxy is responsible for calling initialize/destroy
246    * @throws RuntimeExecption if called
247    */

248   public void initialize() {
249     throw new RuntimeException JavaDoc("must not be called");
250   }
251
252   /**
253    * for Tests only
254    */

255   public StateManager getStateManager() {
256     return stateManager;
257   }
258 }
259
Popular Tags