1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PollsUtil {
53  
54      public static CategoryDataset getVotesDataset(long questionId)
55          throws SystemException {
56  
57          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
58  
59          String seriesName = StringPool.BLANK;
60  
61          for (PollsChoice choice :
62                  PollsChoiceLocalServiceUtil.getChoices(questionId)) {
63  
64              Integer number = choice.getVotesCount();
65  
66              dataset.addValue(number, seriesName, choice.getName());
67          }
68  
69          return dataset;
70      }
71  
72      public static boolean hasVoted(HttpServletRequest request, long questionId)
73          throws PortalException, SystemException {
74  
75          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
76              WebKeys.THEME_DISPLAY);
77  
78          if (themeDisplay.isSignedIn()) {
79              try {
80                  PollsVoteLocalServiceUtil.getVote(
81                      questionId, themeDisplay.getUserId());
82              }
83              catch (NoSuchVoteException nsve) {
84                  return false;
85              }
86  
87              return true;
88          }
89          else {
90              HttpSession session = request.getSession();
91  
92              Boolean hasVoted = (Boolean)session.getAttribute(
93                  PollsQuestion.class.getName() + "." + questionId);
94  
95              if ((hasVoted != null) && (hasVoted.booleanValue())) {
96                  return true;
97              }
98              else {
99                  return false;
100             }
101         }
102     }
103 
104     public static void saveVote(ActionRequest actionRequest, long questionId) {
105         HttpServletRequest request = PortalUtil.getHttpServletRequest(
106             actionRequest);
107 
108         saveVote(request, questionId);
109     }
110 
111     public static void saveVote(RenderRequest renderRequest, long questionId) {
112         HttpServletRequest request = PortalUtil.getHttpServletRequest(
113             renderRequest);
114 
115         saveVote(request, questionId);
116     }
117 
118     public static void saveVote(HttpServletRequest request, long questionId) {
119         HttpSession session = request.getSession();
120 
121         session.setAttribute(
122             PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
123     }
124 
125 }