1 54 55 package org.apache.jetspeed.portal.portlets; 56 57 import java.io.*; 59 import java.util.*; 60 61 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 import org.apache.jetspeed.util.*; 70 import org.apache.jetspeed.portal.*; 71 72 import org.apache.jetspeed.portal.portlets.util.poll.*; 74 75 76 import org.apache.turbine.util.*; 78 79 80 88 public class PollPortlet extends AbstractPortlet { 89 90 91 public static final String VOTE = "Vote"; 92 93 94 public static final String ACTION_KEY = "jetspeed.poll.action"; 95 public static final String VOTE_KEY = "jetspeed.poll.vote"; 96 public static final String QUESTION_KEY = "jetspeed.poll.question"; 97 public static final String ANSWER_KEY = "jetspeed.poll.answer"; 98 public static final String RESULTS_KEY = "jetspeed.poll.results"; 99 100 public static final String WARNING = "NOTE: Do to use this poll for anything important, it might not be accurate!"; 101 102 106 public static final String ACTION_VOTE = VOTE_KEY; 107 108 111 public static final String ACTION_GET_RESULTS = "jetspeed.pool.results"; 112 113 116 public static final String IMG_MAINBAR = "images/poll/mainbar.gif"; 117 118 121 public static final int DEFAULT_IMG_WIDTH = 400; 122 123 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 134 private Hashtable votes = new Hashtable(); 135 136 143 public ConcreteElement getContent(RunData rundata) { 144 145 String 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 if ( ! this.hasVoted( rundata ) ) { 155 this.getAnswerGroup() 156 .getAnswer( rundata 157 .getParameters().getInt( ANSWER_KEY ) ).castVote(); 158 } 159 160 } 161 162 163 if ( action.equals( VOTE_KEY ) || 166 action.equals( RESULTS_KEY ) ) { 167 168 ConcreteElement results = this.getResults( rundata ); 169 170 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 188 public void init() { 189 190 this.setQuestion( new Question( this.getPortletConfig().getInitParameter( QUESTION_KEY ) ) ); 191 192 Enumeration params = this.getPortletConfig().getInitParameterNames(); 194 195 Vector v = new Vector(); 196 197 int id = 0; 198 while ( params.hasMoreElements() ) { 199 200 String key = (String )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 this.setTitle( VOTE ); 215 this.setDescription( VOTE + ": " + this.getQuestion().getTitle() ); 216 } 217 218 221 private ConcreteElement getUI( RunData rundata ) { 222 223 Form form = new Form(); 224 225 form.addElement( new B().addElement( this.getQuestion().getTitle() ) ); 227 228 229 Answer[] answers = this.ag.getAnswers(); 230 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 form.addElement( new Input().setType( "submit" ) 245 .setValue( VOTE ) ); 246 247 248 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 form.addElement( new Input().setType( "hidden" ) 259 .setName( ACTION_KEY ) 260 .setValue( VOTE_KEY ) ); 261 262 return form; 263 } 264 265 270 public AnswerGroup getAnswerGroup() { 271 return this.ag; 272 } 273 274 279 public void setQuestion( Question question ) { 280 this.question = question; 281 } 282 283 288 public Question getQuestion() { 289 return this.question; 290 } 291 292 293 295 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( " " ); 311 312 if ( this.hasVoted( rundata ) ) { 314 ec.addElement( new I().addElement( "You have already voted!" ) ); 315 ec.addElement( new BR() ); 316 } 317 318 319 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 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 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 ec.addElement( new BR() ); 367 ec.addElement( new B() 368 .addElement( Integer.toString( this.getAnswerGroup().getTotal() ) + 369 " total vote(s)" ) ); 370 371 ec.addElement( new BR() ); 373 ec.addElement( WARNING ); 374 375 return ec; 376 } 377 378 384 private TD getColumn( String data ) { 385 386 return getColumn( new StringElement( data ) ); 387 388 } 389 390 391 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 412 private boolean hasVoted( RunData rundata ) { 413 414 421 422 String 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 442 public void setVoted( boolean voted, RunData rundata ) { 443 444 String 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 |