001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.polls.NoSuchVoteException;
024    import com.liferay.portlet.polls.model.PollsChoice;
025    import com.liferay.portlet.polls.model.PollsQuestion;
026    import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
027    import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.RenderRequest;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpSession;
034    
035    import org.jfree.data.category.CategoryDataset;
036    import org.jfree.data.category.DefaultCategoryDataset;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Shepherd Ching
041     */
042    public class PollsUtil {
043    
044            public static CategoryDataset getVotesDataset(long questionId)
045                    throws SystemException {
046    
047                    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
048    
049                    String seriesName = StringPool.BLANK;
050    
051                    for (PollsChoice choice :
052                                    PollsChoiceLocalServiceUtil.getChoices(questionId)) {
053    
054                            Integer number = choice.getVotesCount();
055    
056                            dataset.addValue(number, seriesName, choice.getName());
057                    }
058    
059                    return dataset;
060            }
061    
062            public static boolean hasVoted(HttpServletRequest request, long questionId)
063                    throws PortalException, SystemException {
064    
065                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
066                            WebKeys.THEME_DISPLAY);
067    
068                    if (themeDisplay.isSignedIn()) {
069                            try {
070                                    PollsVoteLocalServiceUtil.getVote(
071                                            questionId, themeDisplay.getUserId());
072                            }
073                            catch (NoSuchVoteException nsve) {
074                                    return false;
075                            }
076    
077                            return true;
078                    }
079                    else {
080                            HttpSession session = request.getSession();
081    
082                            Boolean hasVoted = (Boolean)session.getAttribute(
083                                    PollsQuestion.class.getName() + "." + questionId);
084    
085                            if ((hasVoted != null) && (hasVoted.booleanValue())) {
086                                    return true;
087                            }
088                            else {
089                                    return false;
090                            }
091                    }
092            }
093    
094            public static void saveVote(ActionRequest actionRequest, long questionId) {
095                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
096                            actionRequest);
097    
098                    saveVote(request, questionId);
099            }
100    
101            public static void saveVote(RenderRequest renderRequest, long questionId) {
102                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
103                            renderRequest);
104    
105                    saveVote(request, questionId);
106            }
107    
108            public static void saveVote(HttpServletRequest request, long questionId) {
109                    HttpSession session = request.getSession();
110    
111                    session.setAttribute(
112                            PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
113            }
114    
115    }