壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3986|回复: 5

ADempiere教学动画学习笔记-脚本编写

[复制链接]
发表于 2010/12/16 18:04:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622  。

您需要 登录 才可以下载或查看,没有帐号?注册

x
本帖最后由 peanut 于 2010/12/17 13:08 编辑

ADempiere教学动画学习笔记 -脚本编写

根据HumanFlash

主题:Rule Engine Scripting Callout

Part 1. Why we create scripting callouts via Rule Engine.


Callout is a very useful feature, which populates other fields based on business logic in a window, when a specific field is changed.

Callouts are usually implemented by Java class technology, but we can also implement a callout by scripting technology via Rule Engine.

For example, login Adempiere under ClientAdmin role.

Open "Business Partner" window.

When creating or modifying a business partner, changing "Business Partner Group" won't affect "Employee" checkbox ( in Employee tab ).

....

In this tutorial, we will demonstrate how to create a scripting callout via Rule Engine.

Part 2. How to create scripting callouts with Rule Engine.

Log in System client to create a rule.

Search key: beanshell:ScriptingCalloutExample

Since we will use beanshell scripting technology, the search key is named with a prefix "beanshell";

Event Type: Callout

Rule Type: JSR 223 Scripting APIs

Select JSR 223 Scripting APIs, so that we can use Java BeanShell scripting technology.

Script: enter scripts

Within the script you can use:


    • Window context variables start with a W_ prefix
    • Login context variables start with G_ prefix
    • Parameters for callout start with A_ prefix
          o A_WindowNo
          o A_Tab
          o A_Field
          o A_Value
          o A_OldValue
          o A_Ctx

Open "Table and Column" window.

Go to table "C_BPartner"

Locate "C_BP_Group_ID" column to define its scripting callout.

Callout: @script:beanshell:ScriptingCalloutExample

Now, we have implemented the scripting callout via Rule Engine.

Part 3. How to test scripting callouts with Rule Engine.

Now, start Adempiere client from "Eclipse" to log in non-System client, and start to test the scripting callout.

Open "Business Partner" window.

Business Partner Group: select "Staff"

Save the record.

Go to "Employee" tab.

Look, "Employee" checkbox is ticked.

......

Try to create new record.

testing ...

Good, the scripting callout works as expected.

Part 4.Pros & Cons

Pros: Scripting Callout is such a powerful feature that will take effect without re-building Adempiere source nor re-deploying Adempiere server, because the scripting rules are stored in the database.

Cons: However, scripting is harder to debug than native Java classes.

 楼主| 发表于 2010/12/17 13:08:09 | 显示全部楼层

主题:Rule Engine Scripting Process

本帖最后由 peanut 于 2010/12/17 16:28 编辑

主题之二:Rule Engine Scripting Process

Part 1. Why we create a scripting process via Rule Engine.


"Processes" are usually implemented by Java class technology, but we can also implement a process by scripting technology via Rule Engine.

For example, "Delete Change Logs" is a new process, used to delete records in AD_ChangeLog table.

Click OK button.

Good, all records in AD_ChangeLog table are deleted.

We can requery records in "Change Audit" window.

Look, there is no records in "AD_ChangeLog" table.

In this tutorial, we will use scripting technology via Rule Engine to implement such a new process.

Part 2. How to create a scripting process with Rule Engine.

2.1. Create a new rule.

Now, let's implement the process via rule engine scripting.

Open "Rule" window.

Create a new rule

Search Key: beanshell:DeleteChangeLog
Name: Delete Change Logs
Description: Delete records from AD_ChangeLog table within the client.
Event Type: Process
Rule Type: JSR 223 Scripting APIs
Script:  ... ...
//--------------------------------------------------------
// Script code start
//HumanFlash Sample Code

import org.compiere.util.*;

String sql="DELETE FROM AD_ChangeLog WHERE AD_Client_ID=" + G_AD_Client_ID;

if (P_AD_Table_ID==void)
  P_AD_Table_ID=0;
if (P_AD_Table_ID>0)
  sql = sql + " AND AD_Table_ID=" + P_AD_Table_ID;
