Summary
In a recent IBM developerWorks article, Leons Petrazickis demonstrates an Ajax application that uses the Dojo toolkit on the client, and XML documents for data interchange between client and server. The XML is generated directly from the database via XQuery and XSLT, bypassing a relational-to-object mapping layer.
Advertisement
Developers setting out on a project to add interactivity to Web pages face at least two important decisions: First, what client-side toolkit, if any, to use? And, second, how to communicate data between server-side components and the client-side objects and UI widgets?
Leons Petrazickis's recent IBM developerWorks article, Develop a Web application using Ajax with Dojo and DB2 [IBM developerWorks registration required*], chooses the Dojo toolkit as the client-side framework, and XML as the data exchange protocol to communicate between client and server.
Petrazickis walks through building a real-world Ajax example, a conference registration application. In the process, he shows several ways to generate XML output from a database—IBM's freely available DB2 Express, in this case—via XQuery and SQL, and then transform the output with XSLT. Although he uses PHP as the server-side framework, his examples are easily translated to Java, or some other back-end technology.
The Dojo toolkit, according an Ajaxian.com survey quoted in the article, is third in place in terms of adoption, following Prototype and script.aculo.us. Dojo attempts to provide a comprehensive client-side programming model, with UI widgets and utility classes that mimic rich-client frameworks, such as Swing and Flex.
The most interesting part of Petrazickis's article, however, is the manner in which he accesses server-side data. While most web applications would introduce an object-relational mapping layer—converting database data to objects and then passing those objects into a presentation layer—Petrazickis instead extracts database data in an XML format the Dojo client application can consume. While this does not bypass the need for a controller layer—which Petrazickis implements with a minimal amount of PHP code—it eliminates the need for extensive object-relational mapping.
To achieve that, Petrazickis relies on DB2 Express's ability to accept queries in the XQuery format. XQuery is the W3C's recently finalized recommendation for querying XML data, and is a de facto standard implemented by many enterprise database management systems. (For an in-depth view on XQuery, read Artima's interview with XQuery co-creator Jonathan Robie, Jonathan Robie on When to Use XQuery.)
The XQuery approach is especially suitable for Petrazickis's example application, because that application deals with document-like data, such as a conference session's description. Instead of splitting that data into multiple table columns, Petrazickis stores such documents as native XML data:
CREATE TABLE activities (
activityNumber char(10) NOT NULL PRIMARY KEY,
track varchar(60) REFERENCES tracks,
activity XML
);
Data from this column can be retrieved via a regular SQL query:
SELECT XMLSERIALIZE(activity AS CLOB)
FROM activities
WHERE activityNumber = 'TDA-2737A'
Petrazickis shows an alternate way to retrieve that data, with the result being an XML document instead of an SQL result set:
values(XMLSERIALIZE(
XMLQUERY('
<iod_activities>
{
for $root in
db2-fn:xmlcolumn("ACTIVITIES.ACTIVITY")/iod
where matches($root, "ajax", "i")
order by
$root/sessions/session[1],
$root/title
return $root
}
</iod_activities>
' RETURNING SEQUENCE) AS CLOB))
The resulting XML document is then transformed via XSLT to another XML document the client can directly consume. In the latter half of the article, Petrazickis shows an example of using XQuery to join results from two tables into a single XML result document.
In Petrazickis article, the XSLT transformations represent the "controller" layer. That layer basically service to transform XML results from the database into a client-ready format.
What do you think of the approach of bypassing the step of converting database data into programming language objects, and instead directly returning XML results from a database and transforming that result document to a presentation-ready format via XSLT?
* Note: Although we pointed to this item solely because we thought our audience would find it useful, Artima is currently running a program for IBM to drive registrations at DeveloperWorks.