001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006,2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.poll.tool.producers;
021:
022: import org.sakaiproject.tool.api.ToolManager;
023: import org.sakaiproject.poll.model.Option;
024: import org.sakaiproject.poll.logic.PollListManager;
025: import org.sakaiproject.poll.logic.PollVoteManager;
026: import org.sakaiproject.poll.model.OptionImpl;
027: import org.sakaiproject.poll.model.Poll;
028: import org.sakaiproject.poll.model.Vote;
029: import org.sakaiproject.user.api.UserDirectoryService;
030:
031: import uk.org.ponder.beanutil.entity.EntityID;
032: import uk.org.ponder.messageutil.MessageLocator;
033: import uk.org.ponder.rsf.components.UIContainer;
034: import uk.org.ponder.rsf.view.ComponentChecker;
035: import uk.org.ponder.rsf.view.ViewComponentProducer;
036: import uk.org.ponder.rsf.viewstate.EntityCentredViewParameters;
037: import uk.org.ponder.rsf.viewstate.ViewParameters;
038: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
039: import uk.org.ponder.localeutil.LocaleGetter;
040: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
041: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
042: import uk.org.ponder.rsf.flow.jsfnav.NavigationCaseReporter;
043: import uk.org.ponder.rsf.viewstate.ViewParamsReporter;
044: import uk.org.ponder.rsf.components.UIBranchContainer;
045: import uk.org.ponder.rsf.components.UIOutput;
046: import uk.org.ponder.rsf.components.UIForm;
047: import uk.org.ponder.rsf.components.UICommand;
048: import uk.org.ponder.util.NumberFormatter;
049:
050: import java.util.List;
051: import java.util.ArrayList;
052: import java.math.BigDecimal;
053: import java.math.BigInteger;
054:
055: import org.apache.commons.logging.Log;
056: import org.apache.commons.logging.LogFactory;
057:
058: public class ResultsProducer implements ViewComponentProducer,
059: NavigationCaseReporter, ViewParamsReporter {
060:
061: public static final String VIEW_ID = "voteResults";
062:
063: private UserDirectoryService userDirectoryService;
064: private PollListManager pollListManager;
065: private PollVoteManager pollVoteManager;
066: private ToolManager toolManager;
067: private MessageLocator messageLocator;
068: private LocaleGetter localegetter;
069:
070: private static Log m_log = LogFactory.getLog(ResultsProducer.class);
071:
072: public String getViewID() {
073: // TODO Auto-generated method stub
074: return VIEW_ID;
075: }
076:
077: public void setMessageLocator(MessageLocator messageLocator) {
078:
079: this .messageLocator = messageLocator;
080: }
081:
082: public void setUserDirectoryService(
083: UserDirectoryService userDirectoryService) {
084: this .userDirectoryService = userDirectoryService;
085: }
086:
087: public void setPollListManager(PollListManager pollListManager) {
088: this .pollListManager = pollListManager;
089: }
090:
091: public void setToolManager(ToolManager toolManager) {
092: this .toolManager = toolManager;
093: }
094:
095: public void setLocaleGetter(LocaleGetter localegetter) {
096: this .localegetter = localegetter;
097: }
098:
099: public void setPollVoteManager(PollVoteManager pvm) {
100: this .pollVoteManager = pvm;
101: }
102:
103: public void fillComponents(UIContainer tofill,
104: ViewParameters viewparams, ComponentChecker checker) {
105:
106: EntityCentredViewParameters ecvp = (EntityCentredViewParameters) viewparams;
107:
108: //m_log.info(ecvp.toPathInfo());
109:
110: //m_log.info(ecvp.getELPath());
111:
112: //hack but this needs to work
113: String strId = ecvp.getELPath().substring(
114: ecvp.getELPath().indexOf(".") + 1);
115: m_log.debug("got id of " + strId);
116: Poll poll = pollListManager.getPollById(new Long(strId));
117:
118: //get the number of votes
119: int voters = pollVoteManager.getDisctinctVotersForPoll(poll);
120: //Object[] args = new Object[] { new Integer(voters).toString()};
121: if (poll.getMaxOptions() > 1)
122: UIOutput.make(tofill, "poll-size", messageLocator
123: .getMessage("results_poll_size",
124: new Integer(voters).toString()));
125:
126: m_log.debug(voters + " have voted on this poll");
127:
128: UIOutput.make(tofill, "question", poll.getText());
129: m_log.debug("got poll " + poll.getText());
130: List pollOptions = poll.getPollOptions();
131:
132: m_log.debug("got a list of " + pollOptions.size() + " options");
133: //appeng an option for no votes
134: if (poll.getMinOptions() == 0) {
135: Option noVote = new OptionImpl(new Long(0));
136: noVote.setOptionText(messageLocator
137: .getMessage("result_novote"));
138: noVote.setPollId(poll.getPollId());
139: pollOptions.add(noVote);
140: }
141:
142: List votes = pollVoteManager.getAllVotesForPoll(poll);
143: int totalVotes = votes.size();
144: m_log.debug("got " + totalVotes + " votes");
145: List collation = new ArrayList();
146:
147: for (int i = 0; i < pollOptions.size(); i++) {
148: CollatedVote collatedVote = new CollatedVote();
149: Option option = (Option) pollOptions.get(i);
150: collatedVote.setoptionId(option.getId());
151: collatedVote.setOptionText(option.getOptionText());
152: for (int q = 0; q < votes.size(); q++) {
153: Vote vote = (Vote) votes.get(q);
154: if (vote.getPollOption().equals(option.getId())) {
155: m_log.debug("got a vote for option "
156: + option.getId());
157: collatedVote.incrementVotes();
158:
159: }
160:
161: }
162: collation.add(collatedVote);
163:
164: }
165:
166: UIOutput.make(tofill, "answers-title", messageLocator
167: .getMessage("results_answers_title"));
168:
169: for (int i = 0; i < collation.size(); i++) {
170: CollatedVote cv = (CollatedVote) collation.get(i);
171: UIBranchContainer resultRow = UIBranchContainer.make(
172: tofill, "answer-row:");
173: UIOutput.make(resultRow, "answer-option", cv
174: .getOptionText());
175: UIOutput.make(resultRow, "answer-numVotes", new Long(cv
176: .getVotes()).toString());
177: m_log.debug("about to do the calc: (" + cv.getVotes() + "/"
178: + totalVotes + ")*100");
179: double percent = (double) 0;
180: if (totalVotes > 0)
181: percent = ((double) cv.getVotes() / (double) totalVotes)
182: * (double) 100;
183: else
184: percent = (double) 0;
185: m_log.debug("result is " + percent);
186: NumberFormatter nf = new NumberFormatter();
187: UIOutput.make(resultRow, "answer-percVotes", nf
188: .format(percent)
189: + "%");
190:
191: }
192: UIOutput.make(tofill, "votes-total", new Integer(totalVotes)
193: .toString());
194: //the cancel button
195: UIForm form = UIForm.make(tofill, "actform");
196: UICommand
197: .make(form, "cancel", messageLocator
198: .getMessage("results_cancel"),
199: "#{pollToolBean.cancel}");
200:
201: }
202:
203: public List reportNavigationCases() {
204: List togo = new ArrayList(); // Always navigate back to this view.
205: togo.add(new NavigationCase(null, new SimpleViewParameters(
206: VIEW_ID)));
207: togo.add(new NavigationCase("cancel", new SimpleViewParameters(
208: PollToolProducer.VIEW_ID)));
209: return togo;
210: }
211:
212: public ViewParameters getViewParameters() {
213: return new EntityCentredViewParameters(VIEW_ID, new EntityID(
214: "Poll", null));
215:
216: }
217:
218: private class CollatedVote {
219: private Long optionId;
220: private String optionText;
221: private int votes;
222:
223: public CollatedVote() {
224: this .votes = 0;
225: }
226:
227: public void setoptionId(Long val) {
228: this .optionId = val;
229: }
230:
231: public Long getoptionId() {
232: return this .optionId;
233: }
234:
235: public void setOptionText(String t) {
236: this .optionText = t;
237: }
238:
239: public String getOptionText() {
240: return this .optionText;
241: }
242:
243: public void setVotes(int i) {
244: this .votes = i;
245: }
246:
247: public int getVotes() {
248: return this .votes;
249: }
250:
251: public void incrementVotes() {
252: this.votes++;
253: }
254:
255: }
256: }
|