Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This document contains the Code snippet taken from the Commit notification.
Most of tricks on Groovy are taken from the code committed by Scott Gray , Joe Eckard , Marco Risaliti and others.

Info
titleGroovy Features

Important thing to note down here is that we can use the Beanshell files as it is in Groovy but we should go with Goodies provided with Groovy Programing language.Here I am including few code snippet that is taken from Beanshell files and its equivalent Groovy code snippet.

Groovy Tricks    

  1. Start the name of groovy file from capital letter and then follow the camel case pattern.For example "PendingCommunications.groovy".
     The reason behind is shown below (Comments from Joe on Developer Mailing List) :-
    • The main reason for this is that when a script is run without a class declaration, the filename is used to create the class name and you can experience problems with untyped variables. For example, a script named product.groovybecomes class "product", and if the script contains an untyped variable "product", it assumes you're trying to access the class "product" instead of a new variable "product".
    • A secondary reason would be consistency, as some scripts have already been named this way. (EditProductFeatures.groovy, etc. InfotitleGroovy Features

      Important thing to note down here is that we can use the Beanshell files as it is in Groovy but we should go with Goodies provided with Groovy Programing language.Here I am including few code snippet that is taken from Beanshell files and its equivalent Groovy code snippet.

  2. Wiki Markup
    *Primary Key Definition*
    *Beanshell :\-*
    \-productAssoc = delegator.findByPrimaryKey("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", productIdTo, "productAssocTypeId", productAssocTypeId, "fromDate", fromDate)*);*{*}Groovy :\-*
    \+productAssoc = delegator.findByPrimaryKey("ProductAssoc", \['productId' : productId, 'productIdTo' : productIdTo, 'productAssocTypeId' : productAssocTypeId, 'fromDate' : fromDate*\]);\*
    \\
    *Note :\-* Removed the usage of UtilMis.toMap(). Instead of findByPrimaryKey try to use findOne()*.*
  3. Null check
    Beanshell :-
      -    if (payment == null) continue;
    Groovy :-
    +    if (!payment) continue;
  4. Null + Size greater then Zero Check
    Beanshell :-
    -        if (glAccounts != null && glAccounts.size() > 0) {
    Groovy :-
    +        if (glAccounts*) {*
  5. One line assignment
    Beanshell :- 
    -nowDate = UtilDateTime.nowDate();
    -context.put("nowDate", nowDate);

...

  1. Groovy :-

...

  1. +context.nowDate = UtilDateTime.nowDate();

...

  1.  Note :- Sentence can be kept in single line with the usage of DOT (.) for putting some values in context.
  2. Beanshell :-
    - String nowTimestampString = UtilDateTime.nowTimestamp().toString();
    Groovy :-
    + context.nowTimestampString = UtilDateTime.nowTimestamp().toString();
    Note :- No need to specify the object type*.*
  3. No need to specify semicolon
    Beanshell :-
    -import org.ofbiz.product.inventory.InventoryWorker;
    Groovy :-
    +import org.ofbiz.product.inventory.InventoryWorker
    Note :- We can remove the semicolon in groovy import syntax. Always import the files that are being used in Groovy files instead of importing all the files from the package. 
  4. Beanshell :-
    List allTypes = new LinkedList();
    - i = invoiceItemTypes.iterator();
    - while ( i ) {
    -    GenericValue invoiceItemType = i.next();
    Groovy :-
    +invoiceItemTypes.each {
    +    GenericValue invoiceItemType = it;
    or
      +invoiceItemTypes.each { invoiceItemType ->
  5. Wiki Markup
    *Beanshell :\-*
    \-    invoiceAppls = delegator.findByAnd("PaymentApplication", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", null)); 
    \\
    *Groovy :\-*
    +    invoiceAppls = delegator.findByAnd("PaymentApplication", \[invoiceId : invoiceId, invoiceItemSeqId : null\]);
    \\
    *Note :\-* Instead of findByAnd() use findList(). 
  6. Wiki Markup
    *Empty Map example*
    *Beanshell :\-*
    product = new HashMap(); // Empty map  
    \\
    *Groovy :\-*
    product = \[:\] ;
  7. Wiki Markup
    *Empty  List example*
    *Beanshell :\-*
    products = new ArrayList(); // Empty list  
    \\
    *Groovy :\-*
    products = \[\] ; 
    \\
    This is pretty cool, groovy coerces objects into booleans:
    An empty string,list,map = false otherwise true
    An iterator with no more elements = false otherwise true
    null = false
  8. Beanshell :-
    -while (iter.hasNext()) {
    Groovy :-
    +while (iter) {
    Note :- Another alternate of while statement is the usage of "each" on the list values.
  9. List example
    Beanshell :-
    -    if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
    Groovy :-
    +    if (invoiceItemTypeOrgs) {
    Note :- Not empty list returns true.
  10. Map Example.
    Beanshell :-
    -    if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
    Groovy :-
    +    if (invoiceItemTypeOrgs) {
    Note :- Not empty Map returns true.
  11. String Example
    Beanshell :-
    -if (paymentId != null) {
    Groovy :-
    +if (paymentId) {
    Note :- Not empty String returns true. 
  12. Beanshell :-
    invoiceId = parameters.get("invoiceId");
    Groovy :-
    invoiceId = parameters.invoiceId; 
    Note :- The value coming from parameters map should be considered as String.Other types should be explicitly specified. 
  13. Elvis Operator :- If any string return empty or null value then we can put Default value with the help of Elvis Operator ( ?: ) .Its short form of Java Ternary Operator.
    Beanshell :-
    -    invoiceType = parameters.get("invoiceTypeId");
    -       if (invoiceType == null) invoiceType = "ANY";
    Groovy :-
    -    invoiceType = parameters.invoiceTypeId ?: "ANY" ;
  14. Some important files that are responsible for Groovy handling in OfbizOFBiz.
    a) GroovyUtil.java
    b) GroovyServiceTest.groovy
    c)  GroovyEngine.java
    d) ModelFormAction.java
    e) Some *.jar files that are responsible to run the Groovy scripts are shown below.
       ofbiz/trunk/framework/base/lib/scripting/antlr-2.7.6.jar   (with props)
       ofbiz/trunk/framework/base/lib/scripting/asm-2.2.jar   (with props)
       ofbiz/trunk/framework/base/lib/scripting/asm-analysis-2.2.jar   (with props)
       ofbiz/trunk/framework/base/lib/scripting/asm-tree-2.2.jar   (with props)
       ofbiz/trunk/framework/base/lib/scripting/asm-util-2.2.jar   (with props)
       ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar   (with props)