Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label ViewObject. Show all posts
Showing posts with label ViewObject. Show all posts

Tuesday 10 June 2014

Export ViewObject dataset to XML, Generate customized XML file using ViewObject in Oracle ADF

Hello All,
this post is about exporting a viewObject data to a XML document.
Sometimes we need to generate XML document with the same data in ViewObject, for this ADF provides a facility to directly export data to a XML document using ViewObjectImpl class

See the steps to generate XML for a ViewObject

  • Create a Fusion Web application and model using HR schema (Departments & Employees) table (master detail relation using viewLink)


  • Now to generate XML, i have used writeXML method of ViewObjectImpl class, it produce XML using two parameters



  • from oracle docs-
    writeXML(int depthCount, long options)
    here depthCount - no. of ViewLink levels that should be traversed to produce XML
    options- how many rows you want to export, It can be set any of flags given below
    XMLInterface.XML_OPT_ALL_ROWS
    Includes all rows in the view object's row set in the XML.

    XMLInterface.XML_OPT_LIMIT_RANGE
    Includes only the rows in the current range in the XML.

  • created a method in DepartmentsVOImpl class to export data to XML, added it to client Interface

  •     /**Method to generate XML from ViewObject data
         * @param level
         * @return
         */
        public String writeVoToXml(int level) {
            FileOutputStream out;
            ByteArrayOutputStream opStream = new ByteArrayOutputStream();
            try {
                // Generating XML for All rows and adding it to Output Stream
                ((XMLNode) this.writeXML(level, XMLInterface.XML_OPT_ALL_ROWS)).print(opStream);
                System.out.println(opStream);
                // Creating a XML document in D Drive
                out = new FileOutputStream("D://Departments.xml");
                out.close();
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    
            return opStream.toString();
    
        }
    


  • Now run application module to execute method and see generated XML on console

  • BC4J Tester

    ViewObject XML

  • i have created a simple page with a af:codeEditor to show generated XML , button to generate XML and a spinner to pass depth level of viewLink accessor

  • In case depth level is '0' , it export data only for Departments viewObject

    Change level to '1' , Now it generate XML for Departments --> Employess relation


  • now to how to customize XML ? How to change default tags for attribute names and rows ?
  • To change attribute label (tag in xml)- Suppose i have to change DepartmentName to Name in XML
  • Add attribute level custom property named XML_ELEMENT to a value AnyOtherName to change the XML element name used for that attribute


  • To change Row label (tag in xml)- Suppose i have to change DepartmentsVORow to DepartmentLine in XML
  • Add ViewObject level custom property named XML_ROW_ELEMENT to a value AnyOtherName to change the XML element name used for that Row


  • Now Run and see generated XML -
Cheers :-) Happy Learning

Friday 2 May 2014

Pre-populate selected values in viewObject based shuttle component (multiple selection)- Oracle ADF

