A data service's perform operation accepts requests which are composed of activities. These requests are forwarded by the data service to the target data service resource where they are processed. Activities are the base tasks that the data service resource can perform on your behalf. An activity may expose a capability of the underlying data resource, such as the ability to perform an SQL query on a relational database, or may perform any other kind of operation, such as transforming data from one structure to another using an XSL Transform. Activities can be pipelined so that the output of one activity can be connected to the input of another activity, and in this way chains of two or more activities can be formed. A particular data service resource supports a particular set of activities specified in a configuration file.
The client toolkit provides a client activity class corresponding to each activity provided with OGSA-DAI. The responsibilities of a client activity class are twofold. Firstly, to generate the XML fragment required to run the corresponding activity at the data service resource, and secondly, to provide convenient methods for accessing the results of the activity. All client activity classes implement the abstract uk.org.ogsadai.client.toolkit.activity.Activity class which provides common functionality for connecting activities into pipelines and accessing result data. An ActivityRequest object is used to collect together a number of activities into a single request ready for submission.
This page shows you how to run simple SQL and XPath queries against a database using client activities.
In this example you will run a simple SQL query across a MySQL database and print out the results. For example, select * from littleblackbook where id='3475' will select a single row from the table littleblackbook.
SQLQuery query = new SQLQuery("select * from littleblackbook where id='3475'");
WebRowSet rowset = new WebRowSet( query.getOutput() );
ActivityRequest request = new ActivityRequest(); request.add( query ); request.add( rowset );
Response response = service.perform( request );
String responseString = response.getAsString();The response is an XML document which contains a result element for each activity. Each result describes the status of its activity (COMPLETED, ERROR, UNSTARTED) and holds data that the activity may have produced. In this example, these are the results of the SQL query in WebRowSet format.
See OGSA-DAI/examples/src/uk/org/ogsadai/examples/clienttoolkit/SimpleSQLQueryExample.java for an example solution.
When you have more than one query, you can wrap a number of operations into one request.
SQLQuery query1 = new SQLQuery("select * from littleblackbook where name like '%Krause'"); WebRowSet rowset1 = new WebRowSet( query1.getOutput() ); SQLQuery query2 = new SQLQuery("select * from littleblackbook where name like '%Sugden'"); WebRowSet rowset2 = new WebRowSet( query2.getOutput() );
Request request = new ActivityRequest( new RequestComponent[] { query1, rowset1, query2, rowset2 } );
ActivityRequest request = new ActivityRequest(); SQLQuery query = new SQLQuery("select * from littleblackbook where id = '2359'"); request.add( query ); WebRowSet rowset = new WebRowSet( query.getOutput() ); request.add( rowset );
Response response = service.perform( request );
See OGSA-DAI/examples/src/uk/org/ogsadai/examples/clienttoolkit/SQLQuerySequence.java for an example solution.
This example demonstrates how to evaluate a parameterised SQL query repeatedly against the same data resource, changing the parameter values for each iteration.
ActivityRequest request = new ActivityRequest();
SQLQuery query = new SQLQuery("select * from littleblackbook where id>? and id<?");The "?" represent SQL parameters placeholders as per usual.
query.setParameter(1, String.valueOf(i)); query.setParameter(2, String.valueOf(i + 20));The first argument is the position of the parameter in the SQL string (starting with 1) and the second argument is the value of the parameter.
request.add( query );
WebRowSet rowset = new WebRowSet( query.getOutput() ); request.add( rowset );
See OGSA-DAI/examples/src/uk/org/ogsadai/examples/clienttoolkit/ParameterisedSQLQuery.java for an example solution.
If you have access to an XML database, you can perform XPath queries using the XPathQuery object. The request is performed as before. For example, the XPath expression /entry[@id<10] selects all entry elements with an attribute id whose value is less than 10.
XPathQuery query = new XPathQuery( "/entry[@id<10]" );
See OGSA-DAI/examples/src/uk/org/ogsadai/examples/clienttoolkit/SimpleXPathQueryExample.java for an example.
Back: The Basics | Up: Using the Client Toolkit | Next: Processing Results |
© International Business Machines Corporation, 2002-2006 | © The University of Edinburgh, 2002-2006 |