Groovy Samples (attribute validators,transient attribute val
?
Groovy Samples (attribute validators,transient attribute value calculations,bind variable and attribute default values )
?
In this post lets have Groovy samples. Groovy is a scripting language for the Java platform with Java-like syntax. The Groovy language simplifies the authoring of code by employing dot-separated notation yet still support syntax to manipulate collections,Strings,and JavaBeans. ADF Business Components supports the use of Groovy language in places where access to entity object and view object attributes is useful,
Additionally,ADF Business Components provides a limited set of builtin keywords that can be used in Groovy expressions. article by steve http://radio-weblogs.com/0118231/2007/05/22.html in addition to that,doc as detailed info? http://download.oracle.com/docs/cd/E15523_01/web.1111/b31974/bcintro.htm#CEGJJJBA adf.context adf.error adf.userSession - returns a reference to the ADF Business Components user session adf.currentDate adf.currentDateTime
adf.object
1) adf.object
?
Entity object attributes: reference any methods defined by the base implementation class as specified by the JavaDoc for EntityImpl,and you can reference the attributes of the entity instance.
Entity object script validation rules: The context is the validator object (JboValidatorContext) merged with the entity on which the validator is applied.?
Bind variable in view objects: You can reference the structureDef property to access other information as well as the viewObject property to access the view object in which the bind variable participates. However,access to view object attributes is not supported.
Bind variable in view accessors: The view accessor can derive Groovy-driven values from the current view row in the view?accessors?view object used to formulate the list of valid choices.
Example1:
Transient attributes:
Note : On Java Based Calculation,you need to add the calculation logic in transient attribute method which gets exposed into entity implementation class ( there is no need to specify any data into Literal or Expression field in entity attribute wizard ),if calculation logic is added in your custom method ( not transient exposed method ) in entity impl class then you explicitly need to call your custom method in expression field as "adf.object.mycustomcalculationmethod()" in entity wizard. Note : when you want to reference the method of an entity implementation class in a validation rule (> or < or etc..),you use the source prefix instead object: source.getDefaultSalaryForGrade() Use of the source prefix is necessary in validators because the object keyword implies the validation rule object instead of the entity object (where the method is defined). To allow you to reference members of the validator object (JboValidatorContext),you can use these keywords in your validation rule expression:
For example,you might use the following expression to specify a dynamic validation rule check of the salary for a salesman. if (Job == "SALESMAN"){ return newValue < source.getMaxSalaryForGrade(Job) } else return true Aggregate functions with groovy
You can use the following built-in aggregate functions on Oracle Business Components RowSet objects:
Only VO
For example,in a Dept entity object you could add a transient attribute that displays the sum of all employee salaries that is calculated by this expression: EmployeesInDept.sum("Sal")Or,assume that you want the calculation of the salary total for specific departments to include each employee's benefits package,which varies with job role: EmployeesInDept.sum("Sal + adf.object.getBenefitsValue(Job)")Create DepartmentVO Create EmployeesVO Create ViewLink b/w DepartmentVO and EmployeesVO selecting DepartmentId. add expression adf.object.Employees5.getRowSet().sum("(Salary > 2000 ? Salary : 0)") on SUMVO transiant attributed created in DepartmentVO ( this sums up salary > 2000 per department ) that's it. EO + VO Here is some tips from frank on how to use above. create a master/detail relation between Departments and Employees using the HR schema - For the Departments VO,create a RowImpl class from the Java section - create an attribute SumFromEO in the Departments EO - in the DepartmentsVO,create a new attribute from EO pointing to SumFromEO - Also,in the VO,create a new attribute SumFromVO - make sure the attribue is transiesnt - set the created attribute on the VO "SumFromVO" and "SumFromEO" to use an Expression - Add the following Groovy expression to the SumFromEO transient attribute on the EO adf.object.Employees1.sum("Salary"); - Add the following Groovy expression to the SumFromVO transient attribute on the VO adf.object.EmployeesView.sum("Salary"); When you run this then both,the EO based VO attribute and the VO attribute should show the same value groovy script code snipts Example 1: String acctnumber = newValue; sumofdigits = 0; digit = 0; addend = 0; timesTwo = false; range = acctnumber.length()-1..0 range.each {i -> digit = Integer.parseInt (acctnumber.substring (i,i + 1)); if (timesTwo) { addend = digit * 2; if (addend > 9) { addend -= 9; } } else { addend = digit; } sumofdigits += addend; timesTwo = !timesTwo; } modulus = sumofdigits % 10; return modulus == 0; Example 2: newValue <= (new java.sql.Timestamp(System.currentTimeMillis()) Back to OTN
?
?
?
文章链接:
http://oracle.mobi/quickPage.html?page=23199&content=40163930&pageNum=-1
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |