Showing posts with label IBM Websphere Porltet Preferences. Show all posts
Showing posts with label IBM Websphere Porltet Preferences. Show all posts

Wednesday, 16 April 2014

Implementing Portlet Preferences

Process of building PortletPreferences / Personalization

1. Create PortletPreferences object and setValue() with request.getParameter or something else.
2. Store the portletpreference object into persistent storage using prefobj.store()
3. And can retrieve/set the stored preference object in XXX.jsp or any render method

-> In back-end this preference objects will stored into the Portal database with predefined conditions.
-> We have two important methods store() and reset(), where store() method stores the preference object into database and if we want to reset the values to default we can use reset() method.
-> We should not invoke the store() method in any portlet’s render method, in any portlet mode, which will result in an exception.
-> We can also validate preferences by using interface PreferencesValidator.
-> We can also set preference and preferences-validator in Portlet.xml file as below;

<portlet-app ...>
<portlet>
<portlet-name>yourportlet</portlet-name>
<portlet-class>portlet class path</portlet-class>
...
<portlet-preferences>
<preference>
<name>maxNumOfBooks</name>
<value>1000</value>
</preference>
<preferences-validator>
chapter10.code.listing.validators.BookCatalogPrefsValidator
</preferences-validator>
</portlet-preferences>
</portlet>
...
</portlet-app>


Code in file -- msperfportlet_view.jsp --

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%-- Uncomment below lines to add portlet taglibs to jsp --%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects />

<%
    // We can also get preferences object here in jsp
    PortletPreferences jspprefs = renderRequest.getPreferences();
    //String prefvalues[] = jspprefs.getValues("category",new String[]{"-99"});
    List list = java.util.Arrays.asList(jspprefs.getValues("category",new String[]{"-99"}));
%>


<form action="<portlet:actionURL/>" method="POST">
       
    <h2>User Preference on Cricket</h2>
   
    <select name="cricketPref" multiple="multiple">
        <option value="test"<%=list.contains("test")?"selected":""%> id="userselection">Test Matches</option>
        <option value="od"<%=list.contains("od")?"selected":""%> id="userselection">OD Matches</option>
        <option value="ipl"<%=list.contains("ipl")?"selected":""%> id="userselection">IPL</option>
        <option value="icl"<%=list.contains("icl")?"selected":""%> id="userselection">ICL</option>
        <option value="t20"<%=list.contains("t20")?"selected":""%> id="userselection">T20</option>
    </select>
   
    <input type="submit" value="Submit" />
       

</form>



Code in file -- msperfportlet.java --

package ms.eb;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PreferencesValidator;
import javax.portlet.ProcessAction;
import javax.portlet.RenderMode;
import javax.portlet.ValidatorException;

/**
 * msperfportlet Portlet Class
 */
public class msperfportlet extends GenericPortlet{// implements PreferencesValidator{
   
     
    //@ProcessAction(name = "savePreferences")
    public void processAction(ActionRequest request, ActionResponse response)throws PortletException, IOException {
       
        // Getting parameter values from msperfportlet_view.jsp
        String[] prefCategories = request.getParameterValues("cricketPref");
        for(int i=0; i<prefCategories.length; i++){
            System.out.println("Selected Preference Value is :: "+prefCategories[i]);
        }
       
        // Creating a PortletPreferences object and setting preference value using setValues
        PortletPreferences prefs = request.getPreferences();        
        if(prefCategories != null) {
            // Setting preffered categorites preffered value
            prefs.setValues("category", prefCategories);
        }
       
        // Storing preferences in a persistent store
        // This should be in only processAction method only
        prefs.store();          
       
        // Reseting preference value to default
        // We have to reset preference by reset() only but not with assigning null value to setValues() has null is also taken has valid entry
        //prefs.reset("category");
    }
   
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        response.setContentType("text/html");
        // We Can get preferences object in doview also but we cant store preferences object here
        /*PortletPreferences renderpref = request.getPreferences();
        String str[] = renderpref.getValues("category",new String[]{"-99"});
        for(int i=0; i<str.length; i++){
            System.out.println("Preference Value from doView() :: "+str[i]);
        }*/
        PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/msperfportlet_view.jsp");
        dispatcher.include(request, response);
    }

    public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        response.setContentType("text/html");      
        PortletRequestDispatcher dispatcher =
                getPortletContext().getRequestDispatcher("/WEB-INF/jsp/msperfportlet_edit.jsp");
        dispatcher.include(request, response);
    }
   
//    @RenderMode(name = "edit")
//    public void showPrefs(RenderRequest request, RenderResponse response)throws PortletException, IOException{
//        PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/msperfportlet_edit.jsp");
//        dispatcher.include(request, response);
//    }

    public void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
       
        response.setContentType("text/html");      
        PortletRequestDispatcher dispatcher =
                getPortletContext().getRequestDispatcher("/WEB-INF/jsp/msperfportlet_help.jsp");
        dispatcher.include(request, response);
    }

}