Chapter 2 – Daily Menu portlet (Portlet Preference

)
When I am at work, sometimes I eat lunch at the company’s cafeteria downstairs. Before I go there, I like to know what is on the cafeteria’s menu for that day. It will be really convenient for me if there is a portlet that displays the daily cafeteria special. In this chapter, we will develop the “dailymenu” portlet. We will use IDK to retrieve and set community and administrative preferences. The new project is called “dailymenu”. You can either create a new project from scratch or just duplicate & rename the “helloworld” project. (See previous chapter on how to do that.) The portlet page will be called dailymenu.aspx or dailymenu.jsp. The first step is to import IDK libraries into our project.

1 For Java Developer (NetBeans)
1.1 Import IDK libraries
We will use the library manager tool to import & manage G6 portal libraries. 1. Select <Library Manager> from the <Tools> menu.

2. Create a new library.

1

3. Click <Add JAR/Folder> button and add all libraries from IDK. I usually extract IDK zip file into a directory call “ptedk_java” inside the %PT_BASE% directory. However, you can put it anywhere.

4. Additionally, you can add the documentation zip file in the library manager. This allows you to browse for help as you code right inside the IDE. NetBeans IDE is a beautiful tool.

2

5. Next, we will add library to our project. Right click on the <Libraries> … <Add Library> … and select <g6IDK>. When you expand the libraries node, you can see all your libraries have been imported. The library manager tool is particularly handy. It lets you set a library of class files and manage it across multiple projects.

3

1.2 The “dailymenu” page (Base Version 0)
Java (dailymenu.jsp)
1. <%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> 2. <% 3. //Get portlet context object 4. IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); 5. IPortletRequest portletRequest = portletContext.getRequest(); 6. IPortletResponse portletResponse = portletContext.getResponse(); 7. IPortletUser portletUser = portletContext.getUser(); 8. %> 9. <!-- from portletUser--> 10.Hello: <%=portletUser.getUserName()%> <br> 11.Today is: <%= new java.util.Date()%><br> 12.Your userID: <%=portletUser.getUserID()%> <br> 13.<!--from SettingValue--> 14.Your Email address: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Email")%><br> 15.Your FullName: <%=portletRequest.getSettingValue(SettingType.UserInfo, "FullName")%><br> 16. Your Phone Number: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Phone")%><br>Note: if you try to access the portlet directly

