All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nz.co.senanque.pizzaorder.R10 Maven / Gradle / Ivy

Go to download

This is a plugin to Madura Objects. It provides a rules/constraint engine to assist with validation, deriving new values from user inputs (eg total of invoices entered on this customer) an manipulating metadata (eg because the amount is above X we make some field readonly). Note that the Java that is using the monitored objects is quite unaware of the rules layer implemented here. That means you can change rules without having to go back to your Java code, and it also means you don't have to wonder if everything implemented the same rules. Anything using that Java object has the rules (unless you turn them all off).

There is a newer version: 3.3.5
Show newest version

package nz.co.senanque.pizzaorder;

import java.io.Serializable;
import nz.co.senanque.rules.FieldReference;
import nz.co.senanque.rules.NotTrueException;
import nz.co.senanque.rules.Operations;
import nz.co.senanque.rules.Rule;
import nz.co.senanque.rules.RuleContext;
import nz.co.senanque.rules.RuleProxyField;
import nz.co.senanque.rules.RuleSession;
import nz.co.senanque.rules.UnKnownFieldValueException;
import nz.co.senanque.validationengine.ValidationObject;
import org.springframework.stereotype.Component;


/**
 * rule:Pizza p2
 * {if
 * (size==Small){
 * readonly((testing);
 * activate((testing);
 * amount=10;}}
 * 
 */
@Component
public class R10
    implements Serializable, Rule
{

    private final static long serialVersionUID = 1L;

    public void evaluate(final RuleSession session, final ValidationObject object, final RuleContext ruleContext) {
        final Operations operations = session.getOperations();
        final RuleProxyField proxyFieldtesting = session.getRuleProxyField(session.getMetadata(object).getProxyField("testing"));
        final RuleProxyField proxyFieldsize = session.getRuleProxyField(session.getMetadata(object).getProxyField("size"));
        final RuleProxyField proxyFieldamount = session.getRuleProxyField(session.getMetadata(object).getProxyField("amount"));
        try {
            try {
                operations.checkTrue(operations.eq("Small", ((String) proxyFieldsize.getValue())));
            } catch (NotTrueException _x) {
                return ;
            }
            session.readonly(ruleContext, proxyFieldtesting);
            session.activate(ruleContext, proxyFieldtesting);
            session.assign(ruleContext, 10L, proxyFieldamount);
        } catch (UnKnownFieldValueException _x) {
            return ;
        }
    }

    public String getRuleName() {
        return "R10:p2";
    }

    public String getMessage(final RuleSession session, final ValidationObject object) {
        return session.getMessage("nz.co.senanque.pizzaorder.R10", new Object[] { });
    }

    public String toString() {
        return "R10:p2";
    }

    public String getClassName() {
        return "Pizza";
    }

    public Class getScope() {
        return Pizza.class;
    }

    public FieldReference[] listeners() {
        return new FieldReference[] {new FieldReference("Pizza", "Pizza", "size")};
    }

    public FieldReference[] outputs() {
        return new FieldReference[] {new FieldReference("OrderItem", "Pizza", "amount")};
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy