Welcome!

Java Authors: Maureen O'Gara, Bruce Armstrong, Liz McMillan, Walter H. Pinson, III, Yakov Werde

Related Topics: Java

Java: Article

Using Apache Tuscany SDO and JSF To Build Dynamic Web Forms

Assembling a very generic system

JSF
JSF was chosen as the Web framework since it has an extensive component model, where each HTML element needed is represented by a Java object. It comes with built-in conversion and validation and suits the project well.

Per XML element we want displayed we need a UIOutput object for the label and an UIInput object for the data.

Data Structure
We needed a data structure that would wrap the SDO objects along with the needed JSF objects and found the Composite design pattern useful (http://en.wikipedia.org/wiki/Composite_pattern). Using this pattern we could store our own objects in a structure that matches the SDO data graph. SDO containment properties (complex types) went into the composites and SDO non-containment properties (simple types) went into the leafs as illustrated in Figure 1.

Filling the Data Structure from XSD and XML
From the root data object obtained earlier we're going to construct the JSF components and fill the data structure.

Each property is extracted from the data object and handled recursively. The JSF components are generated, as we recursively traverse the structure, ending in the leafs (see Listing 2).

Now the data structure is filled with the SDO and JSF components - ready to be plugged into a GUI such as added to the child list of a panel:

private void setupXmlElement(XmlElement xmlElement) {

   CompositeXmlElement comp = xmlElement.getCompositeXmlElement();
   if (comp != null) {
     for (XmlElement child : comp.getChildren()) {
       setupXmlElement(child);
     }
   } else {

     LeafXmlElement leaf = (LeafXmlElement) xmlElement;
     if (leaf.getUiInput() != null && leaf.getUiInput().isRendered()) {

       this.getPanel().getChildren().add(leaf.getLabel());
       this.getPanel().getChildren().add(leaf.getUiInput());
     }
   }
}

This very simple example renders to this page (see Figure 2).

Because of the flexibility of JSF validation messages, such things as styles can be easily be added to each relevant component.

Submitting Data Back into the SDO Data Model
Submitting the form is easy. You just call submit on the XmlElement and the composite pattern handles the rest by setting each value from the UIInput in the leaf's submit method:

Object inputValue = this.uiInput.getValue();
String stringValue = inputValue.toString();
Object value = SDOUtil.createFromString(this.property.getType(), stringValue);
this.dataObject.set(property, value);

Conclusion
These were some of the basic operations that made it possible for us to assemble a very generic system. Apache Tuscany SDO turned out to be very flexible. Through its simple APIs it was easy to work with schemas and data and easy to plug this into a presentation framework. The final application contained a lot more than exemplified here, since most schema constructs were supported, but that's beyond the scope of this article. Supplying the application with a Web Service interface was also easy since the SDOs could be used to both import and generate XML directly as shown above.

Apache Tuscany also covers SCA and DAS but that's a whole different article.

More Stories By Christian Landbo Frederiksen

Christian Landbo Frederiksen is a senior consultant in Ementor Denmark and has worked with Java and J2EE since 2000. He holds an M.Sc. in IT from the IT University of Copenhagen and a B.Sc. in computer science from the University of Copenhagen. He is a SCJP, SCJD and SCWCD.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Christian Schuhegger 05/25/08 03:25:53 PM EDT

is the source code for this article available?