[#business-rules]
= Business Rules

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


When a domain object is rendered in the UI or the end-user interacts with the domain object through the UI, the framework applies a series of precondition business rules to each object member (property, collection or action).

When the object is being rendered, the framework checks:

* is the object member visible?
+
Members that are not visible are simply omitted from the page.
If all the members in a fieldset (property group) are hidden, then the fieldset is not shown.
If all the members in a tab are hidden, then the tab is not shown.
If all the members of the object are hidden, then a "404" style message ("no such object") is returned to the user.

* if the object member is visible, is the object member enabled?
+
An enabled property can be edited (otherwise it is read-only), and an enabled action can be invoked (otherwise it's button is "greyed-out").
Note that collections are always read-only.

* for enabled object members, if the user then interacts with that member, are the supplied values valid (can the user "do it").
+
For an editable property this means validating the proposed new value of the property.
For an invokable action this means validating that arguments being used to invoke the action.


One way to remember this is: *"see it, use it, do it"*

.See it, use it, do it
****

* is the object member visible?

* if so, is the object member enabled?

* if so, are the supplied values valid? (can the user "do" it)?
****


The framework provides a multitude of ways to implement these business rules.
The simplest approach is to just implement the business rules imperatively in the domain object, using a supporting method.
For example,

[source,java]
----
public Customer placeOrder(Product p, int quantity) {
    // ...
}
public boolean hidePlaceOrder() {                       // <.>
    return isBlacklisted();
}
----
<.> supporting method, invoked before rendering the customer.

In this example the "place order" action would not be visible for any customer that had been blacklisted.

[TIP]
====
Details on how to actually write business rules (the programming model) can be found xref:userguide:fun:business-rules.adoc[here].
====
