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
53 public class PollsUtil {
54
55 public static CategoryDataset getVotesDataset(long questionId)
56 throws SystemException {
57
58 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
59
60 String seriesName = StringPool.BLANK;
61
62 for (PollsChoice choice :
63 PollsChoiceLocalServiceUtil.getChoices(questionId)) {
64
65 Integer number = choice.getVotesCount();
66
67 dataset.addValue(number, seriesName, choice.getName());
68 }
69
70 return dataset;
71 }
72
73 public static boolean hasVoted(HttpServletRequest request, long questionId)
74 throws PortalException, SystemException {
75
76 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
77 WebKeys.THEME_DISPLAY);
78
79 if (themeDisplay.isSignedIn()) {
80 try {
81 PollsVoteLocalServiceUtil.getVote(
82 questionId, themeDisplay.getUserId());
83 }
84 catch (NoSuchVoteException nsve) {
85 return false;
86 }
87
88 return true;
89 }
90 else {
91 HttpSession session = request.getSession();
92
93 Boolean hasVoted = (Boolean)session.getAttribute(
94 PollsQuestion.class.getName() + "." + questionId);
95
96 if ((hasVoted != null) && (hasVoted.booleanValue())) {
97 return true;
98 }
99 else {
100 return false;
101 }
102 }
103 }
104
105 public static void saveVote(ActionRequest actionRequest, long questionId) {
106 HttpServletRequest request = PortalUtil.getHttpServletRequest(
107 actionRequest);
108
109 saveVote(request, questionId);
110 }
111
112 public static void saveVote(RenderRequest renderRequest, long questionId) {
113 HttpServletRequest request = PortalUtil.getHttpServletRequest(
114 renderRequest);
115
116 saveVote(request, questionId);
117 }
118
119 public static void saveVote(HttpServletRequest request, long questionId) {
120 HttpSession session = request.getSession();
121
122 session.setAttribute(
123 PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
124 }
125
126 }