1   /**
2    * Copyright (c) 2000-2008 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 = new Integer(
66                  PollsVoteLocalServiceUtil.getChoiceVotesCount(
67                      choice.getChoiceId()));
68  
69              dataset.addValue(number, seriesName, choice.getName());
70          }
71  
72          return dataset;
73      }
74  
75      public static boolean hasVoted(HttpServletRequest request, long questionId)
76          throws PortalException, SystemException {
77  
78          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
79              WebKeys.THEME_DISPLAY);
80  
81          if (themeDisplay.isSignedIn()) {
82              try {
83                  PollsVoteLocalServiceUtil.getVote(
84                      questionId, themeDisplay.getUserId());
85              }
86              catch (NoSuchVoteException nsve) {
87                  return false;
88              }
89  
90              return true;
91          }
92          else {
93              HttpSession session = request.getSession();
94  
95              Boolean hasVoted = (Boolean)session.getAttribute(
96                  PollsQuestion.class.getName() + "." + questionId);
97  
98              if ((hasVoted != null) && (hasVoted.booleanValue())) {
99                  return true;
100             }
101             else {
102                 return false;
103             }
104         }
105     }
106 
107     public static void saveVote(ActionRequest actionRequest, long questionId) {
108         HttpServletRequest request = PortalUtil.getHttpServletRequest(
109             actionRequest);
110 
111         saveVote(request, questionId);
112     }
113 
114     public static void saveVote(RenderRequest renderRequest, long questionId) {
115         HttpServletRequest request = PortalUtil.getHttpServletRequest(
116             renderRequest);
117 
118         saveVote(request, questionId);
119     }
120 
121     public static void saveVote(HttpServletRequest request, long questionId) {
122         HttpSession session = request.getSession();
123 
124         session.setAttribute(
125             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
126     }
127 
128 }