The technology world is constantly changing. New technologies are emerging and existing ones are always evolving. These forces produce new paradigms and best practices. Sometimes new technologies seem like shiny paint on top of a re-invented wheel, but sometimes they can truly simplify lives. This article, the first in a four-part series, looks at three emerging technologies that have several things in common. Each is not only interesting from a purely theoretical perspective, but also aim to greatly simplify difficult tasks. You will look at XForms as a technology for creating feature-rich, interactive Web applications. You will look at the powerful IBM® DB2® pureXML features for managing XML documents. Finally, you will tie these two technologies together using the Ruby on Rails Web application framework to create a Web application for streamlining entry of patient information at a doctor's office. Please note that this article series doesn't cover security such as preventing patient access to restricted directories, or securing doctor and nurse forms and areas by requiring them to first log in, both of which are required in production environments.
This article assumes familiarity with XML and with Web applications in general. Prior exposure to the three core technologies, XForms, DB2 pureXML, and Ruby on Rails, is helpful of course, but not necessary. The article was written using the Mozilla XForms plugin version 0.8.0.3. This provides XForms runtime support in any Mozilla browser such as Firefox. Another very useful Mozilla plugin is XForms Buddy. This provides a debugger for XForms. Version 0.5.6 was used in this article. You will also need the DB2 database server from IBM. This article uses DB2 Express-C version 9.5. This is available for Windows®, Linux™, and UNIX® systems. Finally, you will need Ruby on Rails. This article uses Ruby 1.8.6 with Rails 1.2.5. See the Resources for download links.
For the application that manages patient information at a doctor's office, you want patients to be able to enter in data that would otherwise have to be collected by office workers. You will create a Web application for this so any computer that can run a Web browser can serve as a data entry station for patients. Here is where you will make your first technology decision. You are going to use XForms for the user interface for the application.
Is this a case of just using a new technology to be hip and have something flashy to put on your resume? Not at all. XForms is a perfect fit for this kind of situation. It allows you to define your data in a simple XML model and your view using standard HTML form elements. XForms then provides declarative mapping between these elements. That means you will not have to write either client-side or server-side code for taking some submitted value and inserting into an XML structure. XForms handles it for you. It even does all of this asynchronously: changes in the HTML form are bound to the XML model and sent to the server for synchronization. You get the benefits of Ajax without having to write any JavaScript.
Now keep in mind that XForms is not supported in general in all browsers currently. It should be in the future, but it is not yet. So you need to have a plugin installed to add support for it. If you were creating an application for mass consumption, this could pose a problem. However, it works great for specialized applications like the one you are developing. There are a limited number of computers being used for accessing your application, so you can be assured that they all using the right browser and plugin combination.
XForms is a powerful technology to use whenever you are working with XML. It is a client-side technology, so you will need server-side technologies to support it in any kind of dynamic application. Part of the back end you will need is a database to persist data. Your front end is going to use XML to represent data, so wouldn't it be great if your back end did the same thing? With DB2, that is exactly what you can achieve.
If you have developed applications that use XML, then you have probably used one of two common techniques for persisting the XML data to a database. The first technique is to use some kind of XML-to-relational mapping technology. Often the technology in question is custom application logic that you wrote yourself to extract data from an XML document and put it into a column on a table in a relational database. This is often referred to as shredding. Of course if you are going to read the data again later, then you have to be able to "un-shred" it and reassemble an XML document.
The other common technique is to simply dump your XML document inside a CLOB column in the database. This essentially treats the XML as a giant string. You can then later read it form the database, and pass it to a parser to recreate the XML document. This is usually a lot easier to do, since you don't have to write any shredding/unshredding code, but it is not as powerful. There is no efficient way to query data inside the XML document.
XML databases have been gaining popularity over the years because they address this kind of problem. They let you store your data natively as XML, so there is no need to shred. They let you efficiently query it using XQuery, so you don't lose the structure of the document. Many of the databases that supported this were small, specialized databases, but now you can add the IBM flagship database, DB2, to the list of native XML databases.
By using DB2, you will be able to store the same data used in XForms in your database as XML. There is no shredding or mapping or parsing of the data needed. The "dialect" of data in your browser is the same dialect used by your database. DB2 enables the use of XQuery within SQL queries, known as SQL/XML, so any data in your XML document is easily accessible. For example, let's say you wanted to use a patient's last name to retrieve information. You can store this information inside your XML document and write an XQuery to only retrieve documents based on the last name of the patient. Let's take a deeper look at how DB2 makes all this possible.
You will now set up DB2 and create a table that will use native XML. One advantage DB2 has over some other XML databases is that it allows mixing of relational data and XML data. You don't have to pick one or the other; you can use both together. Thus, instead of mapping an XML document directly to a table, you map it to a column. A given row in your table could even have multiple XML documents.
You can create a new table that uses XML in a couple of different ways. The quickest is to use the command line and issue a command. Listing 1 shows a script that creates a table that has a single XML column.
Listing 1. Creating a table with an XML column
CREATE TABLE xmlmd.PATIENTS (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (
START with +1 INCREMENT BY +1),
INFORMATION XML NOT NULL);
|
In the example, the INFORMATION column is of type XML. As you can see, it is a very straightforward syntax, not much different from declaring a column as an integer or a varchar. If you don't like using the command line for creating tables, the DB2 Command Center gives you a graphical alternative for doing the same thing. You can use its create table wizard, as shown in Figure 1.
Figure 1. Starting the Create Table wizard
Clicking on Create New Table initiates the wizard. This will bring up the table name information screen, as shown in Figure 2.
Figure 2. Name your table
The name table screen lets you identify the schema for your table as well as the name of your table. Clicking Next will bring up the Columns definition interface, as shown in Figure 3.
Figure 3. Columns definitions for the new table
Clicking the Add button will bring up the Add Column interface, as shown in Figure 4.
Figure 4. Add column interface
Here is the tricky part. To define a column as having XML data in it, you must set the data type of the column to XML. That's it! XML is already in the list of data types in the wizard; there is nothing special you have to do for it. Once you are done, you can click OK and you should see the XML column defined as shown in Figure 5.
Figure 5. XML column added
You can add more columns, primary and foreign keys, and more with the wizard. When you are done, click Finish and the table will be created.
Either way you create the table, you can insert data into the table using SQL. You can treat the XML as a string when writing the SQL as shown in Listing 2.
Listing 2. Inserting XML
INSERT INTO DOC.PATIENTS(INFORMATION)
VALUES ('<?xml version="1.0" encoding="UTF-8"?>
<Info>
<FirstName>John</FirstName>
<Age>33</Age>
<Insurer>Blue Armor</Insurer>
<ID>555-88-1212</ID>
</Info>');
|
This may look like you are inserting a simple string, but don't worry. DB2 will not treat it like a string. This is very similar to how you would write a date in a query. You write it as a string with a certain syntax that allows the database to parse it into the specialized data type. If your syntax is off, then the query will fail. Working with XML works the same way. You can also query XML by using SQL and XQuery as shown in Listing 3.
Listing 3. SQL/XML Query
SELECT XMLQUERY('<Patients>
{for $i in $x/Info
where $i/Insurer = "Blue Armor"
return <Patient>{$i/FirstName/text()}</Patient>}
</Patients>'
passing P.INFORMATION as "x") from DOC.PATIENTS P;
|
Notice how you have mixed together SQL (SELECTION ... from ...) and XQuery together. This is known as SQL/XML. The key is using the XMLQUERY reserved word to indicate an XQuery that is executed against an XML document that is passed in via the SQL select statement. Running this query should return the XML document shown in Listing 4.
Listing 4. XML document result
<?xml version="1.0" encoding="UTF-16"?>
<Patients>
<Patient>John</Patient>
</Patients>
|
Now that you have seen how easy it is to use DB2 to store your XML natively, you still need something in the middle of the XML used in XForms and the XML in DB2. For this you will use Ruby.
Now it's time to think about how to glue together an XForms front end to a DB2 pureXML backend. Ruby on Rails provides a powerful way to quickly and cleanly create web applications. It also makes it easy to work with databases, but can Rails work with DB2 and in particular, DB2 pureXML? Of course it is possible, and like most things related to Rails, it is surprisingly easy.
Install the DB2 driver for Ruby
The first thing you will need to do is enable DB2 support in Rails. If you have Rails installed, then you should have Ruby Gems installed. If not, then you should install it, as it is package management system is ideal for adding on features to Rails, such as DB2 support. Once you have it installed, then on the command prompt enter the following: >gem install ibm_db.
This downloads a Ruby driver for DB2. Once this is installed, then any of your Ruby applications (not just Rails applications) will have DB2 support. If you are using Windows, you will be prompted about what version of the driver, as shown in Listing 5.
Listing 5. Choosing a DB2 driver
>gem install ibm_db
Bulk updating Gem source index for: http://gems.rubyforge.org
Select which gem to install for your platform (i386-mswin32)
1. ibm_db 0.6.0 (mswin32)
2. ibm_db 0.6.0 (ruby)
3. ibm_db 0.4.6 (ruby)
4. ibm_db 0.4.6 (mswin32)
5. Skip this gem
6. Cancel installation
|
You will need to pick the latest version of the driver, so options #1 or #2. If you installed Ruby using the Windows installer, you will need to pick option #1. Now you are ready to create a Rails application that will use DB2 pureXML.
The following should be familiar to Rails developers. You will create your application as shown in Listing 6.
Listing 6. Creating a Rails application
>rails xmlmd
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
|
This creates the skeleton of your Web application. Now you need to edit config/database.yml so you can connect to DB2 as shown in Listing 7.
Listing 7. Edit config/database.yml
# IBM DB2 Database configuration file
# Install the IBM DB2 driver and get assistance from:
# http://www.alphaworks.ibm.com/tech/db2onrails
development:
adapter: ibm_db
database: health
username: michael
password: your_password_here
schema: xmlmd
# == remote TCP/IP connection (required when no local database catalog entry available)
# host: bigserver // fully qualified hostname or IP address
# port: 50000 // data server TCP/IP port number
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: xmlmd_test
username: root
password:
host: localhost
production:
adapter: mysql
database: xmlmd_production
username: root
password:
host: localhost
|
Notice you only changed the development settings in this example, and left the test and production settings as the defaults. Also notice that for the adapter you set the value equal to ibm_db. This is the driver you just installed. Rails does not know about this driver, as it is not one of the default drivers installed with Rails. Luckily this is easy to fix. You just have to edit one file. Go to your Ruby installation direct and open up the file /lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record.rb. Look for the line that says RAILS_CONNECTION_ADAPTERS and add ibm_db to the end of the list as shown in Listing 8.
Listing 8. Add DB2 support to Rails
RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle
sybase openbase frontbase ibm_db )
|
Now you are going to generate a model that will use XML. First create the model in the usual way using a Rails generation script as shown in Listing 9.
Listing 9. Generate Rails model
>ruby script\generate model doctor
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/doctor.rb
create test/unit/doctor_test.rb
create test/fixtures/doctors.yml
create db/migrate
create db/migrate/001_create_doctors.rb
|
Notice your migration script was generated, just as you would expect. You will use this to generate another table that will use XML, as shown in Listing 10.
Listing 10. Rails Migrate Script: 001_create_doctors.rb
class CreateDoctors < ActiveRecord::Migration
def self.up
create_table :doctors do |t|
t.column :profile, :xml, :null => false
end
end
def self.down
drop_table :doctors
end
end
|
This is a fairly typical Rails migrate script. You are only defining one column called "profile." The only thing particularly interesting is that you defined the type of your column as xml. You have used Rails to create a Ruby script for connecting to the database, and creating a table that uses an XML column. Now you just need to test this script.
Testing your Ruby/DB2 pureXML script is easy to do. It is just a Rails migration like any other. To run it you use Ruby's rake command, as shown in Listing 11.
Listing 11. Running the migration script
>rake db:migrate
(in /xmlmd)
== CreateDoctors: migrating ===================================================
-- create_table(:doctors)
-> 0.7840s
== CreateDoctors: migrated (0.7860s) ==========================================
|
That's all there is. You might want to use the --trace option to help debug any errors. This is often helpful to diagnose connection problems, such as bad username/passwords or wrong database or schema specified, etc. You should now be able to use DB2 to verify the new table exists, as shown in Listing 12.
Listing 12. Verify and describe new table
db2 => describe table xmlmd.doctors
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 No
PROFILE SYSIBM XML 0 0 No
2 record(s) selected.
|
Just like that, you have successfully created a DB2 table with an XML column using Ruby on Rails. From here you can use Rails to do lots of interesting things. You can create additional migration scripts for inserting XML into your database. Ruby makes it particularly easy to create XML documents, and all you have to do is turn those into strings to send to DB2. You can also leverage scaffolding in Rails, with a few adjustments for working with XML.
In this first part of the series, you examined how XForms, DB2 pureXML, and Ruby on Rails can help you more rapidly build XML-centric Web applications. You examined how XForms simplifies creating an interactive front end. You will get the interactivity of Ajax, but without having to write any JavaScript or mapping code. You looked at how easy it is to store and query XML using DB2 pureXML. DB2's SQL/XML will let you mix SQL and XQuery together to easily access XML data in your database. Finally, you looked at how to set up Ruby on Rails to work with DB2 pureXML. With just a few minor adjustments, you were able to create XML-enabled tables in DB2 using Ruby on Rails. You have set up all the key parts of your technology stack. In the next part of the series, you will begin creating the doctor's office Web application using XForms, DB2 pureXML, and Ruby on Rails.
| Description | Name | Size | Download method |
|---|---|---|---|
| Part 1 sample code | part1_doctorsOffice.zip | 2KB | HTTP |
Information about download methods
Learn
-
XForms and Ruby on Rails
at the doctor's office, Part 2: Implementing the patient information XForm (Michael
Galpin, developerWorks, May 2008): Combine XForms, DB2 pureXML, and Ruby for easier Web app creation. In Part 2 of this series, start your hypothetical app that manages patient information in a doctor's office.
-
XForms and Ruby on Rails at the doctor's office, Part 3: Implementing the nurse and doctor XForm (Tyler Anderson, developerWorks, June 2008: Create Web apps easily with XForms, DB2 pureXML, and Ruby. In Part 3, you make a form for nurses to edit existing patient data.
-
XForms and Ruby on Rails at the doctor's office, Part 4: Implementing the doctor and patient lookup XForm (Tyler Anderson, developerWorks, June 2008): Integrate XForms, IBM DB2 pureXML, and Ruby to rapidly build XML-centric Web apps. Complete the sample app with a new XForm to look up patients by last name.
-
For a great introduction to XForms read the three-part series Introduction to XForms (Chris Herbroth, developerWorks, September 2006).
-
Did you know you can use XForms and Ajax together? See how in the XForms tip: Combining Ajax and XForms (Nicholas Chase, developerWorks, October 2006).
-
Go even further with XForms and Ajax by using it with the Google Web Toolkit in the article Integrate XForms with the Google Web Toolkit, Part 1: Introducing GWT's JavaScript Native Interface (Michael Galpin, developerWorks, September 2007).
-
Read one of the first in-depth tutorials on GWT in the article Ajax for Java
developers: Exploring the Google Web Toolkit (Philip McCarthy, developerWorks, June 2006).
-
Get started with Ruby on Rails and DB2 by reading the article An introduction to Ruby on Rails for DB2 developers (Edd Dumbill, developerWorks, June 2006).
-
Read all about using DB2 and Ruby on Rails together in the article DB2 and Ruby on Rails, Part 1: Getting started with DB2 and Ruby on Rails (John Chun, et al., developerWorks, May 2007).
-
See how to use DB2's pureXML with Ruby on Rails in the article DB2 and Ruby on Rails, Part2: DB2 and pureXML with Ruby on Rails (John Chun, et al., developerWorks, June 2007).
-
Find out how to use DB2 to enhance the built-in unit testing facilities in Rails by reading the article DB2 and Ruby on Rails, Part 3: Testing with DB2 and Ruby on Rails (John Chun, et al., developerWorks, June 2007).
-
Get the scoop on the latest enahcements to DB2 in the article Overview of new DB2 Version 9.5 pureXML enhancements (Manaj Sardana, developerWorks, November 2007).
-
Learn about using XQuery with DB2 in the developerWorks article Query DB2 XML data with XQuery (Don Chamberlin and Cynthia Saracco, developerWorks, April 2006).
-
Check out IBM developerWorks' Ajax Resource Center
-
Visit the XForms home at W3C.
-
developerWorks technical events and webcasts: Stay current with developerWorks technical events and webcasts.
-
Podcasts: Tune in and catch up with IBM technical experts.
Get products and technologies
-
Get the XForms extension for Mozilla, Firefox, or Seamonkey.
-
XForms Buddy is Mozilla/Firefox extension that allows you to see the instance data of an XForm. It updates automatically if the instance data changes.
-
Get DB2 Express-C version 9.5 from IBM.
-
Get Ruby 1.8.6 with Rails 1.2.5.
Discuss
Comments (Undergoing maintenance)





