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 = new Integer(
66 PollsVoteLocalServiceUtil.getChoiceVotesCount(
67 choice.getChoiceId()));
68
69 dataset.addValue(number, seriesName, choice.getName());
70 }
71
72 return dataset;
73 }
74
75 public static boolean hasVoted(HttpServletRequest req, long questionId)
76 throws PortalException, SystemException {
77
78 ThemeDisplay themeDisplay =
79 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
80
81 if (themeDisplay.isSignedIn()) {
82 try {
83 PollsVoteLocalServiceUtil.getVote(
84 questionId, themeDisplay.getUserId());
85 }
86 catch (NoSuchVoteException nsve) {
87 return false;
88 }
89
90 return true;
91 }
92 else {
93 HttpSession ses = req.getSession();
94
95 Boolean hasVoted = (Boolean)ses.getAttribute(
96 PollsQuestion.class.getName() + "." + questionId);
97
98 if ((hasVoted != null) && (hasVoted.booleanValue())) {
99 return true;
100 }
101 else {
102 return false;
103 }
104 }
105 }
106
107 public static void saveVote(ActionRequest req, long questionId) {
108 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
109
110 saveVote(httpReq, questionId);
111 }
112
113 public static void saveVote(RenderRequest req, long questionId) {
114 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
115
116 saveVote(httpReq, questionId);
117 }
118
119 public static void saveVote(HttpServletRequest req, long questionId) {
120 HttpSession ses = req.getSession();
121
122 ses.setAttribute(
123 PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
124 }
125
126 }