Pages - Menu

Sunday, September 15, 2013

jasper reports & Oracle ADF, Different Ways || Oracle ADF

Hi
Many days ago, i received many questions about Jasper Reports and how to make it more efficient tool in Oracle ADF world as reporting and Integration purpose ..
from my point of view, we have different goals to use Jasper Reports in Oracle ADF
Examples ..
  • Reporting Tool
  • Generate Reports for integration purpose with other systems specially Faxing Solutions.
  • Generate pdf, xls or any other format with direct / indirect output in front of end user.
so, today i give a complete Back Bean Method with high flexibility to manage different ways of Jasper in ADF.
package Servlets;

import cwcbean.CWCBEAN;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.sql.Connection;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class PrintReport {

    CWCBEAN cwc = new CWCBEAN();

    public String printReport(String RepPathNamewithoutextension, String WriteFilePathWithouExtension,
                              Boolean tprint) throws FileNotFoundException, JRException, NamingException, SQLException,
                                                     IOException {

        if (RepPathNamewithoutextension != null && WriteFilePathWithouExtension != null && tprint != null) {

            InputStream input = new FileInputStream(new File(RepPathNamewithoutextension + “.jrxml”));
            JasperDesign design = JRXmlLoader.load(input);
            JasperReport report = JasperCompileManager.compileReport(design);
            Map parameters = new HashMap();

            // parameters.put(“jasperParamenterName”, “ParameterValue”);

            InitialContext initialContext = new InitialContext();
            DataSource ds = (DataSource)initialContext.lookup(“PM-Data-Source”);
            Connection conn = ds.getConnection();
            JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);

            if (WriteFilePathWithouExtension == null) {
                WriteFilePathWithouExtension = RepPathNamewithoutextension;
            }

            OutputStream ouputStream = new FileOutputStream(new File(WriteFilePathWithouExtension + “.pdf”)); // my pdf
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            JasperExportManager.exportReportToPdfStream(print, byteArrayOutputStream);

            ouputStream.write(byteArrayOutputStream.toByteArray()); // Copy pdf to directory u decided in second parameter — Wael Abdeen
            ouputStream.flush(); // Release
            ouputStream.close(); // Close Output

            if (tprint.equals(true)) {
                Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler ” + WriteFilePathWithouExtension +
                                          “.pdf”); // Only if u want to print the report ..
            }

        } else {

            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader13”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter13”), 3);
        }
        return null;

        // Wael Abdeen ..
    }
}

and now, we can call the report like this
    public String test() {
        String x;
        try {
            x = print.printReport(“d:/report1”, “d:/report1”, Boolean.TRUE);
        } catch (FileNotFoundException e) {
            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader4”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter4”), 2);
        } catch (JRException e) {
            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader4”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter4”), 2);
        } catch (NamingException e) {
            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader4”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter4”), 2);
        } catch (SQLException e) {
            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader4”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter4”), 2);
        } catch (IOException e) {
            cwc.Validate(cwc.getFacesContext(),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageHeader4”),
                         cwc.AccessBundleItemValue(“CS.view.CS-InterfaceBundle”, “ValidatorMessageFooter4”), 2);
        }
        return null;
    }
Note : we need to include all jars required from jasper source to our project based on version you are using from jasper.

3 comments:

  1. Would you explain CWCBEAN

    ReplyDelete
  2. CWC Bean is our old internal developed Back Bean lib, we built to facilitate and speed up our Back / Managed Bean Development.

    in the next phase, has been upgraded to be WA Bean :).

    Regards,

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete