Difference between revisions of "ModelValidator"
m (→See also) |
(new category) |
||
Line 105: | Line 105: | ||
[[Category:Developer documentation]] | [[Category:Developer documentation]] | ||
+ | [[Category:Code snippets]] |
Revision as of 07:20, 20 August 2010
Contents
How to use a model validator
What is a model validator?
A model validator is java class that implements org.compiere.model.ModelValidator. It has two important (callback)methods (modelChange and docValidate) which are called on model change events and document events. The model validator register itself for certain ADempiere tables or documents to react on model changes (data changed, added or deleted) or document actions like complete, void etc.
Model change events
Model Change events occur every time when data of an ADempiere db table is changed. There are events for data insertion (TYPE_BEFORE_NEW/TYPE_AFTER_NEW), change of data (TYPE_BEFORE_CHANGE/TYPE_AFTER_CHANGE) and deletion (TYPE_BEFORE_DELETE/TYPE_AFTER_DELETE).
Document events
Document events occur on the change of the document status. So you have BEFORE/AFTER CLOSE/REACTIVATE/ REVERSECORRECT/REVERSEACCRUAL/COMPLETE/PREPARE/VOID/POST
Login Event
It's called when a user log into Adempiere - you can log or restrict users to log in according to business rules.
FactsValidate Event
It's called when posting a document (accounting). You can change the facts being posted (similar to calling BEFORE_POST document event.
LoadPreferences Event
It's called after the adempiere preferences are loaded - useful to add context variables or things after log in.
SaveProperties Event
It's called when a user is saving properties - useful to restrict changes on properties file (Tools -> Preferences).
Steps to create and register a model validator
1. Create your own validator class which implements the interface org.compiere.model.ModelValidator
- Register the table/document in the initialize() method.
/** * Get Client to be monitored * @return AD_Client_ID client */ public int getAD_Client_ID() { return m_AD_Client_ID; } // getAD_Client_ID public void initialize (ModelValidationEngine engine, MClient client) { // This line must come before registering the model changes //client = null for global validator if (client != null) { m_AD_Client_ID = client.getAD_Client_ID(); } [...] // register for model change on C_Order engine.addModelChange(MOrder.Table_Name, this); // register for document events on MOrder engine.addDocValidate(MOrder.Table_Name, this); } // initialize
- Put your code into modelChange() for model change events and into docValidate() for document events.
- You have to check the parameters po for the table name (po.get_TableName() - if you registered more than one table/document) and timing/type to react on the right events.
public String modelChange (PO po, int type) throws Exception { // we want to validate the order before the deletion if (po.get_TableName().equals("C_Order") && type == TYPE_BEFORE_DELETE) { MOrder order = (MOrder)po; // put your code here } return null; } // modelChange
public String docValidate (PO po, int timing) { if (timing == TIMING_BEFORE_COMPLETE) { if (po.get_TableName().equals(MOrder.Table_Name)) { //put your code here //it is executed every time an order is about to complete } } return null; } // docValidate
2. Register your validator in ADempiere. Login as System Administrator and open the 'Client' window. Here you have to enter you validator class (full qualified name) into the ModelValidationClasses field.
3. Create a jar file with your validator class and rename it to customization.jar. 4. Or jar the class directly for example:
jar cvf customization.jar MyValidatorThailand.class
5. Copy this customization.jar into the Adempiere/lib folder of your ADempiere installation and rerun the RUN_setup.bat/sh. Restart the ADempiere server and install the new client (or restart the client with Java WebStart).
See also
- Take a look at compiere.model.MyValidator or MyValidatorThailand for sourcecode samples.
- Extensions Friendly Proposal
- LCO Validator for a code expose.
- Script ModelValidator to do the same without stopping the Server!
- How To Create Customization.jar.
- Java JPA seems to have 'callbacks' that can replace the hooks we are using.