KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > ViewWelcome


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.actions;
27
28 import net.killingar.Utils;
29 import net.killingar.forum.internal.Poll;
30 import net.killingar.forum.internal.PollAlternative;
31 import net.killingar.forum.internal.PollAnswer;
32 import net.killingar.forum.internal.Quote;
33 import net.killingar.forum.internal.managers.OptionsManager;
34 import net.killingar.forum.internal.managers.PollManager;
35 import net.killingar.forum.internal.managers.QuoteManager;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 public class ViewWelcome
41    extends ActionForumSupport
42 {
43     // Attributes -----------------------------------------------------
44
Quote quote;
45     Poll poll;
46     List JavaDoc pollAlternatives;
47     long totalNumberOfAnswers;
48     boolean vote;
49     long selectedID;
50     List JavaDoc invites;
51
52     // Types ----------------------------------------------------------
53
static class PollAlternativeData
54     {
55         PollAlternative alternative;
56         int width;
57         String JavaDoc barURL;
58         boolean selected;
59         double percent;
60
61         public PollAlternativeData(PollAlternative alternative, double percent, int width, String JavaDoc barURL, boolean selected)
62         {
63             this.alternative = alternative;
64             this.percent = percent;
65             this.width = width;
66             this.barURL = barURL;
67             this.selected = selected;
68         }
69
70         public PollAlternative getAlternative() { return alternative; }
71         public int getWidth() { return width; }
72         public String JavaDoc getBarURL() { return barURL; }
73         public boolean getSelected() { return selected; }
74         public double getPercent() { return percent; }
75     }
76
77     // Setters --------------------------------------------------------
78

79     // Getters --------------------------------------------------------
80
public Quote getQuote() { return quote; }
81     public Poll getPoll() { return poll; }
82     public List JavaDoc getPollAlternatives() { return pollAlternatives; }
83     public long getTotalNumberOfAnswers() { return totalNumberOfAnswers; }
84     public long getSelectedID() { return selectedID; }
85     public boolean getVote() { return vote; }
86     public List JavaDoc getInvites() { return invites; }
87   // Implementation -------------------------------------------------
88
protected String JavaDoc doExecute()
89     {
90         try
91         {
92             // get default bar image url
93
OptionsManager options = (OptionsManager)manager.getManager(OptionsManager.class.getName());
94             String JavaDoc barImageURL = options.get("bar image");
95
96             // quote
97
quote = ((QuoteManager)manager.getManager(QuoteManager.class.getName())).getRandomQuote();
98
99             // invites
100
invites = manager.getInvites();
101
102             // poll
103
if (!"true".equals(Utils.getProperties().get(getClass().getName()+".hidePoll")))
104             {
105                 PollManager pollmgr = (PollManager)manager.getManager(PollManager.class.getName());
106                 poll = pollmgr.getRandomUnAnsweredPoll();
107                 if (poll == null) // view poll
108
{
109                     vote = false;
110
111                     poll = pollmgr.getRandomPoll();
112                 }
113                 else // vote
114
vote = true;
115
116                 if (poll != null)
117                 {
118                     PollAlternative pollAlternativesData[] = pollmgr.getPollAlternatives(poll.getId());
119                     selectedID = 0;
120                     {
121                         PollAnswer foo = pollmgr.getMyAnswer(poll.ID);
122                         if (foo != null)
123                             selectedID = foo.pollAlternativeID;
124                     }
125
126                     pollAlternatives = new ArrayList JavaDoc();
127
128                     totalNumberOfAnswers = 0;
129                     for (int i = 0; i < pollAlternativesData.length; i++)
130                         totalNumberOfAnswers += pollAlternativesData[i].numberOfAnswers;
131
132                     for (int i = 0; i < pollAlternativesData.length; i++)
133                     {
134                         double percent = (totalNumberOfAnswers == 0)?0:(double)((int)((double)pollAlternativesData[i].numberOfAnswers/totalNumberOfAnswers*1000))/10;
135                         int width = (int)(percent*2)+1;
136                         String JavaDoc barURL = pollAlternativesData[i].color.equals("722899")?barImageURL:"servlet/Pixel?"+pollAlternativesData[i].color;
137
138                         pollAlternatives.add(new PollAlternativeData(
139                             pollAlternativesData[i],
140                             percent,
141                             width,
142                             barURL,
143                             selectedID == pollAlternativesData[i].ID
144                             ));
145                     }
146                 }
147             }
148
149             return SUCCESS;
150         }
151         catch (Exception JavaDoc e)
152         {
153             addErrorMessage("displaying welcome page failed, exception thrown ("+e.toString()+")");
154             e.printStackTrace();
155             return ERROR;
156         }
157   }
158 }
Popular Tags