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.validators;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.springframework.validation.Errors;
025: import org.springframework.validation.Validator;
026: import org.sakaiproject.poll.logic.PollListManager;
027: import org.sakaiproject.poll.logic.PollVoteManager;
028: import org.sakaiproject.poll.model.Poll;
029: import org.sakaiproject.poll.model.Vote;
030: import org.sakaiproject.poll.model.VoteCollection;
031: import org.sakaiproject.tool.api.ToolManager;
032: import org.sakaiproject.authz.cover.SecurityService;
033:
034: import java.util.Date;
035: import java.util.List;
036: import java.util.ArrayList;
037:
038: import uk.org.ponder.messageutil.MessageLocator;
039: import uk.org.ponder.util.UniversalRuntimeException;
040: import uk.org.ponder.messageutil.MessageLocator;
041: import uk.org.ponder.messageutil.TargettedMessage;
042: import uk.org.ponder.messageutil.TargettedMessageList;
043:
044: public class VoteValidator implements Validator {
045:
046: /** Logger for this class and subclasses */
047: protected final Log logger = LogFactory.getLog(getClass());
048: private MessageLocator messageLocator;
049: private TargettedMessageList tml = new TargettedMessageList();
050: private PollVoteManager pollVoteManager;
051: private PollListManager manager;
052:
053: public void setPollListManager(PollListManager manager) {
054: this .manager = manager;
055: }
056:
057: public void setPollVoteManager(PollVoteManager pvm) {
058: this .pollVoteManager = pvm;
059: }
060:
061: public void setMessageLocator(MessageLocator messageLocator) {
062:
063: this .messageLocator = messageLocator;
064: }
065:
066: private ToolManager toolManager;
067:
068: public void setToolManager(ToolManager toolManager) {
069: this .toolManager = toolManager;
070: }
071:
072: public boolean supports(Class clazz) {
073: // TODO Auto-generated method stub
074: return clazz.equals(VoteCollection.class);
075: }
076:
077: public void validate(Object obj, Errors errors) {
078:
079: VoteCollection votes = (VoteCollection) obj;
080: logger.debug("we are validating a vote collection of " + votes
081: + " for poll " + votes.getPollId());
082:
083: if (votes.getSubmissionStatus().equals("cancel"))
084: return;
085:
086: //get the poll
087: Poll poll = manager.getPollById(votes.getPollId());
088: logger.debug("this is a vote for " + poll.getText());
089: List options = new ArrayList();
090:
091: //is the poll open?
092: if (!(poll.getVoteClose().after(new Date()) && new Date()
093: .after(poll.getVoteOpen()))) {
094: logger.warn("poll is closed!");
095: errors.reject("vote_closed", "vote closed");
096: return;
097: }
098:
099: //does the user have permission to vote
100: if (!SecurityService.isSuperUser()) {
101: if (!SecurityService.unlock("poll.vote", "/site/"
102: + toolManager.getCurrentPlacement().getContext())) {
103: logger
104: .warn("attempt to vote in "
105: + toolManager.getCurrentPlacement()
106: .getContext()
107: + " by unauthorized user");
108: errors.reject("vote_noperm", "no permissions");
109: return;
110: }
111: }
112:
113: if (votes.getOptionsSelected() == null
114: && votes.getOption() == null
115: && poll.getMinOptions() > 0) {
116: String errStr = new Integer(poll.getMinOptions())
117: .toString();
118: errors.reject("error_novote", new Object[] { errStr },
119: "no vote");
120: return;
121: } else if (votes.getOptionsSelected() == null
122: && votes.getOption() == null
123: && poll.getMinOptions() == 0) {
124: //to do we need to map to somthing special
125: options.add("0");
126: }
127:
128: if (votes.getOptionsSelected() == null
129: && votes.getOption() != null) {
130: options.add(votes.getOption());
131: } else if (votes.getOptionsSelected() != null) {
132: for (int i = 0; i < votes.getOptionsSelected().length; i++) {
133: options.add(votes.getOptionsSelected()[i]);
134: }
135: }
136:
137: logger.debug("options selected is " + options.size());
138: // the exact choise case
139:
140: if (pollVoteManager.userHasVoted(poll.getPollId())
141: && poll.getLimitVoting()) {
142: errors.reject("vote_hasvoted", "has voted");
143: return;
144: }
145:
146: if (poll.getMaxOptions() == poll.getMinOptions()
147: && options.size() != poll.getMaxOptions()) {
148: logger.warn("exact match failure!");
149: String errStr = new Integer(poll.getMinOptions())
150: .toString();
151: errors.reject("error_exact_required",
152: new Object[] { errStr }, "exact required");
153: }
154: if (options.size() > poll.getMaxOptions()) {
155: logger.warn("votes are for more than allowed!");
156: String errStr = new Integer(poll.getMaxOptions())
157: .toString();
158: errors.reject("error_tomany_votes",
159: new Object[] { errStr }, "to many votes");
160: }
161:
162: if (options.size() < poll.getMinOptions()) {
163: logger.warn("votes are for fewer than required!");
164: String errStr = new Integer(poll.getMinOptions())
165: .toString();
166: errors.reject("error_tofew_votes", new Object[] { errStr },
167: "to few");
168: }
169:
170: }
171:
172: }
|