Entity view (Content)

MuleSoft + BIRT Integration

By charles
Jan. 20, 2016

I had a look online for 'how to integrate one of the best reporting framework BIRT Framework (http://eclipse.org/birt/) with one of the best ESBs' (i.e. MuleSoft - https://www.mulesoft.com/resources/esb/what-mule-esb) but couldn't find much. So, after a few trial and errors, I could do some integration on my local, and I thought it's worth sharing it, so I'm writing this blog. This is my first ever tech blog, so if you have any questions then please do get in touch with me!

The advantages of BIRT reports are:

  1. The BIRT documentation is pretty extensive and well maintained.
  2. Because of its library features and easy to maintain .rptdesign files one can create complex and rich reports.
  3. Supports most of the file formats including PDF, HTML, SVG etc.

These advantages, if combined with MuleSoft, can give rich and complex reports for any type of business needs. For example, if one needs to create a complex PDF report from the DB data using MuleSoft, this is a good fit for it. In the following example we will be integrating reporting engine API with MuleSoft project and depending upon the inbound parameters we will be fetching the data for report and creating a PDF out of it.

I've used a common Java component in this case, but in future there is definitely a scope for adding/creating a BIRT connector from MuleSoft.

To run this example we would need the following items:

  1. MySQL (to fetch data from the report, one can use scripted datasource as well but I prefer to use mysql for BIRT reports.)
  2. BIRT Reporting Engine runtime (download from http://download.eclipse.org/birt/downloads/#runtime)
  3. Anypoint Studio for MuleSoft Development (download from https://www.mulesoft.com/studio)

First, let's create a database and a table for the report:

MySQL Setup:
mysql> create database mulesbirtint;

mysql> use mulesbirtint;

 

mysql> CREATE TABLE tech_companies (

  id int(10) unsigned NOT NULL AUTO_INCREMENT,

  name varchar(255) NOT NULL,

  ceo varchar(255) NOT NULL,

  hq varchar(255) NOT NULL,

  state varchar(20) DEFAULT NULL,

  PRIMARY KEY (id)

);

 

mysql> insert into tech_companies values 

(null,'Appnovation','Arnold Leung', 'Vancouver' , 'BC'),

(null,'Salesforce','Marc Benioff', 'San Francisco' , 'CA'),

(null,'Google' , 'Larry Page' , 'Mountain View' , 'CA'),

(null,'Facebook' , 'Mark Zukerberg' , 'Palo Alto', 'CA'),

(null,'Yahoo' , 'Marissa Mayer' , 'Sunnyvale' , 'CA');

 

Reporting Setup:
  • Create a Datasource

 

 

 

 

 

 
    • Create table

 

MuleSoft Setup

Create flows and components that show in the below image. We will be creating an incound http endpoint, and in its header we will pass value for the state. We will also have a Java component which will take the header (state) value from the eventContext and create a final PDF report output in the output folder of the project.

In the above picture, we can see how the project has been setup in Anypoint Studio. The flow starts with http inbound endpoint, then there is a logger to show the inbound parameters and then the Java component. The tech_companies.rptdesign file is kept in the reports folder.

Update the pom.xml to add BIRT dependencies in it:

org.eclipse.birt.runtime

org.eclipse.birt.runtime

4.3.0



org.eclipse.birt.runtime

org.apache.poi



org.apache.poi

poi

3.9



sonatype-nexus-releases

Sonatype Nexus Releases

https://oss.sonatype.org/content/repositories/releases/

In the logger component add the following message, it will print the state value passed from http request:

Request received .. generate report for state #[message.inboundproperties."http.request.params".state]

The Java component is calling the GenerateReport.java class which contains below code:

package com.appnovation.demos;


import org.mule.api.MuleEventContext;

import org.mule.api.lifecycle.Callable;

import java.util.logging.Level;


import org.eclipse.birt.core.framework.Platform;

import org.eclipse.birt.report.engine.api.EngineConfig;

import org.eclipse.birt.report.engine.api.EngineException;

import org.eclipse.birt.report.engine.api.IReportEngine;

import org.eclipse.birt.report.engine.api.IReportEngineFactory;

import org.eclipse.birt.report.engine.api.IReportRunnable;

import org.eclipse.birt.report.engine.api.IRunAndRenderTask;

import org.eclipse.birt.report.engine.api.PDFRenderOption;


public class GenerateReport implements Callable {



@Override

public Object onCall(MuleEventContext eventContext) throws Exception {



String param = eventContext.getMessage().getInboundProperty("state");

generateReportAsPerParam(param);

return "Report generation completed";

}


private void generateReportAsPerParam(String param) throws EngineException {

 

IReportEngine engine = null;

EngineConfig config = null;


try {

config = new EngineConfig();

config.setBIRTHome("/Users/username/Downloads/birt-runtime-4_4_2/ReportEngine");

config.setLogConfig("Users/username/test", Level.FINEST);

Platform.startup(config);

final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform

.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);

engine = FACTORY.createReportEngine(config);


// Open the report design

IReportRunnable design = null;

design = engine

.openReportDesign("reports/tech_companies.rptdesign");


IRunAndRenderTask task = engine.createRunAndRenderTask(design);

task.setParameterValue("param_1", param.toUpperCase());



// final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();

// HTML_OPTIONS.setOutputFileName("output/final_report.html");

// HTML_OPTIONS.setOutputFormat("html");

// HTML_OPTIONS.setHtmlRtLFlag(false);

// HTML_OPTIONS.setEmbeddable(false);

// HTML_OPTIONS.setImageDirectory("C:\\test\\images");


PDFRenderOption PDF_OPTIONS = new PDFRenderOption();

PDF_OPTIONS.setOutputFileName("output/final_report.pdf");

PDF_OPTIONS.setOutputFormat("pdf");


task.setRenderOption(PDF_OPTIONS);

task.run();

task.close();

engine.destroy();

} catch (final Exception EX) {

EX.printStackTrace();

} finally {

Platform.shutdown();

}

}

}

Alright, now that the Muleproject is setup, the only thing remaining is to run it. Right click the project in Anypoint Studio > Run As > Mule Application... once the application is deployed, go to any browser and invoke the http endpoint:

http://localhost:8081/?state=BC

Check the output folder in the project structure. You can see the output report created final_report.pdf. Let me know if you guys face any issues or want to provide any feedback. Cheers!

Post Tags: