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.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import uk.org.ponder.messageutil.MessageLocator;
026: import uk.org.ponder.messageutil.TargettedMessageList;
027: import uk.org.ponder.rsf.components.UIBranchContainer;
028: import uk.org.ponder.rsf.components.UIContainer;
029: import uk.org.ponder.rsf.components.UIELBinding;
030: import uk.org.ponder.rsf.components.UIOutput; //import uk.org.ponder.rsf.components.UIInternalLink;
031: import uk.org.ponder.rsf.components.UIForm;
032: import uk.org.ponder.rsf.components.UICommand;
033:
034: import uk.org.ponder.rsf.view.ComponentChecker;
035: import uk.org.ponder.rsf.view.ViewComponentProducer;
036: import uk.org.ponder.rsf.viewstate.ViewParameters;
037:
038: //import org.sakaiproject.tool.poll.api.VoteCollection;
039: import org.sakaiproject.poll.logic.PollListManager;
040: import org.sakaiproject.poll.model.Poll;
041: import org.sakaiproject.poll.model.Option;
042: import org.sakaiproject.poll.model.OptionImpl;
043: import org.sakaiproject.poll.tool.params.VoteBean; //import org.sakaiproject.tool.poll.impl.PollListManagerDaoImpl;
044:
045: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
046: import uk.org.ponder.rsf.flow.jsfnav.NavigationCaseReporter;
047: import uk.org.ponder.rsf.viewstate.ViewParamsReporter;
048: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
049: import uk.org.ponder.rsf.viewstate.EntityCentredViewParameters;
050: import uk.org.ponder.localeutil.LocaleGetter;
051: import uk.org.ponder.rsf.components.UIInput;
052: import uk.org.ponder.beanutil.entity.EntityID;
053:
054: import java.util.List;
055: import java.util.ArrayList;
056:
057: public class PollOptionProducer implements ViewComponentProducer,
058: ViewParamsReporter, NavigationCaseReporter {
059:
060: public static final String VIEW_ID = "pollOption";
061: private static Log m_log = LogFactory
062: .getLog(PollOptionProducer.class);
063: private VoteBean voteBean;
064:
065: private MessageLocator messageLocator;
066: private LocaleGetter localegetter;
067:
068: private Option optionBean;
069:
070: public void setOption(Option o) {
071: this .optionBean = o;
072: }
073:
074: public String getViewID() {
075:
076: return VIEW_ID;
077: }
078:
079: public void setVoteBean(VoteBean vb) {
080: this .voteBean = vb;
081: }
082:
083: public void setMessageLocator(MessageLocator messageLocator) {
084:
085: this .messageLocator = messageLocator;
086: }
087:
088: public void setLocaleGetter(LocaleGetter localegetter) {
089: this .localegetter = localegetter;
090: }
091:
092: private PollListManager pollListManager;
093:
094: public void setPollListManager(PollListManager p) {
095: this .pollListManager = p;
096: }
097:
098: private TargettedMessageList tml;
099:
100: public void setTargettedMessageList(TargettedMessageList tml) {
101: this .tml = tml;
102: }
103:
104: public void fillComponents(UIContainer tofill,
105: ViewParameters viewparams, ComponentChecker arg2) {
106:
107: if (tml.size() > 0) {
108: for (int i = 0; i < tml.size(); i++) {
109: UIBranchContainer errorRow = UIBranchContainer.make(
110: tofill, "error-row:");
111: String output;
112: if (tml.messageAt(i).args != null) {
113: output = messageLocator.getMessage(tml.messageAt(i)
114: .acquireMessageCode(),
115: tml.messageAt(i).args[0]);
116: } else {
117: output = messageLocator.getMessage(tml.messageAt(i)
118: .acquireMessageCode());
119: }
120: UIOutput.make(errorRow, "error", output);
121: }
122: }
123:
124: Poll poll = voteBean.getPoll();
125: if (poll == null)
126: m_log.warn("Bean poll is null!");
127: else
128: m_log.debug("bean poll is " + poll.getPollId());
129:
130: UIOutput.make(tofill, "new-option-title", messageLocator
131: .getMessage("new_option_title"));
132:
133: Option option = null;
134: EntityCentredViewParameters ecvp = (EntityCentredViewParameters) viewparams;
135:
136: if (ecvp != null
137: && ecvp.mode != null
138: && !ecvp.mode
139: .equals(EntityCentredViewParameters.MODE_NEW)) {
140: //check the ecvp
141:
142: String strId = ecvp.getELPath().substring(
143: ecvp.getELPath().indexOf(".") + 1);
144: String type = strId.substring(0, strId.indexOf('_'));
145: String id = strId.substring(strId.indexOf('_') + 1);
146: m_log.debug("got id of " + strId);
147: if (type.equals("Poll")) {
148: if (id.equals("0"))
149: poll = voteBean.getPoll();
150: else
151: poll = pollListManager.getPollById(new Long(id));
152: } else {
153: if (!id.equals("0"))
154: option = pollListManager
155: .getOptionById(new Long(id));
156: poll = pollListManager.getPollById(option.getPollId());
157: }
158: voteBean.setPoll(poll);
159: }
160:
161: //if the option bean is null set it
162: if (optionBean == null) {
163: m_log.debug("setting the option bean");
164: optionBean = new OptionImpl();
165: //make sure the option is null
166:
167: }
168:
169: UIOutput.make(tofill, "poll_text", poll.getText());
170: UIOutput.make(tofill, "poll-question", messageLocator
171: .getMessage("new_poll_question"));
172: UIForm form = UIForm.make(tofill, "opt-form");
173:
174: UIOutput.make(form, "option-label", messageLocator
175: .getMessage("new_poll_option"));
176: if (ecvp.mode.equals(EntityCentredViewParameters.MODE_NEW)) {
177: UIInput.make(form, "opt-text", "#{option.optionText}", "");
178: m_log.debug("here we are! " + ecvp.mode);
179: } else {
180: m_log.debug("mode is " + ecvp.mode);
181: UIInput.make(form, "opt-text", "#{option.optionText}",
182: option.getOptionText());
183: form.parameters.add(new UIELBinding("#{option.id}", option
184: .getId()));
185: }
186:
187: form.parameters.add(new UIELBinding("#{option.pollId}", poll
188: .getPollId()));
189: UICommand save = UICommand.make(form, "submit-new-option",
190: messageLocator.getMessage("new_poll_submit"),
191: "#{pollToolBean.proccessActionAddOption}");
192: save.parameters.add(new UIELBinding(
193: "#{pollToolBean.submissionStatus}", "save"));
194: UICommand saveAdd = UICommand.make(form, "submit-option-add",
195: messageLocator.getMessage("new_poll_saveoption"),
196: "#{pollToolBean.proccessActionAddOption}");
197: saveAdd.parameters.add(new UIELBinding(
198: "#{pollToolBean.submissionStatus}", "option"));
199:
200: UICommand cancel = UICommand.make(form, "cancel",
201: messageLocator.getMessage("new_poll_cancel"),
202: "#{pollToolBean.cancel}");
203: cancel.parameters.add(new UIELBinding("#{option.status}",
204: "cancel"));
205:
206: }
207:
208: public List reportNavigationCases() {
209: List togo = new ArrayList();
210: togo.add(new NavigationCase("save",
211: new EntityCentredViewParameters(
212: AddPollProducer.VIEW_ID, new EntityID("Poll",
213: "0"))));
214: togo.add(new NavigationCase("cancel",
215: new EntityCentredViewParameters(
216: AddPollProducer.VIEW_ID, new EntityID("Poll",
217: "0"))));
218: return togo;
219: }
220:
221: public ViewParameters getViewParameters() {
222: return new EntityCentredViewParameters(VIEW_ID, new EntityID(
223: "Poll", "Poll_0"));
224:
225: }
226: }
|