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 java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.List;
026: import java.text.ParseException;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.poll.model.Poll;
031: import org.sakaiproject.poll.model.Option;
032: import org.sakaiproject.poll.model.PollImpl;
033: import org.sakaiproject.poll.logic.PollListManager;
034: import org.sakaiproject.poll.tool.params.VoteBean;
035:
036: import uk.org.ponder.beanutil.entity.EntityID;
037: import uk.org.ponder.messageutil.MessageLocator;
038: import uk.org.ponder.rsf.components.UIContainer;
039: import uk.org.ponder.rsf.components.UIOutput;
040: import uk.org.ponder.rsf.view.ComponentChecker;
041: import uk.org.ponder.rsf.view.ViewComponentProducer;
042: import uk.org.ponder.rsf.viewstate.EntityCentredViewParameters;
043: import uk.org.ponder.rsf.viewstate.ViewParameters;
044: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
045: import uk.org.ponder.localeutil.LocaleGetter;
046: import uk.org.ponder.rsf.components.UIForm;
047: import uk.org.ponder.rsf.components.UIInput;
048: import uk.org.ponder.rsf.components.UIInternalLink;
049: import uk.org.ponder.rsf.components.UICommand;
050: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
051: import uk.org.ponder.rsf.flow.jsfnav.NavigationCaseReporter;
052: import uk.org.ponder.rsf.components.UIELBinding;
053: import uk.org.ponder.rsf.components.UISelect;
054: import uk.org.ponder.rsf.components.UISelectChoice;
055: import uk.org.ponder.rsf.components.UISelectLabel;
056: import uk.org.ponder.rsf.components.UIOutputMany;
057: import uk.org.ponder.rsf.components.UIBranchContainer;
058: import uk.org.ponder.rsf.viewstate.ViewParamsReporter;
059: import uk.org.ponder.rsf.evolvers.TextInputEvolver; //import uk.org.ponder.rsf.components.decorators.UITextDimensionsDecorator;
060: //import uk.org.ponder.rsf.components.decorators.DecoratorList;
061:
062: import org.sakaiproject.tool.api.ToolManager;
063: import org.sakaiproject.user.api.User;
064: import org.sakaiproject.user.api.UserDirectoryService;
065: import org.sakaiproject.entity.api.Entity;
066:
067: public class AddPollProducer implements ViewComponentProducer,
068: NavigationCaseReporter, ViewParamsReporter {
069: public static final String VIEW_ID = "voteAdd";
070: private UserDirectoryService userDirectoryService;
071: private PollListManager pollListManager;
072: private ToolManager toolManager;
073: private MessageLocator messageLocator;
074: private LocaleGetter localegetter;
075:
076: private static Log m_log = LogFactory.getLog(AddPollProducer.class);
077:
078: public String getViewID() {
079: return VIEW_ID;
080: }
081:
082: public void setMessageLocator(MessageLocator messageLocator) {
083: this .messageLocator = messageLocator;
084: }
085:
086: public void setUserDirectoryService(
087: UserDirectoryService userDirectoryService) {
088: this .userDirectoryService = userDirectoryService;
089: }
090:
091: public void setPollListManager(PollListManager pollListManager) {
092: this .pollListManager = pollListManager;
093: }
094:
095: public void setToolManager(ToolManager toolManager) {
096: this .toolManager = toolManager;
097: }
098:
099: public void setLocaleGetter(LocaleGetter localegetter) {
100: this .localegetter = localegetter;
101: }
102:
103: private VoteBean voteBean;
104:
105: public void setVoteBean(VoteBean vb) {
106: this .voteBean = vb;
107: }
108:
109: private TextInputEvolver richTextEvolver;
110:
111: public void setRichTextEvolver(TextInputEvolver richTextEvolver) {
112: this .richTextEvolver = richTextEvolver;
113: }
114:
115: public void fillComponents(UIContainer tofill,
116: ViewParameters viewparams, ComponentChecker checker) {
117:
118: User currentuser = userDirectoryService.getCurrentUser();
119: String currentuserid = currentuser.getId();
120:
121: EntityCentredViewParameters ecvp = (EntityCentredViewParameters) viewparams;
122: Poll poll = null;
123: boolean isNew = true;
124:
125: UIForm newPoll = UIForm.make(tofill, "add-poll-form");
126: if (voteBean.getPoll() != null) {
127: poll = voteBean.getPoll();
128: UIOutput.make(tofill, "new_poll_title", messageLocator
129: .getMessage("new_poll_title"));
130: isNew = false;
131: newPoll.parameters
132: .add(new UIELBinding(
133: "#{pollToolBean.newPoll.pollId}", poll
134: .getPollId()));
135:
136: } else if (ecvp.mode
137: .equals(EntityCentredViewParameters.MODE_NEW)) {
138: UIOutput.make(tofill, "new_poll_title", messageLocator
139: .getMessage("new_poll_title"));
140: //build an empty poll
141: poll = new PollImpl();
142: } else {
143: UIOutput.make(tofill, "new_poll_title", messageLocator
144: .getMessage("new_poll_title_edit"));
145: // hack but this needs to work
146: String strId = ecvp.getELPath().substring(
147: ecvp.getELPath().indexOf(".") + 1);
148: m_log.debug("got id of " + strId);
149: poll = pollListManager.getPollById(new Long(strId));
150: voteBean.setPoll(poll);
151: newPoll.parameters
152: .add(new UIELBinding(
153: "#{pollToolBean.newPoll.pollId}", poll
154: .getPollId()));
155:
156: isNew = false;
157: }
158:
159: //only display for exisiting polls
160: if (!isNew) {
161: //fill the options list
162: UIOutput.make(tofill, "options-title", messageLocator
163: .getMessage("new_poll_option_title"));
164: UIInternalLink.make(tofill, "option-add", messageLocator
165: .getMessage("new_poll_option_add"),
166: new EntityCentredViewParameters(
167: PollOptionProducer.VIEW_ID, new EntityID(
168: "Poll", "Poll_"
169: + poll.getPollId()
170: .toString()),
171: EntityCentredViewParameters.MODE_NEW));
172:
173: //new SimpleViewParameters(PollOptionProducer.VIEW_ID));
174: List options = poll.getPollOptions();
175: for (int i = 0; i < options.size(); i++) {
176: Option o = (Option) options.get(i);
177: UIBranchContainer oRow = UIBranchContainer.make(
178: newPoll, "options-row:");
179: UIOutput.make(oRow, "options-name", o.getOptionText());
180: UIInternalLink.make(oRow, "option-edit", messageLocator
181: .getMessage("new_poll_option_edit"),
182: new EntityCentredViewParameters(
183: PollOptionProducer.VIEW_ID,
184: new EntityID("Option", "Option_"
185: + o.getId().toString()),
186: EntityCentredViewParameters.MODE_EDIT));
187: UIInternalLink.make(oRow, "option-delete",
188: messageLocator
189: .getMessage("new_poll_option_delete"),
190: new EntityCentredViewParameters(
191: PollOptionDeleteProducer.VIEW_ID,
192: new EntityID("Option", "Option_"
193: + o.getId().toString())));
194: }
195: }
196:
197: UIOutput.make(tofill, "new-poll-descr", messageLocator
198: .getMessage("new_poll_title"));
199: UIOutput.make(tofill, "new-poll-question-label", messageLocator
200: .getMessage("new_poll_question_label"));
201: UIOutput.make(tofill, "new-poll-descr-label", messageLocator
202: .getMessage("new_poll_descr_label"));
203: UIOutput.make(tofill, "new-poll-descr-label2", messageLocator
204: .getMessage("new_poll_descr_label2"));
205: UIOutput.make(tofill, "new-poll-open-label", messageLocator
206: .getMessage("new_poll_open_label"));
207: UIOutput.make(tofill, "new-poll-close-label", messageLocator
208: .getMessage("new_poll_close_label"));
209: UIOutput.make(tofill, "new-poll-limits", messageLocator
210: .getMessage("new_poll_limits"));
211: UIOutput.make(tofill, "new-poll-min-limits", messageLocator
212: .getMessage("new_poll_min_limits"));
213: UIOutput.make(tofill, "new-poll-max-limits", messageLocator
214: .getMessage("new_poll_max_limits"));
215:
216: //the form fields
217:
218: UIInput.make(newPoll, "new-poll-text",
219: "#{pollToolBean.newPoll.text}", poll.getText());
220: /*
221: UIInput itemText = UIInput.make(newPoll, "new-poll-text:", "#{pollToolBean.newPoll.text}", poll.getText()); //$NON-NLS-1$ //$NON-NLS-2$
222: itemText.decorators = new DecoratorList(new UITextDimensionsDecorator(40, 4));
223: richTextEvolver.evolveTextInput(itemText);
224: */
225: UIInput.make(newPoll, "new-poll-descr",
226: "#{pollToolBean.newPoll.details}", poll.getDetails());
227: /*
228: UIInput itemDescr = UIInput.make(newPoll, "newpolldescr:", "#{pollToolBean.newPoll.details}", poll.getDetails()); //$NON-NLS-1$ //$NON-NLS-2$
229: itemDescr.decorators = new DecoratorList(new UITextDimensionsDecorator(40, 4));
230: richTextEvolver.evolveTextInput(itemDescr);
231: */
232:
233: //we need a date fomater
234: SimpleDateFormat dayf = new SimpleDateFormat("d");
235:
236: // build a days array
237: String[] days = new String[] { "1", "2", "3", "4", "5", "6",
238: "7", "8", "9", "10", "11", "12", "13", "14", "15",
239: "16", "17", "18", "19", "20", "21", "22", "23", "24",
240: "25", "26", "27", "28", "29", "30", "31" };
241: UISelect oDay = UISelect.make(newPoll, "new-poll-opend", days,
242: "#{pollToolBean.openDay}", dayf.format(poll
243: .getVoteOpen()));
244: UISelect cDay = UISelect.make(newPoll, "new-poll-closed", days,
245: "#{pollToolBean.closeDay}", dayf.format(poll
246: .getVoteClose()));
247:
248: oDay.optionlist = UIOutputMany.make(days);
249: oDay.optionnames = UIOutputMany.make(days);
250: cDay.optionlist = UIOutputMany.make(days);
251: cDay.optionnames = UIOutputMany.make(days);
252:
253: //build the month array
254:
255: String[] months = new String[12];
256: String[] monthsI = new String[12];
257: SimpleDateFormat monthIn = new SimpleDateFormat("M");
258: SimpleDateFormat monthf = new SimpleDateFormat("MMM",
259: localegetter.get());
260: for (int i = 0; i < 12; i++) {
261:
262: try {
263: Date d = monthIn.parse(Integer.toString(i + 1));
264: months[i] = monthf.format(d);
265: monthsI[i] = Integer.toString(i + 1);
266: } catch (ParseException e) {
267: e.printStackTrace();
268: }
269:
270: }
271:
272: m_log.debug("this poll opens in month "
273: + monthIn.format(poll.getVoteOpen()) + " on day "
274: + dayf.format(poll.getVoteOpen())
275: + " and closes in month: "
276: + monthIn.format(poll.getVoteClose()));
277: UISelect oMonth = UISelect.make(newPoll, "new-poll-openm",
278: monthsI, "#{pollToolBean.openMonth}", monthIn
279: .format(poll.getVoteOpen()));
280: UISelect cMonth = UISelect.make(newPoll, "new-poll-closem",
281: monthsI, "#{pollToolBean.closeMonth}", monthIn
282: .format(poll.getVoteClose()));
283: //oMonth.optionlist = UIOutputMany.make(months);
284: oMonth.optionnames = UIOutputMany.make(months);
285: //cMonth.optionlist = UIOutputMany.make(months);
286: cMonth.optionnames = UIOutputMany.make(months);
287:
288: String[] years = new String[] { "2006", "2007", "2008", "2009",
289: "2010" };
290: SimpleDateFormat yearf = new SimpleDateFormat("yyyy");
291:
292: UISelect oYear = UISelect.make(newPoll, "new-poll-openy",
293: years, "#{pollToolBean.openYear}", yearf.format(poll
294: .getVoteOpen()));
295: UISelect cYear = UISelect.make(newPoll, "new-poll-closey",
296: years, "#{pollToolBean.closeYear}", yearf.format(poll
297: .getVoteClose()));
298: oYear.optionnames = UIOutputMany.make(years);
299: cYear.optionnames = UIOutputMany.make(years);
300:
301: String[] hours = new String[] { "1", "2", "3", "4", "5", "6",
302: "7", "8", "9", "10", "11", "12" };
303: SimpleDateFormat hoursf = new SimpleDateFormat("h");
304: UISelect oHours = UISelect.make(newPoll, "new-poll-openh",
305: hours, "#{pollToolBean.openHour}", hoursf.format(poll
306: .getVoteOpen()));
307: UISelect cHours = UISelect.make(newPoll, "new-poll-closeh",
308: hours, "#{pollToolBean.closeHour}", hoursf.format(poll
309: .getVoteClose()));
310: cHours.optionnames = UIOutputMany.make(hours);
311: oHours.optionnames = UIOutputMany.make(hours);
312:
313: SimpleDateFormat minf = new SimpleDateFormat("m");
314: String[] minutes = new String[] { "00", "15", "30", "45" };
315: String openM = null;
316: String closeM = null;
317: if (poll == null) {
318: openM = "00";
319: closeM = "00";
320: } else {
321: openM = minf.format(poll.getVoteOpen());
322: closeM = minf.format(poll.getVoteClose());
323: }
324:
325: UISelect.make(newPoll, "new-poll-openms", minutes,
326: "#{pollToolBean.openMinutes}", openM);
327: UISelect.make(newPoll, "new-poll-closems", minutes,
328: "#{pollToolBean.closeMinutes}", closeM);
329:
330: SimpleDateFormat amf = new SimpleDateFormat("a");
331: String[] ampm = new String[] { "AM", "PM" };
332: UISelect.make(newPoll, "new-poll-openampm", ampm,
333: "#{pollToolBean.openAmPm}", amf.format(poll
334: .getVoteOpen()));
335: UISelect.make(newPoll, "new-poll-closeampm", ampm,
336: "#{pollToolBean.closeAmPm}", amf.format(poll
337: .getVoteClose()));
338:
339: String[] minVotes = new String[] { "0", "1", "2", "3", "4",
340: "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
341: "15" };
342: String[] maxVotes = new String[] { "1", "2", "3", "4", "5",
343: "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
344: UISelect.make(newPoll, "min-votes", minVotes,
345: "#{pollToolBean.newPoll.minOptions}", Integer
346: .toString(poll.getMinOptions()));
347: UISelect.make(newPoll, "max-votes", maxVotes,
348: "#{pollToolBean.newPoll.maxOptions}", Integer
349: .toString(poll.getMaxOptions()));
350: /*
351: * open - can be viewd at any time
352: * never - not diplayed
353: * afterVoting - after user has voted
354: * afterClosing
355: *
356: */
357:
358: m_log.debug("finished with the date controlls");
359:
360: String[] values = new String[] { "open", "never",
361: "afterVoting", "afterClosing" };
362: String[] labels = new String[] {
363: messageLocator.getMessage("new_poll_open"),
364: messageLocator.getMessage("new_poll_never"),
365: messageLocator.getMessage("new_poll_aftervoting"),
366: messageLocator.getMessage("new_poll_afterClosing") };
367:
368: UISelect radioselect = UISelect.make(newPoll, "release-select",
369: values, "#{pollToolBean.newPoll.displayResult}", poll
370: .getDisplayResult());
371:
372: radioselect.optionnames = UIOutputMany.make(labels);
373:
374: String selectID = radioselect.getFullID();
375: //StringList optList = new StringList();
376: UIOutput.make(newPoll, "add_results_label", messageLocator
377: .getMessage("new_poll_results_label"));
378: for (int i = 0; i < values.length; ++i) {
379:
380: UIBranchContainer radiobranch = UIBranchContainer.make(
381: newPoll, "releaserow:", Integer.toString(i));
382: UISelectChoice.make(radiobranch, "release", selectID, i);
383: UISelectLabel
384: .make(radiobranch, "releaseLabel", selectID, i);
385:
386: }
387:
388: m_log.debug("About to close the form");
389: newPoll.parameters.add(new UIELBinding(
390: "#{pollToolBean.newPoll.owner}", currentuserid));
391: String siteId = toolManager.getCurrentPlacement().getContext();
392: newPoll.parameters.add(new UIELBinding(
393: "#{pollToolBean.siteID}", siteId));
394:
395: if (ecvp.mode != null
396: && ecvp.mode
397: .equals(EntityCentredViewParameters.MODE_NEW)) {
398: UICommand.make(newPoll, "submit-new-poll", messageLocator
399: .getMessage("new_poll_saveoption"),
400: "#{pollToolBean.processActionAdd}");
401: } else {
402: UICommand.make(newPoll, "submit-new-poll", messageLocator
403: .getMessage("new_poll_submit"),
404: "#{pollToolBean.processActionAdd}");
405: }
406:
407: UICommand cancel = UICommand.make(newPoll, "cancel",
408: messageLocator.getMessage("new_poll_cancel"),
409: "#{pollToolBean.cancel}");
410: cancel.parameters.add(new UIELBinding(
411: "#{voteCollection.submissionStatus}", "cancel"));
412: m_log.debug("Finished generating view");
413: }
414:
415: public List reportNavigationCases() {
416: List togo = new ArrayList(); // Always navigate back to this view.
417: togo.add(new NavigationCase(null, new SimpleViewParameters(
418: VIEW_ID)));
419: togo.add(new NavigationCase("added", new SimpleViewParameters(
420: PollToolProducer.VIEW_ID)));
421: togo.add(new NavigationCase("option",
422: new EntityCentredViewParameters(
423: PollOptionProducer.VIEW_ID, new EntityID(
424: "Option", "new 1"),
425: EntityCentredViewParameters.MODE_NEW)));
426: togo.add(new NavigationCase("cancel", new SimpleViewParameters(
427: PollToolProducer.VIEW_ID)));
428: return togo;
429: }
430:
431: public ViewParameters getViewParameters() {
432: return new EntityCentredViewParameters(VIEW_ID, new EntityID(
433: "Poll", null));
434:
435: }
436: }
|