Monday, February 20, 2012

Portlet Preference

Portlet Preference is a feature provided by portal in order to provide persistent data storage.Persistent data storage means that data once set in portlet preference will remain available even after server re-starts.

Details about the interface can be found at
http://www.bluesunrise.com/portlet-api/javax/portlet/PortletPreferences.html#getMap%28%29

Preference can be of 2 types

1) Read-only : they cannot be changed at runtime  are are defined in deployment descriptor or portlet.xml

2) Dynamic Preference : they can be set in process Action phase of life cycle of portlet and can be read in Render phase.

Following is the example to set and retrieve preference dynamically

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
        if( request.getParameter(FORM_SUBMIT) != null ) {
            // Set form text in the session bean
            PreferencePortletSessionBean sessionBean = getSessionBean(request);
            if( sessionBean != null )
                sessionBean.setFormText(request.getParameter(FORM_TEXT));
        }
       
        PortletPreferences preferences = request.getPreferences();
        preferences.setValue("myVal","mohit");
        preferences.store();
    }


public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        response.setContentType(request.getResponseContentType());

        // Check if portlet session exists
        PreferencePortletSessionBean sessionBean = getSessionBean(request);
        if( sessionBean==null ) {
            response.getWriter().println("<b>NO PORTLET SESSION YET</b>");
            return;
        }
       
        PortletPreferences pref = request.getPreferences();
        pref.getValue("myVal", "no data found");

        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }



Saturday, February 18, 2012

Websphere Portal personalization

Portal personalization can be achieved on a particular portal page or even on a particular portlet with a portal page.Following are the 2 links of different blogs to achieve personalization on portlets

Link 1
http://absolutebrain.blogspot.in/2011/06/personalizing-page-portlet-using.html
Link2
http://wpcertification.blogspot.in/2011/01/how-to-show-or-hide-portal-or-portlet.html 

Following link is an excellent tutorial from IBM developer works to create personalization

Link3 (Pdf is also available in backup email Id)
http://www.ibm.com/developerworks/websphere/tutorials/0702_brunn2/0702_brunn2-pdf.pdf

Above 3 links are enough to help you create the personalization rules and apply them to a portlet

I have used the code from link 3 and have modified it to fetch the data from DB (My Sql) and based on the data fetched from DB,I have returned the value "Pass" or "Fail" which can be used in the personalization rules to show/hide portlet or Portal page.

Sample application which will be put in portal server as a jar is available in backup email.Following is the code for reference from application

package com.ibm.websphere.personalization.examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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();
}

public String getMohitSampleString() {
    return "Hello " + userName;
    }
// Method to fetch the data from DB and return corresponding value to be used in personalization rules.

public String getMyVal()
{
String output="fail";
String nm=null;
int ag;
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Please include Classpath  Where your DB2 Driver is located");
        e.printStackTrace();
       
    }
    System.out.println("DB2 driver is loaded successfully");
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    boolean found = false;
    try {
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/personalization", "root", "root");
        if (conn != null) {
            System.out.println("DB2 Database Connected");
        } else {
            System.out.println("Db2 connection Failed ");
        }
        pstmt = conn.prepareStatement("Select * from person");
        rset = pstmt.executeQuery();
       
       
        if (rset != null) {
            while (rset.next()) {
                found = true;
               
               
                ag=rset.getInt("Age");
                nm=rset.getString("Name");
                if(nm.equalsIgnoreCase("mohit") || ag>25)
                    {output="pass"; }
                System.out.println("Name: " + rset.getString("Name")+output);
            }
        }
        if (found == false) {
            System.out.println("No Information Found");
        }
    } catch (SQLException e) {
        System.out.println("DB2 Database connection Failed");
        e.printStackTrace();
       
    }

    return output;
}
}


Note : Apart from putting this java project as jar in portal server,we need to put mysql connector jar as well into portal server at following locations

D:\IBM\WebSphere\PortalServer\pzn\prereq.pzn\lib
D:\IBM\WebSphere\PortalServer\pzn\prereq.pzn\collections

Note: While creating personalization rule based on parameters of portal,like (if userid of portal user is wpsadmin show the portlet) we need to select attribute (Portal Users-->uid).Value has to be hard coded in value textbox. 

Thursday, February 16, 2012

Customization vs Personalization

When developing a portal application, there is lot of confusion wrt Customization vs Personalization. What the client might be calling Personalization might in fact be Customization or vice versa. So, it is very important to understand the difference between the two.

The term "customization" is used to mean the rendering of portlet content based on users preferences or manipulating the portal layout based on the users security attributes.

The term "personalization" is used to mean delivering portlet content based on a business rule or collaborative filtering.

Customization centers around what resources (portlets and pages) you show the users based on their role. This is a core capability of Portal. There are tools provided that help administrators provision these resources. Typically, portlets allow users to specify what kind of information should display. For example, a weather portlet might be customized to show
weather in the users home town.

Personalization can deliver content to a user based on a profile and/or business rule, or determine characteristics of a user based on previous purchases, or pages viewed. Personalization then selects content that is appropriate for that profile. For example, if a person has a high salary range, personalization can be configured to retrieve information about premium products, the page is assembled with the proper information and the user sees his personalized page.

Personalization includes a rules engine, a recommendation engine and a resource engine. Although a full install of Portal installs and configures personalization functions by default, there are additional databases to consider. For example, personalization uses the DB2 Content Manager Runtime Edition for storage of rules, campaigns and other objects. There is a logging framework used to record information about web site usage to a feedback database. The LikeMinds recommendation engine also requires a database to store information gathered through the logging APIs.

To use personalization, you need content to display to your users. Therefore, personalization and content management go hand in hand. You will also need to understand where content is stored and how it is managed to optimize performance.

Monday, February 13, 2012

Check Portlet Form submit

The first way to determine the submit of a portlet is by using the default technique,i.e the code provided by the basic portlet on creation.We just need to perform following tasks.
1) Change the button name to some custom value like "btn1"
2) Change the following line in portlet.java as

public static final String FORM_SUBMIT   = "btn1";

Above mentioned changes will allow,process action method to determine the submit of form.Following is the check put in processAction method.

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
        if( request.getParameter(FORM_SUBMIT) != null ) {

Custom code goes here................

}
}