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.dao.impl;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.Stack;
029: import java.util.Vector;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.hibernate.criterion.DetachedCriteria;
034: import org.hibernate.criterion.Order;
035: import org.hibernate.criterion.Restrictions;
036: import org.hibernate.Query;
037: import org.hibernate.criterion.Distinct;
038: import org.hibernate.Session;
039:
040: import org.sakaiproject.poll.logic.PollVoteManager;
041: import org.sakaiproject.poll.model.Poll;
042: import org.sakaiproject.poll.model.Vote;
043: import org.sakaiproject.user.cover.UserDirectoryService;
044: import org.sakaiproject.entity.api.Entity;
045: import org.sakaiproject.entity.api.HttpAccess;
046: import org.sakaiproject.entity.api.Reference;
047: import org.sakaiproject.entity.api.ResourceProperties;
048: import org.springframework.dao.DataAccessException;
049: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
050: import org.sakaiproject.event.cover.EventTrackingService;
051: import org.sakaiproject.tool.cover.ToolManager;
052: import org.w3c.dom.Document;
053: import org.w3c.dom.Element;
054:
055: public class PollVoteManagerDaoImpl extends HibernateDaoSupport
056: implements PollVoteManager {
057:
058: // use commons logger
059: private static Log log = LogFactory
060: .getLog(PollListManagerDaoImpl.class);
061:
062: public boolean saveVote(Vote vote) {
063: try {
064: getHibernateTemplate().save(vote);
065:
066: } catch (DataAccessException e) {
067: log.error("Hibernate could not save: " + e.toString());
068: e.printStackTrace();
069: return false;
070: }
071: //Session sess = ;
072: //we need the siteID
073:
074: log.info(" Vote " + vote.getId() + " successfuly saved");
075: EventTrackingService.post(EventTrackingService.newEvent(
076: "poll.vote", "poll/site/"
077: + ToolManager.getCurrentPlacement() + "/poll/"
078: + vote.getPollId(), false));
079: return true;
080: }
081:
082: public void deleteVote(Vote Vote) {
083: // TODO Auto-generated method stub
084:
085: }
086:
087: public List getAllVotesForPoll(Poll poll) {
088: DetachedCriteria d = DetachedCriteria.forClass(Vote.class).add(
089: Restrictions.eq("pollId", poll.getPollId()));
090: Collection pollCollection = getHibernateTemplate()
091: .findByCriteria(d);
092: List votes = new ArrayList();
093: for (Iterator tit = pollCollection.iterator(); tit.hasNext();) {
094: Vote vote = (Vote) tit.next();
095: votes.add(vote);
096: }
097: return votes;
098: }
099:
100: public int getDisctinctVotersForPoll(Poll poll) {
101:
102: Query q = null;
103: Session session = getHibernateTemplate().getSessionFactory()
104: .getCurrentSession();
105: String statement = "SELECT DISTINCT VOTE_SUBMISSION_ID from POLL_VOTE where VOTE_POLL_ID = "
106: + poll.getPollId().toString();
107: q = session.createSQLQuery(statement);
108: List results = q.list();
109: if (results.size() > 0)
110: return results.size();
111:
112: return 0;
113: }
114:
115: public boolean userHasVoted(Long pollid, String userID) {
116: DetachedCriteria d = DetachedCriteria.forClass(Vote.class).add(
117: Restrictions.eq("userId", userID)).add(
118: Restrictions.eq("pollId", pollid));
119:
120: Collection pollCollection = getHibernateTemplate()
121: .findByCriteria(d);
122: //System.out.println("got " + pollCollection.size() + "votes for this poll");
123: if (pollCollection.size() > 0)
124: return true;
125: else
126: return false;
127: }
128:
129: public boolean userHasVoted(Long pollId) {
130:
131: return userHasVoted(pollId, UserDirectoryService
132: .getCurrentUser().getId());
133: }
134:
135: }
|