1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.polls.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.theme.ThemeDisplay;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portal.util.WebKeys;
23  import com.liferay.portlet.polls.NoSuchVoteException;
24  import com.liferay.portlet.polls.model.PollsChoice;
25  import com.liferay.portlet.polls.model.PollsQuestion;
26  import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
27  import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.RenderRequest;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpSession;
34  
35  import org.jfree.data.category.CategoryDataset;
36  import org.jfree.data.category.DefaultCategoryDataset;
37  
38  /**
39   * <a href="PollsUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Shepherd Ching
43   */
44  public class PollsUtil {
45  
46      public static CategoryDataset getVotesDataset(long questionId)
47          throws SystemException {
48  
49          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
50  
51          String seriesName = StringPool.BLANK;
52  
53          for (PollsChoice choice :
54                  PollsChoiceLocalServiceUtil.getChoices(questionId)) {
55  
56              Integer number = choice.getVotesCount();
57  
58              dataset.addValue(number, seriesName, choice.getName());
59          }
60  
61          return dataset;
62      }
63  
64      public static boolean hasVoted(HttpServletRequest request, long questionId)
65          throws PortalException, SystemException {
66  
67          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
68              WebKeys.THEME_DISPLAY);
69  
70          if (themeDisplay.isSignedIn()) {
71              try {
72                  PollsVoteLocalServiceUtil.getVote(
73                      questionId, themeDisplay.getUserId());
74              }
75              catch (NoSuchVoteException nsve) {
76                  return false;
77              }
78  
79              return true;
80          }
81          else {
82              HttpSession session = request.getSession();
83  
84              Boolean hasVoted = (Boolean)session.getAttribute(
85                  PollsQuestion.class.getName() + "." + questionId);
86  
87              if ((hasVoted != null) && (hasVoted.booleanValue())) {
88                  return true;
89              }
90              else {
91                  return false;
92              }
93          }
94      }
95  
96      public static void saveVote(ActionRequest actionRequest, long questionId) {
97          HttpServletRequest request = PortalUtil.getHttpServletRequest(
98              actionRequest);
99  
100         saveVote(request, questionId);
101     }
102 
103     public static void saveVote(RenderRequest renderRequest, long questionId) {
104         HttpServletRequest request = PortalUtil.getHttpServletRequest(
105             renderRequest);
106 
107         saveVote(request, questionId);
108     }
109 
110     public static void saveVote(HttpServletRequest request, long questionId) {
111         HttpSession session = request.getSession();
112 
113         session.setAttribute(
114             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
115     }
116 
117 }