Wednesday, August 19, 2009

BIRT: Restoring a Report Design from a Report Document

I often get asked to help with problems people have with their report designs. Of course, troubleshooting report designs can be a tricky endeavor. People use different data sources, and more often than not I don't have access to this. So, rather than ask people to send me a data extract, their report design, and any relevant icons or images, I typically just ask for the rptDocument, as generated by the report designer under the "Run/Generate Document" menu option.

From this format, it is possible to use as a data source using a special ODA I designed, extract the data using the BIRT Web Viewer to create a CSV in case the ODa gives me issues, and extract the original report design file. So, how do you extract the report design? This was a question asked on the BIRT Exchange, and this was the solutions I provided:

package birt.executor;

import java.io.IOException;

import org.eclipse.birt.core.exception.BirtException;
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.IReportDocument;
import org.eclipse.birt.report.engine.api.ReportEngine;

public class RestoreReportDesign {

/**
* @param args
*/
public static void main(String[] args) {
try {
String birtHome = "C:/Libraries/birt-runtime-2_3_2/ReportEngine";

EngineConfig engineConfig = new EngineConfig();
engineConfig.setBIRTHome(birtHome);

Platform.startup(engineConfig);

ReportEngine re = new ReportEngine(engineConfig);

IReportDocument doc = re.openReportDocument("C:/Contracts/MyReportDesign.rptdocument");

doc.getReportDesign().saveAs("C:/TEMP/extractedReportDesign.rptdesign");

doc.close();
Platform.shutdown();
} catch (EngineException e) {
e.printStackTrace();
} catch (BirtException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}