Search This Blog

Monday, November 8, 2010

Extending Portal Personlaization to external data source

This article to explain how to create a simple Plain Old Java Object (POJO) to use external data source in implementing your custom pzn rule in WebSphere Portal 6.x/7.x.

Steps to be followed.
1. Create simple Java project in RAD 7.x

2. Configure the build path to include the following jar file which are required to buld.
- pznauthor.jar
- pzncommon.jar
- pznquery.jar
- pznresources.jar
- pznruntime.jar

Note: the jar files can be found in in the location
v6.x - [portal-root-dir]/pzn/v6.0/lib and
v7.x - [portal-root-dir]/pzn/prereq.pzn/lib

3. Your POJO class should implement the following interface
- com.ibm.websphere.personalization.applicationObjects. SelfInitializingApplicationObject
& Serializable.

Example :

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();
}
}

4. Export this java project as jar file and copy into the following locations

v6.x - [portal-root-dir]/pzn/v6.0/collections
v7.x - [portal-root-dir]/pzn/prereq.pzn/collections

5. Please don't forget to restart your portal server to take effect.

Configure the application object in Portal Personalization

1. Login to the portal server.

2. Navigate to the Personalization Business Rules page. With the default theme, this page is accessible by clicking Launch => Personalization => Business Rules.

3. Create a folder to contain your work by choosing New => Folder from the menu; name the folder Application Objects and 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.

4. With the Application Object folder open in the Personalization Navigator, choose New => Application Object from the menu.

5. Enter My First Application Object in the name field. This name will display in the rule editor; it can be anything you choose.

6. Enter mysampleobject for 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.

7. 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 MySampleObject class 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.

8. Click Save to store the definition of the application object in Personalization.

Finally you can add this rule to any portlet/page/label to suit your own needs with business logic implemented in POJO class.

Please do remember you can use DB layer approach fetch any data from the external data source to wrap your business rule.

Hope this tutorial will be useful to everyone.

No comments:

Post a Comment