Code Example B-2 Sample Desktop Provider File
// This is a sample desktop provider application used to display
|
// famous/humorous quotations on the desktop.
|
//
|
// It shows how to use the ProfileProviderAdapter API to:
|
// - display HTML content on the desktop
|
// - allow the user to edit attributes
|
//
|
// It also invokes the Session, Profile and Logging APIs in
order to:
|
// - get/set user-level attributes regarding quotation
preferences
|
// - provide logging
|
|
// Where the QuotationProvider.class file can be found.
|
package com.iplanet.portalserver.providers.quotations;
|
|
// Use some basic Java classes
|
import java.lang.*;
|
import java.io.*;
|
import java.util.*;
|
|
// Use some basic Java Servlet classes
|
import javax.servlet.*;
|
import javax.servlet.http.*;
|
|
// Use some iPS classes
|
import com.iplanet.portalserver.desktop.util.*;
|
import com.iplanet.portalserver.providers.Provider;
|
import
com.iplanet.portalserver.providers.ProfileProviderAdapter;
|
import com.iplanet.portalserver.session.Session;
|
import com.iplanet.portalserver.profile.*;
|
|
// The QuotationProvider Class
|
public class QuotationProvider extends ProfileProviderAdapter
implements Provider {
|
|
// The constructor - don't need to do any special
initialization
|
// so just invoke the ProfileProviderAdapter's constructor.
|
public QuotationProvider() {
|
super();
|
}
|
|
// Generate the HTML that will be displayed in this content
provider's
|
// area on the desktop
|
public StringBuffer getHTMLContent() throws Exception {
|
Profile p = getSession().getUserProfile();
|
StringBuffer content = new StringBuffer(); // Contains
HTML to be displayed.
|
Hashtable quotesHash = new Hashtable(); // A Hash of
quote Vectors, one vector per category.
|
BufferedReader quotesReader; // Used to read
the quotes.
|
String quote; // A single
quotation.
|
Random randomGenerator = new Random(); // To randomly
pick quotes.
|
|
// Read in the quotations into a Hashtable of Vectors,
each containg
|
// the quotations of a specific category. If we can't read
the
|
// quotations file it's okay - we just won't display any
quotes later.
|
// The category and quotation are separated by a "|" in
the quotations
|
// file.
|
try {
|
String quotationFileName =
p.getAttributeString("iwtQuotationProvider-fileLocation");
|
quotesReader = new BufferedReader(new FileReader(new
File(quotationFileName)));
|
while ((quote = quotesReader.readLine()) != null) {
|
String type =
quote.substring(0,quote.indexOf('|'));
|
if (!quotesHash.containsKey(type)) {
|
quotesHash.put(type,new Vector());
|
}
|
((Vector)(quotesHash.get(type))).addElement(quote.substring(quot
e.indexOf('|')+1));
|
}
|
} catch (Exception e) {
|
}
|
|
// Determine if user's preference is to display the
category along with a quotation.
|
boolean displayCategories = true;
|
if
(p.getAttributeString("iwtQuotationProvider-displayCategories").
equals("false")) {
|
displayCategories = false;
|
}
|
|
// Get the possible quotation categories.
|
Enumeration e =
p.getAttribute("iwtQuotationProvider-selectedCategories");
|
|
// Print a quotation for each category selected by the
user.
|
while (e.hasMoreElements()) {
|
String category = (String)e.nextElement();
|
|
// If the user wanted to display the category along
with the
|
// quotation then do so.
|
if (displayCategories) {
|
content.append("<BOLD>");
|
content.append(category);
|
content.append("</BOLD>");
|
}
|
|
// If there are any quotations for this category, pick
a random
|
// one and display it, otherwise note that we didn't
find one.
|
if (quotesHash.containsKey(category)) {
|
Vector quoteVector =
(Vector)quotesHash.get(category);
|
int whichQuoteIndex =
Math.abs(randomGenerator.nextInt())%(quoteVector).size();
|
content.append("<UL>");
|
content.append("<LI>");
|
content.append((String)(quoteVector.elementAt(whichQuoteIndex)))
;
|
content.append("</LI>");
|
content.append("</UL>");
|
} else {
|
content.append("<UL>");
|
content.append("<LI>");
|
content.append("(no quotes of this type found)");
|
content.append("</LI>");
|
content.append("</UL>");
|
}
|
}
|
|
// Now return the HTML we have constructed.
|
return content;
|
}
|
|
// Generate the HTML that will be displayed to let the user
set his preferences.
|
public StringBuffer getHTMLEditForm() {
|
StringBuffer content = new StringBuffer();
|
|
try {
|
Profile p = getSession().getUserProfile();
|
|
// Display things in a single column table - so things
line up.
|
content.append("<P><TABLE>");
|
|
// Let the user choose which categories he wants.
|
content.append("<TR>");
|
content.append("<TD VALIGN='top' HALIGN='left'>");
|
content.append("Types of Quotes to Display");
|
content.append("</TD>");
|
|
// Get the user's selected categories
|
Vector selectedCategories = new Vector();
|
Enumeration e2 =
p.getAttribute("iwtQuotationProvider-selectedCategories");
|
while (e2.hasMoreElements()) {
|
selectedCategories.addElement((String)e2.nextElement());
|
}
|
|
// List the possible categories, marking those that
have already
|
// been selected by the user.
|
content.append("<TD VALIGN='top' HALIGN='left'>");
|
Enumeration e =
p.getAttribute("iwtQuotationProvider-possibleCategories");
|
while (e.hasMoreElements()) {
|
String category = (String)e.nextElement();
|
content.append("<INPUT TYPE='checkbox'
NAME='category-");
|
content.append(category);
|
content.append("' VALUE='");
|
content.append(category);
|
content.append("'");
|
if (selectedCategories.contains(category)) {
|
content.append(" CHECKED");
|
|
content.append(">");
|
content.append(category);
|
content.append("<BR>");
|
}
|
content.append("</TD>");
|
content.append("</TR>");
|
|
// Let the user decide if he wants to display a
category with each quotation.
|
content.append("<TR>");
|
content.append("<TD VALIGN='top' HALIGN='left'>");
|
content.append("<INPUT TYPE='checkbox'
NAME='display_categories' VALUE='yes'");
|
if
(p.getAttributeString("iwtQuotationProvider-displayCategories").
equals("true")) {
|
content.append(" CHECKED");
|
}
|
content.append(">Display the Category Along With the
Quotation");
|
content.append("</TD>");
|
content.append("</TR>");
|
|
content.append("</TABLE>");
|
} catch (Exception e) {
|
}
|
|
return content;
|
}
|
|
// Handle the submittal of the edit form.
|
public int processEditForm(HttpServletRequest req) throws
Exception {
|
Profile p = getSession().getUserProfile();
|
Enumeration parameterNames = req.getParameterNames();
|
Vector categorySelections = new Vector();
|
boolean displayCategories = false;
|
|
// Check all the passed parameters and add the selected
categories
|
// as well as check to see if user opted to display
categories.
|
while (parameterNames.hasMoreElements()) {
|
String parameter =
(String)parameterNames.nextElement();
|
if (parameter.equals("display_categories")) {
|
displayCategories = true;
|
} else if (parameter.startsWith("category-")) {
|
String category = req.getParameter(parameter);
|
categorySelections.addElement(category);
|
}
|
}
|
|
// Set the list of selected categories for later storing
in the profile database.
|
p.setAttribute("iwtQuotationProvider-selectedCategories",
categorySelections.elements(), Profile.NEW);
|
|
// Set whether the user wants to display categories or not
for later
|
// storing in the profile database.
|
if (displayCategories) {
|
p.setAttributeString("iwtQuotationProvider-displayCategories",
"true", Profile.NEW);
|
} else {
|
p.setAttributeString("iwtQuotationProvider-displayCategories",
"false", Profile.NEW);
|
}
|
|
// Done processing the edit form - now store the user's
preferences.
|
p.store(true);
|
|
return DtConstants.BUILD_FRONT;
|
}
|
}
|
|