Wednesday, December 22, 2010

ADF Model: Getting attribute values from parent VO to child VO and vice versa using view link accessors

In this post, let us see how to access parent VO attributes from child VO and child VO attributes from parent VO using view link. To have the basic idea about view links and how to create them, you can go through my blog post 'ADF Model: Creating View Link'.

Example Use Case:
For example we have two VOs DeptVO and EmpVO and both are linked via foreign key 'DeptId' using the view link EmpVOToDeptVO. Here, this relationship depicts the parent-child relationship using the foreign key DeptId. In other words, for a given current EmpVO(child) row, I need to know the DeptName from DeptVO(parent). Similarly, for a given current DeptVO(parent) row, I need to know all the empVO(child) rows. Sample application demonstrating this example can be downloaded from here.

Implementation Steps:
1. Create EmpVO and DeptVO and generate RowImpl classes for both of these two VOs.

2. Now, create a new view link say DeptVOToEmpVO between these two VOs via foreign key DeptId.

3. In the view link definition, select options to generate accessors in both source and destination VOs. i.e., in DeptVO and EmpVO.

4. Checking the above options will generate accessor methods in EmpVORowImpl and DeptVORowImpl. The accessor's return type in each VO is based on the type of relationship between the VOs. In other words, as the relationship between Dept and Emp is 1-to-many, the accessor in DeptVORowImpl will return multiple Emp rows(i.e, the return type of the accessor will be RowIterator). And, the accessor in EmpVORowImpl will return a single row (as an employee can be in only one dept).


If you observe the source of EmpVO and DeptVO, you can also see that a tag is added in each of these VOs for the viewLinkAccessor.


5. Now, you can use these accessors to get reference EmpVO from  DeptVO and vice versa. You can also get attribute values from the same. Sample codes below:

Sample method in EmpVORowImpl to get the dept name.
public String getDeptNameViaViewLink() { //Getting reference to deptVO row using the view link accessor getDeptVO1() Row deptRow = this.getDeptVO1(); //Getting the attribute 'Dname' value from the deptRow. return (String)deptRow.getAttribute("Dname"); }

Sample method in DeptVORowImpl to get the list of employees in the dept.
public List getEmpNamesViaViewLink() { //Getting reference to empVO row using the view link accessor getEmpVO() RowIterator empRowIterator = this.getEmpVO(); //Creating an empty List to store all emp names List empNames = new ArrayList(); //iterating through all employee rows while(empRowIterator.hasNext()){ //getting emp row one by one from the iterator Row empRow = empRowIterator.next(); //adding emp name to the empNames list empNames.add(empRow.getAttribute("Ename")); } //returning all empNames corresponding to the current dept return empNames; }

How to call/use these view link accessor methods in AMImpl methods?
This should be now pretty straightforward. Here is the sample AMImpl method which prints emp names in each dept. The code is self-explanatory.
public void sampleMethod() { //getting reference to deptVO ViewObjectImpl deptVO = this.getDeptVO(); //setting range size to -1 to get all dept rows deptVO.setRangeSize(-1); //getting all dept rows Row[] deptRows = deptVO.getAllRowsInRange(); //iterating through all dept rows for (int i = 0; i < deptRows.length; i++) { //getting reference to each dept row. Note that we're type casting the VO reference type to DeptVORowImpl. DeptVORowImpl deptRow = (DeptVORowImpl)deptRows[i]; //printing dept name System.out.println("Employees in dept: " + deptRow.getAttribute("Dname")); //For each dept row, getting reference to empVO which contains all employees corresponding to current dept RowIterator empRows = deptRow.getEmpVO(); //iterating each emp row while (empRows.hasNext()) { Row empRow = empRows.next(); //printing emp name from each row System.out.println(empRow.getAttribute("Ename")); } } }

Here is the sample output in console on running the above AMImpl method.


That's it. Now, you got the idea how to use view link accessors to get the values of child attributes from parent and vice versa. Enjoy!

17 comments:

  1. question:
    Given a ViewLink object obtained from vo.getViewLinks[]

    how could one know the type of cardinality??

    ReplyDelete
  2. Thank you, I was exactly looking for this.

    ReplyDelete
  3. Thanks, This tutorial helps in understanding how view links work.

    ReplyDelete
  4. How can i execute them in JSF Page

    ReplyDelete
  5. I have the same question, how can i access this traversal in JSF Expression language without having to generate java files.

    ReplyDelete
  6. but this logic fails for getting parent vo attributes and setting to child vo attributes.

    ReplyDelete
  7. nice article, dear murali , if i want to get third level records , means third level child??? from its parent of second level, i hve tried you coe and get 2nd level records , now i want to get thord level records , how it is possible ??

    ReplyDelete
  8. I have hindered actually a large number of parents and made that inquiry and the parent will quite often say, "No chance".Britax USA Advocate Car Seat Review

    ReplyDelete
  9. I Have Read Your Article its Amazing Thanks For Sharing Your Knowledge..
    Best Bow Sights

    ReplyDelete
  10. Try not to fool your brain into deduction the kid will do horrible things on the off chance that you don't give them a chance to have what they need.Why father and son should train Karate together

    ReplyDelete
  11. Hi I am using Jdeveloper 12.2.1.2.
    Where I have one parent table and another is child table. While clicking on row of parent table the rows of child table get changed. Now I want to manage the visibility of some columns in child tale using parent table attribute value. I dont want to use custom slectionListener. Without using any problematically java coding I want to check value of selected row attribute from Parrent table on child table. How can I do that.

    Thanks

    ReplyDelete
  12. I honestly appreciate the blog you have posted. Really looking forward to this kind of words.

    ReplyDelete
  13. check over here try here visit this site right here visit here use this link internet

    ReplyDelete
  14. replica bags seoul get redirected here s0t29v1o81 replica bags prada replica bags cheap replica gucci bag t5p88i6s80 9a replica bags replica bags online uae basics k7p18x2o14 replica bags reddit

    ReplyDelete

Related Posts with Thumbnails