Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用Groovy表达式的应用(一):统计VO上金额的值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

客户提出了一个要求:统计查询出来的结果集中金额字段,并显示在RichTable的状态栏上。

 

按照以往的做法,会通过sql做sum统计

 

印象中记得前几天有看到过可以使用Groovy表达式进行字段的计算,今天就姑且试一试。从查询到的资料看出,对于RowSet对象,有如下内置的统计函数可用:

  • rowSetAttr.sum(GroovyExpr)

  • rowSetAttr.count(GroovyExpr)

  • rowSetAttr.avg(GroovyExpr)

  • rowSetAttr.min(GroovyExpr)

  • rowSetAttr.max(GroovyExpr)

  •  

    并且在官网也提供了样例:

    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")
    详细资料可参

     

     

    http://download.oracle.com/docs/cd/E15523_01/web.1111/b31974/bcintro.htm

     

    但是如何将这种表达式应用到实际需求来,着实让我费了一番脑筋。 

    首先在对应的VO上添加一个用于计算金额的字段,假如我需要对ExpSpeActreqPayoffVO的数据集进行金额字段Expenseamount

    统计,则在ExpSpeActreqPayoffVO上创建对应的属性,步骤如下:

    1、通过Application Navigator, 双击打开名为ExpSpeActreqPayoffVO的view object。

    2、在overview 编辑框里,打开Attribute属性页,并点击创建新属性按钮,创建Transient 属性

    3、在弹出的新属性按钮框里,输入属性名为:SumExpenseamount,并选择类型为number

    4、修改属性的值value,把value type选择为Expression

    5、在value的对话框里键入如下表达式:adf.object.getRowSet().sum("Expenseamount")

     

    在步骤5时,一度不能确认表达式该如何写,到底是adf.object.ExpSpeActreqPayoffVO.getRowSet().sum("Expenseamount")或者是其他,当填入值为adf.object.ExpSpeActreqPayoffVO.getRowSet().sum("Expenseamount"),通过TesterExpSpeActreqPayoffVO对象,后来姑且把ExpSpeActreqPayoffVO去掉后,表达式改成adf.object.getRowSet().sum("Expenseamount"),测试结果才是正确的,因此确认EL表达式应该如此编写。

     

     

    那么为何不需要指定具体的VO名称就可以获取到正确的RowSet呢?通过研读以下的几段话可得到结论:

    adf.object - to reference the object on which the expression is being applied (which can also be referenced using the keyword object,without the adf prefiX). Other accessible member names come from the context in which the Groovy script is applied.

     

      --Transient attributes: The context is the current entity or view row. You can reference attributes by name in the entity or view row in which the attribute appears,as well as public methods on that entity or view row. To access methods on the current object,you must use the object keyword to reference the current object (for example,object.methodName( )). The object keyword is equivalent to the this keyword in Java. Without it,in transient expressions,the method will be assumed to exist on the dynamically compiled Groovy script object itself.

     

    由此可见,当在VO上应用了adf.object之后,就意味着可以通过adf.object获得VO上的任意类型的对象,比如属性、RowSet等,在访问VO上的属性时,直接通过属性名访问即可,同样的,访问自身的RowSet也不需要指定名称,因此adf.object.getRowSet().sum("Expenseamount")才有可能获取到最终的结果。对于访问View Accessors的RowSet,则访问方式为:adf.object.ExpSpeActreqHeadVO1.getRowSet().sum("Attributename")

     

     

    在实际操作中,可能需要同时对两个以上的字段进行统计,比如同时计算Expenseamount和ItemQuantity两个字段,可类似以下进行操作进行加减乘除:

  • adf.object.getRowSet().sum("Expenseamount+ItemQuantity")

  • adf.object.getRowSet().sum("Expenseamount-ItemQuantity")

  • adf.object.getRowSet().sum("Expenseamount*ItemQuantity")

  • 大佬总结

    以上是大佬教程为你收集整理的使用Groovy表达式的应用(一):统计VO上金额的值全部内容,希望文章能够帮你解决使用Groovy表达式的应用(一):统计VO上金额的值所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。