Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Design and Runtime Birt
Nat.B
Hello,
Is it possible to change the dataSource of a design report and then run and render a task to generate a PDF file ?
Thanks you,
Cordially
Find more posts tagged with
Comments
mwilliams
There are expression builder for changing connection information. Can you explain more about what your application? How do you know what source you'll use? What type of dataSource are you using?
Nat.B
Hello,
Thank you for your post.
The Java program is designed to develop modify a report JDBC data source, known, and generate the associated PDF.
The report used in the application has a source of production data. All data sources have the same architecture. The data source used in the Java program is initialized by a previous connection to the implementation of this program. It is not known in advance.
Ultimately, this program will be integrated into a web application and data sources used are kind of Web Services.
You will find below the source code used. Can you give me an opinion on my two consecutive utilization of "Platform.startup(design)" please ?
Nat.B
SessionHandle session = null;
ReportDesignHandle reportDesignHandle = null;
ElementFactory elementFactory = null;
IDesignEngine engine = null;
// Get the current directory path
String repertoireCourant = System.getProperty("user.dir");
//Assign the path to the BIRT platform
String BIRT_HOME = repertoireCourant + "/runtime/ReportEngine";
//Assign the name of the report used
String name = repertoireCourant+"/reports/Planning2.rptdesign";
DesignConfig config = new DesignConfig( );
config.setBIRTHome(BIRT_HOME);
try{
Platform.startup( config );
IDesignEngineFactory factory =(IDesignEngineFactory)
Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
} catch( Exception ex){
ex.printStackTrace();
}
session = engine.newSessionHandle(null);
try{
reportDesignHandle = session.openDesign(name);
}catch(DesignFileException eD){
eD.printStackTrace();
}
/*
* Creating a data source element
*/
try{
elementFactory = reportDesignHandle.getElementFactory();
} catch (Exception ex) {
ex.printStackTrace();
}
String nomDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String urlBase = "jdbc:sqlserver://localhost:1433;databaseName=E";
String loginBase = "User1";
String passBase = "test";
String nomSourceDeDonnees = "Data Source";
OdaDataSourceHandle dsHandle = factory.newOdaDataSource(nomSourceDeDonnees, "org.eclipse.birt.report.data.oda.jdbc");
try{
dsHandle.setProperty("odaDriverClass", nomDriver);
dsHandle.setProperty("odaURL", urlBase);
dsHandle.setProperty("odaUser", loginBase);
dsHandle.setProperty("odaPassword", passBase);
OdaDataSourceHandle DataSource = (OdaDataSourceHandle) reportDesignHandle.findDataSource("Data Source");
DataSource.setProperty("odaDriverClass", nomDriver);
DataSource.setProperty("odaURL", urlBase);
DataSource.setProperty("odaUser", loginBase);
DataSource.setProperty("odaPassword", passBase);
reportDesignHandle.getDataSources().add(dsHandle);
}catch (SemanticException ex) {
ex.printStackTrace();
}
/*
* Saving and closing the report
*/
try{
reportDesignHandle.save();
reportDesignHandle.close();
} catch (IOException ex) {
ex.printStackTrace();
}
/*
* PDF generation
*/
IReportEngine engine2 = null;
EngineConfig config2 = new EngineConfig();
config2.setBIRTHome(BIRT_HOME);
try{
// New startup platform fot the PDF generation
Platform.startup( config2 );
IReportEngineFactory factory2 = (IReportEngineFactory)Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine2 = factory2.createReportEngine( config2 );
} catch ( Exception ex){
ex.printStackTrace();
}
engine2.changeLogLevel( Level.FINE);
IReportRunnable design = null;
IRunAndRenderTask task = null;
RenderOption options = null;
// Opening of the report
try
{
design = engine2.openReportDesign(name);
}catch (Exception e)
{
System.err.println("An error occured during the opening of the report file!");
e.printStackTrace();
System.exit(-1);
}
task = engine2.createRunAndRenderTask(design);
options = new RenderOption();
options.setOutputFileName("output/resample/Planning1.pdf");
options.setOutputFormat("pdf");
task.setRenderOption(options);
try
{
task.run();
}
catch (Exception e)
{
System.err.println("An error occured while running the report!");
e.printStackTrace();
System.exit(-1);
}
//Destruction and Platform shutdown
System.out.println("Closing program.");
engine2.destroy();
Platform.shutdown();
//Bugzilla 351052
RegistryProviderFactory.releaseDefault();
}
}
Cordially,
mwilliams
Why are you using two separate platforms and engines? You should be able to use the same ones. Also, after doing this, do you have duplicate dataSources within your report? Let me know.
Nat.B
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="117446" data-time="1370646982" data-date="07 June 2013 - 04:16 PM"><p>
Why are you using two separate platforms and engines? You should be able to use the same ones. Also, after doing this, do you have duplicate dataSources within your report? Let me know.<br /></p></blockquote>
<br />
<br />
Ok. Thanks you for your post.<br />
I managed to get something using IReportEngine. I have added ?Property Binding? on my DataSource based on ?Report parameters? that I created. So, in the main procedure, I initialized them and then I run and render a task to generate pdf file. Here below the execute code source:<br />
<br />
// Get the current directory path<br />
String repertoireCourant = System.getProperty("user.dir");<br />
<br />
//Assign the path to the BIRT platform<br />
String BIRT_HOME = repertoireCourant + "/runtime/ReportEngine";<br />
<br />
//Assign the name of the report used<br />
String PathReport = repertoireCourant+"/reports/" + NomRapport +".rptdesign";<br />
<br />
IReportEngine engine2 = null;<br />
EngineConfig config2 = new EngineConfig();<br />
config2.setBIRTHome(BIRT_HOME);<br />
config2.setLogConfig(repertoireCourant + "/temp", Level.WARNING);<br />
<br />
try{<br />
Platform.startup( config2 );<br />
IReportEngineFactory factory2 = (IReportEngineFactory)Platform.createFactoryObject (IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); <br />
engine2 = factory2.createReportEngine( config2 );<br />
} catch ( Exception ex){<br />
ex.printStackTrace();<br />
}<br />
<br />
engine2.changeLogLevel( Level.WARNING);<br />
<br />
IReportRunnable design = null;<br />
IRunAndRenderTask task = null;<br />
RenderOption options = null;<br />
<br />
// Opening of the report<br />
try<br />
{<br />
design = engine2.openReportDesign(PathReport); <br />
}<br />
catch (Exception e)<br />
{<br />
System.err.println("Une erreur s'est produite lors de l'ouverture du fichier rapport");<br />
e.printStackTrace();<br />
System.exit(-1);<br />
}<br />
<br />
<br />
task = engine2.createRunAndRenderTask(design);<br />
<br />
task.setParameterValue("RP_ParamP", ParameterValue);<br />
<br />
String nomDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";<br />
String urlBase = "jdbc:sqlserver://localhost:1433;databaseName=E";<br />
String loginBase = "User1";<br />
String passBase = "test";<br />
<br />
task.setParameterValue("RP_DriverClass", nomDriver);<br />
task.setParameterValue("RP_DataBaseUrl", urlBase);<br />
task.setParameterValue("RP_UserName", loginBase);<br />
task.setParameterValue("RP_Pwd", passBase);<br />
<br />
task.validateParameters();<br />
<br />
/*<br />
* PDF generation<br />
*/<br />
PDFRenderOption optionPDF = new PDFRenderOption();<br />
optionPDF.setOutputFileName("output/resample/" + NomRapport + "." + GenerationType);<br />
optionPDF.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");<br />
optionPDF.setOutputFormat("pdf");<br />
task.setRenderOption(optionPDF);<br />
<br />
try<br />
{<br />
task.run();<br />
}<br />
catch (Exception e)<br />
{<br />
System.err.println("Une erreur s'est produite lors de l'ex?cution du rapport");<br />
e.printStackTrace();<br />
System.exit(-1);<br />
}<br />
<br />
//Destruction and Platform shutdown<br />
engine2.destroy();<br />
<br />
Platform.shutdown();<br />
<br />
//Bugzilla 351052<br />
RegistryProviderFactory.releaseDefault();<br />
<br />
<br />
<br />
After this, I looked around numbers of data sources and I get the same number before the start of the procedure. <br />
<br />
<br />
<br />
Now, I try to generate Excel file ant I meet some problems when I open the file generated with Excel because the extension is not recognized by the software. I use ?ExcelRenderOption? but it doesn?t work well. Have you got some idea ?<br />
<br />
Cordially,
mwilliams
The Excel issue is because of the type of file that BIRT's XLS emitter creates. It's a different type than the newer versions of Excel prefer. BIRT 4.3 should have a new emitter that will stop this warning from happening. You could try the SpudSoft Excel emitter from the BIRT Exchange marketplace if you cannot upgrade.
Nat.B
Thanks you for your quick reply !
I use BIRT 4.4 but it doesn't seem to work well. I try to use SpudSoft Excel emitter so :
i added the jar "uk.co.spudsoft.birt.emitters.excel_0.8.0.201306032053" to the project and run these code :
EXCELRenderOption optionXLS = new EXCELRenderOption();
optionXLS.setOutputFormat("xls");
optionXLS.setOption(IRenderOption.EMITTER_ID, "uk.co.spudsoft.birt.emitters.excel_0.8.0.201306032053");
optionXLS.setOutputFileName("output/resample/" + NomRapport + "." + GenerationType);
task.setRenderOption(optionXLS);
But i optain this error message :
"EmitterID uk.co.spudsoft.birt.emitters.excel_0.8.0.201306032053 for render option is invalid."
I apologize for the inconvenience,
Cordially,
mwilliams
Here's a snippet of code from a java project where I use the emitter successfully:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
RenderOption options = new RenderOption();
options.setOption("ExcelEmitter.SingleSheet", true);
options.setOption("ExcelEmitter.DisplayGridLines", true);
options.setOption("ExcelEmitter.DisplayRowColHeadings", true);
options.setOption("ExcelEmitter.DisplayZeros", true);
options.setOption("ExcelEmitter.STRUCTURED_HEADER", false);
options.setEmitterID("uk.co.spudsoft.birt.emitters.excel.XlsxEmitter");
options.setOutputFileName("test.xlsx");
task.setRenderOption(options);
</pre>
<br />
Hope this helps.
Nat.B
Great !
!!!
The generation of an excel file works well using the library from the SpudSoft Excel emitter "uk.co.spudsoft.birt.emitters.excel_0.8.0.201306032053" and your source code !
Thank you very much !!!
!!!
I wanted to ask you this point : what do you think about the possibility of using the file generated with older version of Excel (Excel 2007 and older) ?
Cordially,
mwilliams
Great! Glad to help!
The XLSX should work in 2007 for sure. You'll probably have to run an XLS output as well for users that don't have at least the 2007 version of Excel. To change the above code to the xls emitter, just change the file name to .xls and the emitter to XlsEmitter.
Nat.B
Ok. Thank you very much for your help !!!
!!!
mwilliams
You're very welcome! Let us know whenever you have questions!
Migrateduser
<p>Hello,</p>
<p> </p>
<p>I am using "uk.co.spudsoft.birt.emitters.excel_4.4.0.v201405191524" for generating Excel report. While generating the report, the shows one warning message, after clicking yes it generates the report. I saw previous posts #9 of code snippet, could you please tell ,where to put this code? I am new to BIRT report. Need this help urgently.</p>
<p> </p>
<p>Thanks!</p>
mwilliams
<p>The code in post #9 is for running your report using the Spudsoft Excel Emitter in Java. This should be the same as running the xlsx output from the designer. What is the issue you're having?</p>