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.params;
021:
022: import java.util.ArrayList;
023: import java.util.Date;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.poll.model.Option;
032: import org.sakaiproject.poll.model.OptionImpl;
033: import org.sakaiproject.poll.model.Poll;
034: import org.sakaiproject.poll.model.PollImpl;
035: import org.sakaiproject.poll.model.Vote;
036: import org.sakaiproject.poll.model.VoteCollection;
037: import org.sakaiproject.poll.model.VoteImpl;
038: import org.sakaiproject.poll.logic.PollListManager;
039: import org.sakaiproject.poll.logic.PollVoteManager;
040: import org.sakaiproject.exception.PermissionException;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: import uk.org.ponder.localeutil.LocaleGetter;
046:
047: public class PollToolBean {
048: /** A holder for the single new task that may be in creation **/
049: public Poll newPoll = new PollImpl();
050: public String siteID;
051:
052: public Option option;
053:
054: private VoteCollection voteCollection;
055:
056: //values to hold the parts of the date
057: public String openDay;
058: public String openMonth;
059: public String openYear;
060: public String openHour;
061: public String openMinutes;
062: public String openAmPm;
063:
064: public String closeDay;
065: public String closeMonth;
066: public String closeYear;
067: public String closeHour;
068: public String closeMinutes;
069: public String closeAmPm;
070: private VoteBean voteBean;
071: public Long[] deleteids;
072: public String submissionStatus;
073: private PollVoteManager pollVoteManager;
074:
075: public Map perms = null;
076:
077: public void setRoleperms(Map perms) {
078: this .perms = perms;
079: }
080:
081: private static Log m_log = LogFactory.getLog(PollToolBean.class);
082:
083: private PollListManager manager;
084:
085: public void setPollListManager(PollListManager manager) {
086: this .manager = manager;
087: }
088:
089: public void setPollVoteManager(PollVoteManager pvm) {
090: this .pollVoteManager = pvm;
091: }
092:
093: public void setVoteBean(VoteBean vb) {
094: this .voteBean = vb;
095: }
096:
097: public void setVoteCollection(VoteCollection vc) {
098: this .voteCollection = vc;
099: }
100:
101: public void setOption(Option o) {
102: this .option = o;
103: }
104:
105: private LocaleGetter localegetter;
106:
107: public void setLocaleGetter(LocaleGetter localegetter) {
108: this .localegetter = localegetter;
109: }
110:
111: public String processActionAdd() {
112:
113: Poll poll = null;
114: boolean isNew = true;
115: if (newPoll.getPollId() != null) {
116: m_log.debug("Actualy updating poll " + newPoll.getPollId());
117: poll = manager.getPollById(newPoll.getPollId());
118: isNew = false;
119: //check for possible unchanged values
120: m_log.debug(" newPoll test is " + newPoll.getText()
121: + " while poll text is " + poll.getText());
122: if (newPoll.getText().equals("") && poll.getText() != null)
123: newPoll.setText(poll.getText());
124:
125: if (newPoll.getDetails().equals("")
126: && poll.getDetails() != null)
127: newPoll.setDetails(poll.getDetails());
128: }
129:
130: SimpleDateFormat yearf = new SimpleDateFormat("yyyy");
131: if (openYear == null && poll != null) {
132: openYear = yearf.format(poll.getVoteOpen());
133: } else if (openYear == null) {
134: openYear = yearf.format(new Date());
135: }
136: if (closeYear == null && poll != null) {
137: closeYear = yearf.format(poll.getVoteClose());
138: } else if (closeYear == null) {
139: closeYear = yearf.format(new Date());
140: }
141:
142: SimpleDateFormat monthf = new SimpleDateFormat("M",
143: localegetter.get());
144: if (openMonth == null && poll != null) {
145: openMonth = monthf.format(poll.getVoteOpen());
146: } else if (openMonth == null) {
147: openMonth = monthf.format(new Date());
148: }
149: if (closeMonth == null && poll != null) {
150: closeMonth = monthf.format(poll.getVoteClose());
151: } else if (closeMonth == null) {
152: closeMonth = monthf.format(new Date());
153: }
154:
155: SimpleDateFormat dayf = new SimpleDateFormat("d");
156: if (openDay == null && poll != null) {
157: openDay = dayf.format(poll.getVoteOpen());
158: } else if (openDay == null) {
159: openDay = dayf.format(new Date());
160: }
161: if (closeDay == null && poll != null) {
162: closeDay = dayf.format(poll.getVoteClose());
163: } else if (closeDay == null) {
164: closeDay = dayf.format(new Date());
165: }
166:
167: SimpleDateFormat hoursf = new SimpleDateFormat("h");
168: if (openHour == null && poll != null) {
169: openHour = hoursf.format(poll.getVoteOpen());
170: } else if (openHour == null) {
171: openHour = hoursf.format(new Date());
172: }
173: if (closeHour == null && poll != null) {
174: closeHour = hoursf.format(poll.getVoteClose());
175: } else if (closeHour == null) {
176: closeHour = hoursf.format(new Date());
177: }
178:
179: SimpleDateFormat minf = new SimpleDateFormat("m");
180: if (openMinutes == null && poll != null) {
181: openMinutes = minf.format(poll.getVoteOpen());
182: } else if (openMinutes == null) {
183: openMinutes = minf.format(new Date());
184: }
185: if (closeMinutes == null && poll != null) {
186: closeMinutes = minf.format(poll.getVoteClose());
187: } else if (closeMinutes == null) {
188: closeMinutes = minf.format(new Date());
189: }
190:
191: SimpleDateFormat amf = new SimpleDateFormat("a");
192: if (openAmPm == null && poll != null) {
193: openAmPm = amf.format(poll.getVoteOpen());
194: } else if (openAmPm == null) {
195: openAmPm = amf.format(new Date());
196: }
197: if (closeAmPm == null && poll != null) {
198: closeAmPm = amf.format(poll.getVoteClose());
199: } else if (closeAmPm == null) {
200: closeAmPm = amf.format(new Date());
201: }
202:
203: String openString = openYear + "/" + openMonth + "/" + openDay
204: + " " + openHour + ":" + openMinutes + " " + openAmPm;
205:
206: String closeString = closeYear + "/" + closeMonth + "/"
207: + closeDay + " " + closeHour + ":" + closeMinutes + " "
208: + closeAmPm;
209:
210: //conver to dates
211: String strFormat = "yyyy/M/d h:mm a";
212: //2006/12/26 10:00 PM
213: DateFormat myDateFormat = new SimpleDateFormat(strFormat);
214: Date openDate = null;
215: Date closeDate = null;
216: try {
217: openDate = myDateFormat.parse(openString);
218: closeDate = myDateFormat.parse(closeString);
219: } catch (Exception e) {
220: m_log.error("error converting date" + e);
221: }
222:
223: newPoll.setLimitVoting(true);
224:
225: if (poll == null)
226: newPoll.setCreationDate(new Date());
227: else
228: newPoll.setCreationDate(poll.getCreationDate());
229:
230: newPoll.setVoteOpen(openDate);
231: newPoll.setVoteClose(closeDate);
232: newPoll.setSiteId(siteID);
233:
234: m_log.debug("about to save poll " + newPoll);
235: manager.savePoll(newPoll);
236:
237: m_log.info("Poll saved with id of " + newPoll.getPollId());
238:
239: voteBean.poll = newPoll;
240:
241: if (!isNew) {
242: return "added";
243: } else {
244: m_log.info("returning option");
245: return "option";
246: }
247: }
248:
249: public void processActionDelete() {
250:
251: for (int i = 0; i < deleteids.length; i++) {
252: Poll todelete = (Poll) manager.getPollById(new Long(
253: deleteids[i].longValue()));
254: try {
255: manager.deletePoll(todelete);
256: } catch (PermissionException e) {
257: m_log.error(" Permission Error" + e);
258: }
259: }
260:
261: }
262:
263: public String processActionVote() {
264: //m_log.info("got a vote! with " + optionsSelected.length + "options");
265:
266: m_log.info("vote is on poll " + voteCollection.getPollId());
267: Poll poll = manager.getPollById(voteCollection.getPollId());
268:
269: //need to check if the user hasn't already voted on this poll
270: //pollvoteManger.userHasVoted(poll.getPollId();
271:
272: VoteCollection votes = voteCollection;
273: m_log.info("got vote collexction with id " + votes.getId());
274:
275: List options = new ArrayList();
276:
277: if (votes.getOptionsSelected() == null
278: && votes.getOption() != null) {
279: options.add(votes.getOption());
280: } else if (votes.getOptionsSelected() != null) {
281: for (int i = 0; i < votes.getOptionsSelected().length; i++) {
282: options.add(votes.getOptionsSelected()[i]);
283: }
284: }
285:
286: //if options list is empty this may be a spoiled vote
287: if (options.size() == 0 && poll.getMinOptions() == 0) {
288: //to do we need to map to somthing special
289: m_log.warn("this is a spoiled vote");
290: options.add("0");
291: }
292:
293: for (int i = 0; i < options.size(); i++) {
294: //create a new vote
295: m_log.info("this vote is for option " + options.get(i));
296: Option opt = new OptionImpl(new Long((String) options
297: .get(i)));
298: Vote vote = new VoteImpl(poll, opt, votes.getId());
299: if (vote.getIp() == null) {
300: m_log.warn("IP is null");
301: vote.setIp("Nothing");
302: }
303:
304: pollVoteManager.saveVote(vote);
305: voteBean.voteCollection = votes;
306:
307: }
308: m_log.debug("Votes saved about to return");
309: return "Success";
310: }
311:
312: public String proccessActionAddOption() {
313:
314: if (submissionStatus.equals("cancel"))
315: return "cancel";
316:
317: m_log
318: .debug("adding option with text "
319: + option.getOptionText());
320: if (option.getOptionText() == null
321: || option.getOptionText().length() == 0) {
322: m_log.error("OptionText is empty");
323: //errors.reject("vote_closed","vote closed");
324: // return null;
325: }
326:
327: manager.saveOption(option);
328: m_log.info("Succesuly save option with id" + option.getId());
329:
330: voteBean.poll = manager.getPollById(option.getPollId());
331:
332: if (submissionStatus.equals("option"))
333: return "option";
334: else
335: return "save";
336:
337: }
338:
339: public String proccessActionDeleteOption() {
340: m_log.info("about to delete option " + option.getId());
341: Long pollId = option.getPollId();
342: manager.deleteOption(option);
343:
344: //we now need to update the poll object in memory
345: voteBean.setPoll(manager.getPollById(pollId));
346:
347: return "success";
348:
349: }
350:
351: public String cancel() {
352: return "cancel";
353: }
354:
355: }
|