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.
In this case we already have a ViewAccessor to get all employees for this department;
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().
If we test the application module we can now see that for each department we will have the AvgSalary;
You need to be careful though, what happen if the Department doesn’t have any employee?
Correct! You need to take all consideration when writing your code. This will solve the problem;
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.