Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label page fragment. Show all posts
Showing posts with label page fragment. Show all posts

Saturday 23 August 2014

Access BindingContainer (Page Bindings) of another page using DataBindings.cpx in Oracle ADF

Hello all
this post is about a requirement of getting page binding of another page that is not active currently
we use BindingContainer to access bindings of current page, region in managed bean

Oracle Docs says-
The BindingContainer contains the Control Bindings for a reusable unit of View technology. For example, each individual Page, Region, or Panel refers to a unique BindingContainer with a set of Control Bindings that refer to the Model elements used by that Page. The BindingContainer interface is implemented by the data binding framework provider. 

So to access operations, methods exposed in client, listBinding, IteratorBinding we need to get BindingContainer of current viewPort (a page or a page fragment)
this method is used to get BindingContainer -


import oracle.adf.model.BindingContext;
import oracle.binding.BindingContainer;

    /*****Generic Method to get BindingContainer of current page, fragment or region**/
    public BindingContainer getBindingsCont() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

as previously mentioned that BindingContainer contains bindings of current page but sometimes we need to access BindingContainer of any other page in order to access it's operations , iterators



So how to do this ?

for this i have created a 2 page fragments inside a bounded taskFlow



firstPage is very simple , it has only one button and secodPage has Departments (HR Schema ) viewObject as form

First Page Second Page

then i added createInsert operation in secodPage binidng, so here is the pageDef of secondPage
as there is no bindings on firstPage so there is no pageDef file is generated for that



now what i want to do is to call createInsert operation of Departments viewObject from firstPage, but there is no binding of operation in firstPage so if i use
BindingContext.getCurrent().getCurrentBindingsEntry()
to get BindingContainer then it will throw NullPointerException on calling createInsert

Now i have to get BindingContainer of secondPage -
Go to DataBindings.cpx file and see usageId for second page



Copy page usageId from there


pass this usageId in this method to get BindingContainer of secondPage


    /**
     * @param data
     * @return
     */
    public Object resolvEl(String data) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, data, Object.class);
        Object Message = valueExp.getValue(elContext);
        return Message;
    }
    
    /**Method to get BindingContainer of Another page ,pageUsageId is the usageId of page defined in DataBindings.cpx file
     * @param pageUsageId
     * @return
     */
    public BindingContainer getBindingsContOfOtherPage(String pageUsageId) {
        return (BindingContainer) resolvEl("#{data." + pageUsageId + "}");
    }

then call createInsert operation using this BindingContainer


 getBindingsContOfOtherPage("binidngs_view_secondPagePageDef").getOperationBinding("CreateInsert").execute();

Happy Learning Cheers :)

Thursday 19 September 2013

Adding Drag and Drop Functionality for collections in page fragments to create insert

Hello All,
This tutorial is based on using Drag & Drop functionality in collections as af:table to create a new row
i have googled about Drag & Drop but was not able to implement in page fragments(.jsff), all samples was based on JSPX page.

this tutorial is based on DEPARTMENTS table (Default HR Schema) and an other table with Same Structure DEPARTMENTS_DUPL to implement drag and drop.
my scenario is to add row in DEPARTMENTS_DUPL from Departments 
 See the steps to implement- 
  •  First create Departments_dupl table in your HR schema, simply run this script

  • CREATE TABLE DEPARTMENTS_DUPL
      (
        DEPARTMENT_ID   NUMBER(4, 0) ,
        DEPARTMENT_NAME VARCHAR2(30 BYTE) ,
        MANAGER_ID      NUMBER(6, 0) ,
        LOCATION_ID     NUMBER(4, 0)
      )
    

  • Now create Fusion Web Application and create business components
  • Now create a bounded taskflow and a page fragment in it, and drop both tables on page

  • Now drop af:dragSource as child of Departments table and set properties, here discriminant is to ensure compatibility between drag and drop components, and its value must match for Drag source and Drop Target


  • now drop af:dropTarget as child of DepartmentsDupl table and set properties as Action etc and create a DropListener for it that handles drop event, set Flavor class to java.lang.Object





  • Now select af:dropTarget and goto source and set value for discriminant same as drag source

  •   <af:dropTarget dropListener="#{pageFlowScope.DragDropSampleBean.deptDropListener}" actions="COPY">
              <af:dataFlavor discriminant="copyDept" flavorClass="java.lang.Object"/>
            </af:dropTarget>
    



  • Now write code to create a new row in DepartmentsDupl and insert data from Departments on Drop Listener


  •     public void dragDropAction() {
            ViewObject dept = this.getDepartments1();
            ViewObject deptDupl = this.getDepartmentsDupl1();
            Row curDept = dept.getCurrentRow();
         
            Row dupl = deptDupl.createRow();
            dupl.setAttribute("DepartmentId", curDept.getAttribute("DepartmentId"));
            dupl.setAttribute("DepartmentName", curDept.getAttribute("DepartmentName"));
            dupl.setAttribute("ManagerId", curDept.getAttribute("ManagerId"));
            dupl.setAttribute("LocationId", curDept.getAttribute("LocationId"));
            deptDupl.insertRow(dupl);
            deptDupl.executeQuery();
            this.getDBTransaction().commit();
        }
    

  • Now Run your page and use this cool functionality :-)
 Download Sample App Cheers :-)

