Showing posts with label ADF table. Show all posts
Showing posts with label ADF table. Show all posts

Sunday, March 29, 2015

Automatic data refresh Oracle ADF tables using ADF Poll

As you know we are using Oracle ADF tables to represent data at database tables, views, etc... When application loads tables load data from db and displays those. But if data is changed or new added or existing data get deleted..?? Then we need a mechanism to display the updated data.

We can use ADF Poll for this. Then we can use poll event to refresh table data after every time period.


Steps:



  1. Insert Poll inside “panelHeader” in which table is located
  2. Set poll interval from poll properties- This is the time gap for every refresh. for Ex: if you have set poll interval as 5 seconds table data will be refreshed after every 5 seconds.

    If you want you can take this value from template properties.
    Value you entered: #{manage_Template.dataRefreshRate}
    (This is used to get the refresh value by using the method in manage_Template)
  3. Set partial triggers from poll properties - Here you can specify the table which you want to get refreshed. for Ex: table1
  4. Set partial triggers from ADF table - Give the poll id for this field.
    Table --> Behavior --> Partial Triggers --> Set poll id here
  5. Set Poll Listener from poll properties - This is the most important mapping which links what is the action has to be taken out when poll event triggers

    Type a method e.g.: refreshSPIntentionTab ()
    Press enter
    (Method is created in .java file)
  6. Add the below code to the poll listener method using appropriate VOIterators


    There are three stages at this code.


    STAGE 1:

    If you want only to refresh the table you can use the current method. At this method you can’t maintain the current row position after table refresh.

    public void refreshSPIntentionTab (PollEvent pollEvent) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ValueBinding dcb = fctx.getApplication().createValueBinding("#{bindings}");
            DCBindingContainer bindings1 = (DCBindingContainer) dcb.getValue(fctx);
            if(bindings1!=null){
                DCIteratorBinding dciter = bindings1.findIteratorBinding("DeliveryIntentionsSPApprovVO1Iterator ");
                if(dciter!=null){
                    if(dciter.getCurrentRow()!=null){  
                        dciter.executeQuery();
                    }
                }
            }
        }


    STAGE 2:

    If you want to refresh the table and maintain the current row position after the table refresh.... We can apply this only for a single table; not for master-detail tables.

    public void refreshSPIntentionTab (PollEvent pollEvent) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ValueBinding dcb = fctx.getApplication().createValueBinding("#{bindings}");
            DCBindingContainer bindings1 = (DCBindingContainer)dcb.getValue(fctx);
            if (bindings1 != null) {
                DCIteratorBinding dciter = bindings1.findIteratorBinding("DeliveryIntentionsSPApprovVO1Iterator ");
                if (dciter != null) {
                    if (dciter.getCurrentRow() != null) {
                        Key current_row_key = dciter.getCurrentRow().getKey();
                        dciter.executeQuery();
                        if (current_row_key != null) {
                            try {
                                dciter.setCurrentRowWithKey(current_row_key.toStringFormat(true));
                            } catch (Exception ex) {
                                System.out.println("Exception in current_row_key");
                            }
                        }
                    }
                }
            }
        }


    STAGE 3:

    If you want to maintain the current row position of master-detail tables after the table refresh, use the following methods.

    // for the master table refresh
    public void refreshSPIntentionTab (PollEvent pollEvent) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ValueBinding dcb = fctx.getApplication().createValueBinding("#{bindings}");
            DCBindingContainer bindings1 = (DCBindingContainer)dcb.getValue(fctx);
            DCIteratorBinding it = bindings1.findIteratorBinding("DeliveryIntentionsSPApprovVO1Iterator");
            ViewObject vo = it.getViewObject();
            Row row = vo.getCurrentRow();
            Key key = row.getKey();
            int rangePosition = vo.getRangeIndexOf(row);
            int rangeStart = vo.getRangeStart();
            if(rangePosition==(-1)){
                rangePosition=rangePositionCommon;
                rangeStart=rangeStartCommon;
                }
            vo.executeQuery();
            vo.setRangeStart(rangeStart);
            Row[] rows = vo.findByKey(key, 1);
            if (rows != null && rows.length == 1) {            
                vo.scrollRangeTo(rows[0], rangePosition);
                vo.setCurrentRowAtRangeIndex(vo.getRangeIndexOf(rows[0]));
                rangePositionCommon=rangePosition;
                rangeStartCommon=rangeStart;
            }
        }

    // for the detail table refresh
    public void refreshSPIntentionTab (PollEvent pollEvent) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ValueBinding dcb = fctx.getApplication().createValueBinding("#{bindings}");
            DCBindingContainer bindings1 = (DCBindingContainer) dcb.getValue(fctx);
            if(bindings1!=null){
                DCIteratorBinding dciter = bindings1.findIteratorBinding("DeliveryIntentionsSPApprovVO1Iterator ");
                if(dciter!=null){
                    if(dciter.getCurrentRow()!=null){  
                        dciter.executeQuery();
                    }
                }
            }
        }

Now all the setup processes are completed. Please check your new functionality.


Cheers..!!

Thursday, December 15, 2011

