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.
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.
It is good.
ReplyDeleteIs any possibilities to use EJB bean to pull data in application object ?
Thanks in advance !
IF you want to use EJB then you must deploy the ejb ear in app server and your project which will be put in as a jar file into your portal server should call ejb,in this case ejb should pull the data from db and pass it on to the project jar
ReplyDeleteHi,
ReplyDeleteI have a requirement to hide a portlet on the basis of an option selected from a different potlet. I tried stored the selected option in application session but still I am not able to fetch the variable from the init method of ApplicationObject.
Portlet code (processAction):
portletSession.setAttribute("SelectedOption", "DisplayRecords",PortletSession.APPLICATION_SCOPE);
ApplicationObject (init())
SelectedOption = (String) context.getSessionAttribute("SelectedOption");
System.out.println("SelectedOption from ApplicationObject : " + SelectedOption );
Where SelectedOption is String and output is always null in the systemout log.
Hi Debarshi,
ReplyDeleteYour requirement is not very clear but still I will try to provide you a solution.
Case 1 : User selects option once in portlet A to hide portlet B,Portlet B should not render until user select the option to show portlet B from portlet A.
Solution : In this case I will suggest that you store the option selected from Portlet A in portlet preferences and not in session and then fetch the user choice from preference in Portlet B and show/hide portlet B using personalization.
Benefit to store user choice in preference will be that the choice selected by the user will always exist even after portal restart and if user selects not to show Portlet B,it will not render until user deselect this option from Portlet A.
Case 2 :Every time portal page is rendered both the portlets show up and then if user wants to hide portlet B he can select this option from portlet A. In this case every time user has to select the option to hide portlet B after page render.
Solution : You can use portlet wiring or public render parameters to pass the option selected from Portlet A to Portlet B. Even in this case I will not suggest you to use session as session should be the last option to be used to store data in portlet as it may cause some performance issues.
Please feel free to contact me if you have any other queries.
Hi Mohit,
ReplyDeleteThanks for your reply.
My requirement is : "Portlet A" has some options and depending on the option selected from "Portlet A" I need to show/hide "Portlet B".
Case 1 : For me it will not be correct to store the option as preference, since in a single session the user might select multiple options from Portlet A and as a result at times "Portlet B" will be visible or hidden.
Case 2: I have this as my last option, since I need to hide the portlet. For that I will have to switch skin to No Skin to completely hide "Portlet B". And switch it back to a normal skin to show portlet header and border.
I tried to access the ApplicationObject Bean from Portlets but I was unable to get the ApplicationObject.
I was adding this code to processAction of the Portlet A:
ApplicationObject userObject1=null,userObject2=null;
userObject1=(ApplicationObject)portletSession.getPortletContext().getAttribute("SelectedOption");
userObject2=(ApplicationObject)portletSession.getAttribute("SelectedOption",PortletSession.APPLICATION_SCOPE);
But both this objects are null.
In the same way I tried to access a variable added to PortletSession.APPLICATION_SCOPE from PZN bean. But there also I was getting null value.
Can you please suggest a way to modify a personalization bean data member from porlet application code?