001    /**
002     * Copyright (c) 2000-2013 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.CookieKeys;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Time;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.polls.NoSuchVoteException;
027    import com.liferay.portlet.polls.model.PollsChoice;
028    import com.liferay.portlet.polls.model.PollsQuestion;
029    import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
030    import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletResponse;
034    
035    import javax.servlet.http.Cookie;
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.jfree.data.category.CategoryDataset;
040    import org.jfree.data.category.DefaultCategoryDataset;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Shepherd Ching
045     */
046    public class PollsUtil {
047    
048            public static CategoryDataset getVotesDataset(long questionId)
049                    throws SystemException {
050    
051                    DefaultCategoryDataset defaultCategoryDataset =
052                            new DefaultCategoryDataset();
053    
054                    String seriesName = StringPool.BLANK;
055    
056                    for (PollsChoice choice :
057                                    PollsChoiceLocalServiceUtil.getChoices(questionId)) {
058    
059                            Integer number = choice.getVotesCount();
060    
061                            defaultCategoryDataset.addValue(
062                                    number, seriesName, choice.getName());
063                    }
064    
065                    return defaultCategoryDataset;
066            }
067    
068            public static boolean hasVoted(HttpServletRequest request, long questionId)
069                    throws PortalException, SystemException {
070    
071                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
072                            WebKeys.THEME_DISPLAY);
073    
074                    if (themeDisplay.isSignedIn()) {
075                            try {
076                                    PollsVoteLocalServiceUtil.getVote(
077                                            questionId, themeDisplay.getUserId());
078                            }
079                            catch (NoSuchVoteException nsve) {
080                                    return false;
081                            }
082    
083                            return true;
084                    }
085    
086                    String cookie = CookieKeys.getCookie(
087                            request, _getCookieName(questionId));
088    
089                    return GetterUtil.getBoolean(cookie);
090            }
091    
092            public static void saveVote(
093                    HttpServletRequest request, HttpServletResponse response,
094                    long questionId) {
095    
096                    Cookie cookie = new Cookie(_getCookieName(questionId), StringPool.TRUE);
097    
098                    cookie.setMaxAge((int)(Time.WEEK / 1000));
099                    cookie.setPath(StringPool.SLASH);
100    
101                    CookieKeys.addCookie(request, response, cookie);
102            }
103    
104            public static void saveVote(
105                    PortletRequest portletRequest, PortletResponse portletResponse,
106                    long questionId) {
107    
108                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
109                            portletRequest);
110                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
111                            portletResponse);
112    
113                    saveVote(request, response, questionId);
114            }
115    
116            private static String _getCookieName(long questionId) {
117                    return PollsQuestion.class.getName() + StringPool.POUND + questionId;
118            }
119    
120    }