Exemplo n.º 1
0
 private void setupRenderTask(IRenderTask rendTask, RptProperties properties) {
   String outputFormat = null;
   switch (properties.getOutputFormat()) {
     case HTML:
       outputFormat = "html";
       break;
     case DOC:
       outputFormat = "doc";
       break;
     case PDF:
       outputFormat = "pdf";
       break;
     case PPT:
       outputFormat = "ppt";
       break;
     case XLS:
       outputFormat = "xls";
       break;
     default:
       outputFormat = "html";
   }
   IRenderOption renderOption = new RenderOption();
   renderOption.setOutputStream(new ByteArrayOutputStream(bufferSize));
   renderOption.setOutputFormat(outputFormat);
   rendTask.setRenderOption(renderOption);
   rendTask.setLocale(properties.getLocale());
 }
  @SuppressWarnings("unchecked")
  protected void renderMergedOutputModel(
      Map<String, Object> modelData, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    FileInputStream fis = null;
    IReportRunnable runnable = null;
    IReportDocument document = null;
    try {

      if (this.reportParameters == null) this.reportParameters = new HashMap<String, Object>();

      for (String k : modelData.keySet()) this.reportParameters.put(k, modelData.get(k));

      // 2) reportName property
      // 1) report name parameter is available, use that

      String reportName;
      reportName =
          StringUtils.hasText(this.reportName)
              ? this.reportName
              : request.getParameter(this.reportNameRequestParameter); // 'cat'
      String fullReportName = canonicalizeName(reportName);

      String documentName;
      documentName =
          StringUtils.hasText(this.documentName)
              ? this.documentName
              : request.getParameter(this.documentNameRequestParameter); // 'cat'
      String fullDocumentName = canonicalizeDocName(documentName);
      if (documentName == null) {
        fullDocumentName = reportName.replaceAll(".rptdesign", ".rptdocument");
      }

      String format;
      if (this.reportOutputFormat != null) {
        format = this.reportOutputFormat;
      } else {
        format = request.getParameter(this.reportFormatRequestParameter);
      }
      ServletContext sc =
          request.getServletContext(); // / avoid creating an HTTP session if possible.

      if (format == null) {
        format = "html";
      }

      Map<String, Object> mapOfOptions = new HashMap<String, Object>();
      mapOfOptions.put(
          IModuleOption.RESOURCE_FOLDER_KEY,
          birtViewResourcePathCallback.resourceDirectory(sc, request, reportName));
      mapOfOptions.put(IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE);

      // set content type
      String contentType = birtEngine.getMIMEType(format);
      response.setContentType(contentType);
      setContentType(contentType);

      Map<String, Object> appContextMap = new HashMap<String, Object>();
      appContextMap.put(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST, request);

      if (this.dataSource != null) {
        appContextMap.put(IConnectionFactory.PASS_IN_CONNECTION, this.dataSource.getConnection());

        if (this.closeDataSourceConnection)
          appContextMap.put(IConnectionFactory.CLOSE_PASS_IN_CONNECTION, Boolean.TRUE);
      }

      IEngineTask task = null;
      String pathForReport =
          birtViewResourcePathCallback.pathForReport(sc, request, fullReportName);
      fis = new FileInputStream(pathForReport);
      runnable = birtEngine.openReportDesign(fullReportName, fis, mapOfOptions);

      if (runnable != null && this.taskType == AbstractSingleFormatBirtView.RUNRENDERTASK) {
        task = birtEngine.createRunAndRenderTask(runnable);
        task.setParameterValues(discoverAndSetParameters(runnable, request));
        IRunAndRenderTask runAndRenderTask = (IRunAndRenderTask) task;
        IRenderOption options = null == this.renderOption ? new RenderOption() : this.renderOption;
        options.setActionHandler(actionHandler);
        IRenderOption returnedRenderOptions =
            renderReport(
                modelData,
                request,
                response,
                this.birtViewResourcePathCallback,
                appContextMap,
                reportName,
                format,
                options);
        for (String k : appContextMap.keySet())
          runAndRenderTask.getAppContext().put(k, appContextMap.get(k));
        runAndRenderTask.setRenderOption(returnedRenderOptions);
        runAndRenderTask.run();
        runAndRenderTask.close();
      } else {

        // Run then Render
        if (runnable != null) {
          task = birtEngine.createRunTask(runnable);
          task.setParameterValues(discoverAndSetParameters(runnable, request));
          IRunTask runTask = (IRunTask) task;
          for (String k : appContextMap.keySet())
            runTask.getAppContext().put(k, appContextMap.get(k));
          String pathForDocument =
              birtViewResourcePathCallback.pathForDocument(sc, request, fullDocumentName);
          runTask.run(pathForDocument);
          runTask.close();
          document = birtEngine.openReportDocument(fullDocumentName, pathForDocument, mapOfOptions);
          task = birtEngine.createRenderTask(document);
          IRenderTask renderTask = (IRenderTask) task;
          IRenderOption options =
              null == this.renderOption ? new RenderOption() : this.renderOption;
          options.setActionHandler(actionHandler);
          IRenderOption returnedRenderOptions =
              renderReport(
                  modelData,
                  request,
                  response,
                  this.birtViewResourcePathCallback,
                  appContextMap,
                  reportName,
                  format,
                  options);
          for (String k : appContextMap.keySet())
            renderTask.getAppContext().put(k, appContextMap.get(k));
          if (renderRange != null) {

            renderTask.setPageRange(renderRange);
          }
          renderTask.setRenderOption(returnedRenderOptions);
          renderTask.render();
          renderTask.close();
          document.close();
        }
      }

    } catch (Throwable th) {
      throw new RuntimeException(th); // nothing useful to do here
    } finally {
      if (null != fis) IOUtils.closeQuietly(fis);
      if (null != document) document.close();
    }
  }