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.user.api.UserDirectoryService;
024:
025: import org.sakaiproject.poll.logic.PollListManager;
026: import org.sakaiproject.poll.logic.PollVoteManager;
027: import org.sakaiproject.poll.model.Option;
028: import org.sakaiproject.poll.model.Poll;
029:
030: import uk.org.ponder.messageutil.MessageLocator;
031: import uk.org.ponder.rsf.components.UIContainer;
032: import uk.org.ponder.rsf.components.UIOutput;
033: import uk.org.ponder.rsf.components.UICommand;
034: import uk.org.ponder.rsf.components.UIInternalLink;
035: import uk.org.ponder.rsf.view.ComponentChecker;
036: import uk.org.ponder.rsf.view.ViewComponentProducer;
037: import uk.org.ponder.rsf.viewstate.ViewParameters;
038: import uk.org.ponder.localeutil.LocaleGetter;
039: import uk.org.ponder.rsf.viewstate.EntityCentredViewParameters;
040: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
041: import uk.org.ponder.rsf.viewstate.ViewParamsReporter;
042: import uk.org.ponder.beanutil.BeanGetter;
043: import uk.org.ponder.beanutil.entity.EntityID;
044: import uk.org.ponder.rsf.components.UIELBinding;
045: import uk.org.ponder.rsf.components.UIMessage;
046: import uk.org.ponder.rsf.components.UISelect;
047: import uk.org.ponder.rsf.components.UISelectChoice;
048: import uk.org.ponder.rsf.components.UISelectLabel;
049: import uk.org.ponder.rsf.components.UIForm;
050: import uk.org.ponder.rsf.components.UIBranchContainer;
051: import uk.org.ponder.rsf.components.UIOutputMany;
052: import uk.org.ponder.rsf.components.UIVerbatim;
053: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
054: import uk.org.ponder.rsf.flow.jsfnav.NavigationCaseReporter;
055: import uk.org.ponder.messageutil.TargettedMessageList;
056: import java.text.MessageFormat;
057:
058: import java.util.ArrayList;
059: import java.util.List;
060:
061: import org.apache.commons.logging.Log;
062: import org.apache.commons.logging.LogFactory;
063:
064: public class PollVoteProducer implements ViewComponentProducer,
065: ViewParamsReporter, NavigationCaseReporter {
066:
067: public static final String VIEW_ID = "voteQuestion";
068: private BeanGetter rbg;
069: private UserDirectoryService userDirectoryService;
070: private PollListManager pollListManager;
071: private ToolManager toolManager;
072: private MessageLocator messageLocator;
073: private LocaleGetter localegetter;
074: private TargettedMessageList tml;
075: private PollVoteManager pollVoteManager;
076:
077: private static Log m_log = LogFactory
078: .getLog(PollVoteProducer.class);
079:
080: public String getViewID() {
081: // TODO Auto-generated method stub
082: return VIEW_ID;
083: }
084:
085: public void setPollVoteManager(PollVoteManager pvm) {
086: this .pollVoteManager = pvm;
087: }
088:
089: public void setMessageLocator(MessageLocator messageLocator) {
090:
091: this .messageLocator = messageLocator;
092: }
093:
094: public void setUserDirectoryService(
095: UserDirectoryService userDirectoryService) {
096: this .userDirectoryService = userDirectoryService;
097: }
098:
099: public void setPollListManager(PollListManager pollListManager) {
100: this .pollListManager = pollListManager;
101: }
102:
103: public void setToolManager(ToolManager toolManager) {
104: this .toolManager = toolManager;
105: }
106:
107: public void setLocaleGetter(LocaleGetter localegetter) {
108: this .localegetter = localegetter;
109: }
110:
111: public void setTargettedMessageList(TargettedMessageList tml) {
112: this .tml = tml;
113: }
114:
115: public void fillComponents(UIContainer tofill,
116: ViewParameters viewparams, ComponentChecker checker) {
117:
118: if (tml.size() > 0) {
119: for (int i = 0; i < tml.size(); i++) {
120: UIBranchContainer errorRow = UIBranchContainer.make(
121: tofill, "error-row:");
122: String output;
123: if (tml.messageAt(i).args != null) {
124: output = messageLocator.getMessage(tml.messageAt(i)
125: .acquireMessageCode(),
126: tml.messageAt(i).args[0]);
127: } else {
128: output = messageLocator.getMessage(tml.messageAt(i)
129: .acquireMessageCode());
130: }
131: UIOutput.make(errorRow, "error", output);
132: }
133: }
134:
135: UIOutput.make(tofill, "poll-vote-title", messageLocator
136: .getMessage("poll_vote_title"));
137:
138: try {
139:
140: EntityCentredViewParameters ecvp = (EntityCentredViewParameters) viewparams;
141:
142: //m_log.info(ecvp.toPathInfo());
143:
144: //m_log.info(ecvp.getELPath());
145:
146: //hack but this needs to work
147: String strId = ecvp.getELPath().substring(
148: ecvp.getELPath().indexOf(".") + 1);
149: m_log.debug("got id of " + strId);
150: Poll poll = pollListManager.getPollById(new Long(strId));
151:
152: m_log.debug("got poll " + poll.getText());
153:
154: //check if they can vote
155: if (poll.getLimitVoting()
156: && pollVoteManager.userHasVoted(poll.getPollId())) {
157: m_log.warn("This user has already voted!");
158: UIOutput.make(tofill, "hasErrors", messageLocator
159: .getMessage("vote_hasvoted"));
160: return;
161: }
162:
163: UIOutput.make(tofill, "poll-text", poll.getText());
164: if (poll.getDetails() != null) {
165: UIVerbatim.make(tofill, "poll-description", poll
166: .getDetails());
167: }
168:
169: m_log.debug("this poll has " + poll.getPollOptions().size()
170: + " options");
171:
172: UIForm voteForm = UIForm.make(tofill, "options-form", "");
173:
174: List pollOptions = poll.getPollOptions();
175: //build the options + label lists
176: String[] values = new String[pollOptions.size()];
177: for (int i = 0; i < pollOptions.size(); i++) {
178: Option po = (Option) pollOptions.get(i);
179: values[i] = po.getId().toString();
180: }
181:
182: String[] labels = new String[pollOptions.size()];
183: for (int i = 0; i < pollOptions.size(); i++) {
184: Option po = (Option) pollOptions.get(i);
185: if (po.getOptionText() != null) {
186: labels[i] = po.getOptionText();
187: } else {
188: m_log.warn("Option text is null!");
189: labels[i] = "null option!";
190: }
191: }
192:
193: //we need to deside is this a single or multiple?
194: //poll.getMaxOptions()
195: boolean isMultiple = false;
196: if (poll.getMaxOptions() > 1)
197: isMultiple = true;
198:
199: UISelect radio;
200: if (isMultiple)
201: radio = UISelect.makeMultiple(voteForm, "optionform",
202: values, "#{voteCollection.optionsSelected}",
203: new String[] {});
204: else
205: radio = UISelect.make(voteForm, "optionform", values,
206: "#{voteCollection.option}", new String());
207:
208: radio.optionnames = UIOutputMany.make(labels);
209: String selectID = radio.getFullID();
210:
211: for (int i = 0; i < pollOptions.size(); i++) {
212: Option po = (Option) pollOptions.get(i);
213: m_log.debug("got option " + po.getOptionText()
214: + " with id of " + po.getId());
215:
216: UIBranchContainer radioRow = UIBranchContainer.make(
217: voteForm, isMultiple ? "option:select"
218: : "option:radio", Integer.toString(i));
219: UISelectChoice.make(radioRow, "option-radio", selectID,
220: i);
221: UISelectLabel.make(radioRow, "option-label", selectID,
222: i);
223: //UIOutput.make(radioRow,"option-label",labels[i]);
224: }
225: //bind some parameters
226: voteForm.parameters.add(new UIELBinding(
227: "#{voteCollection.pollId}", poll.getPollId()));
228:
229: UICommand sub = UICommand.make(voteForm, "submit-new-vote",
230: messageLocator.getMessage("vote_vote"),
231: "#{pollToolBean.processActionVote}");
232: sub.parameters.add(new UIELBinding(
233: "#{voteCollection.submissionStatus}", "sub"));
234: UICommand cancel = UICommand.make(voteForm, "cancel",
235: messageLocator.getMessage("vote_cancel"),
236: "#{pollToolBean.cancel}");
237: cancel.parameters.add(new UIELBinding(
238: "#{voteCollection.submissionStatus}", "cancel"));
239: UIOutput.make(voteForm, "reset", messageLocator
240: .getMessage("vote_reset"));
241:
242: } catch (Exception e) {
243: m_log.error("Error: " + e);
244: e.printStackTrace();
245: }
246: }
247:
248: public void setELEvaluator(BeanGetter rbg) {
249: this .rbg = rbg;
250: }
251:
252: public ViewParameters getViewParameters() {
253: return new EntityCentredViewParameters(VIEW_ID, new EntityID(
254: "Poll", null));
255:
256: }
257:
258: public List reportNavigationCases() {
259: List togo = new ArrayList(); // Always navigate back to this view.
260: //togo.add(new NavigationCase(null, new SimpleViewParameters(VIEW_ID)));
261: togo.add(new NavigationCase("Error", new SimpleViewParameters(
262: VIEW_ID)));
263: togo.add(new NavigationCase("Success",
264: new SimpleViewParameters(ConfirmProducer.VIEW_ID)));
265: togo.add(new NavigationCase("cancel", new SimpleViewParameters(
266: PollToolProducer.VIEW_ID)));
267: return togo;
268: }
269: }
|