1
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
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 }