Source Code Cross Referenced for PickANumberPortlet.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » demo » simple » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » jetspeed 2.1.3 » org.apache.jetspeed.demo.simple 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.jetspeed.demo.simple;
018:
019:        import java.io.IOException;
020:
021:        import javax.portlet.ActionRequest;
022:        import javax.portlet.ActionResponse;
023:        import javax.portlet.PortletException;
024:        import javax.portlet.PortletMode;
025:        import javax.portlet.PortletPreferences;
026:        import javax.portlet.PortletRequest;
027:        import javax.portlet.PortletSession;
028:        import javax.portlet.RenderRequest;
029:        import javax.portlet.RenderResponse;
030:        import javax.portlet.WindowState;
031:
032:        import org.apache.portals.bridges.common.GenericServletPortlet;
033:
034:        /**
035:         * This class only exists to maintain the Help and View page names.  As soon
036:         * as the container/engine will retain the preferences this class can be
037:         * replaced by configuring portlet preferences.
038:         *
039:         * @version $Id: PickANumberPortlet.java 576707 2007-09-18 05:35:28Z woonsan $
040:         * @task Remove this class when the container/engine retain preferences
041:         */
042:        public class PickANumberPortlet extends GenericServletPortlet {
043:            private static final PortletMode ABOUT_MODE = new PortletMode(
044:                    "about");
045:            private static final PortletMode EDIT_DEFAULTS_MODE = new PortletMode(
046:                    "edit_defaults");
047:            private static final PortletMode PRINT_MODE = new PortletMode(
048:                    "print");
049:
050:            /**
051:             * Default action page when preference does not exist
052:             *
053:             * @see org.apache.portals.bridges.common.GenericServletPortlet#processAction
054:             */
055:            private static final String DEFAULT_ACTION_PAGE = null;
056:
057:            /**
058:             * Default custom page when preference does not exist
059:             *
060:             * @see org.apache.portals.bridges.common.GenericServletPortlet#doCustom
061:             */
062:            private static final String DEFAULT_CUSTOM_PAGE = null;
063:
064:            /**
065:             * Default edit page when preference does not exist
066:             *
067:             * @see org.apache.portals.bridges.common.GenericServletPortlet#doEdit
068:             */
069:            private static final String DEFAULT_EDIT_PAGE = "/WEB-INF/demo/simple/PickANumberEdit.jsp";
070:
071:            /**
072:             * Default help page when preference does not exist
073:             *
074:             * @see org.apache.portals.bridges.common.GenericServletPortlet#doHelp
075:             */
076:            private static final String DEFAULT_HELP_PAGE = "/WEB-INF/demo/simple/PickANumberHelp.jsp";
077:
078:            /**
079:             * Default help page when preference does not exist
080:             *
081:             * @see org.apache.portals.bridges.common.GenericServletPortlet#doView
082:             */
083:
084:            private static final String DEFAULT_VIEW_PAGE = "/WEB-INF/demo/simple/PickANumber.jsp";
085:
086:            /**
087:             * Default about page when preference does not exist
088:             */
089:            private static final String DEFAULT_ABOUT_PAGE = "/WEB-INF/demo/simple/PickANumberAbout.jsp";
090:
091:            /**
092:             * Default edit_defaults page when preference does not exist
093:             */
094:            private static final String DEFAULT_EDIT_DEFAULTS_PAGE = "/WEB-INF/demo/simple/PickANumberEditDefaults.jsp";
095:
096:            /**
097:             * Attribute name of Guess Count
098:             */
099:            private static final String GUESS_COUNT_NAME = "GuessCount";
100:
101:            /**
102:             * Paramter name of current guess
103:             */
104:            private static final String GUESS_PARAMETER_NAME = "Guess";
105:
106:            /**
107:             * Attribute name of the last guess
108:             */
109:            private static final String LAST_GUESS_NAME = "LastGuess";
110:
111:            /**
112:             * Attribute name of Target Value
113:             */
114:            private static final String TARGET_VALUE_NAME = "TargetValue";
115:
116:            /**
117:             * Attribute name of Top Range Value (in Edit Mode)
118:             */
119:            private static final String TOP_RANGE_NAME = "TopRange";
120:
121:            /**
122:             * Set default page values when class is created
123:             */
124:            public PickANumberPortlet() {
125:                setDefaultActionPage(DEFAULT_ACTION_PAGE);
126:                setDefaultCustomPage(DEFAULT_CUSTOM_PAGE);
127:                setDefaultEditPage(DEFAULT_EDIT_PAGE);
128:                setDefaultHelpPage(DEFAULT_HELP_PAGE);
129:                setDefaultViewPage(DEFAULT_VIEW_PAGE);
130:            }
131:
132:            protected void doDispatch(RenderRequest request,
133:                    RenderResponse response) throws PortletException,
134:                    IOException {
135:                if (!request.getWindowState().equals(WindowState.MINIMIZED)) {
136:                    PortletMode curMode = request.getPortletMode();
137:
138:                    // Handle custom PRINT_MODE ourselves as GenericPortlet nor GenericServletPortlet do
139:                    if (PRINT_MODE.equals(curMode)) {
140:                        // simply delegate to doView rendering
141:                        doView(request, response);
142:                    } else if (ABOUT_MODE.equals(curMode)) {
143:                        request.setAttribute(PARAM_VIEW_PAGE,
144:                                DEFAULT_ABOUT_PAGE);
145:                        doView(request, response);
146:                    } else if (EDIT_DEFAULTS_MODE.equals(curMode)) {
147:                        request.setAttribute(PARAM_EDIT_PAGE,
148:                                DEFAULT_EDIT_DEFAULTS_PAGE);
149:                        doEdit(request, response);
150:                    } else {
151:                        super .doDispatch(request, response);
152:                    }
153:                }
154:            }
155:
156:            public void doView(RenderRequest request, RenderResponse response)
157:                    throws PortletException, IOException {
158:                PortletSession session = request.getPortletSession();
159:                Long guessCount = null;
160:                Long targetValue = null;
161:
162:                // get the current value in the prefs
163:                long range = getHighRange(request);
164:
165:                // Get target value
166:
167:                targetValue = (Long) session.getAttribute(TARGET_VALUE_NAME,
168:                        PortletSession.APPLICATION_SCOPE);
169:                if (targetValue == null) {
170:                    targetValue = new Long(Math.round(Math.random() * range));
171:                    //            System.out.println("cheater: target value = " + targetValue);
172:                    guessCount = new Long(0);
173:                    session.setAttribute(TARGET_VALUE_NAME, targetValue,
174:                            PortletSession.APPLICATION_SCOPE);
175:                    long highRange = getHighRange(request);
176:                    session.setAttribute(TOP_RANGE_NAME, new Long(highRange),
177:                            PortletSession.APPLICATION_SCOPE);
178:                }
179:
180:                guessCount = (Long) session.getAttribute(GUESS_COUNT_NAME,
181:                        PortletSession.APPLICATION_SCOPE);
182:                if (guessCount == null) {
183:                    guessCount = new Long(0);
184:                    session.setAttribute(GUESS_COUNT_NAME, guessCount,
185:                            PortletSession.APPLICATION_SCOPE);
186:                }
187:
188:                Long highRange = (Long) session.getAttribute(TOP_RANGE_NAME,
189:                        PortletSession.APPLICATION_SCOPE);
190:
191:                if ((highRange == null) || (highRange.longValue() != range)) {
192:                    session.setAttribute(TOP_RANGE_NAME, new Long(range),
193:                            PortletSession.APPLICATION_SCOPE);
194:                }
195:                super .doView(request, response);
196:            }
197:
198:            /**
199:             * Increment attributes in different scopes
200:             *
201:             * @see javax.portlet.GenericPortlet#processActions
202:             *
203:             */
204:            public void processAction(ActionRequest request,
205:                    ActionResponse actionResponse) throws PortletException,
206:                    IOException {
207:                // Is it an edit (customize) action
208:                if (isEditAction(request)) {
209:                    savePreferences(request);
210:                    return;
211:                }
212:
213:                if (request.getParameter("redirect-test") != null) {
214:                    actionResponse.sendRedirect("/jetspeed/desktop/rss.psml");
215:                    return;
216:                }
217:                Long guessCount = null;
218:                Long targetValue = null;
219:                Long currentGuess = null;
220:                Long lastGuess = null;
221:
222:                PortletSession session = request.getPortletSession();
223:
224:                // Get target value
225:                lastGuess = (Long) session.getAttribute(LAST_GUESS_NAME,
226:                        PortletSession.APPLICATION_SCOPE);
227:
228:                // Get target value
229:                targetValue = (Long) session.getAttribute(TARGET_VALUE_NAME,
230:                        PortletSession.APPLICATION_SCOPE);
231:                if ((targetValue != null) && (lastGuess != null)) {
232:                    if (targetValue.equals(lastGuess)) {
233:                        targetValue = null; // Since the number as guesed, start a new game
234:                    }
235:                }
236:                if (targetValue == null) {
237:                    long random = (Math.round(Math.random()
238:                            * getHighRange(request)));
239:                    if (random == 0) {
240:                        random = 1; // don;t allow 0
241:                    }
242:                    targetValue = new Long(random);
243:                    //            System.out.println("cheater: target value = " + targetValue);
244:                    guessCount = new Long(0);
245:                    session.setAttribute(TARGET_VALUE_NAME, targetValue,
246:                            PortletSession.APPLICATION_SCOPE);
247:                }
248:
249:                // Get the guessCount, if it has not already been set.
250:                if (guessCount == null) {
251:                    guessCount = (Long) session.getAttribute(GUESS_COUNT_NAME,
252:                            PortletSession.APPLICATION_SCOPE);
253:                    if (guessCount == null) {
254:                        guessCount = new Long(0);
255:                    }
256:                }
257:
258:                // Increment the guessCount
259:                guessCount = new Long(guessCount.longValue() + 1);
260:
261:                try {
262:                    String result = request.getParameter(GUESS_PARAMETER_NAME);
263:                    // System.out.println("result = " + result);
264:                    if (result != null) {
265:                        currentGuess = new Long(result);
266:                    }
267:                } catch (Exception e) {
268:                    currentGuess = new Long(0);
269:                }
270:
271:                // Update the attribute values
272:                session.setAttribute(GUESS_COUNT_NAME, guessCount,
273:                        PortletSession.APPLICATION_SCOPE);
274:                session.setAttribute(LAST_GUESS_NAME, currentGuess,
275:                        PortletSession.APPLICATION_SCOPE);
276:                //actionResponse.setRenderParameter(LAST_GUESS_NAME, lastGuess.toString());        
277:                return;
278:            }
279:
280:            private long getHighRange(PortletRequest request) {
281:                PortletPreferences prefs = request.getPreferences();
282:                String highRangePref = prefs.getValue("TopRange", "102");
283:                long range = Long.parseLong(highRangePref);
284:                if (range < 2) {
285:                    range = 102;
286:                }
287:                return range;
288:            }
289:
290:            private boolean isEditAction(ActionRequest request) {
291:                return (request.getParameter(TOP_RANGE_NAME) != null);
292:            }
293:
294:            private void savePreferences(PortletRequest request) {
295:                String topRange = request.getParameter(TOP_RANGE_NAME);
296:                long range = Long.parseLong(topRange);
297:                if (range < 2) {
298:                    // TODO: throw validation exception
299:                    return;
300:                }
301:                PortletPreferences prefs = request.getPreferences();
302:
303:                try {
304:                    prefs.setValue(TOP_RANGE_NAME, topRange);
305:                    prefs.store();
306:                    PortletSession session = request.getPortletSession();
307:                    session.setAttribute(TOP_RANGE_NAME, new Long(range),
308:                            PortletSession.APPLICATION_SCOPE);
309:                } catch (Exception e) {
310:                    // TODO: throw validation exception and redirect to error 
311:                }
312:            }
313:
314:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.