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