Thursday, August 22, 2019

Integrate XML Publisher and OAF ?

Step 1 : XML Publisher Reporting Architecture

  • An XML publisher report consist of a report template designed in RTF or PDF Format and a Data XML file.
  • The template contains the report layout. 
  • During execution the Data XML File is merged with the template to generate a PDF, HTML, EXCEL or RTF report.
Step 2: Designing the OAF BC4J Data Model.

        
   Using jdeveloper create a ViewObject EmpVO using the below query and associate it to ApplicationModule

EmpAM.

SELECT empno,ename,job,mgr,hiredate,comm,deptno FROM emp

Step 3 : Generating the XML for Template Design

Design a OAF Page EmpPG with the Following Code in the Controller EmpCO.

Write below highlighted code in EmpAMImpl,java file(AM).

package reddy.oracle.apps.po.xmlreport.server;

import java.io.ByteArrayOutputStream;

import oracle.apps.fnd.common.MessageToken;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OAApplicationModuleImpl;
import oracle.apps.fnd.framework.test.inputscanner.server.EmpVOImpl;

import oracle.jbo.XMLInterface;

import oracle.xml.parser.v2.XMLNode;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class XmlReportOAFAMImpl extends OAApplicationModuleImpl {
    /**This is the default constructor (do not remove)
     */
    public XmlReportOAFAMImpl() {
    }

    /**Sample main for debugging Business Components code using the tester.
     */
    public static void main(String[] args) {
        launchTester("reddy.oracle.apps.po.xmlreport.server", /* package name */
      "XmlReportOAFAMLocal" /* Configuration Name */);
    }

    /**Container's getter for XmlReportOAFVO1
     */
    public XmlReportOAFVOImpl getXmlReportOAFVO1() 
    {
        return (XmlReportOAFVOImpl)findViewObject("XmlReportOAFVO1");
    }
    public void intiEMPVO() 
    {
        XmlReportOAFVOImpl vo=getXmlReportOAFVO1();
        
        if(vo == null)
        {
            MessageToken errTokens[] = 
        {
            new MessageToken("OBJECT_NAME", "XmlReportOAFVO1")
        };
            throw new OAException("AK", "FWK_TBX_OBJECT_NOT_FOUND", errTokens);
        }
        else 
        {
            vo.executeQuery();  
        }
    }
    
    public void getEmpDataXML() 
    {
        try 
        {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
            OAViewObject vo = (OAViewObject)findViewObject("XmlReportOAFVO1");
            ((XMLNode) vo.writeXML(4, XMLInterface.XML_OPT_ALL_ROWS)).print(outputStream);
            System.out.println(outputStream.toString());
        }    
        catch(Exception e)
        {
                throw new OAException (e.getMessage());
        }
    }
}


Write below highlited code code in CO.

public void processRequest(OAPageContext pageContext, OAWebBean webBean)
{
super.processRequest(pageContext, webBean);
OAApplicationModuleImpl am= (OAApplicationModuleImpl)pageContext.getApplicationModule(webBean);
am.invokeMethod("initEmpVO");
am.invokeMethod("getEmpDataXML");
}

Run the page.On running the page , the data xml file will be printed on the Jdeveloper Embeded OC4J Sever Log (since i have used System.out.println). The XML file will be like

<XmlReportOAFVO>
   <XmlReportOAFVORow>
      <Empno>7369</Empno>
      <Ename>SMITH</Ename>
      <Job>CLERK</Job>
      <Mgr>7902</Mgr>
      <Hiredate>1980-12-17</Hiredate>
      <Deptno>20</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7521</Empno>
      <Ename>WARD</Ename>
      <Job>SALESMAN</Job>
      <Mgr>7698</Mgr>
      <Hiredate>1981-02-22</Hiredate>
      <Comm>500</Comm>
      <Deptno>30</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7566</Empno>
      <Ename>JONES</Ename>
      <Job>MANAGER</Job>
      <Mgr>7839</Mgr>
      <Hiredate>1981-04-02</Hiredate>
      <Deptno>20</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7654</Empno>
      <Ename>MARTIN</Ename>
      <Job>SALESMAN</Job>
      <Mgr>7698</Mgr>
      <Hiredate>1981-09-28</Hiredate>
      <Comm>1400</Comm>
      <Deptno>30</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7698</Empno>
      <Ename>BLAKE</Ename>
      <Job>MANAGER</Job>
      <Mgr>7839</Mgr>
      <Hiredate>1981-05-01</Hiredate>
      <Deptno>30</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7782</Empno>
      <Ename>CLARK</Ename>
      <Job>MANAGER</Job>
      <Mgr>7839</Mgr>
      <Hiredate>1981-06-09</Hiredate>
      <Deptno>10</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7788</Empno>
      <Ename>SCOTT</Ename>
      <Job>ANALYST</Job>
      <Mgr>7566</Mgr>
      <Hiredate>1982-12-09</Hiredate>
      <Deptno>20</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7839</Empno>
      <Ename>KING</Ename>
      <Job>PRESIDENT</Job>
      <Hiredate>1981-11-17</Hiredate>
      <Deptno>10</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7844</Empno>
      <Ename>TURNER</Ename>
      <Job>SALESMAN</Job>
      <Mgr>7698</Mgr>
      <Hiredate>1981-09-08</Hiredate>
      <Comm>0</Comm>
      <Deptno>30</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7876</Empno>
      <Ename>ADAMS</Ename>
      <Job>CLERK</Job>
      <Mgr>7788</Mgr>
      <Hiredate>1983-01-12</Hiredate>
      <Deptno>20</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7900</Empno>
      <Ename>JAMES</Ename>
      <Job>CLERK</Job>
      <Mgr>7698</Mgr>
      <Hiredate>1981-12-03</Hiredate>
      <Deptno>30</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7902</Empno>
      <Ename>FORD</Ename>
      <Job>ANALYST</Job>
      <Mgr>7566</Mgr>
      <Hiredate>1981-12-03</Hiredate>
      <Deptno>20</Deptno>
   </XmlReportOAFVORow>
   <XmlReportOAFVORow>
      <Empno>7934</Empno>
      <Ename>MILLER</Ename>
      <Job>CLERK</Job>
      <Mgr>7782</Mgr>
      <Hiredate>1982-01-23</Hiredate>
      <Deptno>10</Deptno>
   </XmlReportOAFVORow>
