We're on Facebook

Building Logic Tutorial - Roo Logic

This is Part II of the Tutorial to create a project, from scratch, with Business Logic. It presumes you have completed Part I - the creation of a project with domain objects, tests data, and a default User Interface.

Introduction

See here for a summary of the project (repeated below as required). Recall we will not focus on the the Data Model (which is trivial), or the User Interface. In fact, we will utilize Spring Roo since it provides default services for those elements, using standard Java. You can perform a similar tutorial in a wide variety of other IDEs and frameworks.

Introduce Business Logic

Tip: Key Concepts

Watch for boxes like this - they call your attention to the key concepts.

In the prior step, we have created a full application. Spring/Roo has enabled us to create our domain model and user interface with remarkably little effort. But we have no business logic - our data is not properly computed or validated.

Configure JPA

As we'll discuss further below, ABL operates as a Hibernate/JPA event listener. We now configure that.

Multi-table Chaining

Now, any change to LineItem.amount will adjust our amountTotal. You can start the application and verify this, or just proceed with the remaining logic. The callout at the right observes how this new logic automatically chains with the existing logic.

In fact, you can try a number of tests - the business logic is automatically re-used overall all of these Use Cases:

Delete a Purchase Order

Delete a LineItem

Insert a new LineItem

Change LineItem amounts

Note you'll manually enter the LineItem.amount. This is clearly incomplete, and we'll address it momentarily.

Declaring sums and counts is a fundamental pattern of business logic. Your existing data must be correct for proper results. Let's briefly consider how:

Reloaded test data: In our case, we are reloading our test data each time. This load process is layered on Hibernate (not in the Controllers), so our reloaded data reflects our new logic -- sums and counts (and other derivations) will be properly initialized. (This will be apparent after the amount derivations are defined in the next section).

Existing data: If you are using existing data, you need to initialize the existing data to conform to your new rules. ABL provides Recompute to automate this process.

Compute amount

Of course, LineItem.amount is not supposed to be entered directly. As stipulated in Requirements 4 and 5, it is derived as the price * quantity, where the price comes from the Product. We define that logic as follows:

Create the LineItemLogic class as above

Add the import: import com.autobizlogic.abl.annotations.*

Paste in the following:

@Formula("productPrice * qtyOrdered")public void deriveAmount() { }

@ParentCopy("product.price")public void deriveProductPrice() { }

Cascade Option

We could have derived amount as product.productPrice * qtyOrdered, but that would instruct the system that subsequent changes to the price were to be cascaded to previously entered LineItems.

That is not the correct business objective, so we utilize the @ParentCopy logic annotation.

Refine Customer balance

As we noted above, the Customer's balance is not exactly the sum of all that customer's orders - Requirement 2 states we only want to sum the unpaid and ready orders. To define that logic:

Not only does this reduce the implementation effort, it improves quality since you can't "forget" to invoke logic for a corner case (e.g., reassign PurchaseOrder increases new balance, but does not decrease old balance).

Development: the bulk of your business logic code is typically dependency management. As illustrated by this example, the 5 annotations replace 500 lines of java code

Maintenance: maintenance typically is more about the archaeology of deciphering dependencies so new code can be inserted correctly. With automated dependency management, you simply add/change your logic, relying on the automated dependency management of the Business Logic Engine to optimize and order the resultant logic.

Logic Execution, Debugging and Performance

Plug-in Architecture

Event-based logic injection means

No recoding - in this tutorial, there was no need to instrument Spring Web Apps to invoke logic

Assured re-use / integrity - logic execution is thus assured

Hibernate/ORM Integration

The Business Logic Engine operates not by your direct call, but rather by listening for Hibernate/JPA events.

Logic Debugging

As developers, we spend a considerable amount of time staring at the screen, wondering "what is it doing?". As developers, we designed business logic for transparent execution:

Logging: the Business Logic Engine can be configured to generate log tracing for every rule that fires, with full depiction of the Domain Object attribute state, and logic chaining (nesting)

The last concept is particularly important. The full download includes the BusLogicExt project, a library of such extended logic. This is provided both for your use (these represent the automations of patterns we have seen), and as an illustration of how to build your own extensions (for patterns you detect).

Specify Auditing Logic

We add our logic to the CustomerLogic class. To refer to the Customer domain instance being updated, we need to insert the following code. The Business Logic Engine uses the annotation to inject the domain instance object prior to calling your logic, so that your Logic Methods can issue code as as customer.getCreditLimit():

@CurrentBeanCustomer customer; // injected by BusLogicEngine

@OldBeanCustomer customerOld;

Similarly, we need to access some information about the state of transaction (also injected):

Whether you entered the code directly, or copied from this page, it should look something like this:

Note we've inserted a breakpoint at line 42. If you do so, you will see the debug information (the customer row contents) if you select the injected variables. You will also note that the breakpoint is hit multiple times during the Loader process. (This is a useful feature - your test data is subjected to possibly new business logic each time you start the server).

Next Steps

Review the Case-based Training - this tutorial is the "Hello World" of business logic... the training shows that automation scales to complex problems such as a Bill of Materials Explosion, or the allocation of a Payment to a set of outstanding Orders.