1 21 package com.jaspersoft.jasperserver.war.action; 22 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import net.sf.jasperreports.engine.JasperPrint; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.springframework.context.i18n.LocaleContextHolder; 34 import org.springframework.webflow.AttributeMap; 35 import org.springframework.webflow.Event; 36 import org.springframework.webflow.RequestContext; 37 import org.springframework.webflow.ExternalContext.SharedAttributeMap; 38 39 import com.jaspersoft.jasperserver.api.engine.jasperreports.domain.impl.ReportUnitRequest; 40 import com.jaspersoft.jasperserver.api.engine.jasperreports.domain.impl.ReportUnitResult; 41 import com.jaspersoft.jasperserver.war.action.hyperlinks.HyperlinkProducerFactoryFlowFactory; 42 import com.jaspersoft.jasperserver.war.common.JasperServerUtil; 43 44 48 public class ViewReportAction extends ReportParametersAction 49 { 50 protected final static Log log = LogFactory.getLog(ViewReportAction.class); 51 52 public static final String REPORTUNIT_URI = "reportUnit"; 53 54 private String transactionAttributeRequestParsed; 55 private String flowAttributeInhibitRequestParsing; 56 private String requestParameterPageIndex; 57 private String flowAttributePageIndex; 58 private String requestAttributeHtmlLinkHandlerFactory; 59 private String flowAttributeDepth; 60 private String sessionAttributePrefixJasperPrint; 61 private String flowAttributeJasperPrintSessionName; 62 private HyperlinkProducerFactoryFlowFactory hyperlinkProducerFactory; 63 private String flowAttributeIsSubflow; 64 private String requestParameterReportOutput; 65 private String flowAttributeReportOutput; 66 private String flowAttributeUseClientTimezone; 67 68 public Event checkForParams(RequestContext context) throws Exception 69 { 70 AttributeMap flowScope = context.getFlowScope(); 71 72 Integer depth = flowScope.getInteger(getFlowAttributeDepth()); 73 if (depth == null) { 74 depth = new Integer (0); 75 flowScope.put(getFlowAttributeDepth(), depth); 76 } 77 78 boolean isSubflow = !context.getFlowExecutionContext().getActiveSession().isRoot(); 79 flowScope.put(getFlowAttributeIsSubflow(), Boolean.valueOf(isSubflow)); 80 81 String reportOutput = context.getRequestParameters().get(getRequestParameterReportOutput()); 82 if (reportOutput != null) { 83 flowScope.put(getFlowAttributeReportOutput(), reportOutput); 84 } 85 86 Boolean inhibitRequestParsingAttr = flowScope.getBoolean(getFlowAttributeInhibitRequestParsing()); 87 boolean parseRequest = inhibitRequestParsingAttr == null || !inhibitRequestParsingAttr.booleanValue(); 88 89 flowScope.put(getFlowAttributeUseClientTimezone(), Boolean.valueOf(!parseRequest)); 90 91 return createWrappers(context, parseRequest); 92 } 93 94 protected void addCustomParameters(RequestContext context, Map parameterValues) { 95 } 96 97 public Event verifyData(RequestContext context) 98 { 99 AttributeMap transactionAttributes = context.getLastTransition().getAttributeMap(); 100 boolean requestParsed = transactionAttributes.getRequiredBoolean(getTransactionAttributeRequestParsed()).booleanValue(); 101 Map parameterValues = getParameterValues(context, requestParsed); 102 if (parameterValues == null) { 103 return error(); 104 } 105 106 String reportUnitUri = (String ) context.getFlowScope().get(REPORTUNIT_URI); 107 if (reportUnitUri != null) { 108 ReportUnitResult result = 109 (ReportUnitResult)getEngine().execute( 110 JasperServerUtil.getExecutionContext(LocaleContextHolder.getLocale()), 111 new ReportUnitRequest(reportUnitUri, parameterValues) 112 ); 113 114 JasperPrint jasperPrint = result.getJasperPrint(); 115 setJasperPrint(context, jasperPrint); 116 117 context.getFlowScope().remove(getFlowAttributePageIndex()); 118 } 119 return success(); 120 } 121 122 protected void setJasperPrint(RequestContext context, JasperPrint jasperPrint) { 123 SharedAttributeMap sessionMap = context.getExternalContext().getSessionMap(); 124 int depth = context.getFlowScope().getRequiredInteger(getFlowAttributeDepth()).intValue(); 125 cleanJasperPrintObjects(sessionMap, depth); 126 127 String sessionName = getSessionAttributePrefixJasperPrint() + depth; 128 sessionMap.put(sessionName, jasperPrint); 129 context.getFlowScope().put(getFlowAttributeJasperPrintSessionName(), sessionName); 130 } 131 132 protected void cleanJasperPrintObjects(SharedAttributeMap sessionMap, int depth) { 133 String prefix = getSessionAttributePrefixJasperPrint(); 134 List toRemove = new ArrayList (); 135 for (Iterator it = sessionMap.getMap().keySet().iterator(); it.hasNext();) { 136 String key = (String ) it.next(); 137 if (key.startsWith(prefix)) { 138 try { 139 int keyDepth = Integer.parseInt(key.substring(prefix.length())); 140 if (keyDepth > depth) { 141 toRemove.add(key); 142 } 143 } catch (NumberFormatException e) { 144 } 146 } 147 } 148 149 for (Iterator it = toRemove.iterator(); it.hasNext();) { 150 String key = (String ) it.next(); 151 sessionMap.remove(key); 152 } 153 } 154 155 public Event navigate(RequestContext context) { 156 Integer pageIndex = (Integer ) context.getRequestParameters().getNumber(getRequestParameterPageIndex(), Integer .class); 157 if (pageIndex == null) { 158 context.getFlowScope().remove(getFlowAttributePageIndex()); 159 } else { 160 context.getFlowScope().put(getFlowAttributePageIndex(), pageIndex); 161 } 162 return success(); 163 } 164 165 166 public Event prepareReportView(RequestContext context) { 167 if (getHyperlinkProducerFactory() != null) { 168 context.getRequestScope().put(getRequestAttributeHtmlLinkHandlerFactory(), getHyperlinkProducerFactory()); 169 } 170 171 return success(); 172 } 173 174 175 public Event cleanSession(RequestContext context) { 176 String jasperPrintSessionName = context.getFlowScope().getString(getFlowAttributeJasperPrintSessionName()); 177 if (jasperPrintSessionName != null) { 178 context.getExternalContext().getSessionMap().remove(jasperPrintSessionName); 179 } 180 181 return success(); 182 } 183 184 public String getTransactionAttributeRequestParsed() { 185 return transactionAttributeRequestParsed; 186 } 187 188 public void setTransactionAttributeRequestParsed(String transactionAttributeRequestParsed) { 189 this.transactionAttributeRequestParsed = transactionAttributeRequestParsed; 190 } 191 192 public String getFlowAttributeInhibitRequestParsing() { 193 return flowAttributeInhibitRequestParsing; 194 } 195 196 public void setFlowAttributeInhibitRequestParsing( 197 String flowAttributeInhibitRequestParsing) { 198 this.flowAttributeInhibitRequestParsing = flowAttributeInhibitRequestParsing; 199 } 200 201 public String getFlowAttributePageIndex() { 202 return flowAttributePageIndex; 203 } 204 205 public void setFlowAttributePageIndex(String flowAttributePageIndex) { 206 this.flowAttributePageIndex = flowAttributePageIndex; 207 } 208 209 public String getRequestParameterPageIndex() { 210 return requestParameterPageIndex; 211 } 212 213 public void setRequestParameterPageIndex(String requestParameterPageIndex) { 214 this.requestParameterPageIndex = requestParameterPageIndex; 215 } 216 217 public String getRequestAttributeHtmlLinkHandlerFactory() { 218 return requestAttributeHtmlLinkHandlerFactory; 219 } 220 221 public void setRequestAttributeHtmlLinkHandlerFactory( 222 String requestAttributeHtmlLinkHandlerFactory) { 223 this.requestAttributeHtmlLinkHandlerFactory = requestAttributeHtmlLinkHandlerFactory; 224 } 225 226 public String getSessionAttributePrefixJasperPrint() { 227 return sessionAttributePrefixJasperPrint; 228 } 229 230 public void setSessionAttributePrefixJasperPrint( 231 String sessionAttributeJasperPrintStack) { 232 this.sessionAttributePrefixJasperPrint = sessionAttributeJasperPrintStack; 233 } 234 235 public String getFlowAttributeDepth() { 236 return flowAttributeDepth; 237 } 238 239 public void setFlowAttributeDepth(String flowAttributeDepth) { 240 this.flowAttributeDepth = flowAttributeDepth; 241 } 242 243 public String getFlowAttributeJasperPrintSessionName() { 244 return flowAttributeJasperPrintSessionName; 245 } 246 247 public void setFlowAttributeJasperPrintSessionName( 248 String flowAttributeJasperPrintSessionName) { 249 this.flowAttributeJasperPrintSessionName = flowAttributeJasperPrintSessionName; 250 } 251 252 public HyperlinkProducerFactoryFlowFactory getHyperlinkProducerFactory() { 253 return hyperlinkProducerFactory; 254 } 255 256 public void setHyperlinkProducerFactory( 257 HyperlinkProducerFactoryFlowFactory hyperlinkProducerFactory) { 258 this.hyperlinkProducerFactory = hyperlinkProducerFactory; 259 } 260 261 public String getFlowAttributeIsSubflow() { 262 return flowAttributeIsSubflow; 263 } 264 265 public void setFlowAttributeIsSubflow(String requestAttributeIsSubflow) { 266 this.flowAttributeIsSubflow = requestAttributeIsSubflow; 267 } 268 269 public String getFlowAttributeReportOutput() { 270 return flowAttributeReportOutput; 271 } 272 273 public void setFlowAttributeReportOutput(String flowAttributeReportOutput) { 274 this.flowAttributeReportOutput = flowAttributeReportOutput; 275 } 276 277 public String getRequestParameterReportOutput() { 278 return requestParameterReportOutput; 279 } 280 281 public void setRequestParameterReportOutput(String requestParameterReportOutput) { 282 this.requestParameterReportOutput = requestParameterReportOutput; 283 } 284 285 public String getFlowAttributeUseClientTimezone() { 286 return flowAttributeUseClientTimezone; 287 } 288 289 public void setFlowAttributeUseClientTimezone( 290 String flowAttributeUseClientTimezone) { 291 this.flowAttributeUseClientTimezone = flowAttributeUseClientTimezone; 292 } 293 294 } 295 | Popular Tags |