Chapter 5 - Using Preferences (aqualogic Developer's Notebook)

  • Uploaded by: Raymond Gao
  • 0
  • 0
  • August 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Chapter 5 - Using Preferences (aqualogic Developer's Notebook) as PDF for free.

More details

  • Words: 2,222
  • Pages: 19
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 from the menu.

2. Create a new library.

1

3. Click 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 … and select . 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. 10.Hello: <%=portletUser.getUserName()%>
11.Today is: <%= new java.util.Date()%>
12.Your userID: <%=portletUser.getUserID()%>
13. 14.Your Email address: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Email")%>
15.Your FullName: <%=portletRequest.getSettingValue(SettingType.UserInfo, "FullName")%>
16. Your Phone Number: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Phone")%>
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. 15.Hello: <%=portletUser.getUserName()%>
16.Today is: <%= new java.util.Date()%>
17.Your userID: <%=portletUser.getUserID()%>
18. 19.Your Email address: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Email")%>
20.Your FullName: <%=portletRequest.getSettingValue(SettingType.UserInfo, "FullName")%>
21.Your Phone Number: <%=portletRequest.getSettingValue(SettingType.UserInfo, "Phone")%>
22.

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.%>


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"); %>

What are the cafeteria specials for this week?



8



Monday
Tuesday
Wednesday
Thursday
Friday




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.
11.

12. 13.No discount

14.

15.

11

16.5%

17.

18. 19.10%

20.

21. 22.15%

23.

24. 25.20%

26. 27.


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 .

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. 16.Hello: <%=portletUser.GetUserName()%>
17.Today is: <%= DateTime.Now.ToString()%>
18.Your userID: <%=portletUser.GetUserID()%>
19. 20.Your Email address: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "Email")%>
21.Your FullName: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "FullName")%>
22.Your Phone Number: <%=portletRequest.GetSettingValue(SettingType.UserInfo, "Phone")%>
23. 24.

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.%>
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.

5.

6. 7. No discount

8.

9. 10.5%

11.

12. 13.10%

14.

15. 16.15%

17.

18. 19.20%

20. 21.


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.

What are the cafeteria specials for this week?

16.
17. 18. 19. 20. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 44. 45.
Monday 21. 22.
Tuesday
Wednesday
Thursday
Friday
42. 43.
46.
47.


48.



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

Related Documents


More Documents from "rlagreid"