VI. Annexe▲
VI-A. BIRT▲
VI-A-1. Fichier properties▲
birt.viewer.2.3.0.working.folder=${jboss.server.home.dir}/data/birt/report
birt.viewer.2.3.0.document.folder=${jboss.server.home.dir}/data/birt/resource
birt.viewer.2.3.0.image.dir=${jboss.server.home.dir}/data/birt/report/images
birt.viewer.2.3.0.log.dir=${jboss.server.home.dir}/log/birt
birt.viewer.2.3.0.scriptlib.dir=${jboss.server.home.dir}/data/birt/scriplib
birt.viewer.2.3.0.resource.path=${jboss.server.home.dir}/data/birt/resource
VI-A-2. Implémentation de l'API de BIRT▲
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>
reporting</display-name>
...
<!-- BIRT SERVLET -->
<servlet>
<servlet-name>
birt</servlet-name>
<servlet-class>
corp.capgemini.osspartner.birt.WebReport</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>
birt</servlet-name>
<url-pattern>
/birt</url-pattern>
</servlet-mapping>
...
</web-app>
import
java.io.IOException;
import
java.io.PrintWriter;
import
java.util.Iterator;
import
java.util.logging.Logger;
import
javax.servlet.ServletContext;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.eclipse.birt.report.engine.api.HTMLRenderOption;
import
org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import
org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
import
org.eclipse.birt.report.engine.api.IParameterDefnBase;
import
org.eclipse.birt.report.engine.api.IReportEngine;
import
org.eclipse.birt.report.engine.api.IReportRunnable;
import
org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import
org.eclipse.birt.report.engine.api.IScalarParameterDefn;
import
org.eclipse.birt.report.engine.api.PDFRenderOption;
public
class
WebReport extends
HttpServlet {
/**
*
*/
private
static
final
long
serialVersionUID =
1
L;
/**
* Constructor of the object.
*/
private
IReportEngine birtReportEngine =
null
;
protected
static
Logger logger =
Logger.getLogger
(
"org.eclipse.birt"
);
public
WebReport
(
) {
super
(
);
}
public
void
init
(
) throws
ServletException {
BirtEngine.initBirtConfig
(
);
}
public
void
destroy
(
) {
super
.destroy
(
);
// BirtEngine.destroyBirtEngine();
}
/**
* The doGet method of the servlet.
*
*
*/
public
void
doGet
(
HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
/**
* TRAITEMENT PARAMETRE : __format __report __embedded
*/
String __format =
req.getParameter
(
"__format"
);
String __report =
req.getParameter
(
"__report"
);
String __embedded =
req.getParameter
(
"__embedded"
);
// format
if
(
__format !=
null
&&
__format.equalsIgnoreCase
(
"pdf"
)) {
__format =
"pdf"
;
}
else
{
__format =
"html"
;
}
// embedded
if
(
__embedded !=
null
&&
__embedded.equalsIgnoreCase
(
"false"
)) {
__embedded =
"false"
;
}
else
{
__embedded =
"true"
;
}
/**
*
*/
ServletContext sc =
req.getSession
(
).getServletContext
(
);
this
.birtReportEngine =
BirtEngine.getBirtEngine
(
sc);
IReportRunnable design;
try
{
/**
* ouverture rptdesign
*/
design =
birtReportEngine.openReportDesign
(
sc
.getRealPath
(
"/report"
)
+
"/"
+
__report +
".rptdesign"
);
if
(
design !=
null
) {
// create task to run and render report
/**
* creation d'une tache run&render
*/
IRunAndRenderTask task =
birtReportEngine
.createRunAndRenderTask
(
design);
if
(
task !=
null
) {
/**
* Parameters
*/
IGetParameterDefinitionTask params =
birtReportEngine
.createGetParameterDefinitionTask
(
design);
Object tmpParam;
if
(
params !=
null
) {
Iterator iter =
params.getParameterDefns
(
true
)
.iterator
(
);
while
(
iter.hasNext
(
)) {
IParameterDefnBase parameterDefnBase =
(
IParameterDefnBase) iter
.next
(
);
switch
(
parameterDefnBase.getParameterType
(
)) {
case
IParameterDefnBase.SCALAR_PARAMETER:
IScalarParameterDefn scalarParsmeterDefn =
(
IScalarParameterDefn) parameterDefnBase;
switch
(
scalarParsmeterDefn.getDataType
(
)) {
// INTEGER
case
IScalarParameterDefn.TYPE_INTEGER:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
// search value
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
tmpParam =
Integer
.parseInt
((
String) tmpParam);
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
// set default value
else
{
tmpParam =
Integer.parseInt
(
params
.getParameterValue
(
scalarParsmeterDefn
.getName
(
))
.toString
(
));
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
break
;
// FLOAT
case
IScalarParameterDefn.TYPE_FLOAT:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
tmpParam =
Float
.parseFloat
((
String) tmpParam);
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
else
{
tmpParam =
Float.parseFloat
(
params
.getParameterValue
(
scalarParsmeterDefn
.getName
(
))
.toString
(
));
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
// TODO
break
;
// BOOLEAN
case
IScalarParameterDefn.TYPE_BOOLEAN:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
tmpParam =
Boolean
.parseBoolean
((
String) tmpParam);
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
else
{
tmpParam =
Boolean.parseBoolean
(
params
.getParameterValue
(
scalarParsmeterDefn
.getName
(
))
.toString
(
));
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
// TODO
break
;
// DATE
case
IScalarParameterDefn.TYPE_DATE:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
tmpParam =
Boolean
.parseBoolean
((
String) tmpParam);
}
else
{
}
// TODO
break
;
// DATE TIME
case
IScalarParameterDefn.TYPE_DATE_TIME:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
}
else
{
}
// TODO
break
;
// TIME
case
IScalarParameterDefn.TYPE_TIME:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
}
else
{
}
// TODO
break
;
// STRING
case
IScalarParameterDefn.TYPE_STRING:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
task.setParameterValue
(
parameterDefnBase.getName
(
),
tmpParam);
}
else
{
}
// TODO
break
;
// ANY
case
IScalarParameterDefn.TYPE_ANY:
tmpParam =
req
.getParameter
(
scalarParsmeterDefn
.getName
(
));
if
(
tmpParam !=
null
&&
!
tmpParam.equals
(
""
)) {
}
else
{
}
// TODO
break
;
// STRING
default
:
// TODO
break
;
}
break
;
/**
* IParameterGroupDefn
*/
case
IParameterDefnBase.PARAMETER_GROUP:
break
;
/**
* ICascadingParameterGroup
*/
case
IParameterDefnBase.CASCADING_PARAMETER_GROUP:
break
;
/**
*
*/
case
IParameterDefnBase.LIST_PARAMETER:
break
;
/**
*
*/
case
IParameterDefnBase.TABLE_PARAMETER:
break
;
/**
*
*/
default
:
break
;
}
}
}
/**
* PDF
*/
if
(
__format.equalsIgnoreCase
(
"pdf"
)) {
PDFRenderOption optionPDF =
new
PDFRenderOption
(
);
optionPDF
.setOutputFormat
(
PDFRenderOption.OUTPUT_FORMAT_PDF);
optionPDF.setOutputStream
(
resp.getOutputStream
(
));
task.setRenderOption
(
optionPDF);
}
/**
* HTML
*/
else
{
HTMLRenderOption optionHTML =
new
HTMLRenderOption
(
);
// set the image handler to a
// HTMLServerImageHandler if you plan on using
// the base image url.
optionHTML
.setImageHandler
(
new
HTMLServerImageHandler
(
));
optionHTML
.setOutputFormat
(
HTMLRenderOption.OUTPUT_FORMAT_HTML);
// options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
optionHTML.setOutputStream
(
resp.getOutputStream
(
));
optionHTML.setBaseImageURL
(
req.getRequestURL
(
)
+
"/images"
);
optionHTML.setImageDirectory
(
sc
.getRealPath
(
"/birt/images"
));
//
optionHTML.setEmbeddable
(
new
Boolean
(
__embedded));
task.setRenderOption
(
optionHTML);
}
task.run
(
);
task.close
(
);
}
else
{
/**
* TODO
*/
}
}
else
{
/**
* TODO
*/
}
if
(
__format.equalsIgnoreCase
(
"pdf"
)) {
resp.setContentType
(
"application/pdf"
);
resp.setHeader
(
"Content-Disposition"
, "inline; filename="
+
__report +
".pdf"
);
}
/**
* HTML
*/
else
{
resp.setContentType
(
"text/html"
);
}
// get report name and launch the engine
}
catch
(
Exception e) {
e.printStackTrace
(
);
throw
new
ServletException
(
e);
}
}
/**
* The doPost method of the servlet.
*
*
*/
public
void
doPost
(
HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType
(
"text/html"
);
PrintWriter out =
response.getWriter
(
);
out
.println
(
"<!DOCTYPE HTML PUBLIC
\"
-//W3C//DTD HTML 4.01 Transitional//EN
\"
>"
);
out.println
(
"<HTML>"
);
out.println
(
" <HEAD><TITLE>A Servlet</TITLE></HEAD>"
);
out.println
(
" <BODY>"
);
out.println
(
" Post Not Supported"
);
out.println
(
" </BODY>"
);
out.println
(
"</HTML>"
);
out.flush
(
);
out.close
(
);
}
}
VI-B. Jboss▲
VI-B-1. Comment rendre accessible votre serveur Jboss au réseau▲
Pour rendre accessible le serveur Jboss sur le réseau c'est-à-dire une adresse autre que localhost, il suffit d'ajouter au lancement du serveur l'option -b 0.0.0.0.
VI-C. Schéma de l'architecture mise en place▲
VI-D. Références▲
Quelques références utiles :
- Integrating and Extending BIRT Second Edition ;
- BIRT A Field Guide to Reporting Second Edition ;
- Exemple de servlet;
- Jquery.
VI-E. Remerciements▲
Tout d'abord, je voulais remercier Adrien Artero qui m'a poussé à rejoindre l'équipe BI des forums developpez.com et m'a conduit à écrire ce tutoriel. Ensuite, il y a mon Manager Jean Guillaume Lalanne qui a fait en sorte que ce tutoriel à la base confidentiel puisse être diffusé à la communauté open source. Bien entendu, il y a toute l'équipe BI qui apporte des suggestions afin d'améliorer ce tutoriel pour que celui-ci soit plus facile d'accès aux utilisateurs.