View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 576707 2007-09-18 05:35:28Z woonsan $
40   * @task Remove this class when the container/engine retain preferences
41   */
42  public class PickANumberPortlet extends GenericServletPortlet
43  {
44      private static final PortletMode ABOUT_MODE = new PortletMode("about");
45      private static final PortletMode EDIT_DEFAULTS_MODE = new PortletMode("edit_defaults");
46      private static final PortletMode PRINT_MODE = new PortletMode("print");
47      
48      /***
49       * Default action page when preference does not exist
50       *
51       * @see org.apache.portals.bridges.common.GenericServletPortlet#processAction
52       */
53      private static final String DEFAULT_ACTION_PAGE = null;
54      
55      /***
56       * Default custom page when preference does not exist
57       *
58       * @see org.apache.portals.bridges.common.GenericServletPortlet#doCustom
59       */
60      private static final String DEFAULT_CUSTOM_PAGE = null;
61      
62      /***
63       * Default edit page when preference does not exist
64       *
65       * @see org.apache.portals.bridges.common.GenericServletPortlet#doEdit
66       */
67      private static final String DEFAULT_EDIT_PAGE = "/WEB-INF/demo/simple/PickANumberEdit.jsp";
68      
69      /***
70       * Default help page when preference does not exist
71       *
72       * @see org.apache.portals.bridges.common.GenericServletPortlet#doHelp
73       */
74      private static final String DEFAULT_HELP_PAGE = "/WEB-INF/demo/simple/PickANumberHelp.jsp";
75      
76      /***
77       * Default help page when preference does not exist
78       *
79       * @see org.apache.portals.bridges.common.GenericServletPortlet#doView
80       */
81      
82      private static final String DEFAULT_VIEW_PAGE = "/WEB-INF/demo/simple/PickANumber.jsp";
83      
84      /***
85       * Default about page when preference does not exist
86       */
87      private static final String DEFAULT_ABOUT_PAGE = "/WEB-INF/demo/simple/PickANumberAbout.jsp";
88      
89      /***
90       * Default edit_defaults page when preference does not exist
91       */
92      private static final String DEFAULT_EDIT_DEFAULTS_PAGE = "/WEB-INF/demo/simple/PickANumberEditDefaults.jsp";
93      
94      /***
95       * Attribute name of Guess Count
96       */
97      private static final String GUESS_COUNT_NAME = "GuessCount";
98      
99      /***
100      * Paramter name of current guess
101      */
102     private static final String GUESS_PARAMETER_NAME = "Guess";
103     
104     /***
105      * Attribute name of the last guess
106      */
107     private static final String LAST_GUESS_NAME = "LastGuess";
108 
109     /***
110      * Attribute name of Target Value
111      */
112     private static final String TARGET_VALUE_NAME = "TargetValue";
113     
114     /***
115      * Attribute name of Top Range Value (in Edit Mode)
116      */
117     private static final String TOP_RANGE_NAME = "TopRange";
118     
119     /***
120      * Set default page values when class is created
121      */
122     public PickANumberPortlet()
123     {
124         setDefaultActionPage(DEFAULT_ACTION_PAGE);
125         setDefaultCustomPage(DEFAULT_CUSTOM_PAGE);
126         setDefaultEditPage(DEFAULT_EDIT_PAGE);
127         setDefaultHelpPage(DEFAULT_HELP_PAGE);
128         setDefaultViewPage(DEFAULT_VIEW_PAGE);
129     }
130 
131             
132     protected void doDispatch(RenderRequest request, RenderResponse response) throws PortletException, IOException
133     {
134         if ( !request.getWindowState().equals(WindowState.MINIMIZED))
135         {
136             PortletMode curMode = request.getPortletMode();
137             
138             // Handle custom PRINT_MODE ourselves as GenericPortlet nor GenericServletPortlet do
139             if (PRINT_MODE.equals(curMode))
140             {
141                 // simply delegate to doView rendering
142                 doView(request, response);
143             }
144             else if (ABOUT_MODE.equals(curMode))
145             {
146                 request.setAttribute(PARAM_VIEW_PAGE, DEFAULT_ABOUT_PAGE);
147                 doView(request, response);
148             }
149             else if (EDIT_DEFAULTS_MODE.equals(curMode))
150             {
151                 request.setAttribute(PARAM_EDIT_PAGE, DEFAULT_EDIT_DEFAULTS_PAGE);
152                 doEdit(request, response);
153             }
154             else
155             {
156                 super.doDispatch(request, response);
157             }
158         }
159     }
160 
161 
162     public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
163     {
164         PortletSession session = request.getPortletSession();
165         Long guessCount = null;
166         Long targetValue = null;
167         
168         // get the current value in the prefs
169         long range = getHighRange(request);
170 
171         // Get target value
172 
173         targetValue = (Long)session.getAttribute(TARGET_VALUE_NAME, PortletSession.APPLICATION_SCOPE);
174         if (targetValue == null)
175         {            
176             targetValue = new Long(Math.round(Math.random() * range));
177 //            System.out.println("cheater: target value = " + targetValue);
178             guessCount = new Long(0);
179             session.setAttribute( TARGET_VALUE_NAME, targetValue, PortletSession.APPLICATION_SCOPE);
180             long highRange = getHighRange(request);
181             session.setAttribute( TOP_RANGE_NAME, new Long(highRange), PortletSession.APPLICATION_SCOPE);
182         }
183 
184         guessCount = (Long)session.getAttribute(GUESS_COUNT_NAME, PortletSession.APPLICATION_SCOPE);
185         if (guessCount == null)
186         {
187             guessCount = new Long(0);
188             session.setAttribute( GUESS_COUNT_NAME, guessCount, PortletSession.APPLICATION_SCOPE);            
189         }
190 
191         Long highRange = (Long)session.getAttribute(TOP_RANGE_NAME, PortletSession.APPLICATION_SCOPE);
192         
193         if ((highRange == null) || (highRange.longValue() != range))
194         {
195             session.setAttribute( TOP_RANGE_NAME, new Long(range), PortletSession.APPLICATION_SCOPE);
196         }
197         super.doView(request, response);        
198     }
199     
200     /***
201      * Increment attributes in different scopes
202      *
203      * @see javax.portlet.GenericPortlet#processActions
204      *
205      */
206     public void processAction(ActionRequest request, ActionResponse actionResponse)
207     throws PortletException, IOException
208     {
209         // Is it an edit (customize) action
210         if (isEditAction(request))
211         {
212             savePreferences(request);
213             return;
214         }
215         
216         if (request.getParameter("redirect-test") != null)
217         {
218             actionResponse.sendRedirect("/jetspeed/desktop/rss.psml");
219             return;
220         }
221         Long guessCount = null;
222         Long targetValue = null;
223         Long currentGuess = null;
224         Long lastGuess = null;
225         
226         PortletSession session = request.getPortletSession();
227         
228         // Get target value
229         lastGuess = (Long)session.getAttribute(LAST_GUESS_NAME, PortletSession.APPLICATION_SCOPE);
230 
231         // Get target value
232         targetValue = (Long)session.getAttribute(TARGET_VALUE_NAME, PortletSession.APPLICATION_SCOPE);
233         if ((targetValue != null) && (lastGuess != null))
234         {
235             if (targetValue.equals(lastGuess))
236             {
237                 targetValue = null; // Since the number as guesed, start a new game
238             }
239         }
240         if (targetValue == null)
241         {
242             long random = (Math.round(Math.random() * getHighRange(request)));
243             if (random == 0)
244             {
245                 random = 1; // don;t allow 0
246             }
247             targetValue = new Long(random);
248 //            System.out.println("cheater: target value = " + targetValue);
249             guessCount = new Long(0);
250             session.setAttribute( TARGET_VALUE_NAME, targetValue, PortletSession.APPLICATION_SCOPE);
251         }
252 
253         // Get the guessCount, if it has not already been set.
254         if (guessCount == null)
255         {
256             guessCount = (Long)session.getAttribute(GUESS_COUNT_NAME, PortletSession.APPLICATION_SCOPE);
257             if (guessCount == null)
258             {
259                 guessCount = new Long(0);
260             }
261         }
262         
263 
264         // Increment the guessCount
265         guessCount = new Long(guessCount.longValue() + 1);
266         
267         try
268         {
269             String result = request.getParameter(GUESS_PARAMETER_NAME);
270             // System.out.println("result = " + result);
271             if (result != null)
272             {
273                 currentGuess = new Long(result);
274             }
275         }
276         catch (Exception e)
277         {
278             currentGuess = new Long(0);
279         }
280 
281         // Update the attribute values
282         session.setAttribute( GUESS_COUNT_NAME, guessCount, PortletSession.APPLICATION_SCOPE);
283         session.setAttribute( LAST_GUESS_NAME, currentGuess, PortletSession.APPLICATION_SCOPE);
284         //actionResponse.setRenderParameter(LAST_GUESS_NAME, lastGuess.toString());        
285         return;
286     }
287     
288     private long getHighRange(PortletRequest request)
289     {
290         PortletPreferences prefs = request.getPreferences();
291         String highRangePref = prefs.getValue("TopRange", "102");
292         long range = Long.parseLong(highRangePref);
293         if (range < 2)
294         {
295             range = 102;
296         }
297         return range;
298     }
299     
300     private boolean isEditAction(ActionRequest request)
301     {
302         return (request.getParameter(TOP_RANGE_NAME) != null);        
303     }
304     
305     private void savePreferences(PortletRequest request)
306     {
307         String topRange = request.getParameter(TOP_RANGE_NAME);
308         long range = Long.parseLong(topRange);
309         if (range < 2)
310         {
311             // TODO: throw validation exception
312             return;
313         }
314         PortletPreferences prefs = request.getPreferences();
315         
316         try
317         {
318             prefs.setValue(TOP_RANGE_NAME, topRange);
319             prefs.store();
320             PortletSession session = request.getPortletSession();            
321             session.setAttribute( TOP_RANGE_NAME, new Long(range), PortletSession.APPLICATION_SCOPE);            
322         }
323         catch (Exception e)
324         {
325             // TODO: throw validation exception and redirect to error 
326         }
327     }
328     
329 }