Running Reports

Running from Reports Browser

The easiest way to run reports is from the generic browser, which is available in the ReportsRun Reports screen. The user must have the right to access this screen. The list will contain all reports that are available to the user in accordance with their role. If the report has external parameters, they will be requested in a special form when running the report.

Running from Screens

You can run reports from arbitrary screens using special actions and associated buttons or component context menu items. In this case, the availability of the report directly in this screen is checked in addition to a user role.

Action types and examples of their use are provided below.

  • io.jmix.reportsui.action.list.RunReportAction – a standard action that displays the list of all available reports. It should be defined for a Button or a list component (Table, DataGrid, etc.).

    Below is an example of using the declarative action for the GroupTable:

    <actions>
        <action id="run" type="runReport"/> (1)
    </actions>
        <button id="runBtn" action="booksTable.run"/> (2)
    </buttonsPanel>
    1 The type attribute defines a specific runReport action type, provided by the framework.
    2 Add a button with run report action.

    Example of programmatically creating the action together with a button declared in the screen XML descriptor:

    @Autowired
    private Button runReportBtn;
    
    @Autowired
    private Actions actions;
    
    @Subscribe
    public void onInit(InitEvent event) {
        RunReportAction action = actions.create(RunReportAction.class, "runReport");
    
        runReportBtn.setAction(action);
    }

    When the action is performed, a modal Report Run dialog will open where reports related to the current screen will be displayed. When a user selects a report from the list, the parameters input form is displayed (if any were defined) and the report is run.

  • io.jmix.reportsui.action.list.ListPrintFormAction – a standard action for printing reports for entity instances associated with a list component (Table, DataGrid, etc.).

    The action only selects reports having an external parameter of the Entity or the List of entities type and where the parameter entity type matches the entity type displayed by the list component. If only one report is available as a result of selection, it is invoked immediately. If several reports are available, their list is offered to the user for selection.

    The external parameter value is passed to the report using the following rules:

    • If the parameter has the List of entities type, the list of instances currently selected in the list component is passed into it.

    • If the parameter has the Entity type, and the list component has a single instance selected (one row is highlighted), then this instance is passed into the report.

    • If the parameter is of the Entity type, and the list component has several rows selected, then the report runs several times according to the number of selected instances. After execution, the user gets a single ZIP archive containing all generated reports.

      Below is an example of using the declarative action for the GroupTable:

      <actions>
          <action id="list" type="listPrintForm"/> (1)
      </actions>
      <buttonsPanel id="buttonsPanel"
                    alwaysVisible="true">
          <button id="listBtn" action="authorsTable.list"/> (2)
      </buttonsPanel>
      1 The type attribute defines a specific listPrintForm action type, provided by the framework.
      2 Add a button with run report action.

      Example of programmatically creating the action together with a button declared in the screen XML descriptor:

      @Autowired
      private Button listPrintFormBtn;
      
      @Autowired
      private Actions actions;
      
      @Subscribe
      public void onInit(InitEvent event) {
          ListPrintFormAction action = actions.create(ListPrintFormAction.class, "listPrintForm");
          listPrintFormBtn.setAction(action);
      }

      When the action is performed, if no entities were selected from the list component, a confirmation window will be displayed.

      run actions listPrint confirmation
      Figure 1. A confirmation window

      After that, the modal Run reports dialog will open where reports related to the current screen will be displayed. From this modal screen, the user can run some report for the selected entity.

  • io.jmix.reportsui.action.list.EditorPrintFormAction – an action associated with an entity editor screen. The action only selects reports having an external parameter of the Entity or the List of entities type and where the parameter entity type matches the edited entity type. If only one report is available as a result of selection, it is invoked immediately. If several reports are available, their list is offered to user for selection.

    The external parameter value – edited entity instance – is passed into the report. If the parameter has the List of entities type, then a list containing a single item is passed.

    Below is an example of using the action in a button located near the standard OK and Cancel buttons:

    • XML descriptor

      <hbox id="editActions" spacing="true">
          <button id="reportButton" icon="PRINT"/>
      </hbox>
    • Controller

      @Inject
      private Button reportButton;
      
      @Autowired
      private Actions actions;
      
      @Subscribe
      public void onInit(InitEvent event) {
          EditorPrintFormAction action = actions.create(EditorPrintFormAction.class);
          action.setEditor(this);
          action.setReportOutputName(null);
          reportButton.setAction(action);
      }