How to download Oracle ADF tables in PDF format


          At this task we are going to convert ADF BC tables into PDF files and make it downloadable to users. It looks like very easy but it is not. To successfully complete this task I had to use several libraries such as apache fop library.
 
  • What is Apache FOP library.....??



          FOP stands for Formatting Object Processor. Apache FOP is a Java application which reads a Formatting Object (FO) tree and renders the resulting pages to a specific output. Currently supported outputs are:
           PDF, PS, PCL, AFP, XML, Print, AWT and PNG, and to a lesser extend RFT and TXT.
          Developers use Apache FOP libraries to change the output file format very commonly because it’s free and open source. You can download the Apache FOP library as a JAR file from the below link. (There are different links to download FOP.jar; but most of the jar files do not contain all the libraries required for this conversion.)


  •  How to convert ADF BC tables into PDF files

           If we try to analyse the process goes here we can simply represent it as below.

          First of all we want to get the table data and write it into a XML file. We can do it through JDeveloper framework. There is an inbuilt method called “printXML” at ViewObjectImpl class. It will generate the XML to the VO data. So if we can access the ViewObjectImpl class anyway we can generate the XML file. <VO>Impl class is a one which extends the ViewObjectImpl class. So we can generate the <VO>Impl class and generate the XML file.

  • Implement the <VO>Impl and <Application_Module>Impl classes for your MODEL project. (We need those method at those classes to generate the XML file.)
    (<VO>.xml à Java à Java Classes à click on "pencil" icon.)

  • Create a Java class at “View Controller” project to create a managed-bean. We are going to do all the coding inside this bean. All the required codes are mentioned at the end of this article.
     
  • We can access the <VO>Impl class through this path:
    (FacesContext à ExternalContext à AdfFacesContext à HttpServletRequest à BindingContext à DCDataControl à ApplicationModule à AppModuleImpl à ViewObjectImpl)
     
  • When we are creating the Node object we have two options. You can select the one which suits your requirement. (refer code while "// Print the XML" )
    Parameter
    Description
    XML_OPT_ALL_ROWS
    All rows will be rendered in XML
    XML_OPT_ASSOC_CONSISTENT
    attribute accessors that return a RowSet should set the AssociationConsistent flag on, so that new (unposted) rows for the RowSet are also included in the output.
    XML_OPT_CHANGES_ONLY
    For internal framework use only.
    XML_OPT_LIMIT_RANGE
    Rows in the current range will be rendered in XML

  • Then we have to generate the PDF using  a style sheet. Before genarate PDF files you should have to build the style sheet. Then we can generate the output file using FOP libraries (we can select the output format). Output format depends on the MimeConstant you select. There are several output formats supported by FOP libraries as follows. (refer code while "// Generate PDF" )

    MIME Constant
    Output Format
    MIME_PDF
    pdf
    MIME_PLAIN_TEXT
    txt
    MIME_PNG
    png
    MIME_FOP_AREA_TREE
    xml
    MIME_POSTSCRIPT
    ps
    MIME_AFP
    afp
    MIME_TIFF
    tiff
  •   
  • Then insert a Command Button next to your ADF BC table. Create a web page backing bean and write an Action Method. Then set that action method as the “action” property of the created command button. Inside the action method we have to write some codes which will make the generated pdf file to be downloadable.
     
  • When user press the command button pdf file shold be generated and downloadable. To do that  we have to go through the Context layers of the framework, get the http servlet response and set the content type of the response.
     
  • Here we have set the content type as “application/pdf” because we have generated a pdf file. That parameter depends on your file type. (refer code while "// download the PDF" )

      
All the required codes are given below.....

 
// Access the <VO>Impl
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
AdfFacesContext context = AdfFacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
BindingContext ctx = DCUtil.getBindingContext(request);
DCDataControl dc = ctx.findDataControl("AppModuleDataControl");
ApplicationModule service = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = service.findApplicationModule("AppModule");
AppModuleImpl amImpl = (AppModuleImpl)am;
ViewObjectImpl VOImlp = (EmpVOImpl)amImpl.findViewObject("EmpVO1");
HashMap viewDefMap = new HashMap();

// Print the XML
Node n = VOImlp.writeXML(XMLInterface.XML_OPT_ALL_ROWS, viewDefMap);
java.io.File file = new java.io.File(<path to save XML file>);
PrintWriter output = null;
try {  output = new java.io.PrintWriter(file);
          ((XMLNode)n).print(output);
} catch (Exception e) { System.out.println(e.getMessage());
} finally {
try {output.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }

// Generate PDF
File xmlfile = new File(<path to generated XML file>);
File xsltfile = new File(<path to styleSheet>);
File pdffile = new File(<path to save PDF file>);
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = new TransformerFactoryImpl();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
transformer.setParameter("versionParam", "2.0");
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally { out.close(); }

// download the PDF
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
File file = new File(dir1, pdffile1);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
          response.reset();
          response.setContentType("application/pdf");
          response.setContentLength((int)file.length());
response.setHeader("Content-disposition","inline;filename=\""+pdffile1+"\"");
output = new BufferedOutputStream(response.getOutputStream(),DEFAULT_BUFFER_SIZE);
          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
          int length;
          while ((length = input.read(buffer)) > 0) {
              output.write(buffer, 0, length); }
          output.flush(); } finally { close(output); close(input); }
facesContext.responseComplete();

      

CHEERS.......!!

Friday, November 18, 2011

How to download Oracle ADF tables in Excel format



          At a single web application there are several ADF tables which presents different data. Using the web front end we can read and refer those data. But if we have the ability to download those data in Excel format then it would be very easy to process those data and make some decisions.

         It is very easy to convert ADF tables to Excel format using the "exportCollectionActionListener" component. Let's see a very simple example.

  1. Create an ADF table using a VO (View Object)
  2. Then create a command button (or a command link) next to the ADF table. I name it as “Download Excel".
  3. After that drag and drop an “exportCollectionActionListener” component from component pallet on to that command button. Then set parameters as follows.

    • ExportedId: table or collection id you want to download.
    • Type: excelHTML (because we want to download an excel file)
    • filename: Name on the download file.
    • title: title of the download file.