Wednesday 5 June 2013

Download file from url using Oracle ADF & Java- Download Manager

In this tutorial i will show you how to download a file from its url (web)-
as- http://www.tutorialspoint.com/java/java_tutorial.pdf
This will work as a simple download manager, you can add file url to download it and save it.
This tutorial makes use of FileHandling and java.net.URL class in java.

  • User Interface is very simple to design as it have only one input text and one button, so create a fusion web application , and a bounded taskflow with a page fragment in it
  • Now drag a Input text to enter url and a button to perform action on it from Componenet palette .
  • Bind input text to bean to get its value from page

  •  private RichInputText fileUrlBind;
    
        public void setFileUrlBind(RichInputText fileUrlBind) {
            this.fileUrlBind = fileUrlBind;
        }
    
        public RichInputText getFileUrlBind() {
            return fileUrlBind;
        }
    
  • Now create actionListener to managed bean and write code for downloading file from given url- see code

  •     public void DownloadFileButton(ActionEvent actionEvent) {
            try {
                if (fileUrlBind.getValue() != null) {
                    String fileUrl = fileUrlBind.getValue().toString();
                    if (fileUrl.startsWith("http://")) {
                        String msgNm = fileUrl.substring(7);
    
                        cnctmsgBind.setValue("Connecting to " + msgNm + "....");
    
                        URL url = new URL(fileUrl);
                        url.openConnection();
                        InputStream reader = url.openStream();
    
    
                        FileOutputStream writer =
                            new FileOutputStream("C:/javadrive." + fileUrl.substring(fileUrl.lastIndexOf(".")));
                        byte[] buffer = new byte[153600];
                        int totalBytesRead = 0;
                        int bytesRead = 0;
    
                       
                        dwnldMsgBind.setValue("Reading file 150KB blocks at a time");
                        while ((bytesRead = reader.read(buffer)) > 0) {
                            writer.write(buffer, 0, bytesRead);
                            buffer = new byte[153600];
                            totalBytesRead += bytesRead;
                        }
                     
                        alerMsgBind.setValue("File is downloaded successfully, look at your c drive :-)");
                        writer.close();
                        reader.close();
                    } else {
                        FacesMessage errMsg = new FacesMessage("Something went wrong");
                        errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                        errMsg.setDetail("Example- http://www.javadrive.co.in/java.pdf");
                        FacesContext context = FacesContext.getCurrentInstance();
                        context.addMessage(fileUrlBind.getClientId(), errMsg);
                    }
                }
            } catch (MalformedURLException e) {
                FacesMessage errMsg = new FacesMessage("Something went wrong");
                errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, errMsg);
                e.printStackTrace();
            } catch (IOException e) {
                FacesMessage errMsg = new FacesMessage("Something went wrong");
                errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, errMsg);
                e.printStackTrace();
            }
    
    
        }
    
    




  • Now Run your application and see downloaded file in your c drive as path is hard coded in bean


  •  See your downloaded file in c drive


Wednesday 15 May 2013

Beauty of ADF- Taskflow, Difference b/w Facelets and JSP XML fragments in bounded taskflow

Oracle ADF framework support modular architecture for enterprize application development. Multiple small module can be bound together to form a large module .
The greatest advantage of ADF controller over core JSF navigation model is that it splits a single bulky module to multiple reusable and interconnected modules known as Taskflows.

i am not going deeper in Taskflows as every ADF developer know about it ,so the important part is page fragment.
  • When  we create taskflow as bounded taskflow , and check it to create fragment in taskflow.
  • Now when we create pagefragment in bounded taskflow, there is 2 option for creating fragment




Create fragment as Facelets
Create fragment as JSP XML 

