KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > control > OlapModelController


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.war.control;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.springframework.validation.BindException;
34 import org.springframework.web.servlet.ModelAndView;
35
36 import com.jaspersoft.jasperserver.api.JSException;
37 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
38 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
39 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.ResourceLookupImpl;
40 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit;
41 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
42 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
43 import com.tonbeller.jpivot.olap.model.OlapModel;
44 import com.tonbeller.jpivot.tags.OlapModelProxy;
45 import com.tonbeller.wcf.controller.RequestContext;
46
47 /**
48  * The OlapModelController handles the request to load a specified OLAP model
49  *
50  * @author jshih
51  *
52  */

53 public class OlapModelController extends JRBaseMultiActionController {
54
55     protected final Logger logger = Logger.getLogger(getClass());
56
57     private OlapConnectionService olapConnectionService;
58     
59     /**
60      * @return Returns the olapConnectionService.
61      */

62     public OlapConnectionService getOlapConnectionService() {
63         return olapConnectionService;
64     }
65
66     /**
67      * @param olapConnectionService The olapConnectionService to set.
68      */

69     public void setOlapConnectionService(OlapConnectionService olapConnectionService) {
70         this.olapConnectionService = olapConnectionService;
71     }
72
73     public ModelAndView viewOlap(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74         throws ServletException JavaDoc {
75         
76         String JavaDoc viewUri = req.getParameter("name");
77         
78         if (viewUri == null || viewUri.length() == 0) {
79             throw new JSException("No OLAP Model name");
80         }
81         logger.debug("Viewing OLAP Model: " + viewUri);
82         
83         req.setAttribute("name", viewUri);
84         
85         HttpSession JavaDoc sess = req.getSession();
86         Map JavaDoc olapModels = (Map JavaDoc) sess.getAttribute("olapModels");
87         if (olapModels == null) {
88             olapModels = new HashMap JavaDoc();
89             sess.setAttribute("olapModels", olapModels);
90         }
91         
92         OlapSessionState sessionState = (OlapSessionState) olapModels.get(viewUri);
93         if (sessionState == null) {
94             sessionState = getOlapSession(viewUri, sess);
95             olapModels.put(viewUri, sessionState);
96         }
97         req.setAttribute("olapModel", sessionState.getOlapModel());
98         req.setAttribute("olapSession", sessionState);
99         
100         // Because WCF is so behind the times, we have to also set the
101
// session attribute by name
102

103         sess.setAttribute("olapModel", sessionState.getOlapModel());
104         
105         return new ModelAndView("/olap/viewOlap");
106     }
107
108     protected OlapSessionState getOlapSession(String JavaDoc viewUri, HttpSession JavaDoc sess) {
109         
110         logger.debug("Setting OlapModel for " + viewUri);
111         
112         RequestContext context = RequestContext.instance();
113         ExecutionContextImpl executionContext = new ExecutionContextImpl();
114         
115         OlapUnit olapUnit = (OlapUnit) getRepository().getResource(executionContext,
116                 viewUri);
117         
118         if (olapUnit == null) {
119             throw new JSException("No OLAP Model retrieved");
120         }
121         
122         OlapModel model = getOlapConnectionService().createOlapModel(executionContext, olapUnit);
123         
124         if (model == null) {
125             throw new JSException("No OLAP Model created for: " + viewUri);
126         }
127             
128         model = (OlapModel) model.getTopDecorator();
129         model.setLocale(context.getLocale());
130         model.setServletContext(context.getSession().getServletContext());
131         model.setID(viewUri);
132
133         model.setServletContext(sess.getServletContext());
134 /*
135         ClickableExtension ext = (ClickableExtension) model.getExtension(ClickableExtension.ID);
136         if (ext == null) {
137             ext = new ClickableExtensionImpl();
138             model.addExtension(ext);
139         }
140         ext.setClickables(clickables);
141 */

142         // stackMode
143

144         OlapModelProxy omp = OlapModelProxy.instance(viewUri, sess, false);
145 /* if (queryName != null)
146             omp.initializeAndShow(queryName, model);
147         else
148 */

149         try {
150             omp.initializeAndShow(viewUri, model);
151         } catch (Exception JavaDoc e) {
152             throw new JSException(e);
153         }
154
155         return new OlapSessionState(omp, olapUnit);
156     }
157     /**
158      * The handle() method looks up a given OLAP model given by the command parameter.
159      *
160      * @param request
161      * @param response
162      * @param command Specifies the OLAP model to load.
163      * @param errors TODO
164      * @return
165      */

166     protected ModelAndView handle(HttpServletRequest JavaDoc request,
167             HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
168             throws Exception JavaDoc {
169         
170         ModelAndView modelAndView = null;
171
172         ResourceLookupImpl olapUnitCommand = (ResourceLookupImpl) command;
173
174         String JavaDoc olapUnitName = olapUnitCommand.getName();
175         
176         if (olapUnitName != null)
177         {
178             modelAndView = new ModelAndView("olap/viewOlap", "olapUnitName", olapUnitName);
179         }
180         else {
181             // TODO resolve the double clicking problem
182
ExecutionContextImpl executionContext = new ExecutionContextImpl();
183             ResourceLookup[] olapUnits = repository.findResource(executionContext, FilterCriteria.createFilter(OlapUnit.class));
184             modelAndView = new ModelAndView("listOlapViews", "olapUnits", olapUnits);
185         }
186         
187         return modelAndView;
188     }
189     
190     public class OlapSessionState {
191         private OlapModel olapModel;
192         private OlapUnit olapUnit;
193         
194         public OlapSessionState(OlapModel olapModel, OlapUnit olapUnit) {
195             this.olapModel = olapModel;
196             this.olapUnit = olapUnit;
197         }
198         
199         /**
200          * @return Returns the olapModel.
201          */

202         public OlapModel getOlapModel() {
203             return olapModel;
204         }
205         /**
206          * @param olapModel The olapModel to set.
207          */

208         public void setOlapModel(OlapModel olapModel) {
209             this.olapModel = olapModel;
210         }
211         /**
212          * @return Returns the olapUnit.
213          */

214         public OlapUnit getOlapUnit() {
215             return olapUnit;
216         }
217         /**
218          * @param olapUnit The olapUnit to set.
219          */

220         public void setOlapUnit(OlapUnit olapUnit) {
221             this.olapUnit = olapUnit;
222         }
223         
224         
225     }
226 }
227
Popular Tags