|
Home > Archive > WebSphere Portal Server > March 2004 > Request simple sample code for Personalization
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Request simple sample code for Personalization
|
|
|
| I'm a portlet developer. I need develop a portlet which default contain
10 URLs showing in the portlet,
According to each user preference, user can select which URLs show in
the portlet by checking the check-box of each URL(s) in Edit mode. That
mean, each user has his/her own preference of this portlet. For example,
User A checks 5 URLs to show in his portlet and user B checks 8 URLs to
show in his portlet.
How to do it?
Where can I find some simple sample with code of my case?
Thanks!
| |
|
| angel <angel.yuen@housingauthority.gov.hk> wrote in message news:<40581326.2FCB76E0@housingauthority.gov.hk>...
> I'm a portlet developer. I need develop a portlet which default contain
> 10 URLs showing in the portlet,
> According to each user preference, user can select which URLs show in
> the portlet by checking the check-box of each URL(s) in Edit mode. That
> mean, each user has his/her own preference of this portlet. For example,
> User A checks 5 URLs to show in his portlet and user B checks 8 URLs to
> show in his portlet.
> How to do it?
> Where can I find some simple sample with code of my case?
> Thanks!
First off I think the technical word you are looking for is
Customization (user makes changes). Personalization (program decides
based on criteria what the user sees) is something else within the IBM
realm.
I am in the process of coding the same thing you are doing.
1 You need to decide where the URL data is coming from (in portlet
settings or database). This is where you will read to see if the user
has previously stored settings and if not you can use the default
settings. You will also have to match up what they had saved with the
actual data (assuming you don't store the url name and url address -
remember the address could change!).
2 You need to implement the ActionListener and code the
actionPerformed method. This is where you will store the selections
the user made while in edit mode.
Here is some sample code that you can look at to get an idea. I am
storing a link id that will match up with some data in a database.
package yourco_my_favorites;
import java.util.Vector;
import java.util.Enumeration;
import java.io.IOException;
import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlet.event.*;
/**
*
* A sample portlet based on PortletAdapter
*
*/
public class YourcoMyFavoritesPortlet extends PortletAdapter
implements ActionListener {
public static final String VIEW_JSP =
"/yourco_my_favorites/jsp/YourcoMyFavoritesPortletView."; // JSP
file name to be rendered on the view mode
public static final String EDIT_JSP =
"/yourco_my_favorites/jsp/YourcoMyFavoritesPortletEdit."; // JSP
file name to be rendered on the edit mode
public static final String VIEW_BEAN =
"yourco_my_favorites.YourcoMyFavoritesPortletViewBean"; // Bean
name for the view mode request
public static final String EDIT_BEAN =
"yourco_my_favorites.YourcoMyFavoritesPortletEditBean"; // Bean
name for the edit mode request
public static final String SESSION_BEAN =
"yourco_my_favorites.YourcoMyFavoritesPortletSessionBean"; // Bean
name for the portlet session
public static final String FORM_ACTION =
"yourco_my_favorites.YourcoMyFavoritesPortletFormAction"; //
Action name for the edit entry form
public static final String TEXT =
"yourco_my_favorites.YourcoMyFavoritesPortletText"; //
Parameter name for general text input
public static final String SUBMIT =
"yourco_my_favorites.YourcoMyFavoritesPortletSubmit"; //
Parameter name for general submit button
public static final String CANCEL =
"yourco_my_favorites.YourcoMyFavoritesPortletCancel"; //
Parameter name for general cancel button
/**
* @see org.apache.jetspeed.portlet.Portlet#init(PortletConfig)
*/
public void init(PortletConfig portletConfig) throws
UnavailableException {
super.init(portletConfig);
}
/**
* @see org.apache.jetspeed.portlet.PortletAdapter#doView(PortletRequest,
PortletResponse)
*/
public void doView(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
// Check if portlet session exists
YourcoMyFavoritesPortletSessionBean sessionBean =
getSessionBean(request);
if( sessionBean==null ) {
response.getWriter().println("<b>Login to active My
Favorites</b>");
return;
}
PortletData portData = request.getData();
PortalLinks pLinks = (PortalLinks)portData.getAttribute("favlinks");
//new PortalLinks();
if (pLinks == null) {
pLinks = new PortalLinks();
pLinks.init();
}
else {
pLinks.matchNameUrl(); // fill in the url for each link name
}
// temporary data retrieval until portletData gets loaded
Vector links = pLinks.getLinks();
sessionBean.setLinkData(links);
request.setAttribute(SESSION_BEAN, sessionBean);
// Invoke the JSP to render
getPortletConfig().getContext(). include(VIEW_JSP+getJspExtension(request
),
request, response);
}
/**
* @see org.apache.jetspeed.portlet.PortletAdapter#doView(PortletRequest,
PortletResponse)
*/
public void doEdit(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
// Check if portlet session exists
YourcoMyFavoritesPortletSessionBean sessionBean =
getSessionBean(request);
if( sessionBean==null ) {
response.getWriter().println("<b>NO PORTLET SESSION YET</b>");
return;
}
// Make a edit mode bean
YourcoMyFavoritesPortletEditBean editBean = new
YourcoMyFavoritesPortletEditBean();
request.setAttribute(EDIT_BEAN, editBean);
// loop through settings to get all links
PortletSettings ps = request.getPortletSettings();
Enumeration allParameters = ps.getAttributeNames();
// Load up PortalLinks with data to save.
while (allParameters.hasMoreElements()) {
getPortletLog().info("*params available:
"+(String)allParameters.nextElement());
}
// end loop
PortletData portData = request.getData();
PortalLinks pLinks = (PortalLinks)portData.getAttribute("favlinks");
//new PortalLinks();
if (pLinks == null) {
pLinks = new PortalLinks();
pLinks.init();
}
// temporary data retrieval until portletData gets loaded
Vector links = pLinks.getLinks();
sessionBean.setLinkData(links);
request.setAttribute(SESSION_BEAN, sessionBean);
// Set actionURI in the view mode bean
PortletURI formActionURI = response.createURI();
formActionURI.addAction(FORM_ACTION);
editBean.setFormActionURI(formActionURI.toString());
// Invoke the JSP to render
getPortletConfig().getContext(). include(EDIT_JSP+getJspExtension(request
),
request, response);
}
/**
* @see org.apache.jetspeed.portlet.event. ActionListener#actionPerformed(ActionEve
nt)
*/
public void actionPerformed(ActionEvent event) throws
PortletException {
if( getPortletLog().isDebugEnabled() )
getPortletLog().debug("ActionListener - actionPerformed called");
// ActionEvent handler
String actionString = event.getActionString();
// Add action string handler here
PortletRequest request = event.getRequest();
PortletSession session = request.getPortletSession();
YourcoMyFavoritesPortletSessionBean sessionBean =
getSessionBean(request);
if( FORM_ACTION.equals(actionString) ) {
// Set form text in the session bean
//sessionBean.setFormText(request.getParameter(TEXT));
getPortletLog().info("in actionPerformed");
Enumeration allChecked = request.getParameterNames();
PortletData portData = request.getData();
PortalLinks pLinks = new PortalLinks();
PortalLinkBean plb = null;
// Load up PortalLinks with data to save.
while (allChecked.hasMoreElements()) {
String link = (String)allChecked.nextElement();
if (!link.equals(SUBMIT)) {
plb = new PortalLinkBean();
plb.setLinkName(link);
plb.setMarkAsDefault("checked");
pLinks.addLink(plb);
}
}
// Store the data
try {
portData.setAttribute("favlinks", pLinks );
portData.store();
getPortletLog().info("favorites - actionPerformed portData.store
done");
// Go back to previous (view) mode
request.setModeModifier(Portlet.ModeModifier.PREVIOUS);
}
catch (AccessDeniedException ade) {
getPortletLog().error("fav - access denied "+ade.getMessage());
ade.printStackTrace();
}
catch (IOException ioe) {
getPortletLog().error("fav - couldn't write user data io error
"+ioe.getMessage());
}
}
}
/**
* Get SessionBean.
*
* @param request PortletRequest
* @return YourcoMyFavoritesPortletSessionBean
*/
private YourcoMyFavoritesPortletSessionBean
getSessionBean(PortletRequest request) {
PortletSession session = request.getPortletSession();
YourcoMyFavoritesPortletSessionBean sessionBean =
(YourcoMyFavoritesPortletSessionBean)ses
sion.getAttribute(SESSION_BEAN);
if( sessionBean == null ) {
sessionBean = new YourcoMyFavoritesPortletSessionBean();
session.setAttribute(SESSION_BEAN,sessionBean);
}
return sessionBean;
}
/**
* Returns the file extension for the JSP file
*
* @param request PortletRequest
* @return JSP extension
*/
private static String getJspExtension(PortletRequest request) {
String markupName = request.getClient().getMarkupName();
return "jsp";
}
}
----> EDIT JSP
<%@ page contentType="text/html" import="java.util.*,
yourco_my_favorites.*"%>
<%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>
<portletAPI:init/>
<%
YourcoMyFavoritesPortletEditBean editBean =
(YourcoMyFavoritesPortletEditBean)portle
tRequest.getAttribute(YourcoMyFavoritesPortlet.EDIT_BEAN);
YourcoMyFavoritesPortletSessionBean sessionBean =
(YourcoMyFavoritesPortletSessionBean)por
tletRequest.getPortletSession().getAttribute(YourcoMyFavoritesPortlet.SESSION_BEAN);
%>
Select 1 to 10 links to be listed in My Favorites. Click on the
link to sample the contents in a new window.
<FORM method="POST" action="<%=editBean.getFormActionURI()%>">
<%
Vector linkData = sessionBean.getLinkData();
Iterator theData = linkData.iterator();
while(theData.hasNext()) {
PortalLinkBean aLink = (PortalLinkBean)theData.next();
out.println("<INPUT TYPE='checkbox' NAME='"+aLink.getLinkName()+"'
"+aLink.getMarkAsDefault()+"><A href='"+aLink.getLinkUrl()+"'
target='_blank'>"+aLink.getLinkName()+"</A><BR>");
}
%>
<BR>
<INPUT class="wpsButtonText" name="<portletAPI:encodeNamespace
value='<%=YourcoMyFavoritesPortlet.SUBMIT%>'/>" value="Save"
type="submit"/>
</FORM>
|
|
|
|
|