KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 // Java
20
import java.util.List JavaDoc;
21 import java.sql.Connection JavaDoc;
22
23
24 // Turbine
25
import org.apache.turbine.util.Log;
26 import org.apache.turbine.util.RunData;
27 import org.apache.turbine.services.TurbineServices;
28 import org.apache.turbine.util.DynamicURI;
29
30 // Velocity
31
import org.apache.velocity.context.Context;
32
33 //Torque
34
import org.apache.torque.util.Criteria;
35 import org.apache.torque.Torque;
36
37 // Jetspeed
38
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
39 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
40 import org.apache.jetspeed.util.PortletConfigState;
41 import org.apache.jetspeed.util.StringUtils;
42 import org.apache.jetspeed.util.template.JetspeedLink;
43 import org.apache.jetspeed.util.template.JetspeedLinkFactory;
44
45
46 // Tutorial
47
import org.apache.jetspeed.tutorial.om.Cafe;
48 import org.apache.jetspeed.tutorial.om.CafePeer;
49
50 /**
51  * Tutorial 9
52  *
53  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
54  * @version $Id: TutorialCafeAction.java,v 1.1 2004/04/08 17:03:54 taylor Exp $
55  */

56 public class TutorialCafeAction extends VelocityPortletAction
57 {
58     // tutorial reader: you may consider putting these in a constants file
59
// im leaving them here for easy reading of the tutorial
60
public static final String JavaDoc UI_MODE = "js_mode";
61     public static final String JavaDoc UI_ROW_ID = "js_rowid";
62
63     public static final String JavaDoc UI_EDIT = "Edit";
64     public static final String JavaDoc UI_UPDATE = "Update";
65     public static final String JavaDoc UI_ADD = "Add";
66     public static final String JavaDoc UI_DELETE = "Delete";
67     public static final String JavaDoc UI_REFRESH = "Refresh";
68     public static final String JavaDoc UI_VALIDATE = "Validate";
69
70     private static final String JavaDoc SESSION_CAFE = "cafe.instance";
71     private static final String JavaDoc SESSION_UPDATE_MODE = "cafe.mode";
72     private static final String JavaDoc UI_CAFE = "cafe";
73
74     private static final String JavaDoc NO_CURRENT_REC = "Cafe Tutorial: no current record";
75
76     /**
77      * Build the normal state content for this portlet.
78      *
79      * @param portlet The velocity-based portlet that is being built.
80      * @param context The velocity context for this request.
81      * @param rundata The turbine rundata context for this request.
82      */

83
84     protected void buildNormalContext(VelocityPortlet portlet,
85                                        Context context,
86                                        RunData rundata)
87     {
88         String JavaDoc mode = null;
89         Cafe cafe = null;
90
91         try
92         {
93             mode = this.getQueryParameter(rundata, UI_MODE, UI_REFRESH);
94             cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);
95
96             // refresh mode - simply keep state of fields from session,
97
// the request is for another portlet or simply a refresh
98
//
99
if (mode.equalsIgnoreCase(UI_REFRESH))
100             {
101                 if (cafe != null)
102                 {
103                     rundata.getParameters().setProperties(cafe);
104                 }
105             }
106             else if (mode.equalsIgnoreCase(UI_EDIT) ||
107                      mode.equalsIgnoreCase(UI_DELETE))
108             {
109                 int rowid = Integer.parseInt(this.getQueryParameter(rundata, UI_ROW_ID));
110
111                 context.put(UI_ROW_ID, String.valueOf(rowid));
112
113                 // get the primary key and put the object in the context
114
Criteria criteria = new Criteria();
115                 criteria.add( CafePeer.CAFE_ID, rowid);
116                 List JavaDoc cafes = CafePeer.doSelect(criteria);
117                 if (cafes != null && cafes.size() > 0)
118                 {
119                     cafe = (Cafe)cafes.get(0);
120                 }
121                 else
122                 {
123                     throw new Exception JavaDoc("Cafe for id="+rowid+" not found.");
124                 }
125
126                 rundata.getUser().setTemp(SESSION_CAFE, cafe);
127
128                 rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode);
129
130             }
131             else if (mode.equalsIgnoreCase(UI_ADD))
132             {
133                 cafe = new Cafe();
134                 cafe.setCafeName("");
135                 rundata.getUser().setTemp(SESSION_CAFE, cafe);
136                 rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode);
137             }
138
139             context.put(UI_CAFE, cafe);
140             context.put(UI_MODE, mode);
141
142
143         }
144         catch (Exception JavaDoc e)
145         {
146             Log.error(e);
147             rundata.setMessage(e.toString());
148         }
149     }
150
151
152
153     /**
154      * Update the portlet state from the form.
155      *
156      * @param rundata The turbine rundata context for this request.
157      * @param context The velocity context for this request.
158      */

