1 64 package com.jcorporate.expresso.ext.controller; 65 66 import com.jcorporate.expresso.core.controller.ControllerException; 67 import com.jcorporate.expresso.core.controller.ControllerRequest; 68 import com.jcorporate.expresso.core.controller.ControllerResponse; 69 import com.jcorporate.expresso.core.controller.ErrorCollection; 70 import com.jcorporate.expresso.core.controller.Input; 71 import com.jcorporate.expresso.core.controller.NonHandleableException; 72 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 73 import com.jcorporate.expresso.core.controller.State; 74 import com.jcorporate.expresso.core.controller.Transition; 75 import com.jcorporate.expresso.core.db.DBException; 76 import com.jcorporate.expresso.core.dbobj.Schema; 77 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 78 import com.jcorporate.expresso.core.dbobj.ValidValue; 79 import com.jcorporate.expresso.ext.report.ExpressoReport; 80 import com.jcorporate.expresso.ext.report.MultiFormatReport; 81 import com.jcorporate.expresso.ext.report.ReportException; 82 import com.jcorporate.expresso.kernel.util.ClassLocator; 83 import com.jcorporate.expresso.services.dbobj.SchemaList; 84 import org.apache.log4j.Logger; 85 86 import javax.servlet.http.HttpServletResponse ; 87 import java.util.Iterator ; 88 import java.util.Vector ; 89 90 91 109 public class ReportServer 110 extends com.jcorporate.expresso.core.controller.DBController { 111 112 115 public static final transient Logger log = Logger.getLogger(ReportServer.class); 116 117 124 public ReportServer() throws ControllerException { 125 super(); 126 127 State s = new State("prompt", "Prompt"); 128 this.addState(s); 129 this.setInitialState("prompt"); 130 131 s = new State("selectReport", "Select Report"); 132 s.addRequiredParameter("SchemaClass"); 133 this.addState(s); 134 135 s = new State("setParameters", "Set Report Parameters"); 136 s.addRequiredParameter("ReportClass"); 137 s.addOptionalParameter("ReportCode"); 138 this.addState(s); 139 140 s = new State("processReport", "Process Report"); 141 s.addRequiredParameter("ReportClass"); 142 s.addOptionalParameter("ReportCode"); 143 s.addOptionalParameter("ReportFormat"); 144 this.addState(s); 145 } 146 147 153 public String getTitle() { 154 return "Expresso Report Server"; 155 } 156 157 168 protected ExpressoReport instantiateReport(ControllerRequest request) 169 throws ControllerException, ClassNotFoundException { 170 String reportClass = request.getParameter("ReportClass"); 171 172 ExpressoReport report = null; 173 174 try { 175 report = (ExpressoReport) ClassLocator.loadClass(reportClass) 176 .newInstance(); 177 } catch (IllegalAccessException ex) { 178 throw new ControllerException("No public default constructor found in class: " + reportClass, 179 ex); 180 } catch (InstantiationException ex) { 181 throw new ControllerException("Unable to construct new instance of object: " + reportClass, ex); 182 } catch (ClassCastException ex) { 183 throw new ControllerException("Class Requested: " + reportClass + 184 " was not an Expresso Report", ex); 185 } 186 187 return report; 188 } 189 190 200 protected void runProcessReportState(ControllerRequest request, 201 ControllerResponse response) 202 throws ControllerException, NonHandleableException { 203 try { 204 ExpressoReport report = instantiateReport(request); 205 java.util.Map paramMap = new java.util.HashMap (request.getParameters()); 206 report.setReportParameters(paramMap); 207 report.setDataContext(request.getDataContext()); 208 report.setReportCode(request.getParameter("ReportCode")); 209 210 if (report instanceof MultiFormatReport) { 211 ((MultiFormatReport) report).setReportFormat(request.getParameter("ReportFormat")); 212 } 213 214 if (!(request instanceof ServletControllerRequest)) { 215 response.addError("To run the report, report server " + 216 "must be run inside a Servlet environment"); 217 transition("prompt", request, response); 218 219 return; 220 } 221 222 ServletControllerRequest scr = (ServletControllerRequest) request; 223 response.setCustomResponse(true); 224 225 HttpServletResponse servResponse = scr.getHttpServletResponse(); 226 227 servResponse.setHeader("Content-Disposition", 228 "inline;filename=" + "ExpressoReport." + 229 report.getReportFileExtension()); 230 231 servResponse.setContentType(report.getReportMimeType()); 232 233 java.io.OutputStream os = servResponse.getOutputStream(); 234 report.printReport(os); 235 236 return; 237 } catch (ClassNotFoundException ex) { 238 ErrorCollection errs = new ErrorCollection(); 239 log.error("Error, class not found: ", ex); 240 errs.addError("Class specified not found"); 241 response.saveErrors(errs); 242 transition("prompt", request, response); 243 } catch (ReportException ex) { 244 log.error("Report exception: ", ex); 245 throw new ControllerException("Error processing report", ex); 246 } catch (java.io.IOException ex) { 247 log.warn("IO Exception sending report", ex); 248 } 249 } 250 251 261 protected void runPromptState(ControllerRequest request, 262 ControllerResponse response) 263 throws ControllerException, NonHandleableException { 264 try { 265 266 Input chooseSchema = new Input(); 267 chooseSchema.setLabel("Choose Schema"); 268 chooseSchema.setName("SchemaClass"); 269 270 Vector v2 = new Vector (2); 271 v2.addElement(new ValidValue("com.jcorporate.expresso." + 272 "core.ExpressoSchema", "General")); 273 274 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 275 sl.setDataContext(request.getDataContext()); 276 277 SchemaList oneSchema = null; 278 279 for (Iterator e = sl.searchAndRetrieveList("Descrip").iterator(); 280 e.hasNext();) { 281 oneSchema = (SchemaList) e.next(); 282 v2.addElement(new ValidValue(oneSchema.getField("SchemaClass"), 283 oneSchema.getField("Descrip"))); 284 } 285 286 chooseSchema.setValidValues(v2); 287 response.addInput(chooseSchema); 288 289 response.add(new Transition("selectReport", this)); 290 response.setTitle("Expresso Report Server"); 291 } catch (DBException ex) { 292 log.error("Database Error Prompting for Report Schemas", ex); 293 throw new ControllerException("Database Access Error", ex); 294 } 295 } 296 297 307 protected void runSelectReportState(ControllerRequest request, 308 ControllerResponse response) 309 throws ControllerException, NonHandleableException { 310 ErrorCollection ec = request.getErrorCollection(); 311 312 if ((ec != null) && (ec.getErrorCount() > 0)) { 313 response.saveErrors(ec); 314 transition("prompt", request, response); 315 316 return; 317 } 318 319 try { 320 SchemaList mySchema = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 322 mySchema.setDataContext(request.getDataContext()); 323 324 String schemaClass = com.jcorporate.expresso.core.ExpressoSchema.class.getName(); 325 mySchema.setField("SchemaClass", request.getParameter("SchemaClass")); 326 327 if (mySchema.find()) { 328 schemaClass = request.getParameter("SchemaClass"); 329 } else if (!com.jcorporate.expresso.core.ExpressoSchema.class.getName() 330 .equals(request.getParameter("SchemaClass"))) { 331 if (ec == null) { 332 ec = new ErrorCollection(); 333 } 334 335 ec.addError("Schema class: " + 336 request.getParameter("SchemaClass") + 337 " is not installed on this system"); 338 response.saveErrors(ec); 339 transition("prompt", request, response); 340 341 return; 342 } 343 344 Schema schema = (Schema) ClassLocator.loadClass(schemaClass) 345 .newInstance(); 346 Vector v = schema.getReports(); 347 348 if ((v == null) || (v.size() == 0)) { 349 if (ec == null) { 350 ec = new ErrorCollection(); 351 } 352 353 ec.addError("The requested schema doesn't have any reports defined"); 354 response.saveErrors(ec); 355 transition("prompt", request, response); 356 357 return; 358 } 359 360 Vector vv = new Vector (v.size()); 361 362 for (Iterator i = v.iterator(); i.hasNext();) { 363 ExpressoReport oneReport = (ExpressoReport) i.next(); 364 vv.add(new ValidValue(((Object ) oneReport).getClass().getName(), 365 oneReport.getTitle())); 366 } 367 368 Input chooseReport = new Input("ReportClass", "Report To Use"); 369 chooseReport.setValidValues(vv); 370 response.add(chooseReport); 371 372 Transition t = new Transition("process", "Continue", 373 this.getClass(), "setParameters"); 374 response.add(t); 375 response.setTitle("Expresso Report Server"); 376 } catch (DBException ex) { 377 throw new ControllerException("Database access error", ex); 378 } catch (ClassCastException ex) { 379 throw new ControllerException("Class loaded was not a schema", ex); 380 } catch (ClassNotFoundException ex) { 381 ErrorCollection errs = new ErrorCollection(); 382 log.error("Error, class not found: ", ex); 383 errs.addError("Class specified not found"); 384 response.saveErrors(errs); 385 transition("prompt", request, response); 386 } catch (IllegalAccessException ex) { 387 throw new ControllerException("No public default constructor found in class", 388 ex); 389 } catch (InstantiationException ex) { 390 throw new ControllerException("Unable to construct new instance of object", 391 ex); 392 } 393 } 394 395 406 protected void runSetParametersState(ControllerRequest request, 407 ControllerResponse response) 408 throws ControllerException, NonHandleableException { 409 ErrorCollection ec = request.getErrorCollection(); 410 411 if ((ec != null) && (ec.getErrorCount() > 0)) { 412 response.saveErrors(ec); 413 transition("selectReport", request, response); 414 415 return; 416 } 417 418 try { 419 ExpressoReport report = this.instantiateReport(request); 420 String reportClass = ((Object ) report).getClass().getName(); 421 422 java.util.List paramNames = report.getParameterNames(); 423 424 if ((paramNames != null) && (paramNames.size() > 0)) { 425 for (Iterator i = paramNames.iterator(); i.hasNext();) { 427 String oneParamName = (String ) i.next(); 428 Input input = new Input(oneParamName, oneParamName); 429 input.setDefaultValue(report.getDefaultValue(oneParamName)); 430 response.add(input); 431 } 432 } 433 434 Input reportCode = new Input("ReportCode", "Report Code"); 435 response.add(reportCode); 436 437 if (report instanceof MultiFormatReport) { 439 java.util.List l = ((MultiFormatReport) report).getReportFormats(); 440 Input reportFormats = new Input("ReportFormat", 441 "Select Output Format"); 442 reportFormats.setValidValues(new Vector (l)); 443 } 444 445 Transition t = new Transition("prompt", "Start Again", 446 this.getClass(), "prompt"); 447 response.add(t); 448 t = new Transition("process", "Process Report", this.getClass(), 449 "processReport"); 450 t.addParam("ReportClass", reportClass); 451 response.add(t); 452 response.setTitle("Expresso Report Server"); 453 } catch (ClassNotFoundException ex) { 454 ErrorCollection errs = new ErrorCollection(); 455 log.error("Error, class not found: ", ex); 456 errs.addError("Class specified not found"); 457 response.saveErrors(errs); 458 transition("prompt", request, response); 459 } 460 } 461 } 462 | Popular Tags |