1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.polls.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.polls.NoSuchVoteException;
32  import com.liferay.portlet.polls.model.PollsChoice;
33  import com.liferay.portlet.polls.model.PollsQuestion;
34  import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
35  import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.RenderRequest;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpSession;
42  
43  import org.jfree.data.category.CategoryDataset;
44  import org.jfree.data.category.DefaultCategoryDataset;
45  
46  /**
47   * <a href="PollsUtil.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Shepherd Ching
51   *
52   */
53  public class PollsUtil {
54  
55      public static CategoryDataset getVotesDataset(long questionId)
56          throws SystemException {
57  
58          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
59  
60          String seriesName = StringPool.BLANK;
61  
62          for (PollsChoice choice :
63                  PollsChoiceLocalServiceUtil.getChoices(questionId)) {
64  
65              Integer number = choice.getVotesCount();
66  
67              dataset.addValue(number, seriesName, choice.getName());
68          }
69  
70          return dataset;
71      }
72  
73      public static boolean hasVoted(HttpServletRequest request, long questionId)
74          throws PortalException, SystemException {
75  
76          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
77              WebKeys.THEME_DISPLAY);
78  
79          if (themeDisplay.isSignedIn()) {
80              try {
81                  PollsVoteLocalServiceUtil.getVote(
82                      questionId, themeDisplay.getUserId());
83              }
84              catch (NoSuchVoteException nsve) {
85                  return false;
86              }
87  
88              return true;
89          }
90          else {
91              HttpSession session = request.getSession();
92  
93              Boolean hasVoted = (Boolean)session.getAttribute(
94                  PollsQuestion.class.getName() + "." + questionId);
95  
96              if ((hasVoted != null) && (hasVoted.booleanValue())) {
97                  return true;
98              }
99              else {
100                 return false;
101             }
102         }
103     }
104 
105     public static void saveVote(ActionRequest actionRequest, long questionId) {
106         HttpServletRequest request = PortalUtil.getHttpServletRequest(
107             actionRequest);
108 
109         saveVote(request, questionId);
110     }
111 
112     public static void saveVote(RenderRequest renderRequest, long questionId) {
113         HttpServletRequest request = PortalUtil.getHttpServletRequest(
114             renderRequest);
115 
116         saveVote(request, questionId);
117     }
118 
119     public static void saveVote(HttpServletRequest request, long questionId) {
120         HttpSession session = request.getSession();
121 
122         session.setAttribute(
123             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
124     }
125 
126 }