|
|

楼主 |
发表于 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.
|
|