FROM THE BLOG

ADF Tutorial: Transient attributes and how to populate them – Part 1

One of the great advantages of using ADF BC for your data model, is the possibility to use “Transient” attributes in them. A transient attribute means that it is not persistent in data base and it will filled manually or programatically to achieve a certain task within a requirement. For example, a requirement is to display the Average Employee Salary for a Department. If you think touse a backing bean in the viewLayer to achieve this, then you need to read again what is MVC pattern about. We should be able to test our application without using the viewLayer. So, let’s see how can we achieve this using transient attributes in our view objects.

First, you need to create a new attribute for your department view object. Note: the screenshot says Type Number, but it should really be the same type than the Salary in the employee view. In our case this was BigDecimal.

adf_bc_transient_attribute

In this case we already have a ViewAccessor to get all employees for this department;

adf_bc_transient_attribute_accessor

 

Now, we can just go to our newly transient attribute and modify its value to be an Expression. This will be a Groovy Expression which is essentially Java and there is a lot of documentation available – the expression would be EmployeesInThisDep.sum(“Salary”) (which give us the sum of all the attribute “Salary” for this view object) divided by the number of employees in the view using EmployeesInThisDep.getEstimatedRowCount().

adf_bc_transient_attribute_expression

 

If we test the application module we can now see that for each department we will have the AvgSalary;

adf_bc_transient_attribute_amadf_bc_transient_attribute_am_2

 

You need to be careful though, what happen if the Department doesn’t have any employee?

adf_bc_transient_attribute_div_error

Correct! You need to take all consideration when writing your code. This will solve the problem;

adf_bc_transient_attribute_div_error_sol

A question for yourself; are there any consequences of using this approach to avoid the null pointer exception? What happen when there are a lot of employees in the view object?getEstimatedRowCount() is called twice, would this add unnecessary overload to the application? Please feel free to post your thoughts.

In our next post we will show how to fill transient attributes with Java.

 

 

One comment

  1. Gijith

    on

    Reply

    You can write the code in another way as below
    long rowCount=EmployeesInThisDep.getEstimatedRowCount();
    rowCount>0?(EmployeesInThisDep.sum(“Salary”)/rowCount):0

    so that you can avoid the getEstimatedRowCount() calling twice.

Leave a comment

Your email address will not be published. Required fields are marked *

Social Media

Stay up-to-date with our latest ADF and related technical posts via your favourite social network services.