</XmlReportOAFVO>

save this XML file in empXml.xml 

Step 4 : Designing the Template

Open the Microsoft word. 
You should be able to see the following menus and toolbars.
Using the menu Data -> Load XML Data... , load the XML File generated from Jdeveloper

If the XML files gets loaded successfully, then you should get the below confirmation.
Using the Table Wizard as below to create the 'Table Report Format' with all the columns of EMP.
The Table Report Format Template should be like Save the document as Emp.rtf. Preview the report as PDF, XML, HTMl or RTF.

Step 5 : Registering the Template with Oracle Applications.

Login with a user having "XML Publisher Administrator" Responsibility.
Navigate to Home --> Data Definition and define the data definition.
Navigate to Home --> Template and Define the Template

Step 6 : Integrating the OAF page With XML Publisher

Design the EmpPG page to appear as shown below.
Set the Action Type and Event property of the Tourch Image Item to FireAction and GenerateReport
respectively. Modify the CO.


/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package reddy.oracle.apps.po.xmlreport.webui;

import javax.servlet.http.HttpServletResponse;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.server.OAApplicationModuleImpl;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletOutputStream;

import oracle.apps.fnd.framework.OAException;

import oracle.apps.fnd.framework.server.OADBTransactionImpl;
import oracle.apps.xdo.oa.schema.server.TemplateHelper;

import oracle.cabo.ui.data.DataObject;

import oracle.xml.parser.v2.XMLNode;


/**
 * Controller for ...
 */
public class XmlReportOAFCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
   
   private static final String APP_NAME = "PO";
   private static final String TEMPLATE_CODE = "XMLOAFREPORT";
   
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    OAApplicationModuleImpl am= (OAApplicationModuleImpl)pageContext.getApplicationModule(webBean);
    am.invokeMethod("intiEMPVO");
    am.invokeMethod("getEmpDataXML");
  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    OAApplicationModuleImpl am= (OAApplicationModuleImpl)pageContext.getApplicationModule(webBean);
    
    String event = pageContext.getParameter("event");  
    
      if("GenerateReport".equals(event))
      {
          // Get the HttpServletResponse object from the PageContext. The report output is written to HttpServletResponse.
          DataObject sessionDictonary = (DataObject)pageContext.getNamedDataObject("_SessionParameters");
          HttpServletResponse response = (HttpServletResponse)sessionDictonary.selectValue(null,"HttpServletResponse");
          try 
          {
                ServletOutputStream os = response.getOutputStream();
                
                // Set the Output Report File Name and Content Type
                
                String contentDisposition = "attachment;filename=EmpReport.pdf";
                response.setHeader("Content-Disposition",contentDisposition);
                response.setContentType("application/pdf");
                
                // Get the Data XML File as the XMLNode
                XMLNode xmlNode = (XMLNode) am.invokeMethod("getEmpDataXML");
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                xmlNode.print(outputStream);
                ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
                ByteArrayOutputStream pdfFile = new ByteArrayOutputStream();
          
                //Generate the PDF Report.
               
               TemplateHelper.processTemplate(
              ((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getAppsContext(),
              APP_NAME,
              TEMPLATE_CODE,
              ((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getUserLocale().getLanguage(),
              ((OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction()).getUserLocale().getCountry(),
              inputStream,
              TemplateHelper.OUTPUT_TYPE_PDF,
              null,
              pdfFile);
        
                // Write the PDF Report to the HttpServletResponse object and flush.
                byte[] b = pdfFile.toByteArray();
                response.setContentLength(b.length);
                os.write(b, 0, b.length);
                os.flush();
                os.close();
          }
          catch(Exception e)
          {
                response.setContentType("text/html");
                throw new OAException(e.getMessage(), OAException.ERROR);
          }
                pageContext.setDocumentRendered(false);
          }
      }

  }

Run the EmpPG page and click on the tourch icon. 
The File Download window appear. Click on the Open Button to view the report.

No comments:

Post a Comment

AME (Approval Management Engine)

AME (Approval Management Engine) : AME Stands for Oracle Approval Management Engine. AME is a self service web application that enables...