ModelValidator

From ADempiere
Revision as of 04:33, 26 March 2008 by Kthiemann (Talk) (How to create and use a model validator)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

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

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.

public void initialize (ModelValidationEngine engine, MClient client) { [...]

// 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. 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