Employee demo

The employee Demo is a RESTful application that demonstrates many capabilities of IBM® WebSphere® sMash. It provides an interface for managing employee data, including listing, creating, editing and deleting employee records.

Installing the Employee demo (Command Line)

Use the following steps to install the employee demo with the command line:

  1. Install the WebSphere sMash command line utility using the instructions on the WebSphere sMash Web site.
  2. Install the employee demo application from the WebSphere sMash repository using the following command:
    zero create employee.demo from zero:employee.demo
  3. Navigate to the employee.demo directory.
    cd employee.demo
  4. Create the database artifacts by running the following command from the root directory of the project (employee.demo):
    zero runsql setup_db.sql
  5. Start the application with the following command:
    zero start
  6. After following the previous steps, you can interact with the application in the following ways:
    Dojo-based UI
    http://localhost:8080/
    Resource URIs

Features of the Employee demo

This application uses the following features:

  • Resource handling
  • Groovy scripting
  • Data access
  • JSON APIs and rendering
  • Client programming with Dojo

For the following overview, you should be familiar with terms such as REST and JSON.

Resource handling

The employee demo is a RESTful application that uses the simple convention for exposing resources in a RESTful manner as provided by WebSphere sMash. In this case, the set of employees is the collection and each individual employee is a resource. The application must provide the ability to get the set of employees, add to the set, and edit and delete individual employees. The employees collection is represented as a file named employees.groovy under the app/resources folder. It contains methods for getting the set of employees (onList), getting a single employee details (onRetrieve), adding new employees (onCreate), updating an existing employee (onUpdate) and deleting an employee (onDelete). The existence of this file causes WebSphere sMash to automatically route requests for http://<host>/employees http://<host>/employees/<employeeId> to the employees.groovy script.

Groovy scripting

Within the employees.groovy script are the methods onList, onRetrieve, onCreate, onUpdate, and onDelete. The HTTP method used by the incoming request and whether the URI contains an employee id or not determines which method in the script is called. If a GET request is made for http://<host>/employees, for example, the onList will be called and return the list of employees in JSON format.

Data access

When a request comes in to access or manipulate employee data the application needs to talk to a backend database. This is done through the Data WebSphere sMash APIs. After setting up the database configuration information in the config/zero.config, the create call sets up a new instance of the Groovy data manager. Queries and updates to the database can then be done through the Data WebSphere sMash APIs, which allow you to leverage native Groovy features such as GString.

JSON APIs and rendering

WebSphere sMash provides support for serialization and deserialization of JSON objects. Employee Demo leverages this support both for reading in employee information and sending it back to the client. Since JSON is sent by the Dojo client whenever an employee is created or updated, Employee Demo uses the fromData call to pull out the information from the request and insert it into the database call. For rendering JSON back to the client, the Java object representing the employee(s) (returned by the WebSphere sMash Data APIs) can be placed in the Global Context under result and the rendering method set to JSON before the ViewEngine.render is called.

Client programming with Dojo

Employee Demo uses Dojo for its client interface. The index.html file in the public folder contains the HTML and Javascript used by the client, as shown in the following Employee Demo client interface:

screenshot.png

Version 1.0.0.3.25591