Groovy Goodies
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.
2.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);
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 object type.
7.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.
8.Beanshell :-
List allTypes = new LinkedList();
- i = invoiceItemTypes.iterator();
- while ( i ) {
- GenericValue invoiceItemType = i.next();
Groovy :-
+invoiceItemTypes.each {
+ GenericValue invoiceItemType = it;
or
+invoiceItemTypes.each { invoiceItemType ->
9.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().
10.Empty Map example
Beanshell :-
product = new HashMap(); // Empty map
Groovy :-
product = [:] ;
11.Empty List example
Beanshell :-
products = new ArrayList(); // Empty list
Groovy :-
products = [] ;
Important 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
12.Beanshell :-
-while (iter.hasNext()) {
Groovy :-
+while (iter) {
Note :- Another alternate of while statement is the usage of "each" on the list values.
13.List example
Beanshell :-
- if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
Groovy :-
+ if (invoiceItemTypeOrgs) {
Note :- Not empty list returns true.
14.Map Example.
Beanshell :-
- if (UtilValidate.isNotEmpty(invoiceItemTypeOrgs)) {
Groovy :-
+ if (invoiceItemTypeOrgs) {
Note :- Not empty Map returns true.
15.String Example
Beanshell :-
-if (paymentId != null) {
Groovy :-
+if (paymentId) {
Note :- Not empty String returns true.
16.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.
17.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" ;
18.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)
分享到:
相关推荐
### OFBiz 入门操作手册知识点详解 #### 一、OFBiz简介与背景 - **OFBiz**(Open For Business)是一款开源的企业级电子商务框架,主要用于构建和部署各种类型的业务应用,包括电子商务、供应链管理等。 - **目标...
OFBiz-manual-zh.docOFBiz-manual-zh.docOFBiz-manual-zh.docOFBiz-manual-zh.docOFBiz-manual-zh.doc
【Ofbiz学习详解】 Ofbiz,全称Open For Business Project,是一个开源的企业级应用框架,由David E. Jones设计,其目标是提供一个全面的、可扩展的企业应用程序解决方案。Ofbiz以其面向业务的特性,与许多传统的...
Apache Ofbiz是一个开源的企业应用程序框架,它主要用于构建和管理电子商务网站和企业级业务应用。这个压缩包文件"Ofbiz-16-全量数据库873张表.rar"包含了Apache Ofbiz 16版本的全量数据库结构,包括873个数据表的...
最新版OFBiz,apache-ofbiz-16.11.05,apache-ofbiz-16.11.05
OFBiz API 文档,英文html版。使用官方资源中的ant命令自动生成,无任何修改。有兴趣的朋友可以自己生成,ant命令是:docs-all,该命令生成帮助文档时会获取操作系统语言设置,要生成英文文档请先将操作系统语言改为...
通过本教程的学习,您可以了解到OFBiz的基本概念、架构以及如何开发基于OFBiz的应用程序。 #### 二、目标读者 本教程特别适合那些刚开始学习OFBiz的开发人员,无论是希望快速上手还是深入研究的用户都可以从中获益...
文档中提供的"新建 Microsoft Word 文档 (2).docx"和"新建 Microsoft Word 文档.docx"可能是详细的学习笔记或者教程,涵盖了Ofbiz的基础概念、配置步骤、实例分析等内容。建议首先阅读这些文档,以便对Ofbiz有一个...
apache-ofbiz-16.11.02.zip,ofbiz菜鸟笔记,Apache+OFBiz+开发初学者指南.chm
Ofbiz 是一个开源的商务软件系统,它集成了多种优秀的开源项目,如 Tomcat、Ant、BeanShell 和 JBoss,构建了一个强大且灵活的平台。Ofbiz 专注于提供商务应用程序所需的常见组件,如用户认证、工作流程和商业规则...
CVE-2020-9496 ofbiz反序列化漏洞分析 OFBiz是一个非常著名的电子商务平台,是一个非常著名的开源项目,提供了创建基于最新J2EE/XML规范和技术标准,构建大中型企业级、跨平台、跨数据库、跨应用服务器的多层、...
- **第二章:OFBiz实战操作**(Chapter2: Working with OFBiz) - **添加首个字段**:通过修改实体定义文件实现。 - **更改数据**:演示如何更新数据库记录。 - **编辑用户界面**:介绍如何调整前端显示样式。 -...
在OFBiz学习过程中,了解其URL处理机制以及Screen配置是非常基础且重要的环节。OFBiz是一个开源的企业级应用框架,主要用于构建复杂的企业系统。本篇笔记将深入探讨两种URL的输入方式以及Screen的作用。 首先,我们...
总之,OFBiz是一个功能强大的企业级应用框架,学习OFBiz不仅能够提升你的Java开发技能,还能让你掌握复杂的业务系统构建。通过深入源码、熟练工具使用和实战练习,你将逐渐成为OFBiz开发的专家。
OFBiz XXE Apache OFBiz < 16.11.04 的 XXE 注入(文件泄露)漏洞利用 信息 16.11.04 版本之前的 Apache OFBiz 包含两个不同的 XXE 注入漏洞。 每个漏洞的公开披露可以在下面找到: [1] [2] 此漏洞利用针对链接 1...