Exemplo n.º 1
0
 /**
  * Creates a new export task.
  *
  * @param dialog the progress dialog that will monitor the report progress.
  * @param report the report that should be exported.
  */
 public ExcelExportTask(
     final MasterReport report,
     final ReportProgressDialog dialog,
     final SwingGuiContext swingGuiContext)
     throws ReportProcessingException {
   if (report == null) {
     throw new NullPointerException(
         "ExcelExportTask(..): Null report parameter not permitted"); //$NON-NLS-1$
   }
   this.fileName =
       report
           .getConfiguration()
           .getConfigProperty(
               "org.pentaho.reporting.engine.classic.core.modules.gui.xls.FileName"); //$NON-NLS-1$
   if (fileName == null) {
     throw new ReportProcessingException(
         "ExcelExportTask(): Filename is not defined"); //$NON-NLS-1$
   }
   this.progressDialog = dialog;
   this.report = report;
   if (swingGuiContext != null) {
     this.statusListener = swingGuiContext.getStatusListener();
     this.messages =
         new Messages(
             swingGuiContext.getLocale(),
             ExcelExportPlugin.BASE_RESOURCE_CLASS,
             ObjectUtilities.getClassLoader(ExcelExportPlugin.class));
   }
 }
  @Override
  protected boolean performExport(final MasterReport report, final OutputStream outputStream) {
    try {
      String dataDirectory =
          getInputStringValue(AbstractJFreeReportComponent.REPORTDIRECTORYHTML_DATADIR);
      if (dataDirectory == null) {
        dataDirectory = "data"; // $NON-NLS-1$
      }

      final ZipRepository zipRepository = new ZipRepository();
      final ContentLocation root = zipRepository.getRoot();
      final ContentLocation data =
          RepositoryUtilities.createLocation(
              zipRepository, RepositoryUtilities.split(dataDirectory, "/")); // $NON-NLS-1$

      final FlowHtmlOutputProcessor outputProcessor =
          new FlowHtmlOutputProcessor(report.getConfiguration());

      final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager());
      printer.setContentWriter(root, new DefaultNameGenerator(root, "report.html")); // $NON-NLS-1$
      printer.setDataWriter(data, new DefaultNameGenerator(data, "content")); // $NON-NLS-1$
      printer.setUrlRewriter(new SingleRepositoryURLRewriter());
      outputProcessor.setPrinter(printer);

      final FlowReportProcessor sp = new FlowReportProcessor(report, outputProcessor);
      final int yieldRate = getYieldRate();
      if (yieldRate > 0) {
        sp.addReportProgressListener(new YieldReportListener(yieldRate));
      }
      sp.processReport();
      zipRepository.write(outputStream);
      close();
      return true;
    } catch (ReportProcessingException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportZipHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    } catch (IOException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportZipHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    } catch (ContentIOException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportZipHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    }
  }
  /**
   * Saves a report to plain text format.
   *
   * @param report the report.
   * @param filename target file name.
   * @throws Exception if an error occurs.
   */
  public static void createPlainText(final MasterReport report, final String filename)
      throws Exception {
    final OutputStream fout = new BufferedOutputStream(new FileOutputStream(filename));
    // cpi = 15, lpi = 10
    final TextFilePrinterDriver pc = new TextFilePrinterDriver(fout, 15, 10);

    final PageableTextOutputProcessor outputProcessor =
        new PageableTextOutputProcessor(pc, report.getConfiguration());
    final PageableReportProcessor proc = new PageableReportProcessor(report, outputProcessor);
    proc.processReport();
    proc.close();
    fout.close();
  }
  public void testRichText() throws ReportProcessingException, ContentProcessingException {
    final Element e = new Element();
    e.setElementType(LabelType.INSTANCE);
    e.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.RICH_TEXT_TYPE, "text/html");
    e.setAttribute(
        AttributeNames.Core.NAMESPACE,
        AttributeNames.Core.VALUE,
        "Hi I am trying to use the <b>rich text type = text/html</b> in PRD version - 3.7.");
    e.getStyle().setStyleProperty(TextStyleKeys.FONTSIZE, 12);
    e.getStyle().setStyleProperty(TextStyleKeys.FONT, "Arial");
    e.getStyle().setStyleProperty(ElementStyleKeys.MIN_WIDTH, 285f);
    e.getStyle().setStyleProperty(ElementStyleKeys.MIN_HEIGHT, 20f);

    final MasterReport report = new MasterReport();
    report.getReportHeader().addElement(e);

    final LogicalPageBox logicalPageBox =
        DebugReportRunner.layoutSingleBand(report, report.getReportHeader(), false, false);
    logicalPageBox.getRepeatFooterArea().setY(logicalPageBox.getContentArea().getHeight());
    logicalPageBox.getFooterArea().setY(logicalPageBox.getContentArea().getHeight());

    // ModelPrinter.INSTANCE.print(logicalPageBox);

    final RenderNode[] elementsByNodeType =
        MatchFactory.findElementsByNodeType(logicalPageBox, LayoutNodeTypes.TYPE_NODE_TEXT);
    assertEquals(
        17, elementsByNodeType.length); // quick and easy way to see that all elements are there.

    // debugPrintText(elementsByNodeType);

    final LocalFontRegistry registry = new LocalFontRegistry();
    registry.initialize();

    final GraphicsOutputProcessorMetaData metaData =
        new GraphicsOutputProcessorMetaData(new DefaultFontStorage(registry));
    metaData.initialize(report.getConfiguration());

    final LogicalPageDrawable drawable = new LogicalPageDrawable();
    drawable.init(logicalPageBox, metaData, report.getResourceManager());

    final TracingGraphics g2 = new TracingGraphics();
    drawable.draw(g2, new Rectangle2D.Double(0, 0, 500, 500));
    /*
        for (int i = 0; i < g2.records.size(); i++)
        {
          final TextTraceRecord record = g2.records.get(i);
          System.out.println ("goldenSamples.add(new TextTraceRecord(" + record.x + ", " + record.y + ", \"" + record.text +"\"));");
        }
    */
    assertEquals(getSamples(), g2.records);
  }
  protected PageableReportProcessor createReportProcessor(
      final MasterReport report, final int yieldRate) throws ReportProcessingException {

    proxyOutputStream = new ProxyOutputStream();

    printer = new AllItemsHtmlPrinter(report.getResourceManager());
    printer.setUrlRewriter(new PentahoURLRewriter(contentHandlerPattern, false));

    final PageableHtmlOutputProcessor outputProcessor =
        new PageableHtmlOutputProcessor(report.getConfiguration());
    outputProcessor.setPrinter(printer);
    proc = new PageableReportProcessor(report, outputProcessor);

    if (yieldRate > 0) {
      proc.addReportProgressListener(new YieldReportListener(yieldRate));
    }

    return proc;
  }
 /**
  * Saves a report to PDF format.
  *
  * @param report the report.
  * @param fileName target file name.
  * @return true or false.
  */
 public static boolean createPDF(final MasterReport report, final String fileName) {
   OutputStream out = null;
   try {
     out = new BufferedOutputStream(new FileOutputStream(new File(fileName)));
     final PdfOutputProcessor outputProcessor =
         new PdfOutputProcessor(report.getConfiguration(), out, report.getResourceManager());
     final PageableReportProcessor proc = new PageableReportProcessor(report, outputProcessor);
     proc.processReport();
     proc.close();
     out.close();
     return true;
   } catch (Exception e) {
     logger.error("Writing PDF failed.", e);
     return false;
   } finally {
     try {
       if (out != null) {
         out.close();
       }
     } catch (Exception e) {
       logger.error("Saving PDF failed.", e);
     }
   }
 }
  @SuppressWarnings("deprecation")
  @Override
  protected boolean performExport(final MasterReport report, final OutputStream outputStream) {
    try {
      IContentRepository contentRepository = null;
      try {
        contentRepository = PentahoSystem.get(IContentRepository.class, getSession());
      } catch (Throwable t) {
        debug(
            Messages.getInstance()
                .getString("JFreeReportHtmlComponent.DEBUG_0044_PROCESSING_WITHOUT_CONTENT_REPOS"),
            t); //$NON-NLS-1$
      }

      String contentHandlerPattern =
          getInputStringValue(AbstractJFreeReportComponent.REPORTHTML_CONTENTHANDLER);
      if (contentHandlerPattern == null) {
        final Configuration globalConfig = ClassicEngineBoot.getInstance().getGlobalConfig();
        contentHandlerPattern =
            globalConfig.getConfigProperty("org.pentaho.web.ContentHandler"); // $NON-NLS-1$
      }

      final IApplicationContext ctx = PentahoSystem.getApplicationContext();

      final URLRewriter rewriter;
      final ContentLocation dataLocation;
      final NameGenerator dataNameGenerator;
      if ((contentRepository == null)
          || JFreeReportHtmlComponent.DO_NOT_USE_THE_CONTENT_REPOSITORY) {
        debug(
            Messages.getInstance()
                .getString(
                    "JFreeReportHtmlComponent.DEBUG_0044_PROCESSING_WITHOUT_CONTENT_REPOS")); //$NON-NLS-1$
        if (ctx != null) {
          File dataDirectory = new File(ctx.getFileOutputPath("system/tmp/")); // $NON-NLS-1$
          if (dataDirectory.exists() && (dataDirectory.isDirectory() == false)) {
            dataDirectory = dataDirectory.getParentFile();
            if (dataDirectory.isDirectory() == false) {
              throw new ReportProcessingException(
                  Messages.getInstance()
                      .getErrorString(
                          "JFreeReportDirectoryComponent.ERROR_0001_INVALID_DIR",
                          dataDirectory.getPath())); // $NON-NLS-1$
            }
          } else if (dataDirectory.exists() == false) {
            dataDirectory.mkdirs();
          }

          final FileRepository dataRepository = new FileRepository(dataDirectory);
          dataLocation = dataRepository.getRoot();
          dataNameGenerator = new DefaultNameGenerator(dataLocation);
          rewriter = new PentahoURLRewriter(contentHandlerPattern);
        } else {
          dataLocation = null;
          dataNameGenerator = null;
          rewriter = new PentahoURLRewriter(contentHandlerPattern);
        }
      } else {
        debug(
            Messages.getInstance()
                .getString(
                    "JFreeReportHtmlComponent.DEBUG_045_PROCESSING_WITH_CONTENT_REPOS")); //$NON-NLS-1$
        final String thePath =
            getSolutionName()
                + "/"
                + getSolutionPath()
                + "/"
                + getSession().getId(); // $NON-NLS-1$//$NON-NLS-2$
        final IContentLocation pentahoContentLocation =
            contentRepository.newContentLocation(
                thePath, getActionName(), getActionTitle(), getSolutionPath(), true);
        // todo
        final ReportContentRepository repository =
            new ReportContentRepository(pentahoContentLocation, getActionName());
        dataLocation = repository.getRoot();
        dataNameGenerator = new DefaultNameGenerator(dataLocation);
        rewriter = new PentahoURLRewriter(contentHandlerPattern);
      }

      final StreamRepository targetRepository = new StreamRepository(null, outputStream);
      final ContentLocation targetRoot = targetRepository.getRoot();

      final HtmlOutputProcessor outputProcessor =
          new StreamHtmlOutputProcessor(report.getConfiguration());
      final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager());
      printer.setContentWriter(
          targetRoot,
          new DefaultNameGenerator(targetRoot, "index", "html")); // $NON-NLS-1$//$NON-NLS-2$
      printer.setDataWriter(dataLocation, dataNameGenerator);
      printer.setUrlRewriter(rewriter);
      outputProcessor.setPrinter(printer);

      final StreamReportProcessor sp = new StreamReportProcessor(report, outputProcessor);
      final int yieldRate = getYieldRate();
      if (yieldRate > 0) {
        sp.addReportProgressListener(new YieldReportListener(yieldRate));
      }
      sp.processReport();
      sp.close();

      outputStream.flush();
      close();
      return true;
    } catch (ReportProcessingException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    } catch (IOException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    } catch (ContentIOException e) {
      error(
          Messages.getInstance()
              .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"),
          e); //$NON-NLS-1$
      return false;
    }
  }