Page Fragment Type - Facelets or JSPX

Facelets- is default and official view handler for JSF pages, priviously JSP was used to view JSF pages but it didn't support all component so Facelets comes in picture under APACHE open source license. It supprts all UI component used by JSF (Java Server Faces).
Facelets was developed by Jacob Hookom in 2005.
If you create Facelets fragment in bounded taskflow, then taskflow must be dropped in a JSF page (parent page) not in .jspx (JSP XML ) page.
if you try to drop it into JSP XML page-


JSP XML- jspx is XML variant of JSP(Java Server Pages) to support XML document . JSP XML fragments are used in ADF inorder to support XML document and more powerful page validation techniques.
If you create jsp xml fragments in bounded taskflow, then taskflow must be dropped in a JSP XML(.jspx) page(parent page) not in JSF page.

Tuesday 14 May 2013

Using Contextual Event in Oracle ADF (Region Communication)

Contextual event ,in simple terms is a way to communicate between taskflows.
Sometimes we have taskflow open in a region and have to get some values from that taskflow .
This scenario can be achieved by contextual event.

 
Contextual Event have two parts-
  1. Publisher Event (Producer)- As button or any component that can raise event 
  2. Handler Event (Customer)- that listens and process event published by producer
This tutorial is based on example developed on default HR schema of Oracle DB 11g
I have created two application and called first one in secod application as region.
  • Create first application using Department table of HR schema and drag Department Name on page fragment, now create publisher event (follow steps) for Department Name .
  • Select Department Name field in structure window and go to property Inspector select ContextualEvent
    click on green add icon and select event type and name for publisher event

  • Select field value from Iterator Binding
  • here you are done with publisher event or producer create a jar of this application in order to use it in second application.
  • Now start Second Application that will handle and process this event, create a page and put an output text and set its value from managed bean


  • Now create a event handler class to process published event and to make it available to page binding level , we have to create DataControl for this class.





  • Right click on event class and Click on CreateDataControl.
  • Now add this method binding to page so that it can be accessible from page binding
  • Now drag and drop taskflow from jar library on page as region by this published event will also be available to page
  • Now go to page binding and goto ContextualEvents tab ,click on subscribers tab and click on add icon this will open a popup window ,click on search button , it will show available publisher event, now select event and goto handler search option and select function that you have previously added in page binding
  • Give consumer parameter name same as event handler function

Now Run your Application - Download ADF Sample Application

Monday 22 October 2012

Refresh Page in Oracle ADF by Java Code, Set partial trigger programmatically

Some times we need to refresh whole page , then we can use this managed bean code to refresh whole page in ADF.


    
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

//Method to reload page
    protected void refreshPage() {

        FacesContext fctx = FacesContext.getCurrentInstance();
        String page = fctx.getViewRoot().getViewId();
        ViewHandler ViewH = fctx.getApplication().getViewHandler();
        UIViewRoot UIV = ViewH.createView(fctx, page);
        UIV.setViewId(page);
        fctx.setViewRoot(UIV);

    }

Partially refresh any UIComponent, Set partial trigger programmatically-




import oracle.adf.view.rich.context.AdfFacesContext;

AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponentBinding);



Monday 15 October 2012

ADF Basics: .jspx and .jsff page in Oracle ADF


.jspx-


.jspx page is JSP/XML representation, it is standalone page means it can run without any supporting or base page.
Jdeveloper 11g Release1 supports .jspx page but  Release2 supports both jspx and Facelets


.jsff page-


.jsff (JSF fragments) page is fragment of JSF(Java Server Faces) page, sometimes pages become to much complex and large and it is not easy to edit those pages, in that case it should be devided in some fragments.
JSF page can be broken in some smaller page fragments to avoid difficulties in editing and maintaining
page fragments can't run independently, it requires a base of .jsf(JSF page) or .jspx (JSP/XML)
page.

Facelets-


Java Server pages(JSP) technology previously used as view declaration for Java Server Faces (JSF) but it doesn't support all the feature of JSF available in JDK1.6 (Java 6), Facelets is introduced under Apche license and default view declaration technology for Java Server Faces.
Facelets supports all the new features introduced in JSF technology, Facelets requires XML document to work
  • Facelets supports HTML and XHTML for designing
  • Fasetr execution than JSP
  • Supports Facelets tab library with JSF and JSTL tag lib

JSP and JSPX-


The main difference I know is that JSP supports HTML and JSPX is XML variant of JSP. .jspx supports more component than jsp page and also compatible with JSF page fragments.