Reference‎ > ‎

Rules: Action

Definition

An Action rule is defined by a method with the @Action annotation:

import com.autobizlogic.abl.businesslogic.annotations.*;

public class CustomerLogic {

    @CurrentBean
    private Customer customer;
    
    @Action(verbs=Verbs.INSERT_UPDATE)
    public void myAction() {
        if (customer.getBalance() > 1000)
           etc...
    }

This annotation has only one (optional) parameter verbs, which specifies the verb(s) under which this constraint should execute. It can specify any combination of INSERT, UPDATE or DELETE. If specified, the action will execute only in those conditions. If not specified, the action will always execute.

The name of the method has no special meaning, but the method must be public, otherwise it cannot be invoked successfully.

Effect

An Action is invoked any time an object is touched (inserted, modified or deleted).

If an Action throws an exception, the current transaction will be rolled back.

There are no specific limitations on what can be done in an Action. Objects can be modified, created and deleted, and all changes will become part of the current transaction.

An Action may get invoked more than once within a transaction if the execution of the business logic dictates it.


Changing persistent objects

If an action affects persistent objects, it must do the following for each affected object:

Inserts
The newly inserted object must be saved using LogicContext.insert(), and not e.g. Hibernate's session.save().

Deletes
The object to be deleted must be passed to LogicContext.delete(), and not e.g. Hibernate's session.delete().

Updates
Any object that is to be updated must be bracketed between a call to LogicContext.touch() and a call to LogicContext.update().
The call to LogicContext.touch() must be made before any changes are made to the object's persistent state.


Example:

@CurrentBean
private PurchaseOrder order;

@LogicContextObject
private LogicContext context;

@Action
public void myAction() {
    // Any update must be bracketed as follows
    context.touch(order);
    order.setNotes("This is a test");
    context.update(order);

    // Inserts and deletes must be handled by the LogicContext
    LineItem item = new LineItem();
    item.setTotal(123);
    item.setOrder(order);
    context.insert(item);
}


Comments