1 package com.jaspersoft.jasperserver.war.action; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpSession ; 8 9 import org.apache.log4j.Logger; 10 import org.springframework.webflow.Event; 11 import org.springframework.webflow.RequestContext; 12 import org.springframework.webflow.context.servlet.ServletExternalContext; 13 14 import com.jaspersoft.jasperserver.api.JSException; 15 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl; 16 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 17 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit; 18 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService; 19 import com.tonbeller.jpivot.olap.model.OlapModel; 20 import com.tonbeller.jpivot.tags.OlapModelProxy; 21 22 28 public class ViewOlapModelAction extends RepositoryAction { 29 protected final Logger logger = Logger.getLogger(getClass()); 30 public static final String OLAPUNIT_ATTR = "olapUnit"; 31 private OlapConnectionService olapConnectionService; 32 private RepositoryService repository; 33 private RequestContext requestContext; 34 private OlapConnectionService olapConnection; 35 private String olapUnitName; 36 private OlapUnit olapUnit; 37 private OlapModel olapModel; 38 39 45 public Event initOlapModel(RequestContext context) 46 { 47 String viewUri = (String ) context.getFlowScope().get("name"); 48 ServletExternalContext sec = (ServletExternalContext) context.getExternalContext(); 49 HttpServletRequest req = sec.getRequest(); 50 init(viewUri, req); 51 return success(); 52 } 53 54 59 private void init(String viewUri, HttpServletRequest req) { 60 if (viewUri == null || viewUri.length() == 0) { 61 throw new JSException("No OLAP Model name"); 62 } 63 logger.debug("Viewing OLAP Model: " + viewUri); 64 65 req.setAttribute("name", viewUri); 66 67 HttpSession sess = req.getSession(); 68 Map olapModels = (Map ) sess.getAttribute("olapModels"); 69 if (olapModels == null) { 70 olapModels = new HashMap (); 71 sess.setAttribute("olapModels", olapModels); 72 } 73 74 OlapSessionState sessionState = (OlapSessionState) olapModels.get(viewUri); 75 if (sessionState == null) { 76 sessionState = getOlapSession(viewUri, sess); 77 olapModels.put(viewUri, sessionState); 78 } 79 req.setAttribute("olapModel", sessionState.getOlapModel()); 80 req.setAttribute("olapSession", sessionState); 81 82 85 sess.setAttribute("olapModel", sessionState.getOlapModel()); 86 } 87 88 95 protected OlapSessionState getOlapSession(String viewUri, HttpSession sess) { 96 97 logger.debug("Setting OlapModel for " + viewUri); 98 99 com.tonbeller.wcf.controller.RequestContext context = com.tonbeller.wcf.controller.RequestContext.instance(); 100 ExecutionContextImpl executionContext = new ExecutionContextImpl(); 101 102 OlapUnit olapUnit = (OlapUnit) getRepository().getResource(executionContext, 103 viewUri); 104 105 if (olapUnit == null) { 106 throw new JSException("No OLAP Model retrieved"); 107 } 108 109 OlapModel model = getOlapConnectionService().createOlapModel(executionContext, olapUnit); 110 111 if (model == null) { 112 throw new JSException("No OLAP Model created for: " + viewUri); 113 } 114 115 model = (OlapModel) model.getTopDecorator(); 116 model.setLocale(context.getLocale()); 117 model.setServletContext(context.getSession().getServletContext()); 118 model.setID(viewUri); 119 120 model.setServletContext(sess.getServletContext()); 121 129 131 OlapModelProxy omp = OlapModelProxy.instance(viewUri, sess, false); 132 136 try { 137 omp.initializeAndShow(viewUri, model); 138 } catch (Exception e) { 139 throw new JSException(e); 140 } 141 142 return new OlapSessionState(omp, olapUnit); 143 } 144 145 151 public class OlapSessionState { 152 private OlapModel olapModel; 153 private OlapUnit olapUnit; 154 155 public OlapSessionState(OlapModel olapModel, OlapUnit olapUnit) { 156 this.olapModel = olapModel; 157 this.olapUnit = olapUnit; 158 } 159 160 163 public OlapModel getOlapModel() { 164 return olapModel; 165 } 166 169 public void setOlapModel(OlapModel olapModel) { 170 this.olapModel = olapModel; 171 } 172 175 public OlapUnit getOlapUnit() { 176 return olapUnit; 177 } 178 181 public void setOlapUnit(OlapUnit olapUnit) { 182 this.olapUnit = olapUnit; 183 } 184 } 185 186 192 public Event getOlapUnit(RequestContext requestContext) { 193 Event result = success(); 194 ExecutionContextImpl executionContext = new ExecutionContextImpl(); 195 this.requestContext = requestContext; 196 if ((olapUnitName = (String ) requestContext.getFlowScope().get( 197 OLAPUNIT_ATTR)) == null) { 198 result = error(); 199 } else if ((olapUnit = (OlapUnit) repository.getResource( 200 executionContext, olapUnitName)) == null) { 201 result = error(); 202 } 203 204 return result; 205 } 206 207 213 public Event createOlapModel(RequestContext requestContext) 214 { 215 Event result = success(); 216 ExecutionContextImpl executionContext = new ExecutionContextImpl(); 217 olapModel = olapConnection.createOlapModel(executionContext, olapUnit); 218 if (olapModel == null) { 219 result = error(); 220 } 221 return result; 222 } 223 224 230 public Event displayOlapModel(RequestContext requestContext) { 231 Event result = success(); 232 233 requestContext.getRequestScope().put("olapUnitName", olapUnitName); 234 235 return result; 236 } 237 238 241 public OlapConnectionService getOlapConnectionService() { 242 return olapConnectionService; 243 } 244 245 248 public void setOlapConnectionService(OlapConnectionService olapConnectionService) { 249 this.olapConnectionService = olapConnectionService; 250 } 251 252 public RepositoryService getRepository() { 253 return repository; 254 } 255 256 public void setRepository(RepositoryService repository) { 257 this.repository = repository; 258 } 259 260 public OlapConnectionService getOlapConnection() { 261 return olapConnection; 262 } 263 264 public void setOlapConnection(OlapConnectionService olapConnection) { 265 this.olapConnection = olapConnection; 266 } 267 268 public OlapModel getOlapModel() { 269 return olapModel; 270 } 271 272 public void setOlapModel(OlapModel olapModel) { 273 this.olapModel = olapModel; 274 } 275 276 } 277 | Popular Tags |