Exemplo n.º 8
0
  /** Exports the report into an Excel file. */
  public void run() {
    OutputStream out = null;
    File file = null;
    try {
      file = new File(fileName).getCanonicalFile();
      final File directory = file.getParentFile();
      if (directory != null) {
        if (directory.exists() == false) {
          if (directory.mkdirs() == false) {
            ExcelExportTask.logger.warn(
                "Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
          }
        }
      }
      out = new BufferedOutputStream(new FileOutputStream(file));
      final FlowExcelOutputProcessor target =
          new FlowExcelOutputProcessor(report.getConfiguration(), out, report.getResourceManager());
      target.setUseXlsxFormat(false);
      final FlowReportProcessor reportProcessor = new FlowReportProcessor(report, target);
      if (progressDialog != null) {
        progressDialog.setModal(false);
        progressDialog.setVisible(true);
        reportProcessor.addReportProgressListener(progressDialog);
      }
      reportProcessor.processReport();
      out.close();
      out = null;
      if (progressDialog != null) {
        reportProcessor.removeReportProgressListener(progressDialog);
      }

      if (statusListener != null) {
        statusListener.setStatus(
            StatusType.INFORMATION,
            messages.getString("ExcelExportTask.USER_TASK_FINISHED"),
            null); //$NON-NLS-1$
      }

    } catch (ReportInterruptedException re) {
      if (statusListener != null) {
        statusListener.setStatus(
            StatusType.WARNING,
            messages.getString("ExcelExportTask.USER_TASK_ABORTED"),
            null); //$NON-NLS-1$
      }

      try {
        out.close();
        out = null;
        if (file.delete() == false) {
          ExcelExportTask.logger.warn("Unable to delete incomplete export:" + file); // $NON-NLS-1$
        }
      } catch (SecurityException se) {
        // ignore me
      } catch (IOException ioe) {
        // ignore me...
      }
    } catch (Exception re) {
      ExcelExportTask.logger.error("Excel export failed", re); // $NON-NLS-1$
      if (statusListener != null) {
        statusListener.setStatus(
            StatusType.WARNING,
            messages.getString("ExcelExportTask.USER_TASK_FAILED"),
            re); //$NON-NLS-1$
      }
    } finally {
      try {
        if (out != null) {
          out.close();
        }
      } catch (Exception e) {
        ExcelExportTask.logger.error("Unable to close the output stream.", e); // $NON-NLS-1$
        if (statusListener != null) {
          statusListener.setStatus(
              StatusType.WARNING,
              messages.getString("ExcelExportTask.USER_TASK_FAILED"),
              e); //$NON-NLS-1$
        }
        // if there is already another error, this exception is
        // just a minor obstactle. Something big crashed before ...
      }
    }
    if (progressDialog != null) {
      progressDialog.setVisible(false);
    }
  }