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 java.util.Iterator;
38
39 import javax.portlet.ActionRequest;
40 import javax.portlet.RenderRequest;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpSession;
44
45 import org.jfree.data.category.CategoryDataset;
46 import org.jfree.data.category.DefaultCategoryDataset;
47
48
55 public class PollsUtil {
56
57 public static CategoryDataset getVotesDataset(long questionId)
58 throws SystemException {
59
60 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
61
62 String seriesName = StringPool.BLANK;
63
64 Iterator itr = PollsChoiceLocalServiceUtil.getChoices(
65 questionId).iterator();
66
67 while (itr.hasNext()) {
68 PollsChoice choice = (PollsChoice)itr.next();
69
70 Integer number = new Integer(
71 PollsVoteLocalServiceUtil.getChoiceVotesCount(
72 choice.getChoiceId()));
73
74 dataset.addValue(number, seriesName, choice.getName());
75 }
76
77 return dataset;
78 }
79
80 public static boolean hasVoted(HttpServletRequest req, long questionId)
81 throws PortalException, SystemException {
82
83 ThemeDisplay themeDisplay =
84 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
85
86 if (themeDisplay.isSignedIn()) {
87 try {
88 PollsVoteLocalServiceUtil.getVote(
89 questionId, themeDisplay.getUserId());
90 }
91 catch (NoSuchVoteException nsve) {
92 return false;
93 }
94
95 return true;
96 }
97 else {
98 HttpSession ses = req.getSession();
99
100 Boolean hasVoted = (Boolean)ses.getAttribute(
101 PollsQuestion.class.getName() + "." + questionId);
102
103 if ((hasVoted != null) && (hasVoted.booleanValue())) {
104 return true;
105 }
106 else {
107 return false;
108 }
109 }
110 }
111
112 public static void saveVote(ActionRequest req, long questionId) {
113 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
114
115 saveVote(httpReq, questionId);
116 }
117
118 public static void saveVote(RenderRequest req, long questionId) {
119 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
120
121 saveVote(httpReq, questionId);
122 }
123
124 public static void saveVote(HttpServletRequest req, long questionId) {
125 HttpSession ses = req.getSession();
126
127 ses.setAttribute(
128 PollsQuestion.class.getName() + "." + questionId, Boolean.TRUE);
129 }
130
131 }