Tuesday, March 3, 2009

Business Intelligence - The Code

A little later than intended - here is the code we were using on the Zero stand at the (most excellent) PHP London conference last Friday. I had it running as a blog in Drupal but this will run just as well standalone. More detailed destructions for getting this running are up on YouTube here (the video is six minutes long).

<?php
require_once 'startup.php';
$engine = startup();

$root_directory = zget("/config/root");
$design_file = $root_directory."/samples/hello_world.rptdesign";
$design = $engine->openReportDesign($design_file);
$task = $engine->createRunAndRenderTask($design);
$task->validateParameters();
java_import("org.eclipse.birt.report.engine.api.HTMLRenderOption");
$generated_report_file = tempnam(sys_get_temp_dir(), 'report');

$options = new HTMLRenderOption();
$options->setOutputFileName($generated_report_file);
$options->setOutputFormat("html");
$task->setRenderOption($options);
$task->run();
$task->close();

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

And the include file that configures the BIRT runtime is as follows:

<?php
function startup() {
  zpost("/app#lock", "/engine"); 
  $engine = zget("/tmp/factory");
  if ($engine != NULL) {
zpost("/app#unlock", "/engine"); 
return $engine; // Did someone else get in first?
  }

  $root_directory = zget("/config/root");
$report_directory = $root_directory."/birt-runtime-2_3_1/ReportEngine";
  $log_directory = zget("/config/appLogDir");

java_import("org.eclipse.birt.core.framework.Platform");
  java_import("java.util.logging.Level");
  java_import("org.eclipse.birt.report.engine.api.EngineConfig");
  java_import("org.eclipse.birt.report.engine.api.IReportEngineFactory");

  $config = new EngineConfig();
  $config->setBIRTHome($report_directory);
  $config->setLogConfig($log_directory, Level::$SEVERE);
  Platform::startup($config);

$factory = Platform::createFactoryObject(
  IReportEngineFactory::$EXTENSION_REPORT_ENGINE_FACTORY);

  // Store in process wide non persistent zone
$engine = $factory->createReportEngine($config);
  zput("/tmp/factory", $engine);

  // Unlock when we are finished!
  zpost("/app#unlock", "/engine"); 

return $engine;
}
?>

No comments:

Post a Comment