001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.polls.util;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.theme.ThemeDisplay;
027: import com.liferay.portal.util.PortalUtil;
028: import com.liferay.portal.util.WebKeys;
029: import com.liferay.portlet.polls.NoSuchVoteException;
030: import com.liferay.portlet.polls.model.PollsChoice;
031: import com.liferay.portlet.polls.model.PollsQuestion;
032: import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
033: import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
034:
035: import java.util.Iterator;
036:
037: import javax.portlet.ActionRequest;
038: import javax.portlet.RenderRequest;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpSession;
042:
043: import org.jfree.data.category.CategoryDataset;
044: import org.jfree.data.category.DefaultCategoryDataset;
045:
046: /**
047: * <a href="PollsUtil.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: * @author Shepherd Ching
051: *
052: */
053: public class PollsUtil {
054:
055: public static CategoryDataset getVotesDataset(long questionId)
056: throws SystemException {
057:
058: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
059:
060: String seriesName = StringPool.BLANK;
061:
062: Iterator itr = PollsChoiceLocalServiceUtil.getChoices(
063: questionId).iterator();
064:
065: while (itr.hasNext()) {
066: PollsChoice choice = (PollsChoice) itr.next();
067:
068: Integer number = new Integer(PollsVoteLocalServiceUtil
069: .getChoiceVotesCount(choice.getChoiceId()));
070:
071: dataset.addValue(number, seriesName, choice.getName());
072: }
073:
074: return dataset;
075: }
076:
077: public static boolean hasVoted(HttpServletRequest req,
078: long questionId) throws PortalException, SystemException {
079:
080: ThemeDisplay themeDisplay = (ThemeDisplay) req
081: .getAttribute(WebKeys.THEME_DISPLAY);
082:
083: if (themeDisplay.isSignedIn()) {
084: try {
085: PollsVoteLocalServiceUtil.getVote(questionId,
086: themeDisplay.getUserId());
087: } catch (NoSuchVoteException nsve) {
088: return false;
089: }
090:
091: return true;
092: } else {
093: HttpSession ses = req.getSession();
094:
095: Boolean hasVoted = (Boolean) ses
096: .getAttribute(PollsQuestion.class.getName() + "."
097: + questionId);
098:
099: if ((hasVoted != null) && (hasVoted.booleanValue())) {
100: return true;
101: } else {
102: return false;
103: }
104: }
105: }
106:
107: public static void saveVote(ActionRequest req, long questionId) {
108: HttpServletRequest httpReq = PortalUtil
109: .getHttpServletRequest(req);
110:
111: saveVote(httpReq, questionId);
112: }
113:
114: public static void saveVote(RenderRequest req, long questionId) {
115: HttpServletRequest httpReq = PortalUtil
116: .getHttpServletRequest(req);
117:
118: saveVote(httpReq, questionId);
119: }
120:
121: public static void saveVote(HttpServletRequest req, long questionId) {
122: HttpSession ses = req.getSession();
123:
124: ses.setAttribute(PollsQuestion.class.getName() + "."
125: + questionId, Boolean.TRUE);
126: }
127:
128: }
|