Hello All,
This post talks about a common development requirement
while using shuttle component sometimes we need to show some values on selected side of af:selectManyShuttle , so to do this normally we use a custom shuttle component that is populated from managed bean code
but when i have all data in a viewObject so why should i iterate viewObject to get all data in managed bean and then set some values in an arrayList to populate selected shuttle values.
it is time consuming and requires lot of coding, so for viewObject based shuttle we can do it in efficient way.

  • In this example I'm using Departments table of HR schema to populate af:selectManyShuttle, i think you all know how to create a shuttle in ADF
  • So next is to populate selected shuttle values on some event, created a fusion web application using Departments table

  • It's time to drop Departments ViewObject on page as shuttle component

  • <af:selectManyShuttle value="#{bindings.Departments1.inputValue}" label="#{bindings.Departments1.label}"
                                          id="sms1" size="15" binding="#{pageFlowScope.ShuttleSelectBean.shuttleBind}"
                                          partialTriggers="b1">
                        <f:selectItems value="#{bindings.Departments1.items}" id="si1"/>
                        <f:validator binding="#{bindings.Departments1.validator}"/>
                    </af:selectManyShuttle>
    

  • So suppose on a button click i want to set selected values in shuttle, so i have created a actionEvent for button in managed bean and binding of af:selectManyShuttle, so basic structure of page is like this




  • See the code of actionEvent , create an arrayList and add all values in it that you want to set as selected values and then just set the value in shuttle using binding

  •     /**Method to set selected values in af:selectManyShuttle
         * @param actionEvent
         */
        public void selectValuesShuttle(ActionEvent actionEvent) {
            DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
            ViewObject vo = iter.getViewObject();
            RowSetIterator rsi = vo.createRowSetIterator(null);
            ArrayList listVal = new ArrayList(20);
    
            while (rsi.hasNext()) {
                Row nextRow = rsi.next();
                if (nextRow.getAttribute("DepartmentId") != null) {
                    listVal.add(nextRow.getAttribute("DepartmentId"));
                }
            }
            shuttleBind.resetValue();// Reset Shuttle Component
            System.out.println("Setting values -" + listVal);
            shuttleBind.setValue(listVal.toArray()); // Setting selected values 
            AdfFacesContext.getCurrentInstance().addPartialTarget(shuttleBind);
        }
    

  • Now run this page and test your shuttle component



  • Cheers- Happy Coding

  • But this method works only when you have to set values on some action or other event after page load, because before page load , binding of shuttle component will not be accessible
  • So if you have a requirement of setting selected values on page load then you must use a list defined in managed bean and bind it to  value property of shuttle component, value property denotes selected values of shuttle 


  • And just write same code on getter of List, in this case selected values in shuttle are populated on page load

  •     List pageLoad = new ArrayList(20);
        public void setPageLoad(List pageLoad) {
            this.pageLoad = pageLoad;
        }
    
        public List getPageLoad() {
            DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
            ViewObject vo = iter.getViewObject();
            RowSetIterator rsi = vo.createRowSetIterator(null);
            while (rsi.hasNext()) {
                Row nextRow = rsi.next();
                if (nextRow.getAttribute("DepartmentId") != null) {
                    pageLoad.add(nextRow.getAttribute("DepartmentId"));
                }
            }
            return pageLoad;
        }
    

    Cheers- Use both method as per your requirement :-) Download Sample App

Thursday 17 April 2014

ViewObject Validation rules for transient attributes (Model level validation in ADF)

Hello All,
this post is about model level validation for transient attributes, often we need to apply some common types of validation on transient attributes (as for negative value, for email, for phone number)
in ADF ViewObject there is a editor for validation rules for transient attributes, so now see how to use that editor and rules


  • Create a fusion web application and prepare model layer using Departments table of HR schema


  • Then create a transient attribute in viewObject and you can see that Validation Rules tab appears only in case of transient attribute



  • Now click on add icon to add new validation rule for attribute, you can see there are multiple types for that validation rule 





  • In this post i have used Compare and Regular Expression rules, suppose i have to check that value of transient attribute must not be 100 , so for that rule will be like this, and write a proper message for failure handling 



  • Now run your BC4J tester and check , a window with error message appears there in case of failure


  • You can also check this validation on your page, some more- i have added one more transient field for Email Address and applied a regular expression to validate this field (ADF by default provides regular expression for Email and Phone Number pattern for others you can use your own pattern)


  • After model level configurations , i have dropped Departments ViewObject on page as ADF form, here you can see both validation are working 




Cheers :-) Happy Learning

Monday 7 April 2014

Get updated model values in value change listener instantly, ProcessUpdates in ADF (a beauty)

Hello All,

