1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.demo.simple;
18
19 import java.io.IOException;
20
21 import javax.portlet.ActionRequest;
22 import javax.portlet.ActionResponse;
23 import javax.portlet.PortletException;
24 import javax.portlet.PortletMode;
25 import javax.portlet.PortletPreferences;
26 import javax.portlet.PortletRequest;
27 import javax.portlet.PortletSession;
28 import javax.portlet.RenderRequest;
29 import javax.portlet.RenderResponse;
30 import javax.portlet.WindowState;
31
32 import org.apache.portals.bridges.common.GenericServletPortlet;
33
34 /***
35 * This class only exists to maintain the Help and View page names. As soon
36 * as the container/engine will retain the preferences this class can be
37 * replaced by configuring portlet preferences.
38 *
39 * @version $Id: PickANumberPortlet.java 553019 2007-07-03 23:29:28Z ate $
40 * @task Remove this class when the container/engine retain preferences
41 */
42 public class PickANumberPortlet extends GenericServletPortlet
43 {
44 private static final PortletMode PRINT_MODE = new PortletMode("print");
45
46 /***
47 * Default action page when preference does not exist
48 *
49 * @see org.apache.portals.bridges.common.GenericServletPortlet#processAction
50 */
51 private static final String DEFAULT_ACTION_PAGE = null;
52
53 /***
54 * Default custom page when preference does not exist
55 *
56 * @see org.apache.portals.bridges.common.GenericServletPortlet#doCustom
57 */
58 private static final String DEFAULT_CUSTOM_PAGE = null;
59
60 /***
61 * Default edit page when preference does not exist
62 *
63 * @see org.apache.portals.bridges.common.GenericServletPortlet#doEdit
64 */
65 private static final String DEFAULT_EDIT_PAGE = "/WEB-INF/demo/simple/PickANumberEdit.jsp";
66
67 /***
68 * Default help page when preference does not exist
69 *
70 * @see org.apache.portals.bridges.common.GenericServletPortlet#doHelp
71 */
72 private static final String DEFAULT_HELP_PAGE = "/WEB-INF/demo/simple/PickANumberHelp.jsp";
73
74 /***
75 * Default help page when preference does not exist
76 *
77 * @see org.apache.portals.bridges.common.GenericServletPortlet#doView
78 */
79
80 private static final String DEFAULT_VIEW_PAGE = "/WEB-INF/demo/simple/PickANumber.jsp";
81
82 /***
83 * Attribute name of Guess Count
84 */
85 private static final String GUESS_COUNT_NAME = "GuessCount";
86
87 /***
88 * Paramter name of current guess
89 */
90 private static final String GUESS_PARAMETER_NAME = "Guess";
91
92 /***
93 * Attribute name of the last guess
94 */
95 private static final String LAST_GUESS_NAME = "LastGuess";
96
97 /***
98 * Attribute name of Target Value
99 */
100 private static final String TARGET_VALUE_NAME = "TargetValue";
101
102 /***
103 * Attribute name of Top Range Value (in Edit Mode)
104 */
105 private static final String TOP_RANGE_NAME = "TopRange";
106
107 /***
108 * Set default page values when class is created
109 */
110 public PickANumberPortlet()
111 {
112 setDefaultActionPage(DEFAULT_ACTION_PAGE);
113 setDefaultCustomPage(DEFAULT_CUSTOM_PAGE);
114 setDefaultEditPage(DEFAULT_EDIT_PAGE);
115 setDefaultHelpPage(DEFAULT_HELP_PAGE);
116 setDefaultViewPage(DEFAULT_VIEW_PAGE);
117 }
118
119
120 protected void doDispatch(RenderRequest request, RenderResponse response) throws PortletException, IOException
121 {
122 if ( !request.getWindowState().equals(WindowState.MINIMIZED))
123 {
124
125 if (PRINT_MODE.equals(request.getPortletMode()))
126 {
127
128 doView(request, response);
129 }
130 else
131 {
132 super.doDispatch(request, response);
133 }
134 }
135 }
136
137
138 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
139 {
140 PortletSession session = request.getPortletSession();
141 Long guessCount = null;
142 Long targetValue = null;
143
144
145 long range = getHighRange(request);
146
147
148
149 targetValue = (Long)session.getAttribute(TARGET_VALUE_NAME, PortletSession.APPLICATION_SCOPE);
150 if (targetValue == null)
151 {
152 targetValue = new Long(Math.round(Math.random() * range));
153
154 guessCount = new Long(0);
155 session.setAttribute( TARGET_VALUE_NAME, targetValue, PortletSession.APPLICATION_SCOPE);
156 long highRange = getHighRange(request);
157 session.setAttribute( TOP_RANGE_NAME, new Long(highRange), PortletSession.APPLICATION_SCOPE);
158 }
159
160 guessCount = (Long)session.getAttribute(GUESS_COUNT_NAME, PortletSession.APPLICATION_SCOPE);
161 if (guessCount == null)
162 {
163 guessCount = new Long(0);
164 session.setAttribute( GUESS_COUNT_NAME, guessCount, PortletSession.APPLICATION_SCOPE);
165 }
166
167 Long highRange = (Long)session.getAttribute(TOP_RANGE_NAME, PortletSession.APPLICATION_SCOPE);
168
169 if ((highRange == null) || (highRange.longValue() != range))
170 {
171 session.setAttribute( TOP_RANGE_NAME, new Long(range), PortletSession.APPLICATION_SCOPE);
172 }
173 super.doView(request, response);
174 }
175
176 /***
177 * Increment attributes in different scopes
178 *
179 * @see javax.portlet.GenericPortlet#processActions
180 *
181 */
182 public void processAction(ActionRequest request, ActionResponse actionResponse)
183 throws PortletException, IOException
184 {
185
186 if (isEditAction(request))
187 {
188 savePreferences(request);
189 return;
190 }
191
192 if (request.getParameter("redirect-test") != null)
193 {
194 actionResponse.sendRedirect("/jetspeed/desktop/rss.psml");
195 return;
196 }
197 Long guessCount = null;
198 Long targetValue = null;
199 Long currentGuess = null;
200 Long lastGuess = null;
201
202 PortletSession session = request.getPortletSession();
203
204
205 lastGuess = (Long)session.getAttribute(LAST_GUESS_NAME, PortletSession.APPLICATION_SCOPE);
206
207
208 targetValue = (Long)session.getAttribute(TARGET_VALUE_NAME, PortletSession.APPLICATION_SCOPE);
209 if ((targetValue != null) && (lastGuess != null))
210 {
211 if (targetValue.equals(lastGuess))
212 {
213 targetValue = null;
214 }
215 }
216 if (targetValue == null)
217 {
218 long random = (Math.round(Math.random() * getHighRange(request)));
219 if (random == 0)
220 {
221 random = 1;
222 }
223 targetValue = new Long(random);
224
225 guessCount = new Long(0);
226 session.setAttribute( TARGET_VALUE_NAME, targetValue, PortletSession.APPLICATION_SCOPE);
227 }
228
229
230 if (guessCount == null)
231 {
232 guessCount = (Long)session.getAttribute(GUESS_COUNT_NAME, PortletSession.APPLICATION_SCOPE);
233 if (guessCount == null)
234 {
235 guessCount = new Long(0);
236 }
237 }
238
239
240
241 guessCount = new Long(guessCount.longValue() + 1);
242
243 try
244 {
245 String result = request.getParameter(GUESS_PARAMETER_NAME);
246
247 if (result != null)
248 {
249 currentGuess = new Long(result);
250 }
251 }
252 catch (Exception e)
253 {
254 currentGuess = new Long(0);
255 }
256
257
258 session.setAttribute( GUESS_COUNT_NAME, guessCount, PortletSession.APPLICATION_SCOPE);
259 session.setAttribute( LAST_GUESS_NAME, currentGuess, PortletSession.APPLICATION_SCOPE);
260
261 return;
262 }
263
264 private long getHighRange(PortletRequest request)
265 {
266 PortletPreferences prefs = request.getPreferences();
267 String highRangePref = prefs.getValue("TopRange", "102");
268 long range = Long.parseLong(highRangePref);
269 if (range < 2)
270 {
271 range = 102;
272 }
273 return range;
274 }
275
276 private boolean isEditAction(ActionRequest request)
277 {
278 return (request.getParameter(TOP_RANGE_NAME) != null);
279 }
280
281 private void savePreferences(PortletRequest request)
282 {
283 String topRange = request.getParameter(TOP_RANGE_NAME);
284 long range = Long.parseLong(topRange);
285 if (range < 2)
286 {
287
288 return;
289 }
290 PortletPreferences prefs = request.getPreferences();
291 try
292 {
293 prefs.setValue(TOP_RANGE_NAME, topRange);
294 prefs.store();
295 PortletSession session = request.getPortletSession();
296 session.setAttribute( TOP_RANGE_NAME, new Long(range), PortletSession.APPLICATION_SCOPE);
297 }
298 catch (Exception e)
299 {
300
301 }
302 }
303
304 }