ShowTable of Contents
The XML data retrieved from a back-end server may not be in the format needed by your WEF application. Sometimes the structure of the data needs to be changed; sometimes the values of the data need to be changed. This sample shows how to use a "restructure handler" to change the structure of the XML data.
To run this sample in Web Experience Factory Designer, you will need to download the attached project ZIP file
, and import it into a WEF project. Once imported, the project should look like this:
Figure 1. Project Explorer
The project has both a provider model and a consumer model. This is a best practice for WEF projects that access data from a back-end server. The provider model contains all of the builders associated with accessing the back-end data, including a Service Definition builder that exposes the service to the consumer model. The consumer model contains all of the builders associated with the user interface, including a Service Consumer builder that is associated with the service from the provider model. The builders we're interested in for this sample are in the provider model.
REST Service Call builder
In order to use data from a back-end server in a WEF application, you first need to know how to access the data. In this sample we will access a publicly accessible REST service -- an RSS feed from the Web Experience Factory Forum. The REST Service Call builder in the sample model contains the URL to the forum, and specifies a "threadId" parameter. The value for this parameter has has been left blank, which indicates that it is an input for this builder.
Figure 2. Inputs for the REST Service Call builder
So that it can generate the correct schema for the specified REST service, the REST Service Call builder needs information about how to access sample data. In this case, we will just specify the same REST service URL as before, and supply a known value for the "threadId" parameter.
Figure 3. Schema inputs for the REST Service Call builder
Notice that the value of "Restructure Type" in the screen shot above is "Auto". If the consumer model is run with this value in the provider model, the results will look like this:
Figure 4. REST Service Call builder results before restructuring
To illustrate how the REST Service Call builder results can be modified using a restructure handler, we will split the <creationDate> element into two separate elements -- <creationDate> and <creationTime>. The restructure handler is registered by adding a property file to the WEB-INF\config\rest_handlers directory. The property file defines how the handler will be invoked, and the Java class used to modify the results.
The configuration file for this sample can be found in the project at WebContent\WEB-INF\config\rest_handlers\sample_restructure_forum_feed.properties, and the Java class can be found at WebContent\WEB-INF\work\source\com\ibm\wef\samples\feed\RestructureForumFeed.java.
The property file contains the following properties:
auto. Specifies whether the handler will be used when the "Auto" restructure type is selected. If the value is "true", the canProcess() method of the specified Java class will be called to determine whether this handler is suitable for automatic restructuring of the feed. If the value is "false", the handler can be selected from the "Restructure Type" list -- the getName() method of the specified Java class will be called to provide the label.
class.name. Specifies the Java class to be used for the restructure handler. This class must implement the com.bowstreet.builders.webapp.methods.RestructureFeedData interface.
In this sample, the "auto" property is set to false, so the REST Service Call builder will call the getName() method of the Java class and include the result in the "Restructure Type" list. The Java class for this sample returns "Restructure Forum Feed Sample" from the getName() method, so that item will be included in the"Restructure Type" list. When that item is selected, the Java class for the restructure handler will be called at runtime to process the results of the REST Service Call builder.
Figure 5. Inputs for the REST Service Call builder, with restructure handler selected
If the consumer model is run with the "Restructure Forum Feed Sample" item selected, the results will look like this:
Figure 6. REST Service Call builder results after restructuring
The shaded area shows the changes made by the restructure handler. For each result item, the creation date now contains only the date value, and a new creation time column has been added that contains only the time value.