Tutorial‎ > ‎

Building Logic Tutorial - Roo

You can use this Tutorial to understand how to add business logic to a Java-based web project, from scratch.  The project can be completed in under 20 minutes.

Version 1.2.1

This Tutorial works with Roo Version 1.2.1, only.

Do not execute this with older versions of Roo, such as 1.2.  There are different Roo commands, and the domain classes contain different import and annotations.

The easiest way to upgrade is to obtain a current version of SpringSource Tool Suite (2.9.1 RELEASE, at the time of this writing).


This is Part I, where we create a project with Domain Objects, sample data, and a default User Interface.  This should take approximately 5 minutes.  Completing this prepares you for Part II, where we add business logic to the project created here.

This tutorial introduces the following key concepts: 

  • @Logic - how to define Logic Classes and Logic Annotations
  • Complex multi-table logic - how your logic addresses complex, multi-table logic
  • Automated Dependency Management - how logic dependencies are detected and automated
  • Object Declarative - how you can use Java for complexity and extensibility
  • Assured Re-use / Integrity - how the Business Logic Engine ensures your logic is re-used
  • Logic Debugging - how verify your logic is operating properly



Introduction

In this Tutorial, we will build a small Customer, PurchaseOrder, LineItem and Product application from scratch.  The bottom of this page includes several zip files provided to save typing.

Our focus is on the business logic, not the User Interface, persistence or IDE.  Our business logic objectives, captured on the obligatory cocktail napkin:

 Requirements

The requirements are enforced over a series of Use Cases that touch this data:
  • Adding and changing new PurchaseOrder and their LineItems

  • Making an PurchaseOrder ready, or paid

  • Reassigning a PurchaseOrder to a different Customer

  • Changing LineItem quantities, or Products


We will not focus on 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.

Prerequisites

You will need SpringSource Tool Suite (STS), as explained here.  STS is an extended version of Eclipse, which includes Roo (and Grails, a similar productivity environment built around Groovy; ABL supports both frameworks, and a number of others).  Verify that you have version 1.1.5 of Roo (it will be displayed after project creation).

This tutorial presumes a basic understanding of Hibernate/JPA and Java.  It does not presume a background in STS or Roo.


Create Project

Execute the steps below to create a new project, its domain model, sample data, and a user interface.  This is standard Roo functionality, preparing a basic system to which we will then add business logic.


Create Empty Folder (STS Workspace) and start STS

Create a new empty folder called BusLogicDemoRoo (important - the name is critical), and open STS specifying that as your workspace.  Alter preferences as desired (it is usually a good idea to set Preferences > Run/Debug > Console to unlimited).


Create Roo Project

Right-click on the left view (Package Explorer) to create a New > Roo Project (important - be sure to select a Roo Project):



Specify the project properties on the ensuing dialog, and verify:
  •  names are critical: Project Name BusLogicDemoRoo, and top level package ablroodemo)

  • Note the Roo version is 1.2.1 (you can verify this with the creation wizards)

  • Note you may need to accept the license agreement (watch for Roo Shell messages)

  • you should not see "null" on the Roo Shell (Press Enter to verify the Roo Shell is working)

    • in some cases, you may need to close the Roo Shell, then reopen it (select the project, right click for Spring Tools > Open Roo Shell)

Specify Persistence Layer

You now have an empty project.  Observe that STS has opened the Roo Shell (wait for it to initialize).

ABL depends on the Hibernate/JPA event mechanism, so we enter the following command to specify our persistence properties (in memory database suits our purpose - this is just test data, and we will provide a Loader below):

persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY

Ensure this command runs properly:
  • you should not see "null" on the Roo Shell

  • in some cases, you may need to close the Roo Shell, then reopen it (select the project, right click for Spring Tools > Open Roo Shell)

  • ensure there are no errors (e.g, a pom.xml file with errors indicates a configuration issue)
The output should be along the lines of:



Verify Java Build Path, Maven

Right click on the Project, and select Properties > Java Build Path > Source, and Add the src/main/java folder if it is not already present:


Verify there are no Markers.  It may be necessary to right click the project, and select Maven > Update Project Configuration (with Force Update of Snapshots/Releases).

Create Database Entity

You now have an empty project.  In the Roo Shell, enter the following command:

entity jpa --class ~.domain.Customer

This creates a Java class, including the necessary annotations for JPA persistence:



Create rest of Database model

You can now enter the fields, either using the Roo Shell or in the code editor.  Since we want to focus on business logic and move past the data model, we accelerate this phase:
  1. download and unpack the ablroodemo.domain.zip file, and 
  2. paste the files into ablroodemo.domain (replace the existing file).
You should now see the following (the Roo Shell illustrates that Roo is creating many related classes for setters/getters, toString and all manner of boilerplate code):



Create Loader class for Test Data

In the next step, we'll create our user interface, but it will be boring if there is no test data.  Spring/Roo enables you to process events such as system startup, which we'll use to populate our database:
  1. Under src/main/java, create a New Package ablroodemo.util
  2. Download the Loader class
  3. Paste it into STS at abldemoroo.util
    • If you encounter a package error, utilize the Code Editors correction services to move Loader.java to package ablroodemo.util. The project should be error-free after the correction.



Create User Interface

In the Roo Shell, enter the following command:

controller all --package ~.web

This will generate a number of files.


Test User Interface


To test the generated User Interface, select the Servers view, then Add and Remove...



Then, add BusLogicDemoRoo as shown below



Start the server (click the bug icon on the upper right of the Servers view)


You should then be able to start http://localhost:8080/BusLogicDemoRoo/ in your browser; click List All Customers and you will see loaded (but incorrect - note the Balance is not computed) data:



Project Creation Complete - Part 1

You now have a simple, but complete, Roo project, including a database schema and a web interface.

Well, maybe not that complete!  The business logic - see our cocktail napkin above - is totally missing.  This is first apparent from the loaded data (all the Customer balances are 0), and continues to be missing if you create, update or delete orders.

Not to fault Roo - frameworks simply aren't built to address this element of your app.  The problem is, this is about half the app (even more as framework automation increases).

We are now ready to introduce business logic.

ċ
Loader.java
(4k)
Val Huber,
Apr 19, 2012, 9:34 PM
ċ
ablroodemo.domain.zip
(2k)
Val Huber,
Apr 21, 2012, 12:06 PM
Comments