Creating the Hello World object
In this section you create and deploy the application object.
Start by creating a Java project to hold the application components.
- Start Rational Application Developer.
- Create a Java project.
- Select New => Other => Java => Java Project.
- Enter
CustomApplicationObjects. - Click Finish.
- Add Personalization classes to your project’s classpath.
- Right-click on the project in the workspace; choose Properties => Java Build Path => Libraries.
- Click Add External JARs. Navigate to the pzn/v6.0/lib directory under your WebSphere Portal install root; (for example,
C:\ibm\PortalServer\pzn\v6.0\lib). If your portal server is not local, you can copy the contents of this lib directory locally and then reference the JARs from the local directory. - Select pznauthor.jar, pzncommon.jar, pznquery.jar, pznresources.jar, and pznruntime.jar.
- Click Open.
- Verify your Java Build Path looks like this:
- Click Ok to close this window.
Create the Java class for the application object
- Choose File => New => Class (or File => New => Other => Java => Class).
- Enter
com.ibm.websphere.personalization.examplesin the Package field,MySampleObjectin the Name field, and make sure the Source Folder isCustomApplicationObjects. - Click Finish.
- Copy this code into the Application Object
- Double-click on the MySampleObject.java file to open it if it is not opened.
- Copy the following source into MySampleObject.
package com.ibm.websphere.personalization.examples; import java.util.Date; import com.ibm.websphere.personalization.RequestContext; import com.ibm.websphere.personalization.applicationObjects. SelfInitializingApplicationObject; import java.io.Serializable; public class MySampleObject implements SelfInitializingApplicationObject, Serializable { private String userName; public void init(RequestContext context) { userName = context.getRequestUsername(); context.setSessionAttribute("mysampleobject", this); } public String getMySampleString() { return "Hello " + userName; } public int getMySampleInt() { return 1; } public Date getMySampleDate() { return new Date(); } }
- Select File => Save.
Deploy the object to the server
- Export the Java project as a jar
- Select the CustomApplicationObjects project.
- Click File => Export => JAR file.
- Click Next.
- Enter a JAR file destination. If your portal server is local, enter
<wps>pzn\v6.0\collections\CustomApplicationObjects.jarsubstituting your portal server installation directory for<wps>, as inC:\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 is intended to store custom application object and resource collection classes. - Click Finish to export the JAR.
- Restart you portal server to pick up the new JAR.
Configure the application object in Portal Personalization
- Login to the portal server.
- Navigate to the Personalization Business Rules page. With the default theme, this page is accessible by clicking Launch => Personalization => Business Rules.
- Create a folder to contain your work by choosing New => Folder from the menu; name the folder
Application Objectsand click Save. Using a folder here is optional. Because export, import, access control, and versioning operate on folders, it is a good idea to give some thought to the folder structure you use. Moving some objects in Personalization after their creation will break references to those objects. - With the Application Object folder open in the Personalization Navigator, choose New => Application Object from the menu.
- Enter
My First Application Objectin the name field. This name will display in the rule editor; it can be anything you choose. - Enter
mysampleobjectfor the Session Key. This name should match the key you used in the code in step 5c. Keeping these values the same insures that the rules engine will pick up the object from the session. - Complete the Class Name field with the name of our Application Object Java class, com.ibm.websphere.personalization.examples.MySampleObject.
If you have not put the
MySampleObjectclass in the classpath by copying the JAR into the collections folder, or if you have not restarted the server, you might see a message:The application object class 'com.ibm.websphere.personalization.examples.MySampleObject' could not be located. Specify a valid class for this application object.If you see this message, check the location of the JAR, and restart the portal server. - Click Save to store the definition of the application object in Personalization.
- Navigate to the portlet to which you want to apply the rule. The screen shots show the About WebSphere Portal portlet, which you can access by selecting Launch => About.
- Choose Edit Page Layout on the context menu for the page. The Edit Page Layout portlet is also accessible from the Administration pages in WebSphere Portal.
- Click Show Portlet Rule Mappings to enable the Personalization tools on the Edit Layout portlet
- Click on the context menu next to the text No rule mapped on the portlet where the rule should apply.
- In the rule editor, click the attribute link. The application object you added is now available in the rule editor. Each get method is exposed as a property in the rule editor.
- Choose the My First Application Object => mySampleString property.
- Choose includes for the operator instead of is.
- Enter
Hellofor the value. - Give your rule a name such as
Test Application Object. - Click the Preview tab to see that the rule returns Show, which indicatesthat the portlet will display for the current user.
- Select the New Rule tab, and change the value to something which is not included in the property value; for example,
Hello World.Remember, the property value should be "Hello" + userName because this is what we coded in our application object. Hello World is not included in the value, and so the rule will return Hide. - Click Preview to verify that the rule returns hide.
- Save the rule.
- View the page with the rule set each of the two ways, and verify that the portlet displays and is then hidden, respectively. When the portlet is hidden the page should display as follows, without the About WebSphere Portal portlet.
- Congratulations! You have used values returned from an application object to determine if a portlet is shown or hidden. This is a simple "Hello World" style example.
This part of the Extending Personalization series showed you how to create a simple application object. The values returned by the application object need to be more meaningful to write more meaningful rules. In Part 3, you create an application object which uses a Web service to return more meaningful data.