This posts talks about updating model instantly when a attribute is changed on page level.
In ValueChangeListener of component if we try to get new value from Model layer, it always returns old value, then we have only one option to get new value, use event object (evt.getNewValue();)
but sometimes we need to update model same time
So how to do this-

  • Created a Fusion Web Application using Departments table of HR Schema (Oracle)


  • Dropped Departments Vo on page as a form, and created ValueChangeListener on DepartmnetName field to get new value 





  • Now i have written a code to get DepartmentName from current row of Departments VO (ViewObject) and to get from ValueChangeEvent

  • Method in AMImpl- Called in ValueChangeListener through binding Layer

    /**Method to print Department Name */
        public void getDeptNameAction() {
            ViewObject depart = this.getDepartments1();
            Row curRow = depart.getCurrentRow();
            if (curRow != null) {
                System.out.println("Current Department from model is-" + curRow.getAttribute("DepartmentName"));
            }
        }
    


    Code in ValueChangeListener - AM Method Called and Code to get new value from event itself

        /**Value change listener for Department Name
         * @param vce
         */
        public void deptNmVCE(ValueChangeEvent vce) {
            if (vce.getNewValue() != null) {
                System.out.println("New Value in VCE-" + vce.getNewValue());
                BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
                OperationBinding ob = bc.getOperationBinding("getDeptNameAction");
                ob.execute();
            }
        }
    

  • In this case when i have changed value on page 



  • see the result -output from valuechangelistener- Model layer returns old value :-(


  • Now to update value in Model layer , i have just called processUpdates in ValueChangeListener- See the code

  •     /**Value change listener for Department Name
         * @param vce
         */
        public void deptNmVCE(ValueChangeEvent vce) {
            if (vce.getNewValue() != null) {
                //Method to update Model in VCE
                vce.getComponent().processUpdates(FacesContext.getCurrentInstance());
                System.out.println("New Value in VCE-" + vce.getNewValue());
                BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
                OperationBinding ob = bc.getOperationBinding("getDeptNameAction");
                ob.execute();
            }
        }
    

  • see the result -Value is updated in model instantly (in VCE)

so this is how processUpdates works, Cheers :-)
See one more post on this concept with different usecase- Update Model Values while Validation (Exception) occurs on page on Value Change event
Happy Learning

Wednesday 2 April 2014

Performing Partial Rollback (Undo Changes) operation in ADF, Stay on current row after rollback

Hello All ,
This post talks about a common requirement of using partial rollback in ADF

Suppose there is two tables on page Departments and Employees, and i have changed one row in Departments table and same time created a row in Employees table, now i want to rollback the changes done in Departments table only

In this case if i use default Rollback operation then it will not only undo the changes of Department table but also remove the newly created row of Employees table
but this was not my purpose.

So to do this kind of things we can use partial rollback operation
  • I have created a fusion web application (Model & VC) using Departments & Employees Table of Oracle's default HR Schema

  • Now drop departments & employees VO on page with it's default operations (CreateInsert, Delete, Execute, Commit & Rollback) and a button to execute partial rollback of departments ViewObject


  • Here in this example i am creating partial rollback for Departments VO only, so to do this add a new transient attribute in Departments Vo to get current state of each row




  • How to get state of each row , in RowImpl class of departments ViewObject , write this code in getter of transient attribute or see my previous blog-post Identifying Modified/newely added row in af:table, get all modified rows of viewobject in bean

  •     /**
         * Gets the attribute value for the calculated attribute RowStatusTrans.
         * @return the RowStatusTrans
         */
        public Integer getRowStatusTrans() {
            /*here row is reference variable of collection, this expression returns an int value if it is
             2-Modified
             0-New
             1-Unmodified
            -1-Initialized
            */
            byte entityState = this.getEntity(0).getEntityState();
            return new Integer(entityState);
        }
    

  • Have created a method to remove newly added row , and to undo changes in existing rows of departments VO in Impl class

  •     /**Method to revert changes of current row
         * @param curRow
         */
        public void revertChangesCurrentRow(Row curRow) {
            if (curRow != null) {
                curRow.refresh(Row.REFRESH_UNDO_CHANGES | Row.REFRESH_WITH_DB_FORGET_CHANGES);
            }
        }
    
        /**Method to check whether row should be removed or not 
         * If it is new - removed
         * If old one- Undo Changes
         * */
        public void revertOrremoveRowValues() {
            ViewObject deptVo = this;
            RowSetIterator deptIter = deptVo.createRowSetIterator(null);
            while (deptIter.hasNext()) {
                Row nextRow = deptIter.next();
                if (nextRow.getAttribute("RowStatusTrans") != null) {
                    Integer rowStatus = (Integer) nextRow.getAttribute("RowStatusTrans");
                    if (rowStatus == 2) {
                        System.out.println("Modified Rows-" + nextRow.getAttribute("DepartmentId"));
                        revertChangesCurrentRow(nextRow);
                    } else if (rowStatus == 0) {
                        System.out.println("New Row Removed");
                        nextRow.remove();
                    }
                }
            }
            this.executeQuery();
        }
    

  • To read more about REFRESH_UNDO_CHANGES and other constants -http://docs.oracle.com/cd/B14099_19/web.1012/b14022/oracle/jbo/Row.html
  • Exposed this method to client and added it to page bindings then called it on Partial Rollback button


  • After running application, i have changed some rows of Departments table and created a new row in employees table


  • Now if i use rollback, it will also remove the new row of employees table, this is the dis-advantage of using rollback


  • Again i have changed some rows of departments table and created a new row in employees table


  • Now see when i click on partial rollback button, it will only undo changes done in department table and employee table is untouched


  • And to stay on current row after partial rollback operation just remove executeQuery from revertOrremoveRowValues() method
