Difference between revisions of "ZH/Case-Study-01-Journal-31"
From ADempiere
This Wiki is read-only for reference purposes to avoid broken links.
(→Rule Engine下写脚本) |
(→链接) |
||
Line 44: | Line 44: | ||
javax.swing.JOptionPane.showMessageDialog(null,"Doing something in Callout!"); | javax.swing.JOptionPane.showMessageDialog(null,"Doing something in Callout!"); | ||
− | = | + | ==2010-12-28== |
+ | ===自动选择最低报价=== | ||
+ | * 今天用Rule引擎编写脚本,实现了一个功能——通过比较价格1与价格2,自动选择最低报价。 | ||
+ | * 具体实现如下: | ||
+ | * '''Search Key''': beanshell:C_RfQ_AutoChooseLowerPrice | ||
+ | * '''Name''': Auto Choose Lower Price in RfQ | ||
+ | * '''Description''': Auto choose lower price in RfQ Line, by comparing Price 1 with Price 2. | ||
+ | * '''Comment/Help''': Last update: Dec 28, 2010. Ver 1.1. Peanut | ||
+ | * '''Event Type''': Callout | ||
+ | * '''Rule Type''': JSR 223 Scripting APIs | ||
+ | * '''Script''': | ||
+ | <pre> | ||
+ | int rowCount = A_Tab.getRowCount(); //统计行数 | ||
+ | for(int i = 0; i< rowCount; i++){ | ||
+ | |||
+ | A_Tab.setCurrentRow(i); //设置为当前行 | ||
+ | |||
+ | int iCompare=A_Tab.getValue("Price").compareTo(A_Tab.getValue("Price2")); //比较价格 | ||
+ | |||
+ | if (iCompare==-1) | ||
+ | A_Tab.setValue("ChooseQuotation", "A"); // Price 1 < Price 2,则选择供应商A | ||
+ | else if (iCompare==1) | ||
+ | A_Tab.setValue("ChooseQuotation", "B"); // Price 1 > Price 2,则选择供应商B | ||
+ | } | ||
+ | |||
+ | result = ""; | ||
+ | |||
+ | </pre> | ||
+ | ===脚本使用问题小结=== | ||
+ | ====弹出对话框==== | ||
+ | * 这个脚本是用于弹出对话框,经测试没有任何响应,同时也没有报错信息。不知道有没有其他办法可以弹出对话框? | ||
+ | |||
+ | javax.swing.JOptionPane.showMessageDialog(null,"Doing something in Callout!"); | ||
+ | ====Yes/No字段==== | ||
+ | * Yes/No字段不能直接赋值给字符字段,要转为String才行,例如.toString()。示例: | ||
+ | |||
+ | A_Tab.setValue("Purpose",A_Tab.getValue("SelectBPartnerA").toString()); | ||
+ | |||
+ | ====Number对象的处理==== | ||
+ | * 对于Number、Cost+Price、Quantity等对象,ADempiere是作为java.math.BigDecimal对象处理的。 | ||
+ | * BigDecimal对象不能像float一样 BigDecimal_1 >= BigDecimal_2 来进行比较,而是要通过 BigDecimal.compareTo(BigDecimal val)方法来比较。 | ||
+ | * 我一开始是直接比较: | ||
+ | |||
+ | if (A_Tab.getValue("Price") > A_Tab.getValue("Price2") ) | ||
+ | A_Tab.setValue("ChooseQuotation", "B"); | ||
+ | * 于是得到错误代码: | ||
+ | <pre> | ||
+ | ===========> GridTab.processCallout: [12] | ||
+ | javax.script.ScriptException: Sourced file: inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' : Invalid types in binary operator : at Line: 8 : in file: inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' : ) { | ||
+ | in inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' at line number 8 | ||
+ | at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:92) | ||
+ | </pre> | ||
+ | * 后来尝试直接转为float: | ||
+ | <pre> | ||
+ | float priceA=(float) A_Tab.getValue("Price"); | ||
+ | float priceB=(float) A_Tab.getValue("Price2"); | ||
+ | <pre> | ||
+ | * 于是得到错误代码: | ||
+ | <pre> | ||
+ | java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) | ||
+ | caused by: java.lang.ClassCastException: Cannot cast java.math.BigDecimal to float | ||
+ | </pre> | ||
+ | * 这时才明白ADempiere将Number作为BigDecimal对象处理的。要想转为float的话可以用BigDecimal.floatValue() 方法。 | ||
+ | |||
+ | ====脚本使用体会==== | ||
+ | # Script响应速度明显要比编译好的Callout慢。 | ||
+ | # Script的测试和排错的确不方便。 | ||
+ | |||
+ | |||
+ | =链接= | ||
[[Category:Chinese]] | [[Category:Chinese]] |
Revision as of 00:24, 28 December 2010
Contents
实施日志-31-脚本编写与Rule引擎
- 本文属于机械装备制造业进销存实施案例——ADempiere ERP案例研究之一。
- 本文记录ADempiere ERP项目实施的过程
2010-12-16
Rule Engine与脚本
- 今天开始学习和探索Rule Engine(引擎)的Script(脚本编程)。
- 主要目的:从于从RfQ(询价单)创建Purchase Request(采购申请)。
- 介绍:ADempiere的Rule窗口可以写脚本,集成了BeanShell。
- ADempiere的Rule Engine要用到BeanShell,于是了解了一下BeanShell。
- 相关链接:
讨论栏
2010-12-17
Rule Engine下写脚本
- 今天把Rule Engine(引擎)的Script弄清楚了。
- Script Callout主要用于窗体,Script Process主要用于Process。
- 对于从 询价单 生成 采购申请单,可以利用Script Process执行SQL语句来实现。
org.compiere.util.DB.executeUpdate(sql, A_TrxName);
- 学习了HumanFlash的动画视频,做了两篇笔记。见ADempiere教学动画学习笔记-脚本编写
- 相关链接:
脚本使用体会
- 优点:脚本的功能很强大。同时脚本既不需要重新编译ADempiere源代码,也不需要重新布署ADempiere服务器,因为脚本规则都存储在数据库中了。
- 缺点:不容易进行错误调试。我今天测试的Script当中有个SQL语句语法错误,居然没有任何报错。
对Rule Engine的疑问
- Rule Engine为什么要区分成不同Event Type?比如Callout与Process,为什么不合并在一起?
- Rule Engine的Rule Type为什么是JSR 223 Scripting APIs,而不是BeanShell官方网站上JSR 274?
- A_TrxName的TrxName是什么意思?
- Rule Engine中能否实现弹出对话框?代码什么?
- 后来找到一段代码,不过尚未测试:
javax.swing.JOptionPane.showMessageDialog(null,"Doing something in Callout!");
2010-12-28
自动选择最低报价
- 今天用Rule引擎编写脚本,实现了一个功能——通过比较价格1与价格2,自动选择最低报价。
- 具体实现如下:
- Search Key: beanshell:C_RfQ_AutoChooseLowerPrice
- Name: Auto Choose Lower Price in RfQ
- Description: Auto choose lower price in RfQ Line, by comparing Price 1 with Price 2.
- Comment/Help: Last update: Dec 28, 2010. Ver 1.1. Peanut
- Event Type: Callout
- Rule Type: JSR 223 Scripting APIs
- Script:
int rowCount = A_Tab.getRowCount(); //统计行数 for(int i = 0; i< rowCount; i++){ A_Tab.setCurrentRow(i); //设置为当前行 int iCompare=A_Tab.getValue("Price").compareTo(A_Tab.getValue("Price2")); //比较价格 if (iCompare==-1) A_Tab.setValue("ChooseQuotation", "A"); // Price 1 < Price 2,则选择供应商A else if (iCompare==1) A_Tab.setValue("ChooseQuotation", "B"); // Price 1 > Price 2,则选择供应商B } result = "";
脚本使用问题小结
弹出对话框
- 这个脚本是用于弹出对话框,经测试没有任何响应,同时也没有报错信息。不知道有没有其他办法可以弹出对话框?
javax.swing.JOptionPane.showMessageDialog(null,"Doing something in Callout!");
Yes/No字段
- Yes/No字段不能直接赋值给字符字段,要转为String才行,例如.toString()。示例:
A_Tab.setValue("Purpose",A_Tab.getValue("SelectBPartnerA").toString());
Number对象的处理
- 对于Number、Cost+Price、Quantity等对象,ADempiere是作为java.math.BigDecimal对象处理的。
- BigDecimal对象不能像float一样 BigDecimal_1 >= BigDecimal_2 来进行比较,而是要通过 BigDecimal.compareTo(BigDecimal val)方法来比较。
- 我一开始是直接比较:
if (A_Tab.getValue("Price") > A_Tab.getValue("Price2") ) A_Tab.setValue("ChooseQuotation", "B");
- 于是得到错误代码:
===========> GridTab.processCallout: [12] javax.script.ScriptException: Sourced file: inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' : Invalid types in binary operator : at Line: 8 : in file: inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' : ) { in inline evaluation of: ``int rowCount = A_Tab.getRowCount(); float priceA; float priceB; for(int i = 0; i . . . '' at line number 8 at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:92)
- 后来尝试直接转为float:
float priceA=(float) A_Tab.getValue("Price"); float priceB=(float) A_Tab.getValue("Price2"); <pre> * 于是得到错误代码: <pre> java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) caused by: java.lang.ClassCastException: Cannot cast java.math.BigDecimal to float
- 这时才明白ADempiere将Number作为BigDecimal对象处理的。要想转为float的话可以用BigDecimal.floatValue() 方法。
脚本使用体会
- Script响应速度明显要比编译好的Callout慢。
- Script的测试和排错的确不方便。