KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > tutorial > modules > actions > portlets > CobiJonesPortletAction


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.jetspeed.tutorial.modules.actions.portlets;
17
18 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
19
20 // Turbine stuff
21
import org.apache.turbine.util.Log;
22 import org.apache.turbine.util.RunData;
23 import org.apache.turbine.services.TurbineServices;
24
25 // Velocity Stuff
26
import org.apache.velocity.context.Context;
27
28 // Jetspeed stuff
29
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
30 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
31 import org.apache.jetspeed.util.PortletConfigState;
32 import org.apache.jetspeed.util.StringUtils;
33
34 /**
35  * Tutorial 7 - Action Events
36  *
37  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
38  * @version $Id: CobiJonesPortletAction.java,v 1.1 2004/04/08 17:03:54 taylor Exp $
39  */

40 public class CobiJonesPortletAction extends VelocityPortletAction
41 {
42
43     public static final String JavaDoc INPUT_FIRST_NAME = "firstname";
44     public static final String JavaDoc INPUT_LAST_NAME = "lastname";
45     public static final String JavaDoc INPUT_POSITION = "position";
46     public static final String JavaDoc INPUT_CAPS = "caps";
47     public static final String JavaDoc INPUT_ACTIVE = "active";
48
49     public static final String JavaDoc PLAYER = "player";
50     public static final String JavaDoc COBI_ERROR = "cobierror";
51
52
53     /**
54      * Build the normal state content for this portlet.
55      *
56      * @param portlet The velocity-based portlet that is being built.
57      * @param context The velocity context for this request.
58      * @param rundata The turbine rundata context for this request.
59      */

60
61
62     protected void buildNormalContext(VelocityPortlet portlet,
63                                        Context context,
64                                        RunData rundata)
65     {
66         try
67         {
68             Player player = (Player)rundata.getUser().getTemp(PLAYER);
69             if (null == player)
70             {
71                 player = createNewPlayer();
72                 rundata.getUser().setTemp(PLAYER, player);
73             }
74             context.put(PLAYER, player);
75
76             // make sure they don't sub Cobi!
77
String JavaDoc cobiError = (String JavaDoc)rundata.getRequest().getAttribute(COBI_ERROR);
78             if (null != cobiError)
79             {
80                 context.put(COBI_ERROR, cobiError);
81             }
82         }
83         catch (Exception JavaDoc e)
84         {
85             Log.error(e);
86             context.put(COBI_ERROR, e.toString());
87         }
88     }
89
90
91     /**
92      * Update the portlet state from the form.
93      *
94      * @param rundata The turbine rundata context for this request.
95      * @param context The velocity context for this request.
96      */

97     public void doUpdate(RunData rundata, Context context) throws Exception JavaDoc
98     {
99         Player player = (Player)rundata.getUser().getTemp(PLAYER);
100         if (null == player)
101         {
102             player = createNewPlayer();
103             rundata.getUser().setTemp(PLAYER, player);
104         }
105
106         String JavaDoc cobi = rundata.getParameters().getString(INPUT_FIRST_NAME);
107         String JavaDoc jones = rundata.getParameters().getString(INPUT_LAST_NAME);
108         if (!cobi.equalsIgnoreCase("Cobi") || !jones.equalsIgnoreCase("Jones"))
109         {
110             rundata.getRequest().setAttribute(COBI_ERROR,
111                                              "Hey now, you cant substitute Cobi with "
112                                               + cobi + " " + jones + "!");
113         }
114         player.setFirstName(cobi);
115         player.setLastName(jones);
116         player.setPosition(rundata.getParameters().getString(INPUT_POSITION));
117         player.setCaps(rundata.getParameters().getInt(INPUT_CAPS));
118         player.setActive(rundata.getParameters().getBoolean(INPUT_ACTIVE));
119
120         rundata.getUser().setTemp(PLAYER, player);
121
122     }
123
124     ///////////////////////////////////////////////////////////////////////////
125

126     public Player createNewPlayer()
127     {
128         return new Player("Cobi", "Jones", "Midfielder", 150, true);
129
130     }
131
132     public class Player
133     {
134         private String JavaDoc firstName;
135         private String JavaDoc lastName;
136         private String JavaDoc position;
137         private int caps;
138         boolean active;
139
140         public Player(String JavaDoc firstName,
141                       String JavaDoc lastName,
142                       String JavaDoc position,
143                       int caps,
144                       boolean active)
145         {
146             this.firstName = firstName;
147             this.lastName = lastName;
148             this.position = position;
149             this.caps = caps;
150             this.active = active;
151         }
152
153
154         public String JavaDoc getFirstName()
155         {
156             return this.firstName;
157         }
158
159         public void setFirstName(String JavaDoc firstName)
160         {
161             this.firstName = firstName;
162         }
163
164         public String JavaDoc getLastName()
165         {
166             return this.lastName;
167         }
168
169         public void setLastName(String JavaDoc lastName)
170         {
171             this.lastName = lastName;
172         }
173
174         public String JavaDoc getPosition()
175         {
176             return this.position;
177         }
178
179         public void setPosition(String JavaDoc position)
180         {
181             this.position = position;
182         }
183
184         public int getCaps()
185         {
186             return this.caps;
187         }
188
189         public void setCaps(int caps)
190         {
191             this.caps = caps;
192         }
193
194         public boolean getActive()
195          {
196              return this.active;
197          }
198
199          public void setActive(boolean active)
200          {
201              this.active = active;
202          }
203     }
204 }
205
Popular Tags