KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > portlets > PollPortlet


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" and
27  * "Apache Jetspeed" must not be used to endorse or promote products
28  * derived from this software without prior written permission. For
29  * written permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache" or
32  * "Apache Jetspeed", nor may "Apache" appear in their name, without
33  * prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54
55 package org.apache.jetspeed.portal.portlets;
56
57 //standard java stuff
58
import java.io.*;
59 import java.util.*;
60
61 //Element Construction Set
62
import org.apache.ecs.html.*;
63 import org.apache.ecs.ElementContainer;
64 import org.apache.ecs.ConcreteElement;
65 import org.apache.ecs.ClearElement;
66 import org.apache.ecs.StringElement;
67
68 //standard Jetspeed stuff
69
import org.apache.jetspeed.util.*;
70 import org.apache.jetspeed.portal.*;
71
72 //utils used by the PollPortlet
73
import org.apache.jetspeed.portal.portlets.util.poll.*;
74
75
76 //turbine
77
import org.apache.turbine.util.*;
78
79
80 /**
81 A simple portlet for allowing users to vote on features.
82
83 THIS IS CURRENTLY EXPERIMENTAL!!!
84
85 @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
86 @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
87 */

88 public class PollPortlet extends AbstractPortlet {
89
90
91     public static final String JavaDoc VOTE = "Vote";
92     
93
94     public static final String JavaDoc ACTION_KEY = "jetspeed.poll.action";
95     public static final String JavaDoc VOTE_KEY = "jetspeed.poll.vote";
96     public static final String JavaDoc QUESTION_KEY = "jetspeed.poll.question";
97     public static final String JavaDoc ANSWER_KEY = "jetspeed.poll.answer";
98     public static final String JavaDoc RESULTS_KEY = "jetspeed.poll.results";
99     
100     public static final String JavaDoc WARNING = "NOTE: Do to use this poll for anything important, it might not be accurate!";
101     
102     //misc actions..
103
/**
104     Used when casting your vote
105     */

106     public static final String JavaDoc ACTION_VOTE = VOTE_KEY;
107
108     /**
109     Used to get the results.
110     */

111     public static final String JavaDoc ACTION_GET_RESULTS = "jetspeed.pool.results";
112
113     /**
114     The mainbar gif used when building a vote
115     */

116     public static final String JavaDoc IMG_MAINBAR = "images/poll/mainbar.gif";
117
118     /**
119     The default width for the polling image.
120     */

121     public static final int DEFAULT_IMG_WIDTH = 400;
122
123     /**
124     The default height for the polling image.
125     */

126     public static final int DEFAULT_IMG_HEIGHT = 20;
127     
128     private AnswerGroup ag = null;
129     private Question question = new Question( "Question? What question? :)" );
130
131     /**
132     Holds the IP addresses of people that have already voted.
133     */

134     private Hashtable votes = new Hashtable();
135     
136     /**
137     Get the vote prompt or post the vote.
138     
139     @see Portlet.getContent
140     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
141     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
142     */

143     public ConcreteElement getContent(RunData rundata) {
144         
145         String JavaDoc action = rundata.getParameters().getString( ACTION_KEY );
146
147         if ( action == null ) {
148
149             return getUI( rundata );
150
151         } else if ( action.equals( VOTE_KEY ) ) {
152
153             //cast the vote if necessary.
154
if ( ! this.hasVoted( rundata ) ) {
155                 this.getAnswerGroup()
156                     .getAnswer( rundata
157                         .getParameters().getInt( ANSWER_KEY ) ).castVote();
158             }
159
160         }
161         
162         
163         //if the user requests the results or just votes... then return the
164
//vote screen.
165
if ( action.equals( VOTE_KEY ) ||
166              action.equals( RESULTS_KEY ) ) {
167
168             ConcreteElement results = this.getResults( rundata );
169                  
170             //set this user/IP to already voted.
171
if ( action.equals( VOTE_KEY ) ) {
172                 this.setVoted( true, rundata );
173             }
174                  
175             return results;
176              
177         }
178
179         return getUI( rundata );
180
181     }
182     
183     /**
184     Parse out the init parameters to get the questions and various answers.
185
186     @see Portlet.init()
187     */

188     public void init() {
189         
190         this.setQuestion( new Question( this.getPortletConfig().getInitParameter( QUESTION_KEY ) ) );
191         
192         //get the questions and answers for this Poll.
193
Enumeration params = this.getPortletConfig().getInitParameterNames();
194         
195         Vector v = new Vector();
196         
197         int id = 0;
198         while ( params.hasMoreElements() ) {
199             
200             String JavaDoc key = (String JavaDoc)params.nextElement();
201             if( key.indexOf( ANSWER_KEY ) > -1 ) {
202                 v.addElement( new Answer( this.getPortletConfig().getInitParameter( key ), id ) );
203                 ++id;
204             }
205             
206         }
207
208         
209         Answer[] answers = new Answer[v.size()];
210         v.copyInto( answers );
211         this.ag = new AnswerGroup( answers );
212
213         //set the title and description.
214
this.setTitle( VOTE );
215         this.setDescription( VOTE + ": " + this.getQuestion().getTitle() );
216     }
217
218     /**
219         Builds the voting form
220     */

221     private ConcreteElement getUI( RunData rundata ) {
222         
223         Form form = new Form();
224         
225         //add the title to the form
226
form.addElement( new B().addElement( this.getQuestion().getTitle() ) );
227         
228         
229         Answer[] answers = this.ag.getAnswers();
230         //add the answers to the form
231
for( int i = 0; i < answers.length; ++i ) {
232             
233             form.addElement( new BR() );
234             form.addElement( new Input().setType( "radio" )
235                                         .setName( ANSWER_KEY )
236                                         .setValue( answers[i].getID() )
237                                 .addElement( answers[i].getTitle() ) );
238             
239         }
240         
241         form.addElement( new BR() );
242
243         //add the vote button.
244
form.addElement( new Input().setType( "submit" )
245                                     .setValue( VOTE ) );
246
247                                     
248         //add the link s that you can see results.
249
form.addElement( " [ " );
250         
251         DynamicURI uri = new DynamicURI( rundata );
252         uri.addQueryData( ACTION_KEY, RESULTS_KEY );
253         form.addElement( new A( uri.toString() ).addElement( "Results" ) );
254         
255         form.addElement( " ] " );
256         
257         //add the action for voting.
258
form.addElement( new Input().setType( "hidden" )
259                                     .setName( ACTION_KEY )
260                                     .setValue( VOTE_KEY ) );
261                                     
262         return form;
263     }
264     
265     /**
266     Get all available answers for this poll.
267     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
268     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
269     */

270     public AnswerGroup getAnswerGroup() {
271         return this.ag;
272     }
273     
274     /**
275     Set the question for this poll
276     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
277     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
278     */

279     public void setQuestion( Question question ) {
280         this.question = question;
281     }
282     
283     /**
284     Get the question for this poll.
285     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
286     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
287     */

288     public Question getQuestion() {
289         return this.question;
290     }
291     
292     
293     //begin private IMPL methods
294

295     /**
296     Get the results of this poll and display them in a table format.
297     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
298     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
299     */

300     private ConcreteElement getResults( RunData rundata ) {
301         
302         ElementContainer ec = new ElementContainer();
303
304         AnswerGroup ag = this.getAnswerGroup();
305         
306         Answer[] answers = ag.getAnswers();
307         
308         int top = ag.getTopAnswer().getVoteCount();
309         
310         ConcreteElement nbsp = new ClearElement( "&nbsp;" );
311
312         //make sure the user hasn't voted before.
313
if ( this.hasVoted( rundata ) ) {
314             ec.addElement( new I().addElement( "You have already voted!" ) );
315             ec.addElement( new BR() );
316         }
317         
318         
319         //add the question to the Portlet so that he user knows what they just
320
//voted on.
321
ec.addElement( new B().addElement( this.getQuestion().getTitle() ) );
322         
323         Table table = new Table();
324         
325         for( int i = 0; i < answers.length; ++i ) {
326             
327             Answer answer = answers[i];
328             
329             //compute the width of this vote item.
330

331             int width = 0;
332             
333             TR row = new TR();
334
335             row.addElement( getColumn( answer.getTitle() ) );
336             
337             if ( answer.getVoteCount() == 0 ) {
338                 width = 1;
339             } else {
340                 width = ( DEFAULT_IMG_WIDTH / top ) * answer.getVoteCount();
341             }
342             
343
344             
345             TD result = getColumn( new IMG().setSrc( IMG_MAINBAR )
346                                                 .setWidth( width )
347                                                 .setHeight( DEFAULT_IMG_HEIGHT ) );
348
349             //get the current vote count and percentage
350
result.addElement( nbsp );
351             result.addElement( Integer.toString( answer.getVoteCount() ) +
352                                " / " +
353                                Integer.toString( ag.getPercentage( answer ) ) +
354                                "%" );
355                                                 
356             row.addElement( result );
357             
358             row.addElement( new TD().setWidth( "100%" ) );
359             
360             table.addElement( row );
361         }
362         
363         ec.addElement( table );
364         
365         //now add the total number of votes.
366
ec.addElement( new BR() );
367         ec.addElement( new B()
368             .addElement( Integer.toString( this.getAnswerGroup().getTotal() ) +
369                          " total vote(s)" ) );
370
371         //add the warning
372
ec.addElement( new BR() );
373         ec.addElement( WARNING );
374                          
375         return ec;
376     }
377
378     /**
379     Get a column for the vote table.
380
381     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
382     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
383     */

384     private TD getColumn( String JavaDoc data ) {
385         
386         return getColumn( new StringElement( data ) );
387         
388     }
389     
390
391     /**
392     Get a column for the vote table.
393
394     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
395     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
396     */

397     private TD getColumn( ConcreteElement ce ) {
398         
399         return new TD().setVAlign( "middle" )
400                        .setAlign( "left" )
401                        .setNoWrap( true )
402                        .addElement( ce );
403         
404     }
405     
406     /**
407     Returns true if this user/IP has already voted
408
409     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
410     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
411     */

412     private boolean hasVoted( RunData rundata ) {
413         
414         /* FIX ME:
415           
416           This uses RunData and the Servlet API directly... a change is pending
417           to the Turbine User object for getRemoteAddr and this code should be
418           changed when done.
419         
420         */

421         
422         String JavaDoc addr = rundata.getRequest().getRemoteAddr();
423         
424         if ( addr == null ) {
425             return false;
426         }
427         
428         if ( this.votes.get( addr ) == null ) {
429             return false;
430         } else {
431             return true;
432         }
433         
434     }
435
436     /**
437     Set the current user's status to voted.
438     
439     @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
440     @version $Id: PollPortlet.java,v 1.3 2001/03/07 07:12:35 taylor Exp $
441     */

442     public void setVoted( boolean voted, RunData rundata ) {
443         
444         String JavaDoc addr = rundata.getRequest().getRemoteAddr();
445
446         if ( voted ) {
447
448             if ( this.votes.get( addr ) == null ) {
449                 this.votes.put( addr, addr );
450             }
451             
452         } else {
453             this.votes.remove( addr );
454         }
455             
456         
457     }
458     
459 }
460
461
462
463
464
Popular Tags