Class AbstractReportProvider

  • All Implemented Interfaces:
    PluginProvider, ReportProvider, UIExtensionProvider

    public abstract class AbstractReportProvider
    extends java.lang.Object
    implements ReportProvider
    Provides an abstract interface and standard set of methods for creating custom report types within Morpheus. The report engine typically leverages a concept of a defined ReportType and its associated OptionType filters as well as a means to process a report and subsequently render the results in a nice view format.

    A custom report has 2 parts. One is the method for processing/generating the report. This takes an input of a ReportResult for details as to the users configured filters as well as where the result rows should be stored. The process method should send data back as result rows grouped by section. These sections can be header,footer,main or custom. But be aware, only the main section is used when automatically providing csv export functionality.

    Example Process (Groovy Code):

    
     void process(ReportResult reportResult) {
          morpheus.report.updateReportResultStatus(reportResult,ReportResult.Status.generating).blockingGet();
          Long displayOrder = 0
          List<GroovyRowResult> results = []
          Connection dbConnection
    
          try {
              dbConnection = morpheus.report.getReadOnlyDatabaseConnection().blockingGet()
              if(reportResult.configMap?.phrase) {
                  String phraseMatch = "${reportResult.configMap?.phrase}%"
                  results = new Sql(dbConnection).rows("SELECT id,name,status from instance WHERE name LIKE ${phraseMatch} order by name asc;")
              } else {
                  results = new Sql(dbConnection).rows("SELECT id,name,status from instance order by name asc;")
              }
          } finally {
              morpheus.report.releaseDatabaseConnection(dbConnection)
          }
          log.info("Results: ${results}")
          Observable<GroovyRowResult> observable = Observable.fromIterable(results) as Observable<GroovyRowResult>
          observable.map{ resultRow ->
              log.info("Mapping resultRow ${resultRow}")
              Map<String,Object> data = [name: resultRow.name, id: resultRow.id, status: resultRow.status]
              ReportResultRow resultRowRecord = new ReportResultRow(section: ReportResultRow.SECTION_MAIN, displayOrder: displayOrder++, dataMap: data)
              log.info("resultRowRecord: ${resultRowRecord.dump()}")
              return resultRowRecord
          }.buffer(50).doOnComplete {
              morpheus.report.updateReportResultStatus(reportResult,ReportResult.Status.ready).blockingGet();
          }.doOnError { Throwable t ->
              morpheus.report.updateReportResultStatus(reportResult,ReportResult.Status.failed).blockingGet();
          }.subscribe {resultRows ->
              morpheus.report.appendResultRows(reportResult,resultRows).blockingGet()
          }
      }
     

    The second part of the report is the rendering/visual aspect. This is done via the ReportProvider.renderTemplate(ReportResult, Map) method. This contains a reference to the originating report result as well as the rows grouped by section.

    This particular abstract simply initializes a HandlebarsRenderer and provides registration of helper methods for accessing static assets
    Since:
    0.8.0
    See Also:
    ReportProvider
    • Constructor Detail

      • AbstractReportProvider

        public AbstractReportProvider()
    • Method Detail

      • withDbConnection

        public java.lang.Object withDbConnection​(AbstractReportProvider.WithDbConnectionFunction connectionFunction)
        Provides a lambda method for wrapping code that needs a temporary read only database connection. Exposes a Connection for use by things like Groovy SQL. When the script is complete, the connection is automatically released from the context for use by another operation.
        Parameters:
        connectionFunction - the lambda function containing the code needing a db connection.