Saturday, September 12, 2015

Integration of Oracle ADF with Jasper Report

An application using statistical data requires often to generate reports (PDF, XLS , Docx etc.) in order to provide a visual document with the selected information.
There are some tools to provide report generation, and I’d like to present you a comprehensive environment – Jasper with its template builder iReports.

There are 3 ways of populating data into the jasper Report through Oracle ADF.

Below are the following:

1. JavaBean Data source with Array Value.

2. JavaBean Data Source with Map value through parameter.(through collection)

3. Schema Data source(creating connection with Database)

In this post i am going to share  3rd one:

Integrating Oracle ADF with Jasper Report.

Setup before Start creating the application:

1- You download iReport-5.6.0 (http://sourceforge.net/projects/ireport/files/latest/download)

2- You have to database Oracle XE(http://www.oracle.com/technetwork/database/database-technologies/express-edition/downloads/index.html) and Grand the role to HR which already a part of schema.
3- Setup JDeveloper 11g.

Below are the steps:

Step1. In J developer Create ADF application and add the following libraries in the project:

1-commons-digester-1.7.jar
2-itext-1.4.5.jar
3-jasperreports-3.0.0.jar
4-poi-2.5.1-final-20040804.jar

5- jasper-compiler-jdt-5.5.15.jar

Add this libraries to your ViewController project ---> Right click on the project ---> Project Properties--->Libraries And Classpath ---> Add JAR/Directory

Step2. Now open iReport tool:

iReport has not oracle database driver so you use oracle database you should first add a ojdbc jar file to the classpath of iReport

In iReport goto Tools---> Options ---> Classpath and add ojdbc6.jar

Make a connection to Hr Schema

Both in iReport and Jdeveloper(Database navigator)



 
Step 3:Create Template in iReport and path is under your workspace/viewcontroller/ like below:




Step4:Create One Jspx page in ADF application and create a class which contain method of compilation and running the jrxml template which we are created in iReport tool:

The method is:

 public BindingContainer getBindings()
  {
    return BindingContext.getCurrent().getCurrentBindingsEntry();
  } 
  
  public Connection getDataSourceConnection(String dataSourceName) throws Exception
    {
      Context ctx = new InitialContext();
      DataSource ds = (DataSource)ctx.lookup(dataSourceName);
      return ds.getConnection();
    }
  
  private Connection getConnection() throws Exception
  {
    return getDataSourceConnection("hrDS");// use datasourse in your application
  }
  
  public  ServletContext getContext()
  {
    return (ServletContext)getFacesContext().getExternalContext().getContext();
  }
  
  public  HttpServletResponse getResponse()
  {
    return (HttpServletResponse)getFacesContext().getExternalContext().getResponse();
  }
  
  public static FacesContext getFacesContext()
  {
    return FacesContext.getCurrentInstance();
  }
  
  public void runReport(String repPath, java.util.Map param) throws Exception
  {
    Connection ds = null;
    try
    {
      HttpServletResponse response = getResponse();
      ServletOutputStream out = response.getOutputStream();
      response.setHeader("Cache-Control", "max-age=0");
      response.setContentType("application/pdf");
      ServletContext context = getContext();
      InputStream fs = context.getResourceAsStream("/reports/" + repPath);//we will put the report under folder "reports" under Web Content
      JasperReport template = (JasperReport) JRLoader.loadObject(fs);
      template.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
      conn = getConnection();
      JasperPrint print = JasperFillManager.fillReport(template, param, ds);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      JasperExportManager.exportReportToPdfStream(print, baos);
      out.write(baos.toByteArray());
      out.flush();
      out.close();
      FacesContext.getCurrentInstance().responseComplete();
    }
    catch (Exception jex)
    {
      jex.printStackTrace();
    }
    finally
    {    
      close(conn);
    }
  }
  
  public void close(Connection con)
   {
     if (con != null)
     {
       try
       {
         con.close();
       }
       catch (Exception e)
       {
       }
     }
   }


Hope Blog will help you in integrating ADF with Jasper.