| | |
|
|
|
Employee demo
Employee Demo is a RESTful application that demonstrates many of Project 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 when you download the Project Zero Eclipse Plugin for Java and Groovy. Instructions for getting started with 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 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 Zero 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. 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 locations.groovy script, 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:
|
|
r12 - 05 Feb 2008 - 17:10:17 - ngawor
|
|
|
| | |