• No results found

Web service API methods

Chapter 6 Framework Implementation

6.3 Framework API implementation

6.3.5 Web service API methods

70

Insert data

The web service insertSample() is shown in Figure 6-18. The web service calls the validator to check the sample attributes based on the predefined validation rules. If there are any violations, the proper error messages are returned to the end user applications.

The validator in Part 1 looks for violations within a single object. Other database validation (e.g. checking that a sample name is unique) requires existing data to be investigated. Part 2 shows how the DAO uses findByExample to see if a duplicate sample already exists in the database. If the sample data is valid, the web service method calls the DAO to save the sample object to the database.

public String InsertSample (Sample sample) {

//1. Validate domain objects based on the predefined validation rules

violations = this.validator.validate(sample);

if ( violations.size() > 0 ) {

return violations.iterator().next().getMessage(); }

//2. Use the dao object to check duplicate date in the database

else if (this.dao.findByExample(sample){

return "ERR:" +"you cannot insert duplicate samples in the database,

the sample name must be unique"; }

// 3. Using the dao object to save data

else {

this.dao.persist(sample);

return “The sample has been inserted"; } ...

}

Figure 6-18 Example code of insertSample

In order to create associations between assay and sample objects an insertAssay method was implemented in the web service. The method takes an assay object and a collection of

sampleIDs as input parameters in order to store the assay object and associated samples in the database. The logic is similar to the insertSample method, but it needs to validate the

sampleIDs to makes sure they are valid samples in the database. There are four steps: 1. The insertAssay method checks that each sample exists using the sampleId. If a null

value is returned from the findByID method for any sample in the collection

parameter, the insertAssay method returns an error message to the client application. 2. Another database validation (e.g. checking that an assay name is unique) requires

existing data to be investigated; the DAO uses findByExample to see if a duplicate

1

3

71

assay already exists in the database. The method checks the duplicate assay data in the database based on provided assay name.

3. The web service calls the validator to check the assay attributes conform to the predefined validation rules.

4. If the input assay object is valid, the web service uses the DAO to save the assay data in the database. The DAO is able to add sampleId and assayId as foreign keys in the SAMPLE_ASSAY table in order to store the associations.

Retrieve data

Retrieval methods allow EUDs to retrieve existing data from the database for updating. These use the DAO methods (described in section 6.3.3) to extract data from database and return the data as objects to the client applications. Delete and update methods are also provided to manage the returned objects.

Currently, the web service uses the Hibernate DAO to provide two retrieval methods:

findbyID and findbyType. Although the findbyQuery implemented in the DAO can be used to retrieve subsets of the sample object collections, it is still difficult for novice EUDs to write such queries. In Chapter 8 we will discuss how to provide a simpler alternative to retrieve subsets of the collections by specific criteria conditions.

Updating data and deleting data

The update and delete methods allow maintenance of existing objects in the database. Once retrieve methods return object data from the database, the update method can modify the object. Similarly, the delete method can remove the objects and their related associations, e.g. assays objects with their associated samples.

72

6.4 Summary

Sections 6.2 and 6.4 introduced the framework, database and web service implementations. Table 6-1 summarises each component:

Table 6-1 Summary of the framework components

Name Description

ORM file Defines the mapping between framework classes and

database tables.

Constraint file Defines constraints for domain objects

Build script Reads the ORM file to generate classes and tables.

Deploys all classes and tables and required libraries to the application server.

Data Access Object Parses the ORM file to manage the persistence logic

and mapping

Object Validator Parses the constraint files to manage the validation

logic

Web service API Manages the DAO and validator to provide data

management functionality Spring Application

configuration file

Defines the dependencies between application objects Used by Spring library to manage application object initialisation

The framework was implemented to support EUDs to have a data store that allows for modifications and extensions. We also proposed the idea of framework development tools (discussed in Chapter 7) to allow framework managers to easily customise domain classes with minimal effort.

The web service API is developed for EUDs to manage the data. It encapsulates the database implementation and is able to generate SQL to manage the data persistence logic. This means that EUDs do not need to spend considerable effort writing SQL to update and retrieve

information. With the client development toolkit (discussed in Chapter 7), EUDs need only to write a few lines of code to include the database functionality in their commonly used

73

Related documents