Friday, March 6, 2009

developerWorks Article: Business Intelligence

The developerWorks article on business intelligence I mentioned a while back has been published. It covers several use cases for integrating BIRT reports into a Zero PHP application. It uses the sample database and reports provided by BIRT and runs against a MySQL database.

From my perspective the most interesting example is the last one: scripted data sources. In this use case the report doesn't get its data from a database query. Instead the PHP script creates the data and pushes it into the BIRT runtime ready for the report to render. Here is the sample code:

<?php
require_once 'startup.php';
$engine = startup();
$root_directory = zget("/config/root");
$report_engine = $root_directory."/ReportEngine";
$design_file = $root_directory.
    "/samples/data_source.rptdesign";
$images_directory = $root_directory.
    "/public/images/logos";

java_import(
    "org.eclipse.birt.report.engine.api.HTMLRenderOption");
java_import(
    "org.eclipse.birt.report.engine.api.HTMLServerImageHandler");

$design = $engine->openReportDesign($design_file);
$task = $engine->createRunAndRenderTask($design);
$task->validateParameters();
$generated_report_file = 
    tempnam(sys_get_temp_dir(), 'report');

$models[] = array("ANG Resellers", 
"1952 Alpine Renault 1300", "Red");
$models[] = array("AV Stores, Co.", 
"1969 Harley Davidson Ultimate Chopper", "Blue");
$models[] = array("Alpha Cognac", 
"1969 Ford Falcon", "Blue");
$models[] = array("Asian Shopping Network, Co", 
"1969 Dodge Charger", "Plum Crazy Purple");
$models[] = array("Asian Treasures, Inc.", 
"1969 Corvair Monza", "Red");

zput("/request/report", $models);

$options = new HTMLRenderOption();
$options->setOutputFileName(
    $generated_report_file);
$options->setOutputFormat("html");
$options->setImageDirectory($images_directory);
$options->setBaseImageURL("/images/logos/");
$options->setImageHandler(new HTMLServerImageHandler());

$task->setRenderOption($options);
$task->run();
$task->close();

echo file_get_contents($generated_report_file);
unlink($generated_report_file);
?>

The bold section hard codes some data for the report. A more realistic example might query an external system like a web service to build the data set. 

The magic that makes this possible is the JavaScript engine that BIRT embeds. When BIRT renders the report it executes JavaScript embedded in the report with the Rhino scripting engine. Because the BIRT runtime and the PHP application are all running on the same JVM they can share data very easily and efficiently. In this case the PHP code puts the data collection into the Global Context, and the JavaScript in the report pulls it out.


1 comment:

  1. I chanced upon to view your blog and found it very interesting. Great ... Keep it up!

    ReplyDelete