16. Implementing Controller Methods using Actions and Steps

PROBLEM

A controller is responsible for orchestrating the server logic necessary for processing a request from a web client. The logic associated with a controller is contained within one or more related controller methods, that must be implemented to define the behaviour of the application. There needs to be a mechanism for leveraging automation for defining and implementing controller methods.

SOLUTION

The Skyway Generation Framework has generators for the different layers of a Spring application, and the framework also supports fine-grained specification of the application logic. The metadata model encompasses an extensible toolbox of pre-built functionality (Skyway Steps) that can be used to specify the application logic. Skyway Steps introduce automation to the application development process. Using steps to implement logic (instead of custom code) is very appealing to developers and architects. First of all it reduces the redundancy of application development. Rather than continuously having to write the same code over and over with only minor differences, steps automate the generation of code for the developer. By using steps there is also more uniformity in the code because all the code was generated in the same consistent manner (using a customizable step template). There will also be less deviation in code as attributed by different developer styles, conventions or preferences. Enterprise architects love steps because they are easy to create, and it ensures that developers are accessing enterprise resources in a prescribed and controlled manner.

To achieve the desired application behavior, a developer will

  1. define a Skyway Operation for each controller method

  2. define the signature of the operation using inputs/outputs

  3. define at least one action for each Skyway Operation

  4. add one or more steps from the Step Pallette (toolbox) to the Action

  5. configure the steps according to application requirements

  6. sequence the steps into a flow

The Skyway Generation Framework then converts the Operation and Action into a controller method and the sequence of steps into well-formed java code.

HOW IT WORKS

Operations define web controller methods for handling user-generated events, and Actions are used to model the operation logic. Skyway Builder enables a Operation to be implemented using a model-based approach, that defines the logic to be performed by configuring and sequencing one or more Skyway Steps together.

Steps for creating an Action:

  1. Right click the on an operation, and select New-->Action to open the New Action Wizard.

  2. From the New Action panel enter a common name for the Action. The common name is for referencing the Action from other Actions in the same operation. Click Finish. The Action Editor will be opened.

Steps for implementing an Action:

  1. Click on a desired step from the step palette. The steps are grouped by related functionality into drawers.

  2. Next click on the location on action canvas that you want to place the step..

  3. Configure the step using the properties in the properties panel.

  4. Repeat the process until all steps are located on the canvas and configured.

  5. Next draw a connector (available from the palette) between steps. The steps should connected in the order that they must execute.

  6. When done, right-click on the step that you desire to be the first step to run, and mark it as the Start Step using the menu option.

Figure 2.11. Action Editor

Action Editor

The Step Palette contains all the available steps. The steps are divided into functional groups, and they are separated by each other using drawers.

Figure 2.12. Step Palette

Step Palette

In order to accomplish more complex tasks the application logic may be implemented using several Actions. A operation is configured to call a specific Action (entry point action), which will orchestrate calls to other Actions (using Invoke Action step) or Operations (using Invoke Operation step) in the Service layer of the application.

The following figures shows an abbreviated version of the code that is generated from an Action artifact. To see the fully generated code, see the SkywayBlog sample project.

Example 2.8. Action (Generated)

package skywayblog.web.blogcontroller.browsemodel;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// Additional imports to support Action logic

/**
 * SelectPost - Action 
 *
 * @generated
 */
@Scope("prototype")
@Component("BlogController.BrowseModel.selectPost")
public class SelectPost implements IVariableContextProvider {


  /**
   * The SLF4J logging class.
   * @generated
   */
  private transient Logger logger = LoggerFactory.getLogger(getClass());

  /**
   * @generated
   */
  private transient ApplicationContext applicationContext;

  /**
   * Reference to Model used by Action
   * @generated
   */
  @Resource(name = "BlogController.BrowseModel")
  private BrowseModel variableStorage;

  // Constructor 
  ...

  /**
   * @generated
   */
  public void doInvoke() throws Exception {

  GENERATATED APPLICATION LOGIC GOES HERE  
  }


}

RELATED RECIPES

  1. Blending Actions with non-generated Java Code

  2. Blending Actions with non-generated Spring Beans

  3. Accessing Request and Session Parameters

  4. Using Groovy in Actions