1   /**
2    * Copyright (c) 2000-2007 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 java.util.Iterator;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.RenderRequest;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpSession;
44  
45  import org.jfree.data.category.CategoryDataset;
46  import org.jfree.data.category.DefaultCategoryDataset;
47  
48  /**
49   * <a href="PollsUtil.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Shepherd Ching
53   *
54   */
55  public class PollsUtil {
56  
57      public static CategoryDataset getVotesDataset(long questionId)
58          throws SystemException {
59  
60          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
61  
62          String seriesName = StringPool.BLANK;
63  
64          Iterator itr = PollsChoiceLocalServiceUtil.getChoices(
65              questionId).iterator();
66  
67          while (itr.hasNext()) {
68              PollsChoice choice = (PollsChoice)itr.next();
69  
70              Integer number = new Integer(
71                  PollsVoteLocalServiceUtil.getChoiceVotesCount(
72                      choice.getChoiceId()));
73  
74              dataset.addValue(number, seriesName, choice.getName());
75          }
76  
77          return dataset;
78      }
79  
80      public static boolean hasVoted(HttpServletRequest req, long questionId)
81          throws PortalException, SystemException {
82  
83          ThemeDisplay themeDisplay =
84              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
85  
86          if (themeDisplay.isSignedIn()) {
87              try {
88                  PollsVoteLocalServiceUtil.getVote(
89                      questionId, themeDisplay.getUserId());
90              }
91              catch (NoSuchVoteException nsve) {
92                  return false;
93              }
94  
95              return true;
96          }
97          else {
98              HttpSession ses = req.getSession();
99  
100             Boolean hasVoted = (Boolean)ses.getAttribute(
101                 PollsQuestion.class.getName() + "." + questionId);
102 
103             if ((hasVoted != null) && (hasVoted.booleanValue())) {
104                 return true;
105             }
106             else {
107                 return false;
108             }
109         }
110     }
111 
112     public static void saveVote(ActionRequest req, long questionId) {
113         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
114 
115         saveVote(httpReq, questionId);
116     }
117 
118     public static void saveVote(RenderRequest req, long questionId) {
119         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
120 
121         saveVote(httpReq, questionId);
122     }
123 
124     public static void saveVote(HttpServletRequest req, long questionId) {
125         HttpSession ses = req.getSession();
126 
127         ses.setAttribute(
128             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
129     }
130 
131 }