Download Sample App
Cheers :-)  Happy Learning

Monday 24 March 2014

Iterating ViewObject vertically (get all attributes of ViewObject) programmatically

Hello All,
This post talks about a requirement of getting all attributes (column names) of a viewObject programmatically

  • Create a fusion web application and business component using department table

  • you can see all attributes in  Departments entity object

  • Now to get ViewObject's attributes name , there is a method written in AMImpl class , see it, i have added a facesMessage to show all names on a Message Board



  •     /**Method to get all attributes of a viewObject
         * */
        public void iterateVoVertically() {
            ViewObjectImpl vo = this.getDepartments1();
            ViewAttributeDefImpl[] attrDefs = vo.getViewAttributeDefImpls();
            int count = 0;
            StringBuilder infoMsg =
                new StringBuilder("<html><body><b><p style='color:red'> Attribute of Department ViewObject are-</p></b>");
            infoMsg.append("<ul>");
    
    
            for (ViewAttributeDefImpl attrDef : attrDefs) {
                byte attrKind =
                    attrDefs[count].getAttributeKind(); //checks attribute kind for each element in an array of AttributeDefs
                if (attrKind != AttributeDef.ATTR_ASSOCIATED_ROW && attrKind != AttributeDef.ATTR_ASSOCIATED_ROWITERATOR) {
                    String columnName = attrDef.getName();
                    infoMsg.append("<li> <b>" + attrDef.getName() + "</b></li>");
                    System.out.println("Column Name-" + columnName);
                }
            }
            infoMsg.append("</ul><br>");
            infoMsg.append("</body></html>");
            FacesMessage msg = new FacesMessage(infoMsg.toString());
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    

  • I have called this method through page binding and AM Client on a button of page

  • now on click of button all attributes of Departments ViewObject are shown in FacesMessage, you can use it in your code
 Cheers :-)

Wednesday 5 March 2014

Igonre null values in viewCriteria -Jdeveloper 12c (Not a bug but a change)

Hello All,
In jdev 12c there is some change in viewCriteria design window, there is a checkbox to ignore null values in criteria , in 11g release it was enabled for all values in criteria item

but in 12c it is disabled for all values of ViewCriteria bind variable


there is two different section for creating bind variable in 12c
1. ViewCriteria Bind Variable
2. Query Bind Variable(Required)
So when you create a bind var of string type in query variables.




and use it in viewCriteria , only then this 'ignore null values' checkbox will be enabled


and when you use viewCriteria bind varibles it is disabled  but sometimes you need to change it, so can change it using ViewObject xml source

  • Select ViewCriteria and go to source
  • Now change GenerateIsNullClauseForBindVars="false"  to true

 Cheers :-)