001: // QuotationProvider.java
002: // This is a sample desktop provider application used to display
003: // famous/humorous quotations on the desktop.
004: //
005: // It shows how to use the BaseProvider API to:
006: // - display html content on the desktop
007: // - allow the user to edit attributes
008: //
009:
010: // Where the QuotationProvider.class file can be found.
011: package samples.quotation;
012:
013: // Use some basic Java classes
014: import java.lang.*;
015: import java.io.*;
016: import java.util.*;
017: import java.net.*;
018:
019: // Use some basic Java Servlet classes
020: import javax.servlet.*;
021: import javax.servlet.http.*;
022:
023: // Use some iPS classes
024: import com.sun.portal.providers.*;
025:
026: // The QuotationProvider Class
027: public class QuotationProvider extends ProfileProviderAdapter {
028: // The constructor - don't need to do any special initialization
029: // so just invoke the ProfileProviderAdapter's constructor
030: public QuotationProvider() {
031: super ();
032: }
033:
034: // Generate the HTML that will be displayed in this content provider's
035: // area on the desktop
036: public StringBuffer getContent(HttpServletRequest req,
037: HttpServletResponse res) throws ProviderException {
038:
039: StringBuffer content = new StringBuffer(); // Contains the HTML to be
040: // displayed
041: Hashtable quotesHash = new Hashtable(); // A Hash of quote vectors,
042: // one vector per category
043: BufferedReader quotesReader; // Used to read the quotes
044: String quote; // A single quotation
045: Random randomGenerator = new Random(); // To randomly pick quotes
046:
047: // Read in the quotations into a Hashtable of Vectors, each containg
048: // the quotations of a specific category. If we can't read the
049: // quotations file it's okay - we just won't display any quotes later.
050: // The category and quotation are separated by a "|" in the quotations
051: // file.
052:
053: try {
054: String quotationFileName = getStringProperty("fileLocation");
055: quotesReader = new BufferedReader(new FileReader(new File(
056: quotationFileName)));
057: while ((quote = quotesReader.readLine()) != null) {
058: String type = quote.substring(0, quote.indexOf('|'));
059: if (!quotesHash.containsKey(type)) {
060: quotesHash.put(type, new Vector());
061: }
062: ((Vector) (quotesHash.get(type))).addElement(quote
063: .substring(quote.indexOf('|') + 1));
064: }
065: } catch (Exception e) {
066: }
067:
068: // Determine is user's preference is to display the category along with
069: // a quotation.
070: boolean displayCategories = getBooleanProperty("displayCategories");
071:
072: // Get the possible quotation categories.
073: List categoriesSel = getListProperty("selectedCategories");
074:
075: // Print a quotation for each category selected by the user.
076: for (int i = 0; i < categoriesSel.size(); i++) {
077: String category = (String) categoriesSel.get(i);
078:
079: // If the user wanted to display the category along with the
080: // quotation then do so.
081: if (displayCategories) {
082: content.append("<B><I>");
083: content.append(category);
084: content.append("</B></I>");
085: }
086:
087: // If there are any quotations for this category, pick a random
088: // one and display it, otherwise note that we didn't find one.
089: if (quotesHash.containsKey(category)) {
090: Vector quoteVector = (Vector) quotesHash.get(category);
091: int whichQuoteIndex = Math.abs(randomGenerator
092: .nextInt())
093: % (quoteVector).size();
094: content.append("<UL>");
095: content.append("<LI>");
096: content.append((String) (quoteVector
097: .elementAt(whichQuoteIndex)));
098: content.append("</LI>");
099: content.append("</UL>");
100: } else {
101: content.append("<UL>");
102: content.append("<LI>");
103: content.append("(no quotes of this type found)");
104: content.append("</LI>");
105: content.append("</UL>");
106: }
107: }
108:
109: // Now return the HTML we have constructed.
110: return content;
111: }
112:
113: // Generate the HTML that will be displayed to let the user set
114: // his/her preferneces.
115: public StringBuffer getEdit(HttpServletRequest req,
116: HttpServletResponse res) throws ProviderException {
117: // Contains the HTML to be displayed
118: StringBuffer content = new StringBuffer();
119:
120: // Display things in a single column table - so things line up.
121: content.append("<P><TABLE>");
122:
123: // Let the user choose which categories s/he wants.
124: content.append("<TR>");
125: content.append("<TD VALIGN=\"top\" HALIGN=\"left\">");
126: content.append("Types of Quotes to Display");
127: content.append("</TD>");
128:
129: // Get the user's selected categories
130: List selectedCategories = getListProperty("selectedCategories");
131:
132: // List the possible categories, marking those that have already
133: // been selected by the user.
134: content.append("<TD VALIGN=\"top\" HALIGN=\"left\">");
135: List possibleCategories = getListProperty("possibleCategories");
136:
137: for (int i = 0; i < possibleCategories.size(); i++) {
138: String category = (String) possibleCategories.get(i);
139: content.append("<INPUT TYPE=\"checkbox\" NAME=\"category-");
140: content.append(category);
141: content.append("\" VALUE=\"");
142: content.append(category);
143: content.append("\"");
144: if (selectedCategories.contains(category)) {
145: content.append(" CHECKED");
146: }
147: content.append(">");
148: content.append(category);
149: content.append("<BR>");
150: }
151: content.append("</TD>");
152: content.append("</TR>");
153:
154: // Let the user decide if they want to display a category with
155: // each quotation.
156: content.append("<TR>");
157: content.append("<TD VALIGN=\"top\" HALIGN=\"left\">");
158: content
159: .append("<INPUT TYPE=\"checkboX\" NAME=\"display_categories\" VALUE=\"yes\"");
160:
161: if (getBooleanProperty("displayCategories")) {
162: content.append(" CHECKED");
163: }
164: content
165: .append(">Display the Category Along With the Quotation");
166: content.append("</TD>");
167: content.append("</TR>");
168:
169: content.append("</TABLE>");
170:
171: return content;
172: }
173:
174: // Handle the submittal of the edit form.
175: public URL processEdit(HttpServletRequest req,
176: HttpServletResponse res) throws ProviderException {
177:
178: List categorySelections = new ArrayList(); // Categories user picked
179: boolean displayCategories = false; // Display category too ?
180:
181: for (Enumeration e = req.getParameterNames(); e
182: .hasMoreElements();) {
183:
184: // Check all the passed parameters and add the selected categories
185: // as well as check to see if user opted to display categories.
186: String parameter = (String) e.nextElement();
187: if (parameter.equals("display_categories")) {
188: displayCategories = true;
189: } else if (parameter.startsWith("category-")) {
190: String[] category = (String[]) req
191: .getParameterValues(parameter);
192: categorySelections.add(category[0]);
193: }
194: }
195:
196: // Set the list of selected categories for later storing in the
197: // profile database.
198: setListProperty("selectedCategories", categorySelections);
199:
200: // Set whether the user wants to display categories or not for later
201: // storing in the profile database.
202: setBooleanProperty("displayCategories", displayCategories);
203:
204: return null;
205:
206: }
207: }
|