Versions Compared

Key

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

Please Don't consider this document as the final one.Currently I am working on it.
 

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.

...

Important thing to note down in Groovy conversion is that we can use the whole bsh file as it is with groovy extension.
Following points are the features of Groovy scripting language.
The following sentence can be used instead of Beanshell one (Also including the contents of Beanshell statement to better understand the code).

24)
Beanshell :-
-productAssoc = delegator.findByPrimaryKey("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", productIdTo, "productAssocTypeId", productAssocTypeId, "fromDate", fromDate));

...

Note :- Removed the usage of UtilMis.toMap(). Instead of findByPrimaryKey try to use findOne().

5) Null check3)
Beanshell :-
  -    if (payment == null) continue;

Groovy :-
+    if (!payment) continue;

Note :- NOT (!) can be used for null check.

64)
Beanshell :-

-        if (glAccounts != null && glAccounts.size() > 0) {

Groovy :-

+        if (glAccounts) {

Note :-

57)
Beanshell :- 
-nowDate = UtilDateTime.nowDate();
-context.put("nowDate", nowDate);

...

Note :- Sentence can be kept in single line with the usage of DOT (.) for putting some values in context.

68)
Beanshell :-
-String nowTimestampString = UtilDateTime.nowTimestamp().toString();

...

Note :- No need to specify the return type.

7 9 )
Beanshell :-

-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.

8 10 )   

Beanshell :-

 List allTypes = new LinkedList();
-i = invoiceItemTypes.iterator();
-while(info) {
-    GenericValue invoiceItemType = i.next();

Groovy :-

+invoiceItemTypes.each {
+    GenericValue invoiceItemType = it;

or

  +invoiceItemTypes.each { invoiceItemType ->

Note :-

 
9 11 )
Beanshell :-

-    invoiceAppls = delegator.findByAnd("PaymentApplication", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", null)); 

Groovy :-

unmigrated-wiki-markup
+    invoiceAppls = delegator.findByAnd("PaymentApplication", \[invoiceId : invoiceId, invoiceItemSeqId : null\]);

Note :-
Instead of findByAnd() use findList().

1210) Empty Map example. 

Beanshell :-

...

Wiki Markup
product = \[:\] ; 

Note :-
 

1013 ) Empty  List example. 

Beanshell :-

...

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

1114)
Beanshell :-
-while (iter.hasNext()) {

...

Note :- Another alternate of while statement is the usage of "each" on the list values.

1215) List example. 

 Beanshell :-

...

Note :- Not empty list returns true.

13 16 ) Map Example.

 Beanshell :-

...

+    if (invoiceItemTypeOrgs) {

Note :- Not empty Map returns true.

17 ) String Example.
 Beanshell :-
-if (paymentId != null) {

Groovy :-
+if (paymentId) {

Note :- Not empty String returns true.

18)

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.

19) Elvis Operator :- If any string return empty or null value then we can put Default value with the help of Elvis Operator (?(smile).Its short form of Java Ternary Operator.

Beanshell :-

-    invoiceType = parameters.get("invoiceTypeId");

  -        if (invoiceType == null) invoiceType = "ANY";

 Groovy :-

  -    invoiceType = parameters.invoiceTypeId ?: "ANY" ;