Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Dynamic Report Servlet"
Line 8: | Line 8: | ||
== Source == | == Source == | ||
+ | ===Dynamic.html=== | ||
+ | Place this file in the webapps/DynamicReport directory. | ||
+ | <html> | ||
+ | <head> | ||
+ | </head> | ||
+ | <body> | ||
+ | The Query that is used for this example is Select * From OFFICES | ||
+ | <form id="form1" method="get" action="http://localhost:8080/DynamicReport/run"> | ||
+ | <select name="dyna1" multiple size="8" > | ||
+ | <option value="COUNTRY">COUNTRY</option> | ||
+ | <option value="CITY">CITY</option> | ||
+ | <option value="STATE">STATE</option> | ||
+ | <option value="OFFICECODE">OFFICECODE</option> | ||
+ | <option value="ADDRESSLINE1">ADDRESSLINE1</option> | ||
+ | <option value="ADDRESSLINE2">ADDRESSLINE2</option> | ||
+ | <option value="POSTALCODE">POSTALCODE</option> | ||
+ | <option value="TERRITORY">TERRITORY</option> | ||
+ | </select><br> | ||
+ | <INPUT TYPE="SUBMIT" VALUE="Run Dynamic Report"> | ||
+ | <INPUT TYPE="HIDDEN" NAME="ReportName" Value="DynamicTableExample.rpttemplate"> | ||
+ | </form> | ||
+ | </body> | ||
+ | </html> | ||
===DynamicReport.java=== | ===DynamicReport.java=== | ||
Line 73: | Line 96: | ||
* | * | ||
*/ | */ | ||
− | + | public void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | throws ServletException, IOException { | ||
Line 81: | Line 104: | ||
//resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); | //resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); | ||
String reportName = req.getParameter("ReportName"); | String reportName = req.getParameter("ReportName"); | ||
+ | String[] cols = (String[])req.getParameterMap().get("dyna1"); | ||
ServletContext sc = req.getSession().getServletContext(); | ServletContext sc = req.getSession().getServletContext(); | ||
this.birtReportEngine = BirtEngine.getBirtEngine(sc); | this.birtReportEngine = BirtEngine.getBirtEngine(sc); | ||
Line 87: | Line 111: | ||
HTMLRenderContext renderContext = new HTMLRenderContext(); | HTMLRenderContext renderContext = new HTMLRenderContext(); | ||
renderContext.setBaseImageURL(req.getContextPath()+"/images"); | renderContext.setBaseImageURL(req.getContextPath()+"/images"); | ||
− | + | renderContext.setImageDirectory(sc.getRealPath("/images")); | |
logger.log( Level.FINE, "image directory " + sc.getRealPath("/images")); | logger.log( Level.FINE, "image directory " + sc.getRealPath("/images")); | ||
System.out.println("stdout image directory " + sc.getRealPath("/images")); | System.out.println("stdout image directory " + sc.getRealPath("/images")); | ||
− | + | ||
HashMap contextMap = new HashMap(); | HashMap contextMap = new HashMap(); | ||
− | + | contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext ); | |
IReportRunnable design; | IReportRunnable design; | ||
try | try | ||
{ | { | ||
− | //Open report design | + | //Open report design |
design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName ); | design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName ); | ||
− | + | ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( ); | |
− | buildReport( report ); | + | buildReport( cols, report ); |
− | + | ||
//create task to run and render report | //create task to run and render report | ||
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); | IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); | ||
Line 110: | Line 134: | ||
HTMLRenderOption options = new HTMLRenderOption(); | HTMLRenderOption options = new HTMLRenderOption(); | ||
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); | options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); | ||
− | + | //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF); | |
options.setOutputStream(resp.getOutputStream()); | options.setOutputStream(resp.getOutputStream()); | ||
task.setRenderOption(options); | task.setRenderOption(options); | ||
Line 119: | Line 143: | ||
}catch (Exception e){ | }catch (Exception e){ | ||
− | + | e.printStackTrace(); | |
− | + | throw new ServletException( e ); | |
} | } | ||
} | } | ||
− | public void buildReport(ReportDesignHandle designHandle){ | + | public void buildReport(String[] cols, ReportDesignHandle designHandle){ |
− | + | ||
try{ | try{ | ||
ElementFactory designFactory = designHandle.getElementFactory( ); | ElementFactory designFactory = designHandle.getElementFactory( ); | ||
Line 132: | Line 156: | ||
buildDataSource(designFactory, designHandle); | buildDataSource(designFactory, designHandle); | ||
− | + | ||
− | ArrayList cols = new ArrayList(); | + | //ArrayList cols = new ArrayList(); |
− | cols.add("OFFICECODE"); | + | //cols.add("OFFICECODE"); |
− | cols.add("CITY"); | + | //cols.add("CITY"); |
− | cols.add("COUNTRY"); | + | //cols.add("COUNTRY"); |
buildDataSet(cols, "From Offices", designFactory, designHandle); | buildDataSet(cols, "From Offices", designFactory, designHandle); | ||
− | TableHandle table = designFactory.newTableItem( "table", cols. | + | TableHandle table = designFactory.newTableItem( "table", cols.length ); |
table.setWidth( "100%" ); | table.setWidth( "100%" ); | ||
table.setDataSet( designHandle.findDataSet( "ds" ) ); | table.setDataSet( designHandle.findDataSet( "ds" ) ); | ||
Line 148: | Line 172: | ||
ComputedColumn cs1 = null; | ComputedColumn cs1 = null; | ||
− | for( int i=0; i < cols. | + | for( int i=0; i < cols.length; i++){ |
− | + | cs1 = StructureFactory.createComputedColumn(); | |
− | cs1.setName((String)cols | + | cs1.setName((String)cols[i]); |
− | cs1.setExpression("dataSetRow[\"" + (String)cols | + | cs1.setExpression("dataSetRow[\"" + (String)cols[i] + "\"]"); |
computedSet.addItem(cs1); | computedSet.addItem(cs1); | ||
} | } | ||
Line 160: | Line 184: | ||
− | for( int i=0; i < cols. | + | for( int i=0; i < cols.length; i++){ |
− | LabelHandle label1 = designFactory.newLabel( (String)cols | + | LabelHandle label1 = designFactory.newLabel( (String)cols[i] ); |
− | + | label1.setText((String)cols[i]); | |
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i ); | CellHandle cell = (CellHandle) tableheader.getCells( ).get( i ); | ||
cell.getContent( ).add( label1 ); | cell.getContent( ).add( label1 ); | ||
− | + | } | |
// table detail | // table detail | ||
− | + | RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 ); | |
− | for( int i=0; i < cols. | + | for( int i=0; i < cols.length; i++){ |
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i ); | CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i ); | ||
− | DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols | + | DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols[i] ); |
− | data.setResultSetColumn( (String)cols | + | data.setResultSetColumn( (String)cols[i]); |
cell.getContent( ).add( data ); | cell.getContent( ).add( data ); | ||
} | } | ||
− | + | ||
− | + | designHandle.getBody( ).add( table ); | |
}catch(Exception e){ | }catch(Exception e){ | ||
e.printStackTrace(); | e.printStackTrace(); | ||
} | } | ||
− | + | ||
} | } | ||
Line 191: | Line 215: | ||
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" ); | "Data Source", "org.eclipse.birt.report.data.oda.jdbc" ); | ||
dsHandle.setProperty( "odaDriverClass", | dsHandle.setProperty( "odaDriverClass", | ||
− | + | "org.eclipse.birt.report.data.oda.sampledb.Driver" ); | |
dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" ); | dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" ); | ||
dsHandle.setProperty( "odaUser", "ClassicModels" ); | dsHandle.setProperty( "odaUser", "ClassicModels" ); | ||
− | + | dsHandle.setProperty( "odaPassword", "" ); | |
designHandle.getDataSources( ).add( dsHandle ); | designHandle.getDataSources( ).add( dsHandle ); | ||
Line 200: | Line 224: | ||
} | } | ||
− | void buildDataSet( | + | void buildDataSet(String[] cols, String fromClause, ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException |
{ | { | ||
− | + | ||
OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds", | OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds", | ||
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" ); | "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" ); | ||
dsHandle.setDataSource( "Data Source" ); | dsHandle.setDataSource( "Data Source" ); | ||
− | + | String qry = "Select "; | |
− | for( int i=0; i < cols. | + | for( int i=0; i < cols.length; i++){ |
− | qry += " " + cols | + | qry += " " + cols[i]; |
− | if( i != (cols. | + | if( i != (cols.length -1) ){ |
qry += ","; | qry += ","; | ||
} | } | ||
} | } | ||
− | + | qry += " " + fromClause; | |
dsHandle.setQueryText( qry ); | dsHandle.setQueryText( qry ); | ||
+ | |||
+ | designHandle.getDataSets( ).add( dsHandle ); | ||
− | + | ||
− | + | } | |
− | + | /** | |
− | } | + | * The doPost method of the servlet. <br> |
− | + | * | |
− | + | */ | |
− | + | public void doPost(HttpServletRequest request, HttpServletResponse response) | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
throws ServletException, IOException { | throws ServletException, IOException { | ||
Revision as of 19:37, 12 October 2006
Dynamic Report Servlet
This example demonstrate using the DE API to add a table to an existing report or template while running it within a servlet. This example should be setup as described in the Report Engine API Servlet Example. This class should replace the WebReport class. Add comments at the bottom of the example.
BIRT Design Engine API Return to the BIRT Design Engine API examples
Source
Dynamic.html
Place this file in the webapps/DynamicReport directory.
<html> <head> </head> <body> The Query that is used for this example is Select * From OFFICES <form id="form1" method="get" action="http://localhost:8080/DynamicReport/run"> <select name="dyna1" multiple size="8" > <option value="COUNTRY">COUNTRY</option> <option value="CITY">CITY</option> <option value="STATE">STATE</option> <option value="OFFICECODE">OFFICECODE</option> <option value="ADDRESSLINE1">ADDRESSLINE1</option> <option value="ADDRESSLINE2">ADDRESSLINE2</option> <option value="POSTALCODE">POSTALCODE</option> <option value="TERRITORY">TERRITORY</option> </select>
<INPUT TYPE="SUBMIT" VALUE="Run Dynamic Report"> <INPUT TYPE="HIDDEN" NAME="ReportName" Value="DynamicTableExample.rpttemplate"> </form> </body> </html>
DynamicReport.java
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; 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.EngineConstants; import org.eclipse.birt.report.engine.api.HTMLRenderContext; import org.eclipse.birt.report.engine.api.HTMLRenderOption; import org.eclipse.birt.report.engine.api.IReportRunnable; import org.eclipse.birt.report.engine.api.IRunAndRenderTask; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.model.api.CellHandle; import org.eclipse.birt.report.model.api.DataItemHandle; import org.eclipse.birt.report.model.api.ElementFactory; import org.eclipse.birt.report.model.api.LabelHandle; import org.eclipse.birt.report.model.api.OdaDataSetHandle; import org.eclipse.birt.report.model.api.OdaDataSourceHandle; import org.eclipse.birt.report.model.api.PropertyHandle; import org.eclipse.birt.report.model.api.ReportDesignHandle; import org.eclipse.birt.report.model.api.RowHandle; import org.eclipse.birt.report.model.api.StructureFactory; import org.eclipse.birt.report.model.api.TableHandle; import org.eclipse.birt.report.model.api.activity.SemanticException; import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; public class DynamicReport extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ private IReportEngine birtReportEngine = null; protected static Logger logger = Logger.getLogger( "org.eclipse.birt" ); public DynamicReport() { super(); } /** * Destruction of the servlet.
*/ public void destroy() { super.destroy(); BirtEngine.destroyBirtEngine(); } /** * The doGet method of the servlet.
* */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //get report name and launch the engine resp.setContentType("text/html"); //resp.setContentType( "application/pdf" ); //resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); String reportName = req.getParameter("ReportName"); String[] cols = (String[])req.getParameterMap().get("dyna1"); ServletContext sc = req.getSession().getServletContext(); this.birtReportEngine = BirtEngine.getBirtEngine(sc); //setup image directory HTMLRenderContext renderContext = new HTMLRenderContext(); renderContext.setBaseImageURL(req.getContextPath()+"/images"); renderContext.setImageDirectory(sc.getRealPath("/images")); logger.log( Level.FINE, "image directory " + sc.getRealPath("/images")); System.out.println("stdout image directory " + sc.getRealPath("/images")); HashMap contextMap = new HashMap(); contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext ); IReportRunnable design; try { //Open report design design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName ); ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( ); buildReport( cols, report ); //create task to run and render report IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); task.setAppContext( contextMap ); //set output options HTMLRenderOption options = new HTMLRenderOption(); options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF); options.setOutputStream(resp.getOutputStream()); task.setRenderOption(options); //run report task.run(); task.close(); }catch (Exception e){ e.printStackTrace(); throw new ServletException( e ); } } public void buildReport(String[] cols, ReportDesignHandle designHandle){ try{ ElementFactory designFactory = designHandle.getElementFactory( ); buildDataSource(designFactory, designHandle); //ArrayList cols = new ArrayList(); //cols.add("OFFICECODE"); //cols.add("CITY"); //cols.add("COUNTRY"); buildDataSet(cols, "From Offices", designFactory, designHandle); TableHandle table = designFactory.newTableItem( "table", cols.length ); table.setWidth( "100%" ); table.setDataSet( designHandle.findDataSet( "ds" ) ); PropertyHandle computedSet = table.getColumnBindings( ); ComputedColumn cs1 = null; for( int i=0; i < cols.length; i++){ cs1 = StructureFactory.createComputedColumn(); cs1.setName((String)cols[i]); cs1.setExpression("dataSetRow[\"" + (String)cols[i] + "\"]"); computedSet.addItem(cs1); } // table header RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 ); for( int i=0; i < cols.length; i++){ LabelHandle label1 = designFactory.newLabel( (String)cols[i] ); label1.setText((String)cols[i]); CellHandle cell = (CellHandle) tableheader.getCells( ).get( i ); cell.getContent( ).add( label1 ); } // table detail RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 ); for( int i=0; i < cols.length; i++){ CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i ); DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols[i] ); data.setResultSetColumn( (String)cols[i]); cell.getContent( ).add( data ); } designHandle.getBody( ).add( table ); }catch(Exception e){ e.printStackTrace(); } } void buildDataSource( ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException { OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource( "Data Source", "org.eclipse.birt.report.data.oda.jdbc" ); dsHandle.setProperty( "odaDriverClass", "org.eclipse.birt.report.data.oda.sampledb.Driver" ); dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" ); dsHandle.setProperty( "odaUser", "ClassicModels" ); dsHandle.setProperty( "odaPassword", "" ); designHandle.getDataSources( ).add( dsHandle ); } void buildDataSet(String[] cols, String fromClause, ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException { OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds", "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" ); dsHandle.setDataSource( "Data Source" ); String qry = "Select "; for( int i=0; i < cols.length; i++){ qry += " " + cols[i]; if( i != (cols.length -1) ){ qry += ","; } } qry += " " + fromClause; dsHandle.setQueryText( qry ); designHandle.getDataSets( ).add( dsHandle ); } /** * 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(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occure */ public void init() throws ServletException { BirtEngine.initBirtConfig(); } }
Comments
Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.