Reports API

ReportRunner

ReportRunner is an interface used for running reports. All its methods return the ReportOutputDocument object that contains the result of report execution.

Below are a few examples using ReportRunner.

  1. Running a report based on the information specified in the ReportRunContext object:

    @Autowired
    protected ReportRunner reportRunner;
    
    @Subscribe("rrcBtn2")
    protected void onRrcBtn2Click(Button.ClickEvent event) {
        ReportOutputDocument document = reportRunner
                .run(new ReportRunContext(report).setParams(paramsMap)); (1)
    }
    1 The ReportRunContext contains the report entity and parameters.
  2. Running a report by its code and additional information specified using the fluent interface:

    @Autowired
    protected ReportRunner reportRunner;
    
    @Subscribe("rrBtn1")
    protected void onRrBtn1Click(Button.ClickEvent event) {
        ReportOutputDocument document = reportRunner.byReportCode("BOOKS") (1)
                .addParam("type", type) (2)
                .withTemplateCode("books-template") (3)
                .run(); (4)
    }
    1 Entry point to the fluent interface for a report with a specified code.
    2 Adds an input parameter to the parameter map.
    3 Sets a code of template that will be used to run a report.
    4 Builds a ReportRunContext instance and runs a report using this run context.
  3. Running a report by the report entity and additional information specified using the fluent interface:

    @Autowired
    protected ReportRunner reportRunner;
    
    @Subscribe("rrBtn2")
    protected void onRrBtn2Click(Button.ClickEvent event) {
        ReportOutputDocument document = reportRunner.byReportEntity(report)
                .addParam("type", type)
                .withOutputType(ReportOutputType.PDF) (1)
                .withOutputNamePattern("Books") (2)
                .run();
    }
    1 Sets a type of output document.
    2 Sets a name pattern of an output document.

You can get the report contents and file name directly from ReportOutputDocument:

String documentName = document.getDocumentName();

byte[] content = document.getContent();

UIReportRunner

UiReportRunner is an interface for executing reports from the application screens. In addition to the options required to run a report, UiReportRunner allows you to configure the following features:

  • Displaying the result of the report execution in the browser (in the case of chart/pivot table/table templates).

    uiReportRunner.runAndShow(new UiReportRunContext(report));
  • Whether to show a dialog to input the report parameters before run. Use ParametersDialogShowMode for this purpose. Three modes are supported:

    • YES - to show the dialog to input the report parameters.

    • NO - do not show the dialog to input the report parameters.

    • IF_REQUIRED - to show the dialog to input the report parameters if:

      • The report has input parameters;

      • The report has several templates;

      • The report has one template with an alterable output type.

  • Execute report generation synchronously or in the background:

    uiReportRunner.byReportEntity(report)
            .withParametersDialogShowMode(ParametersDialogShowMode.IF_REQUIRED)
            .inBackground(RunReportScreen.this)
            .runAndShow();
  • Run a report several times for the specified parameter alias and values:

    uiReportRunner.byReportEntity(report)
            .withOutputType(ReportOutputType.PDF)
            .withTemplateCode("publication-template")
            .withParametersDialogShowMode(ParametersDialogShowMode.NO)
            .runMultipleReports("entity", publicationList);

    The runMultipleReports() method runs a report for each object from the specified collection. Objects in the collection should have the same type as an input parameter with specified alias.

ReportRunContext

The ReportRunContext class stores the following information required for report running:

  • The Report entity;

  • The ReportTemplate entity: if not specified, the default template is used;

  • Input parameters;

  • Type of output document;

  • Output name pattern.

Let’s look at examples of creating a ReportRunContext:

ReportRunContext context = new ReportRunContext(report)
        .addParam("type", type)
        .setOutputNamePattern("Books");

ReportRunContext context = new ReportRunContext(report)
        .setReportTemplate(template)
        .setOutputType(ReportOutputType.PDF)
        .setParams(paramsMap);

ReportZipUtils

The ReportZipUtils interface helps package a list of ReportOutputDocument objects into one ZIP archive.

byte[] zipArchiveContent = reportZipUtils.createZipArchive(documents);
downloader.download(zipArchiveContent, "Reports.zip", DownloadFormat.ZIP);