
nz.co.senanque.oneinvoice.R1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of madura-rules Show documentation
Show all versions of madura-rules Show documentation
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).
package nz.co.senanque.oneinvoice;
import java.io.Serializable;
import java.util.List;
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.ProxyField;
import nz.co.senanque.validationengine.ValidationObject;
import org.springframework.stereotype.Component;
/**
* rule:Customer Determine customerType from name
* {if
* (amount>27D){
* oneInvoice.amount=amount;
* oneInvoice.date=toDate((2000-01-15);}}
*
*/
@Component
public class R1
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 proxyFieldamount = session.getRuleProxyField(session.getMetadata(object).getProxyField("amount"));
final List proxyFieldoneInvoice_date = session.getMetadata(object).getProxyFields("oneInvoice", "date");
final List proxyFieldoneInvoice_amount = session.getMetadata(object).getProxyFields("oneInvoice", "amount");
try {
try {
operations.checkTrue(operations.gt(27.0D, ((Double) proxyFieldamount.getValue())));
} catch (NotTrueException _x) {
return ;
}
session.assign(ruleContext, ((Double) proxyFieldamount.getValue()), proxyFieldoneInvoice_amount);
session.assign(ruleContext, operations.toDate("2000-01-15"), proxyFieldoneInvoice_date);
} catch (UnKnownFieldValueException _x) {
return ;
}
}
public String getRuleName() {
return "R1:Determine customerType from name";
}
public String getMessage(final RuleSession session, final ValidationObject object) {
return session.getMessage("nz.co.senanque.oneinvoice.R1", new Object[] { });
}
public String toString() {
return "R1:Determine customerType from name";
}
public String getClassName() {
return "Customer";
}
public Class getScope() {
return Customer.class;
}
public FieldReference[] listeners() {
return new FieldReference[] {new FieldReference("Customer", "Customer", "amount")};
}
public FieldReference[] outputs() {
return new FieldReference[] {new FieldReference("Invoice", "Customer", "amount"), new FieldReference("Invoice", "Customer", "date")};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy