Access to add and change pages is restricted. See: https://cwiki.apache.org/confluence/display/OFBIZ/Wiki+access

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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.

1) Some important files that are responsible for Groovy handling in Ofbiz.
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)

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

 3)  If you add the new "groovy" file then the properties values for groovy files will be as shown below :-
svn:eol-style = native
svn:-mime-type = text/plain
svn:keywords = "Date Rev Author URL Id" 

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

2)
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().

3)
Beanshell :-

Groovy :-

Note :-

4)
Beanshell :-

Groovy :-

Note :-

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

Groovy :-
+context.nowDate = UtilDateTime.nowDate();
 

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

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

Groovy :-
+context.nowTimestampString = UtilDateTime.nowTimestamp().toString();

Note :- No need to specify the return type.

7 )
Beanshell :-

-import org.ofbiz.product.inventory.InventoryWorker;

Groovy :-
+import org.ofbiz.product.inventory.InventoryWorker

Note :-We can remove the semicolon in groovy import syntax.

8 )   

Beanshell :-

Groovy :-

Note :-

 
9)

Beanshell :-

Groovy :-

Note :-

10) Empty Map example. 

Beanshell :-

product = new HashMap(); // Empty map  

Groovy :-

product = [:] ; 

Note :-
 

10) Empty  List example. 

Beanshell :-

products = new ArrayList(); // Empty list  

Groovy :-

products = [] ; 

Note :-
 

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

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

Groovy :-
+while (iter) {

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

12) List example. 

 Beanshell :-

-    if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {

Groovy :-

+    if (invoiceItemTypeOrgs) {

Note :- Not empty list returns true.

13 ) Map Example.

 Beanshell :-

-    if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {

Groovy :-

+    if (invoiceItemTypeOrgs) {

Note :- Not empty Map returns true.

  • No labels