Thursday, May 26, 2011

Jasper report in ADF aaplication

This post is to create an application which will use to generate Jasper report as created with the help of iReport tool.
Next are the steps of creating that needed application.
  • In application create entity object, view object and application module use to database table employees and departments from HR user.
  • Go to employee view object and create List of values for department id. Create a transient attribute for file types in which report will be generated.
  • So create a list of values in this attribute and take static values from a static view object like pdf, xls, csv etc.
  • Before creating any page or java class, Jasper report related libraries should be imported in jdeveloper so that jasper related java classes can be used.
    • commons-beanutils-1.8.3.jar
    • commons-collections-3.2.1.jar
    • commons-digester-2.1.jar
    • commons-logging-1.1.1.jar
    • iText-2.1.7.jar
    • jasperreports-4.0.1.jar
    • jfreechart-1.0.13.jar
    • poi-3.7-20101029.jar
  • Now create a page to select report output file type and to pass the parameter to jasper report. Add both list of values to page for run time selection.









  • When user will select the values for department and file type and run the report, these values will be passed to servlet from java class. To get selected values in java class lov should be changed. Add a for each element in select one choice and get item value from this.
  • Bind these both list to bean class so that selected values will be passed to servlet.
  • Now create a servlet for request and response for report. For this go to new, select servlet from category and http servlet from items, click ok.
  • Give servlet name, its package, type to html, and select doget and doPost methods. Click next. 
  • Now it will show created servlet on editor . Write code for jasper report in this servlet.
  • Write code in one of the method doGet() or doPost().
  • public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    
    response.setContentType(CONTENT_TYPE);
  • Create a connection so that report will use this connection to get data from database.
try{ 
PreparedStatement st = null;

String amDef = "jasper.model.module.AppModuleAM";
String config = "AppModuleAMLocal";
AppModuleAMImpl am =
(AppModuleAMImpl)Configuration.createRootApplicationModule(amDef,
config);    
st = am.getDBTransaction().createPreparedStatement("select 1 from dual", 0);
Connection conn = st.getConnection();
  • Place jrxml file in public-html folder of application so that it will pick file from server location . To get this file input stream is created .
InputStream input = new FileInputStream(new File(getServletConfig().getServletContext()
.getRealPath("/reports/parReport.jrxml")));

JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design); 
  • Now get Attributes which are set at the time of servlet calling.
Integer code = (Integer)request.getAttribute("deptno");
String reporttype = (String)request.getAttribute("reporttype");
  • Set value for parameter of report. If you have more than one parameter in your report, then you have to call the put method for each parameter.
Map parameters = new HashMap();
parameters.put("DeptNo", code);
JasperPrint jasperPrint=JasperFillManager.fillReport(report, parameters, conn);
  • Now create a output stream to send file to client. Then Check file type and export file to client in right format.
OutputStream ouputStream = response.getOutputStream();
JRExporter exporter = null;

if( "pdf".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/pdf");

response.setHeader("Content-Disposition", "attachement; filename=\"file.pdf\"");


exporter = new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "rtf".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/rtf");
response.setHeader("Pragma", "no-cache");
response.setHeader("Content-Disposition", "attachement; filename=\"file.rtf\"");

exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "html".equalsIgnoreCase(reporttype) )
{
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "xls".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/xls");
response.setHeader("Content-Disposition", "attachement; filename=\"file.xls\"");

exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "csv".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachement; filename=\"file.csv\"");

exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}

try
{
exporter.exportReport();
}
catch (JRException e)
{
throw new ServletException(e);
}
finally
{
if (ouputStream != null)
{
try
{
ouputStream.flush(); 
ouputStream.close();

}
catch (IOException ex)
{
System.out.println(ex.getMessage());
throw (ex);
}
}
}

}catch(Exception ex) {
ex.printStackTrace();
}
  • After writing servlet we have to call this servlet and pass dynamic values from page to servlet. For this write a function and call this function from a button on page.
  • To send request to servlet we will use http request and response cycle. In request we will set attributes, which we will need in servlet to pass to jasper report and to print report according to its format.
public void callServlet(ActionEvent actionEvent) throws Exception  {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
HttpServletResponse response =(HttpServletResponse) ctx.getExternalContext().getResponse();

request.setAttribute("deptno", deList.getValue());
request.setAttribute("reporttype", fileTypeList.getValue());

request.getRequestDispatcher(response.encodeURL("/jasperservlet")).forward(request, response);   

response.flushBuffer();  
ctx.responseComplete();    

}