Creating a Model-based Builder This presentation provides an example of how to create a Model-based builder in WebSphere Portlet Factory. This presentation will provide step by step instructions in the creation of a new Model-based builder. The goal of the new Model-based builder is to provide functions to render a text message on a page, but only a single time. This function would be useful if a developer needs to display a success or failure message to a user following an update operation. The result of the update operation can be rendered at a location on a page, and subsequently cleared automatically so that it doesn't re-display the next time the page is rendered.
Steps to Create a New Builder The creation of a new Model-based builder involves the following steps: create the model to act as the new builder profile selected inputs in the model use the Builder Skeleton builder
Step 1 Create the model A Model-based builder uses an existing Portlet Factory model as the basis for the new builder. The existing model is converted into a builder through the underlying use of the Imported Model builder. We start by creating a new model in our project.
Step 1 Create the model Add a new Variable builder to the model. We will setup the variable to be an XML variable with elements to contain both the message text and message color.
Step 1 Create the model Add a new Text builder to the model. We will configure the Text builder to display the messagetext element of the variable on a page. Leave the Page Location blank for now.
Step 1 Create the model Add a new Attribute Setter builder to the model. We will configure the Attribute Setter builder to set the style attribute and provide values for the color and the font-weight attributes. Leave the Page Location blank for now.
Step 1 Create the model Add a pair of Variable builders to the model. These two variables will hold the values to be used for the color of the message. One for success we'll use a default color of blue. The other for failure we'll use a default color of red.
Step 1 Create the model Add a new Action List builder to the model. In the Actions input, we will configure this builder to clear the messagetext element of our message variable.
Step 1 Create the model Add a new Method builder to the model. This method will clear the messagetext element of the message variable but only if the current request is a portlet RenderRequest or if it's executing in a servlet context.
Step 1 Create the model Add a new Event Handler builder to the model. The Event Handler builder will listen for the System:OnRequestComplete event. When this event happens, we need to invoke the Method we just created to clear the messagetext element of the message variable.
Step 1 Create the model Add a new Method builder to the model. The purpose of this method will be to select between two provided color inputs. If the color input is null or an empty string, then it will return the defaultcolor input.
Step 1 Create the model Add a new Action List builder to the model. This Action List will set the message text and set the message color using the success color variable.
Step 1 Create the model Add a new Action List builder to the model. This Action List will set the message text and set the message color using the failure color variable.
Step 2 Profile selected inputs The next step in creating a Model-based builder is to profile selected inputs in the model. By profiling some inputs in the model, we can configure the model to use different values each time it is used as a builder. Any inputs that are profiled will be available as inputs to the new builder that is created.
Step 2 Profile selected inputs Open the Text builder in the model, and profile the Page Location input. This will allow the user to choose the page location (page and tag) where the message will be rendered.
Step 2 Profile selected inputs Open the Attribute Setter builder in the model, and profile the Page Location input. This will allow the user to choose the page location (page and tag) where the style (color) attribute will be applied. (Be sure to use the same Profile Entry Name for this input as was used for the Text builder.)
Step 2 Profile selected inputs Open the Variable builder in the model for the success color, and profile the Initial Value input. This will allow the user to choose the color for rendering success messages.
Step 2 Profile selected inputs Open the Variable builder in the model for the failure color, and profile the Initial Value input. This will allow the user to choose the color for rendering failure messages.
Step 3 Use Builder Skeleton The final step in creating a Model-based builder is to use the Builder Skeleton builder. The Builder Skeleton builder will examine the model and create a new builder definition file in the project. The builder definition file is an XML file that describes the builder and its inputs. Builder definition files are placed in the WEB-INF/builders folder of the project. The Builder Skeleton builder also creates a builder regeneration class. The builder regeneration class is a Java class that executes during model generation.
Step 3 Use Builder Skeleton Add a new Builder Skeleton builder to the model.
Step 3 Use Builder Skeleton Configure the Builder Inputs section of the Builder Skeleton. We'll set the Name and PageLocation inputs as required. Also change the Input Type for the Name and PageLocation inputs.
Step 3 Use Builder Skeleton After you click OK or Apply to save the Builder Skeleton builder, the builder definition file and builder regeneration class will be created. The Builder Skeleton has done its work and should now be disabled or removed from the model it is no longer needed. Open the OneTimeMessage.bdef file (under WEB-INF/builders) to examine the contents of the builder definition file. Open the OneTimeMessage.java file (under WEB- INF/work/source) to examine the contents of the builder regeneration class.
Step 3 Use Builder Skeleton In the builder regeneration class, you will see that the One Time Message builder extends the Imported Model builder.
Step 3 Use Builder Skeleton You may have also noticed that throughout this example, we have used a prefix of basename for all of the builders. This is a feature of the Imported Model builder and we can see it being used in the builder regeneration class. The basename prefix will be replaced with the name of the builder when it is used.
Using the new Builder You can now create a new model and use the new One Time Message builder. In the Builder Palette dialog, click the Refresh button to refresh the list of builders. The new builder should be there now.
Using the new Builder Add a new One Time Message builder to the model. You should see the set of inputs that were defined when we used the Builder Skeleton builder.
Using the new Builder In the WebApp Tree view of the model, you can see the artifacts that have been added to the model by the One Time Message builder. Notice how the basename prefix has been replaced with message (the name we used for the builder). You can call the message_setsuccessmessage and message_setfailuremesage methods to set a message to be displayed.