(http://localhost/dailymenu/dailymenu.aspx), you will get a “Request not gatewayed” exception. Why is this happening? Answer: A portlet is not a standalone application. It must rely on the portal server for application context. You cannot call a portlet directly because it does not have standalone context.

4

Portlet Web Service Portal Server Application Server

Portal Server

Reading above code, you can see that first we obtained a handle of the IPortletContext object using PortletContextFactory. Next, we obtained IPortletRequest, IPortletResponse, IPortletUser type objects. You can model the conversation between the web browser and the web server to a people-to-people conversation. In a conversation, there is a topic (PortletContext), a question (PortletRequest), an answer (PortletResponse), and a speaking partner (IPortletUser). If you find your speaking partner interesting, you may provide him/her with your personal information, e.g. phone number, and e-mail. Likewise, you can configure your web services to provide more information about you. In the “User information” page of the web services, you can check the appropriate boxes for what information you would like to provide.

5

You can then retrieve those information using
portletRequest.getSettingValue(SettingType.UserInfo, "FullName"); portletRequest.GetSettingValue(SettingType.UserInfo, "Email"); portletRequest.GetSettingValue(SettingType.UserInfo, "Phone);

UserInfo is a special type of portlet preference. It is read only and its contents cannot be modified programmatically. There are six types of portlet preferences (SettingType.xxxx) • Administrative (SettingType.Admin) – apply across all instances of the portlet to all users and communities. • User (SettingType.User) – apply to all portlets used by an individual • UserInfo (SettingType.UserIno) – Read only preference, cannot be modified programmatically • Portlet (SettingType.Portlet) – apply to the current Portlet and the current user. Before version 5.0, these settings were called "Gadget" settings. • Community (SettingType.Community) - apply to all Portlets in the current Community, and they are shared for all users of that community. Generally, Community settings are set by a Community manager. • CommunityPortlet (SettingType.CommunityPortlet) – apply to a particular Portlet, but are shared by all users of the current Community. Generally, CommunityPortlet settings are set by a Community manager.

1.3 Adding “menulist” through Community Preference Page
Many other people eat at the cafeteria and want to know the daily menu as well. Hence there is a community of people who are interested in this portlet. The cafeteria staff who updates the daily menu is the manager of this community. The menu portlet can updated through a community preference page. We will modify our dailymenu.jsp file to use Community Preference. 6

dailymenu.jsp
1. <%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> 2. <% 3. //Get portlet context object 4. IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); 5. IPortletRequest portletRequest = portletContext.getRequest(); 6. IPortletResponse portletResponse = portletContext.getResponse(); 7. IPortletUser portletUser = portletContext.getUser(); 8. String MondaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "MondaySpecial"); 9. String TuesdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial"); 10.String WednesdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial"); 11.String ThursdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial"); 12.String FridaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "FridaySpecial"); 13.%> 14.<!-- from portletUser--> 15.Hello: <%=portletUser.getUserName()%> <br> 16.Today is: <%= new java.util.Date()%><br> 17.Your userID: <%=portletUser.getUserID()%> <br> 18.<!--from SettingValue--> 19.Your Email address: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Email")%><br> 20.Your FullName: <%=portletRequest.getSettingValue(SettingType.UserInfo, "FullName")%><br> 21.Your Phone Number: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Phone")%><br> 22.<p> 23.<% 24.DateFormat myformat = new SimpleDateFormat("EEEE"); 25.String today=myformat.format((new java.util.Date())); 26.if (today.equalsIgnoreCase("Monday")) out.println("Monday's special is: " + MondaySpecial); 27.else if (today.equalsIgnoreCase("Tuesday")) out.println("Tuesday's Special is: " + TuesdaySpecial); 28.else if (today.equalsIgnoreCase("Wednesday")) out.println("Wednesday's special is: " + WednesdaySpecial); 29.else if (today.equalsIgnoreCase("Thursday")) out.println("Thursday's Special is: " + ThursdaySpecial); 30.else if (today.equalsIgnoreCase("Friday")) out.println("Friday's special is: " + FridaySpecial); 31.else out.println("Cafeteria is closed on Weekend."); 32.%><br>

The new lines are in red, showing how to retrieve Community Preferences. The web service for the “dailymenu” portlet must be updated to accept community preference variables.

7

We will also add the “menulist.jsp” (a page where the menu list can be updated) and the “setmenulist.jsp” page (working in the background to update the menu list). menulist.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> <% //Get portlet context object IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); IPortletRequest portletRequest = portletContext.getRequest(); IPortletResponse portletResponse = portletContext.getResponse(); IPortletUser portletUser = portletContext.getUser(); String MondaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "MondaySpecial"); String TuesdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial"); String WednesdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial"); String ThursdaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial"); String FridaySpecial = portletRequest.getSettingValue(SettingType.CommunityPortlet, "FridaySpecial"); %> <p>What are the cafeteria specials for this week?</p> <form name="dailymenuComPrefs" method="post" action="setmenulist.jsp"> <table width="432" border="1"> <tr> <td width="204">Monday</td> <td width="212"> <input type="text" name="MondaySpecial" value="<%=MondaySpecial%>"> </td> </tr> <tr> <td>Tuesday</td> <td><input type="text" name="TuesdaySpecial" value="<%=TuesdaySpecial%>"></td> </tr>

8

<td>Wednesday</td> <td><input type="text" name="WednesdaySpecial" value="<%=WednesdaySpecial%>"></td> </tr> <tr> <td>Thursday</td> <td><input type="text" name="ThursdaySpecial" value="<%=ThursdaySpecial%>"></td> </tr> <tr> <td>Friday</td> <td><input type="text" name="FridaySpecial" value="<%=FridaySpecial%>"></td> </tr> <tr> <td colspan="2"><div align="center"> <input name="save" type="submit" value="Save"> </div></td> </tr> </table> </form> <p><br> </p>

<tr>

setmenulist.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> <% //Get portlet context object IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); IPortletResponse portletResponse = portletContext.getResponse(); //Get community preference from incoming form, and set it into the portletResponse portletResponse.setSettingValue(SettingType.CommunityPortlet, "MondaySpecial", request.getParameter("MondaySpecial")); portletResponse.setSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial", request.getParameter("TuesdaySpecial")); portletResponse.setSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial", request.getParameter("WednesdaySpecial")); portletResponse.setSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial", request.getParameter("ThursdaySpecial")); portletResponse.setSettingValue(SettingType.CommunityPortlet, "FridaySpecial", request.getParameter("FridaySpecial")); portletResponse.returnToPortal(); %>

Next, we create a community called “dailymenu Community” and a community manager “cafeteria staff” who is charge of this community and responsible for updating the menu. The “dailymenu” portlet will be added to a new page in this community.

9

And, the portlet’s community preference can be changed by clicking on the pencil icon of the portlet preference page in a community.

10

1.4 Adding “discount” through Administrative Preference
Companies often provide discount to employees on a variety of internal services and products. If your company has a flat rate discount across products and services, e.g. from discount on lunch in the cafteria, to on purchasing computer hardwares, and to books and manuals, there could be many many preferences to set. With Administrative preference, you can set “discount” across a variety of products and communities with a single page. We will now add two additional pages – “discount.jsp” and “setdiscount.jsp”. discount.jsp
1. <%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> 2. <% 3. //Get portlet context object 4. IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); 5. IPortletRequest portletRequest = portletContext.getRequest(); 6. IPortletResponse portletResponse = portletContext.getResponse(); 7. IPortletUser portletUser = portletContext.getUser(); 8. %> 9. Do you wish to give company employee discount in the cafeteria? 10.<form name="setdailymenuAdminPrefs" method="post" action="setdiscount.jsp"> 11.<p> 12.<input name="discount" type="radio" value="0"> 13.No discount</p> 14.<p> 15.<input name="discount" type="radio" value="5">

11

16.5%</p> 17.<p> 18.<input name="discount" type="radio" value="10"> 19.10% </p> 20.<p> 21.<input name="discount" type="radio" value="15"> 22.15% </p> 23.<p> 24.<input name="discount" type="radio" value="20"> 25.20%</p> 26.<input name="save" type="submit" value="Save"> 27.</form>

setdiscount.jsp
1. <%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*, java.util.*, java.text.*" %> 2. <% 3. //Get portlet context object 4. IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); 5. IPortletResponse portletResponse = portletContext.getResponse(); 6. //Get community preference from incoming form, and set it into the portletResponse 7. portletResponse.setSettingValue(SettingType.Admin, "discount", request.getParameter("discount")); 8. portletResponse.returnToPortal(); 9. %>

Additionally, the “dailymenu.jsp” page should be updated. dailymenu.jsp
1. <% 2. String discount = portletRequest.getSettingValue(SettingType.Admin, "discount"); 3. if (discount != null) out.println("This coupon entitles this employee " + discount + "% discount at the company cafeteria."); 4. else out.println("no discount available."); 5. %>

And, the “dailymenu.jsp” services should be updated to add the administrative page.

There is an “Edit” button on the portlet’s editor page allowing administrative preference to be set. 12

Note: Admin preference is special and should set in the portlet editor. (An example is the Admin Objects Directory: Portal Resources: General Information portlet. This portlet is responsible for My Account. You can add additional property categories to this portlet)

13

14

2 For .Net user (Visual Studio 2003)
2.1 Import IDK library
1. Right click on the <References> … <Add References>.

2. Add DLL from the IDK dotNet zip file extract.

15

C# (dailymenu.aspx)
1. <%@ Page language="c#" Codebehind="dailymenu.aspx.cs" AutoEventWireup="false" Inherits="dailymenu.dailymenu" trace="false" %> 2. <%@ Import namespace="Plumtree.Remote.Portlet" %> 3. <% 4. //Get portlet context object 5. IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request,Response); 6. IPortletRequest portletRequest = portletContext.GetRequest(); 7. IPortletResponse portletResponse = portletContext.GetResponse(); 8. IPortletUser portletUser = portletContext.GetUser(); 9. String MondaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "MondaySpecial"); 10.String TuesdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial"); 11.String WednesdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial"); 12.String ThursdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial"); 13.String FridaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "FridaySpecial"); 14.%> 15.<!-- from portletUser--> 16.Hello: <%=portletUser.GetUserName()%> <br> 17.Today is: <%= DateTime.Now.ToString()%><br> 18.Your userID: <%=portletUser.GetUserID()%> <br> 19.<!--from SettingValue--> 20.Your Email address: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "Email")%><br> 21.Your FullName: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "FullName")%><br> 22.Your Phone Number: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "Phone")%><br> 23.<!--The day of the week is: <%=DateTime.Now.DayOfWeek%><br>--> 24.<p> 25.<% 26.string today = DateTime.Now.DayOfWeek.ToString(); 27.if (today.ToUpper().Equals("Monday")) Response.Write("Monday's special is: " + MondaySpecial); 28.else if (today.ToUpper().Equals("Tuesday")) Response.Write("Tuesday's Special is: " + TuesdaySpecial); 29.else if (today.ToUpper().Equals("Wednesday")) Response.Write("Wednesday's special is: " + WednesdaySpecial); 30.else if (today.ToUpper().Equals("Thursday")) Response.Write("Thursday's Special is: " + ThursdaySpecial); 31.else if (today.ToUpper().Equals("Friday")) Response.Write("Friday's special is: " + FridaySpecial); 32.else Response.Write("Cafeteria is closed on Weekend."); 33.%><br> 34.<% 35.String discount = portletRequest.GetSettingValue(SettingType.Admin, "discount");

16

36.if (discount != null) Response.Write("This coupon entitles this employee " + discount + "% discount at the company cafeteria."); 37.else Response.Write("no discount available."); 38.%>

discount.aspx
1. <%@ Page language="c#" Codebehind="discount.aspx.cs" AutoEventWireup="false" Inherits="dailymenu.discount" %> 2. <%@ Import namespace="Plumtree.Remote.Portlet" %> 3. Do you wish to give company employee a discount in the cafeteria? 4. <form name="setdailymenuAdminPrefs" method="post" action="setdiscount.aspx"> 5. <p> 6. <input name="discount" type="radio" value="0"> 7. No discount</p> 8. <p> 9. <input name="discount" type="radio" value="5"> 10.5%</p> 11.<p> 12.<input name="discount" type="radio" value="10"> 13.10% </p> 14.<p> 15.<input name="discount" type="radio" value="15"> 16.15% </p> 17.<p> 18.<input name="discount" type="radio" value="20"> 19.20%</p> 20.<input name="save" type="submit" value="Save"> 21.</form>

setdiscount.aspx
1. <%@ Page language="c#" Codebehind="setdiscount.aspx.cs" AutoEventWireup="false" Inherits="dailymenu.setdiscount" %> 2. <%@ Import namespace="Plumtree.Remote.Portlet" %> 3. <% 4. //Get portlet context object 5. IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request,Response); 6. IPortletResponse portletResponse = portletContext.GetResponse(); 7. //Get admin preference from incoming form, and set it into the portletResponse 8. portletResponse.SetSettingValue(SettingType.Admin, "discount", Request.Params["discount"]); 9. portletResponse.ReturnToPortal(); 10.%>

menulist.aspx
1. <%@ Page language="c#" Codebehind="menulist.aspx.cs" AutoEventWireup="false" Inherits="dailymenu.menulist" trace="false" %> 2. <%@ Import namespace="Plumtree.Remote.Portlet" %> 3. <% 4. //Get portlet context object 5. IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request,Response); 6. IPortletRequest portletRequest = portletContext.GetRequest(); 7. IPortletResponse portletResponse = portletContext.GetResponse(); 8. IPortletUser portletUser = portletContext.GetUser();

17

9. String MondaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "MondaySpecial"); 10.String TuesdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial"); 11.String WednesdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial"); 12.String ThursdaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial"); 13.String FridaySpecial = portletRequest.GetSettingValue(SettingType.CommunityPortlet, "FridaySpecial"); 14.%> 15.<p>What are the cafeteria specials for this week?</p> 16.<form name="dailymenuComPrefs" method="post" action="setdailymenuComPrefs.aspx"> 17.<table width="432" border="1"> 18.<tr> 19.<td width="204">Monday</td> 20.<td width="212"> 21.<input type="text" name="MondaySpecial" value="<%=MondaySpecial%>"> 22.</td> 23.</tr> 24.<tr> 25.<td>Tuesday</td> 26.<td><input type="text" name="TuesdaySpecial" value="<%=TuesdaySpecial%>"></td> 27.</tr> 28.<tr> 29.<td>Wednesday</td> 30.<td><input type="text" name="WednesdaySpecial" value="<%=WednesdaySpecial%>"></td> 31.</tr> 32.<tr> 33.<td>Thursday</td> 34.<td><input type="text" name="ThursdaySpecial" value="<%=ThursdaySpecial%>"></td> 35.</tr> 36.<tr> 37.<td>Friday</td> 38.<td><input type="text" name="FridaySpecial" value="<%=FridaySpecial%>"></td> 39.</tr> 40.<tr> 41.<td colspan="2"><div align="center"> 42.<input name="save" type="submit" value="Save"> 43.</div></td> 44.</tr> 45.</table> 46.</form> 47.<p><br> 48.</p>