int no = DB.executeUpdate(sql, A_TrxName);
if (P_AD_Table_ID>0)
  A_ProcessInfo.addLog(0, null, null, "Deleted" + no + " record(s) from AD_ChangeLog.");
else
  A_ProcessInfo.addLog(0, null, null, "All records in AD_ChangeLog table have been deleted.");

result="OK";
// Script code end
//--------------------------------------------------------

Within the script you can use:




    • Login context variables start with G_ prefix
    • Process Parameters for the process start with P_ prefix, for example P_Name
    • If the parameter is a range then the parameters will be P_Name1 and P_Name2
And the following predefined arguments:




    • A_Ctx - the context
    • A_Trx - the transaction
    • A_TrxName
    • A_Record_ID
    • A_AD_Client_ID
    • A_AD_User_ID
    • A_AD_PInstance_ID
    • A_Table_ID
If "AD_Table_ID" parameter is empty, all records will be deleted from "AD_ChangeLog" table, where AD_Client_ID security is restricted.

If AD_Table_ID parameter is not empty, all records will be deleted from AD_ChangeLog table, where AD_Client_ID security is restricted, and the related AD_Table_ID restriction will be applied too.

Save this new scripting rule

2.2. Create a new Process.

Open Report and Process window.

Create a new process.

Search Key: AD_ChangeLog_Delete
Name: Delete Change Logs
Comment/Help:
All records in AD_ChangeLog will be deleted.
If a table is selected, only those records applied to this table will be deleted;
If no table is selected, all records applied to all tables will be deleted.
Data Access Level: System+Client.
Classname: @script:beanshell:DeleteChangeLog

Save this process.

Go to Parameter tab to define parameters

Name: Table
Description: Database Table information
DB Column Name: AD_Table_ID
System Element: AD_Table_ID
Reference: Table Direct

The new scripting process has been defined.

2.3 Menu

Open Menu window to add the new scripting process to the menu tree.

Create a new one.
Name: Delete Change Logs
Action: Process
Process: AD_ChangeLog_Delete

Now, we have implemented the scripting process via Rule Engine.

Part 3. How to test scripting process with Rule Engine.

Now, start Adempiere client from Eclipse to log in non-System client, and start to test the scripting process.

3.1.

Role: GardenWorld Admin

Open "Role" window.

Tick "Maintain Change Log" checkbox to automatically log changes.
Go to role: GardenWorld Admin , GardenWorld User.

Open Change Audit window to view change logs.

Run "Cache Reset"

3.2.
Open "Business Partner" window to make some changes.

Open "Product" window to make some changes.

Open "Change Audit" window to view logged changes.

Look, there is a log record for C_BPartner table, so can we remove all records for that table only?

Run "Delete Change Logs" process.

Select table C_BPartner.

The message indicates how many records are removed from AD_ChangeLog table.

Before refreshing "Change Audit" window.

After refreshing "Change Audit" window, records related to C_BPartner table are removed.

3.3.
This time, we do not specify any table, and click OK button.

Go to "Change Audit" window.

Good, the new scripting process works as expected.

3.4.
The new scripting process is driven by the predefined "class".

Part 4.Pros & Cons

Pros: Scripting Process is such a powerful feature that will take effect without re-building Adempiere source nor re-deploying Adempiere server, because the scripting rules are stored in the database.

Cons: However, scripting is harder to debug than native Java classes.

Well done! You've learned how to implement scripting process via Rule Engine.

发表于 2010/12/18 11:21:01 | 显示全部楼层
这东西不错啊,值得收藏
发表于 2010/12/18 11:22:06 | 显示全部楼层
还有没有?
 楼主| 发表于 2010/12/25 21:20:12 | 显示全部楼层
aoslee 发表于 2010/12/18 11:22
还有没有?

呵呵,这是HumanFlash动画课程的最后一节,关于rule engine后面还有 scripting model validator,分三个部分:document event、Login event、table event。以后有机会就发一部分摘要上来。
发表于 2010/12/28 14:07:23 | 显示全部楼层
顶一下!!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 11:20 , Processed in 0.014403 second(s), 14 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表