1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.polls.util;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.polls.NoSuchVoteException;
29  import com.liferay.portlet.polls.model.PollsChoice;
30  import com.liferay.portlet.polls.model.PollsQuestion;
31  import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
32  import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.RenderRequest;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpSession;
39  
40  import org.jfree.data.category.CategoryDataset;
41  import org.jfree.data.category.DefaultCategoryDataset;
42  
43  /**
44   * <a href="PollsUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Shepherd Ching
48   *
49   */
50  public class PollsUtil {
51  
52      public static CategoryDataset getVotesDataset(long questionId)
53          throws SystemException {
54  
55          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
56  
57          String seriesName = StringPool.BLANK;
58  
59          for (PollsChoice choice :
60                  PollsChoiceLocalServiceUtil.getChoices(questionId)) {
61  
62              Integer number = choice.getVotesCount();
63  
64              dataset.addValue(number, seriesName, choice.getName());
65          }
66  
67          return dataset;
68      }
69  
70      public static boolean hasVoted(HttpServletRequest request, long questionId)
71          throws PortalException, SystemException {
72  
73          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
74              WebKeys.THEME_DISPLAY);
75  
76          if (themeDisplay.isSignedIn()) {
77              try {
78                  PollsVoteLocalServiceUtil.getVote(
79                      questionId, themeDisplay.getUserId());
80              }
81              catch (NoSuchVoteException nsve) {
82                  return false;
83              }
84  
85              return true;
86          }
87          else {
88              HttpSession session = request.getSession();
89  
90              Boolean hasVoted = (Boolean)session.getAttribute(
91                  PollsQuestion.class.getName() + "." + questionId);
92  
93              if ((hasVoted != null) && (hasVoted.booleanValue())) {
94                  return true;
95              }
96              else {
97                  return false;
98              }
99          }
100     }
101 
102     public static void saveVote(ActionRequest actionRequest, long questionId) {
103         HttpServletRequest request = PortalUtil.getHttpServletRequest(
104             actionRequest);
105 
106         saveVote(request, questionId);
107     }
108 
109     public static void saveVote(RenderRequest renderRequest, long questionId) {
110         HttpServletRequest request = PortalUtil.getHttpServletRequest(
111             renderRequest);
112 
113         saveVote(request, questionId);
114     }
115 
116     public static void saveVote(HttpServletRequest request, long questionId) {
117         HttpSession session = request.getSession();
118 
119         session.setAttribute(
120             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
121     }
122 
123 }