Employee Demo
Employee Demo is a RESTful application that demonstrates many of Zero's capabilities. It provides an interface for managing employee data, including listing, creating, editing and deleting employee records. It makes use of the following features:
- Resource handling
- Groovy scripting
- Data access
- JSON APIs and rendering
- Groovy templates
- Client programming with Dojo
Employee Demo is available from the
download page under the Project Zero Examples Plugin for Eclipse. Instructions for installing Employee Demo and other samples are available in the
Getting Started section of the Core Developer's Guide. Additional post-install steps are documented in the README.TXT file in the Employee Demo folder.
For the following overview we assume you have completed the
tutorial and are familiar with terms such as REST and JSON.
Resource handling
Employee Demo is a RESTful application, and makes use of Zero's simple convention for exposing resources in a RESTful manner. For more information on the resource handling convention, see the
corresponding section in the Core Developer's Guide. In this case, the set of employees is the collection and each individual employee is a resource. The application needs to be able to get the set of employees and add to it and to be able to edit and delete individual employees. The employees collection is represented as a folder under the app/resources folder. It contains a collection.groovy file (with methods for getting the set of employees and adding new employees) and an item.groovy file (with methods for editing and deleting individual employees). The existence of this file causes Zero to automatically route requests for http://<host>/employees to the collection.groovy script and requests for http://<host>/employees/<employeeId> to the item.groovy script.
Groovy scripting
Within the collection.groovy script are the methods onGET and onPOST. The HTTP method used by the incoming request determines which method in the script is called. If a GET request is made for http://<host>/employees, for example, the onGET will be called and return the list of employees in JSON format. The item.groovy script, on the other hand, has the methods onGET, onPUT, and onDELETE, which are used for getting, editing, and deleting employee information respectively. For more information on Groovy scripting, see the
corresponding section in the Core Developer's Guide.
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 Zero 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 Zero APIs, which allow you to leverage native Groovy features such as GString. For more information on the Data Zero APIs, see the
corresponding section in the Core Developer's Guide.
JSON APIs and rendering
Zero 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 more information on JSON support see the
corresponding section in the Core Developer's Guide. For rendering JSON back to the client, the Java object representing the employee(s) (returned by the Zero Data APIs) can be placed in the Global Context under result and the rendering method set to JSON before the ViewEngine.render is called.
Groovy templates
The Employee Demo needs to return some custom JSON to the client in order to populate a Dojo combo box, which expects its input in a particular form. This custom rendering is done through a Groovy template which is placed in app/views/locations.gt. At the end of the collection.groovy script for locations, the script sets request.view to the name of the script (locations.gt) in app/views before calling ViewEngine.render. This ensures the Groovy template is rendered. For more information on Groovy templates, see the
corresponding section in the Core Developer's Guide.
Client programming with Dojo
Employee Demo uses Dojo for its client interface. It mainly makes use of the Dojo Filtering Table for displaying employee information. The index.html file in the public folder contains the HTML and Javascript used by the client. For more information on client programming see the
corresponding section in the Core Developer's Guide.
The Employee Demo client interface: