Extending WebSphere Portal V6 personalization capabilities: Part 3. Creating a Web service application object

This series describes and shows you how to extend WebSphere Portal Personalization capabilities to make data from external sources available to your portal using Personalization rules.

In Part 1, you learned the fundamentals for working with external data and Personalization rules.

In Part2, you built a simple application object to and used that object's attributes to decide when to show and hide a portlet.

In this part, you work through a more advanced scenario that uses a Web service to obtain data values for the application object. You use those values to show or hide a portlet depending upon the weather conditions in a user's area.

Jon Brunn (jbrunn@us.ibm.com), Senior Software Engineer, IBM China

Jon Brunn is a Senior Software Engineer working on social and team collaboration software. Jon has more than 10 years of experience with content management systems including IBM Content Manager and FileNet P8. Jon has contributed to Lotus Quickr, Lotus Connections, WebSphere Portal, Web Content Management, FileNet, and IBM Content Manager. You can reach him at jbrunn@us.ibm.com.


developerWorks Contributing author
        level

21 February 2007

Before you start

Introduction

Now that you understand the mechanics of extending Personalization from the second tutorial in this series, you explore a more advanced scenario. In this tutorial, you use a Web service to obtain the data values for your application object. Then, you use those values to decide whether to display a page or portlet. The goal of such an exercise is to be able to show and hide pages based on weather conditions in a user's area.

In this tutorial, you:

  • Implement a Personalization application object to get weather data from a Web service
  • Deploy that object to a server
  • Use the data for that object to decide if a portlet should be shown or hidden

System requirements

You need IBM Rational Application Developer V6.0.1.1 or higher and WebSphere Portal V6.0 or higher to work through the tutorials in this series.


Creating the application objects

Prepare the environment

  1. Start Rational Application Developer.
  2. If you have not already done so, perform steps 2 and 3 from the first tutorial to create a project, and to name that project CustomApplicationObjects in your workspace.
  3. Next, create a reference to your server in Rational Application Developer. You need to create this reference for the web service client wizard. To begin, click File => New => Other => Server => Server.

    Tip: If you have not used servers before you may have to click Show All Wizards for the Server Web Service Client option to appear.

  4. Click Next.
  5. Accept the defaults, or override if your Portal installation is on a different host.
  6. Click Next.
  7. Select SOAP and enter a connector port of 10033. Enter WebSphere_Portal as the server name.
  8. Click Finish.

Generate a Java client for the weather Web service

The Web service you use in this exercise is from the National Oceanic and Atmospheric Administration (NOAA).

  1. Select File => New => Other => Web Service => Web Service Client. Tip: If you have not used web services before, you might need to click Show All Wizards for the Web Service Client option to display.
  2. Click Next.
  3. For the Client proxy type, select Java proxy.
  4. De-select Test the Web Service and Monitor the Web service options.
  5. Click Next.
  6. In the field titled Enter an URI to a WSDL, WSIL or HTML document, enter the NOAA Web service WSDL file name: http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl:
  7. Before clicking Next, wait a few seconds so that the wizard can populate the "Select a WSDL" box. You do not need to select anything in this box because there is only one WSDL file. After the page displays as shown above, click Next.
  8. Wait while the program analyzes the WSDL file; this operation can take a moment or two.
  9. Verify that the Client type is Java and the Client project is CustomApplicationObjects, and then click Finish.
  10. Wait while the program generates the Web service Java client.
  11. Verify that your project now has the contents as shown below.

Customize the Java client

Search the generated code in the gov.weather.www package and make the following global replacements.

Tip: Include the " marks in your search and replace.

Replace all occurrences of:With:
dateTimestring
java.util.Calendarjava.lang.String
"date""string"
{http://www.w3.org/2001/XMLSchema}date{http://www.w3.org/2001/XMLSchema}string

The code generator for web service clients included with Rational Application Developer handles most web services gracefully. Occasionally, a web service does not conform to all the details of the specifications. In this case, the NOAA web service does not accept all valid forms of dates allowed by the w3c specification for xs:dateTime (see XML Schema Part 2: Datatypes Second Edition). At the time of writing of this article, the NOAA web service returns an error 502 when the client sends a dateTime in a format other than yyyy-MM-dd'T'HH:mm:ss. Changing the Java client to treat the dateTime as a string lets you format the date and send it to the web service in the expected format.

To add Portal classes to your project's class path:

  1. Right-click on the CustomApplicationObjects project in the workspace, and choose Properties => Java Build Path => Libraries.
  2. Click on WebSphere v6.0 Runtime Library, and then click Remove. You remove the default library because it does not have everything that is necessary to run the web service in Java 2 Standard Edition (J2SE). To test the web service, you replace the library with ibm-jaxrpc-client.jar that can be used in a J2SE environment.
  3. To add ibm-jaxrpc-client.jar to the class path, click on Add External JARs. Navigate to the runtime directory under your WebSphere Application Server's install root (for example, C:\ibm\AppServer\runtimes). Tip: If your application server is not local, copy the contents of this runtime directory locally and then reference the JARs from the local directory.
  4. Select ibm-jaxrpc-client.jar, and click Open.
  5. Click Add External JARs. Navigate to the shared/app directory under your WebSphere Portal server's install root (for example, C:\ibm\PortalServer\shared\app).

    Tip: If your portal server is not local, copy the contents of this lib directory locally, and then reference the JARs from the local directory.

  6. Select these JAR files so that you can compile the references to the Portal User classes: jsr168-api.jar wp.base.jar, wp.model.api.jar, wp.pe.api.standard.jar, wp.user.api.jar.
  7. Click Open.
  8. Verify your Java Build Path looks like this:
  9. Click Ok to close this page.

Package the object

  1. If you have not already done so, download the code samples .
  2. Copy the files Weather.java and WeatherTest.java into the com.ibm.websphere.personalization.examples package in your CustomApplicationObjects project from the weatherappobj-sample.zip file that you downloaded.
  3. Verify there are no compile errors in your project. If you do encounter errors, review your actions in Customize the Java client and Prepare the enviornment.
  4. Do either step a or step b:
    1. Go to Yahoo Developer Network and obtain an application id. Continue with Step 4.
    2. If you do not want to obtain an application id, hard code the latitude and longitude values in the application object, as shown below.
      private BigDecimal latitude = new BigDecimal(41.88);
      private BigDecimal longitude = new BigDecimal(-87.62);

      Your object will no longer respond to the user's profile. Skip to Test the application object.

  5. In Weather.java, replace the call to throw an IllegalArgumentException in in the getAppId method with the id you obtained in step 3. For example, if your id from step 3 is ExampleId, then the method should look like this:
    protected String getAppid() {
        return "ExampleId";
    }

Test the application object

To test the Weather application object using the WeatherTest class:

  1. Click Window => Show View => Console.
  2. Click on the WeatherTest.java file in the workspace.
  3. Click Run => Run As => Java Application.
  4. Verify that you see output such as [Weather@896164533 latitude=41.8834, longitude=-87.6261, chanceOfPrecipitation=0]. For the WeatherTest.java, the zip code is hard coded to 60602 so the chance of precipitation will vary depending on the weather in Chicago.

Export the application object

To export the Java project as a jar

  1. Select the CustomApplicationObjects project.
  2. Click File => Export => JAR file.
  3. Click Next.
  4. Enter a JAR file destination. If your Portal Server is local, enter <wps>pzn\v6.0\collections\CustomApplicationObjects.jar , and substitute your portal server installation directory for <wps>; for example: C:\ibm\PortalServer\pzn\v6.0\collections\CustomApplicationObjects.jar. If you are using a remote Portal Server, save the JAR locally and then copy the JAR to the collections directory on your Portal Server. The collections folder is part of a shared library created during the install and intended to store custom Application Object and Resource Collection classes.
  5. Choose to overwrite files if prompted.
  6. Click Finish to export the JAR.
  7. Restart your portal server to pick up the changes to the JAR.

Configure the application object in Portal Personalization

  1. Log in to the portal server.
  2. Navigate to the Personalization Business Rules page. If you are using the default theme, you can find this page by clicking Launch => Personalization => Business Rules.

If one does not already exist from Part 1, create a folder to contain your work by choosing New => Folder from the menu, name the folder Application Objects, and click Save.

  1. With the Application Object folder open in the Personalization Navigator, choose New => Application Object from the menu.
  2. Enter Weather in the name field. This name that will display in the rule editor, and it can be anything you choose.
  3. Enter weather for the Session Key. This value hould match the key used in the init method for Weather.java. Keeping these values the same insures that the rules engine will pick up the object from the session.
  4. Complete the Class Name field with the name of our Application Object Java class, com.ibm.websphere.personalization.examples.Weather.
  5. Click Save to store the definition of the Application Object in Personalization.
  6. Set a zip code in the profile for your user.
  7. Click Launch => Edit My Profile.
  8. On the Edit My Profile portlet, from the portlet's context menu, click Configure.
  9. Check the postalCode field, and click OK.
  10. Enter a postalCode, such as 27703, and click OK.

Attach a rule to a portlet

  1. Navigate to the portlet to which you want to apply the rule. For this example, the screen shots were taken using the About WebSphere Portal portlet, which you access by selecting Launch => About.
  2. On the context menu for the page, select Edit Page Layout. (The Edit Page Layout portlet is also accessible from the Administration pages in WebSphere Portal.)
  3. Click Show Portlet Rule Mappings, to enable the Personalization tools on the Edit Layout portlet.
  4. Click on the context menu next to the text No rule mapped on the portlet where the rule should apply.
  5. In the rule editor, click the attribute link. The weather application object you added is now available in the rule editor, and each get method is exposed as a property in the rule editor.
  6. Choose the Weather => chanceOfPrecipitation property.
  7. For the operator, select is greater than or equal to instead of is.
  8. Enter 50 for the value.
  9. Give your rule a name such as: Show if weather is bad.
  10. Click the Preview tab to check the result. The result will vary depending on the weather in the zip code you entered in your profile. The first time you click on preview will be the first time the object is loaded and the weather data is retrieved. After this initial view, the weather data will be in the session, so subsequent page views will be faster.
  11. Save the rule.
  12. View the page with different zip codes in your profile, or with different users, and see the portlet display and disappear, depending on the weather in that area.

    Tip: Because data is cached in the session, you might need to logout and login between changing your zip code or modifying the Edit My Profile portlet to clear the session values used by Personalization.

Congratulations! You have used values returned from a web service to decide if a portlet is displayed or hidden.

Conclusion

In this tutorial, as well as in the Hello World tutorial in Part 2, you pulled in user data from external sources. In Part 4, you will use feeds to bring external content into your site.

Application objects and resource collection coding can be very simple or very complex. The complexity of the code you write varies depending on the APIs and data sources you reference within your objects. You can use application objects to extend the Personalization engine with additional "current" information, such as current user information, current business statistics, current date information, current weather conditions, and so on. Consider using resource collections to enable selection of lists of content from external content sources.

Check the WebSphere Portal zone soon for Part 4 to see how to use an Atom feed resource collection to further extend the capabilities of Portal Personalization.


Download

DescriptionNameSize
Code samplesweatherappobj-sample.zip52KB

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=977843
ArticleTitle=Extending WebSphere Portal V6 personalization capabilities: Part 3. Creating a Web service application object
publish-date=02212007