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