@TransactionAttribute(
      TransactionAttributeType.SUPPORTS) // Never begin a transaction for report rendering
  @RolesAllowed({org.nightlabs.jfire.reporting.RoleConstants.renderReport_roleID})
  @Override
  public void processScheduledReport(TaskID taskID) throws Exception {

    PersistenceManager pm = createPersistenceManager();
    try {
      if (logger.isDebugEnabled())
        logger.debug("processScheduledReport started, determine parameters");
      Task task = (Task) pm.getObjectById(taskID);
      ScheduledReport scheduledReport = (ScheduledReport) task.getParam();
      if (logger.isDebugEnabled())
        logger.debug(
            "processScheduledReport have "
                + ScheduledReport.describeScheduledReport(scheduledReport));
      RenderReportRequest renderReportRequest = scheduledReport.getRenderReportRequest();
      if (renderReportRequest == null) {
        throw new RenderReportException(
            "Can not render ScheduledReport as the RenderReportRequest is not set. "
                + ScheduledReport.describeScheduledReport(scheduledReport));
      }
      ReportRegistryItemID reportLayoutID = scheduledReport.getReportLayoutID();
      if (reportLayoutID == null) {
        throw new RenderReportException(
            "Can not render ScheduledReport as no ReportLayout is set for it. "
                + ScheduledReport.describeScheduledReport(scheduledReport));
      }
      if (logger.isDebugEnabled())
        logger.debug("processScheduledReport have reportLayoutID " + reportLayoutID);

      Map<String, Object> newParams =
          convertScheduledReportParams(scheduledReport, renderReportRequest);
      renderReportRequest.setParameters(newParams);

      IScheduledReportDeliveryDelegate deliveryDelegate = scheduledReport.getDeliveryDelegate();
      if (deliveryDelegate == null) {
        throw new IllegalStateException(
            "Can not render ScheduledReport as no ScheduledReportDeliveryDelegate is set for it. "
                + ScheduledReport.describeScheduledReport(scheduledReport));
      }
      if (logger.isDebugEnabled())
        logger.debug("processScheduledReport have deliveryDelegate " + deliveryDelegate);

      renderReportRequest.setReportRegistryItemID(reportLayoutID);

      if (logger.isDebugEnabled())
        logger.debug("processScheduledReport rendering ReportLayout " + reportLayoutID);
      RenderedReportLayout renderedReportLayout =
          ReportLayoutRendererUtil.renderReport(pm, renderReportRequest);

      if (logger.isDebugEnabled())
        logger.debug("processScheduledReport delivering rendered report.");
      deliveryDelegate.deliverReportOutput(scheduledReport, renderedReportLayout);

    } finally {
      pm.close();
    }
  }
 private Map<String, Object> convertScheduledReportParams(
     ScheduledReport scheduledReport, RenderReportRequest renderReportRequest) throws Exception {
   if (logger.isDebugEnabled())
     logger.debug("Check report parameters for InputTimePatterns and converting to Date");
   // We make sure we can modify the map
   Map<String, Object> newParams =
       new HashMap<String, Object>(renderReportRequest.getParameters());
   Map<String, Object> convertedParams = new HashMap<String, Object>();
   Date now = new Date();
   for (Map.Entry<String, Object> paramEntry : newParams.entrySet()) {
     Object paramValue = paramEntry.getValue();
     if (paramValue instanceof InputTimePattern) {
       if (logger.isDebugEnabled())
         logger.debug(
             "Have InputTimePattern for parameter " + paramEntry.getKey() + ", converting....");
       Date convertedParam = null;
       try {
         convertedParam = ((InputTimePattern) paramValue).getTime(now);
         if (logger.isDebugEnabled())
           logger.debug(
               "InputTimePattern for parameter "
                   + paramEntry.getKey()
                   + ", converted "
                   + paramValue
                   + " to "
                   + convertedParam);
       } catch (Exception e) {
         logger.error(
             "Error converting InputTimePattern parameter of schedledReport "
                 + ScheduledReport.describeScheduledReport(scheduledReport)
                 + " to a date. Will proceed with un-set parameter.",
             e);
         convertedParam = null;
       }
       convertedParams.put(paramEntry.getKey(), convertedParam);
     }
     if (paramValue instanceof InputTimePatternPeriod) {
       if (logger.isDebugEnabled())
         logger.debug(
             "Have InputTimePatternPeriod for parameter "
                 + paramEntry.getKey()
                 + ", converting....");
       TimePeriod convertedParam = ((InputTimePatternPeriod) paramValue).getTimePeriod(now, false);
       if (logger.isDebugEnabled())
         logger.debug(
             "InputTimePattern for parameter "
                 + paramEntry.getKey()
                 + ", converted "
                 + paramValue
                 + " to "
                 + convertedParam);
       convertedParams.put(paramEntry.getKey(), convertedParam);
     }
   }
   newParams.putAll(convertedParams);
   return newParams;
 }
コード例 #3
0
  /**
   * Prints the given report (registryItemID) with the given parameters. The {@link ReportUseCase}
   * for the given report will be searched and used for printing.
   *
   * @param registryItemID The id of the report to print.
   * @param params The parameters to use to render the report.
   * @param monitor The monitor to report progress to.
   * @throws PrinterException If an error occurs while printing.
   */
  public void printWithParams(
      ReportRegistryItemID registryItemID, Map<String, Object> params, ProgressMonitor monitor)
      throws PrinterException {
    monitor.beginTask(
        Messages.getString(
            "org.nightlabs.jfire.reporting.ui.layout.action.print.AbstractPrintReportLayoutAction.task.printingReportLayout"),
        6); //$NON-NLS-1$
    String useCaseID = getReportUseCaseID(registryItemID, params);
    if (useCaseID == null) {
      // Try to lookup the UseCase by the reportLayoutType
      ReportUseCase useCase =
          ReportUseCaseRegistry.sharedInstance()
              .getReportUseCaseByLayoutType(registryItemID.reportRegistryItemType);
      if (useCase != null) useCaseID = useCase.getId();
    }
    if (useCaseID == null) {
      // if no usecase found till now, try to find the default/fallback one
      ReportUseCase useCase =
          ReportUseCaseRegistry.sharedInstance()
              .getReportUseCase(ReportingPlugin.DEFAULT_REPORT_USE_CASE_ID);
      if (useCase == null) {
        throw new PrinterException(
            "No useCaseID could be found. A usecase for the type "
                + registryItemID.reportRegistryItemType
                + " could not be found and also the default/fallback usecase seems not to be registered."); //$NON-NLS-1$ //$NON-NLS-2$
      }
      useCaseID = useCase.getId();
    }

    RenderReportRequest renderRequest = new RenderReportRequest();
    renderRequest.setReportRegistryItemID(registryItemID);
    renderRequest.setParameters(params);
    Locale requestLocale =
        getRenderRequestLocale(registryItemID, params, new SubProgressMonitor(monitor, 1));
    if (requestLocale != null) {
      renderRequest.setLocale(requestLocale);
    } else {
      renderRequest.setLocale(Locale.getDefault());
    }
    PrintReportLayoutUtil.printReportLayoutWithDefaultFormat(
        renderRequest, useCaseID, new SubProgressMonitor(monitor, 5));
    monitor.done();
  }