setmenulist.aspx
1. <%@ Page language="c#" Codebehind="setmenulist.aspx.cs" AutoEventWireup="false" Inherits="dailymenu.setmenulist" %>

18

2. 3. 4. 5.

<%@ Import namespace="Plumtree.Remote.Portlet" %> <% //Get portlet context object IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request,Response); 6. IPortletResponse portletResponse = portletContext.GetResponse(); 7. //Get community preference from incoming form, and set it into the portletResponse 8. portletResponse.SetSettingValue(SettingType.CommunityPortlet, "MondaySpecial", Request.Params["MondaySpecial"]); 9. portletResponse.SetSettingValue(SettingType.CommunityPortlet, "TuesdaySpecial", Request.Params["TuesdaySpecial"]); 10.portletResponse.SetSettingValue(SettingType.CommunityPortlet, "WednesdaySpecial", Request.Params["WednesdaySpecial"]); 11.portletResponse.SetSettingValue(SettingType.CommunityPortlet, "ThursdaySpecial", Request.Params["ThursdaySpecial"]); 12.portletResponse.SetSettingValue(SettingType.CommunityPortlet, "FridaySpecial", Request.Params["FridaySpecial"]); 13.portletResponse.ReturnToPortal(); 14.%>

2.2 Difference between C# and Java
As you can see, the Java and C# code are pretty much similar. However, you must pay attention to the following. 1. The first character of a class member (variable or method) is lower-case for Java and upper-case for C#. 2. To write output to HTML, JSP and ASPX use different method calls. a. (JSP) – out.println(“---your output text---”); b. (ASPX) – Response.Write(“---your output text---”); 3. To read a parameter from the input page. a. (JSP) – request.getParameter("parameter name") b. (ASPX) –Request.Params["parameter name"]

19