159     public void doUpdate(RunData rundata, Context context) throws Exception JavaDoc
160     {
161         Cafe cafe = null;
162         Connection JavaDoc con = null;
163
164         try
165         {
166             con = Torque.getConnection();
167
168             cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);
169
170             if(cafe == null)
171             {
172                 Log.error(NO_CURRENT_REC);
173                 rundata.setMessage(NO_CURRENT_REC);
174                 return;
175             }
176         
177             rundata.getParameters().setProperties(cafe);
178
179             validate(cafe);
180
181             cafe.save(con);
182
183             con.commit();
184
185             returnToBrowser(rundata, true);
186             
187         }
188         catch (Exception JavaDoc e)
189         {
190             Log.error("error in saving cafe: " + e);
191             rundata.setMessage(e.toString());
192             if (con != null)
193             {
194                 con.rollback();
195             }
196
197         }
198         finally
199         {
200             try
201             {
202                 Torque.closeConnection(con);
203             }
204             catch (Exception JavaDoc e)
205             {}
206         }
207
208     }
209
210     /**
211      * Delete current record
212      *
213      * @param rundata The turbine rundata context for this request.
214      * @param context The velocity context for this request.
215      */

216     public void doDelete(RunData rundata, Context context) throws Exception JavaDoc
217     {
218         Cafe cafe = null;
219         Connection JavaDoc con = null;
220
221         try
222         {
223             con = Torque.getConnection();
224
225             cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);
226
227             if(cafe == null)
228             {
229                 Log.error(NO_CURRENT_REC);
230                 rundata.setMessage(NO_CURRENT_REC);
231                 return;
232             }
233         
234
235             CafePeer.doDelete(cafe, con);
236
237             con.commit();
238
239             returnToBrowser(rundata, true);
240             
241         }
242         catch (Exception JavaDoc e)
243         {
244             Log.error("error in deleting cafe: " + e);
245             rundata.setMessage(e.toString());
246
247             if (con != null)
248             {
249                 con.rollback();
250             }
251
252         }
253         finally
254         {
255             try
256             {
257                 Torque.closeConnection(con);
258             }
259             catch (Exception JavaDoc e)
260             {}
261         }
262
263     }
264
265     /**
266      * Cancel editing or deletion
267      *
268      * @param rundata The turbine rundata context for this request.
269      * @param context The velocity context for this request.
270      */

271     public void doCancel(RunData rundata, Context context) throws Exception JavaDoc
272     {
273         returnToBrowser(rundata, false);
274     }
275
276     /**
277      * Helper function to get query param from request
278      *
279      */

280     public static String JavaDoc getQueryParameter(RunData rundata, String JavaDoc attrName)
281     {
282         String JavaDoc str = rundata.getParameters().getString(attrName);
283         if(str != null && str.length() > 0 && str.trim().length() > 0 )
284             return str;
285         return null;
286     }
287
288     /**
289      * Helper function to get query param from request with a default value
290      *
291      */

292     public static String JavaDoc getQueryParameter(RunData rundata, String JavaDoc attrName, String JavaDoc defaultValue)
293     {
294         String JavaDoc str = getQueryParameter(rundata, attrName);
295         if(str == null)
296             return defaultValue;
297         else
298             return str;
299     }
300
301
302     /**
303      * validate that its a valid record
304      *
305      */

306     private void validate(Cafe cafe) throws Exception JavaDoc
307     {
308
309         if(isEmpty(cafe.getCafeName()))
310         {
311             throw new Exception JavaDoc("Cafe Name must be entered.");
312         }
313     }
314
315     /**
316      * Check for string bein empty or null
317      * this should be put in some utility package
318      *
319      */

320     public static boolean isEmpty(String JavaDoc str)
321     {
322         if(!(str != null && str.trim().length() > 0))
323         {
324             return true;
325         }
326         return false;
327     }
328
329     /**
330      * redirect back to browser
331      *
332      */

333     private void returnToBrowser(RunData rundata, boolean refresh)
334     {
335         try
336         {
337             JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
338             DynamicURI duri = link.getPaneByName("TutorialCafeBrowser");
339             if (refresh)
340             {
341                 duri.addQueryData(CafeBrowserAction.BROWSER_COMMAND, CafeBrowserAction.BROWSER_REFRESH);
342             }
343             rundata.setRedirectURI(duri.toString());
344             JetspeedLinkFactory.putInstance(link);
345         }
346         catch (Exception JavaDoc e)
347         {
348             Log.error(e);
349             rundata.setMessage(e.toString());
350         }
351     }
352
353 }
354
Popular Tags