A
download TutorialCafeAction.java
Language: Java
License: AL20
Copyright: Copyright 2000-2001,2004 The Apache Software Foundation.
LOC: 214
Project Info
jakarta-jetspeed
Server: Apache
Type: svn
...l\modules\actions\portlets\
   CafeBrowserAction.java
   ...JonesPortletAction.java
   ExampleBrowserItem.java
   TutorialCafeAction.java
   ...lStockQuoteAction1.java
   ...StockQuoteAction12.java
   ...lStockQuoteAction2.java
   ...lStockQuoteAction8.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright 2000-2001,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.tutorial.modules.actions.portlets;


// Java 
import java.util.List;
import java.sql.Connection;


// Turbine 
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.util.DynamicURI;

// Velocity 
import org.apache.velocity.context.Context;

//Torque 
import org.apache.torque.util.Criteria;
import org.apache.torque.Torque;

// Jetspeed 
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
import org.apache.jetspeed.util.PortletConfigState;
import org.apache.jetspeed.util.StringUtils;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;


// Tutorial
import org.apache.jetspeed.tutorial.om.Cafe;
import org.apache.jetspeed.tutorial.om.CafePeer;

/**
 * Tutorial 9 
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @version $Id: TutorialCafeAction.java,v 1.1 2004/04/08 17:03:54 taylor Exp $
 */
public class TutorialCafeAction extends VelocityPortletAction
{
    // tutorial reader: you may consider putting these in a constants file
    // im leaving them here for easy reading of the tutorial
    public static final String UI_MODE = "js_mode";
    public static final String UI_ROW_ID = "js_rowid";

    public static final String UI_EDIT = "Edit";
    public static final String UI_UPDATE = "Update";
    public static final String UI_ADD = "Add";
    public static final String UI_DELETE = "Delete";
    public static final String UI_REFRESH = "Refresh";
    public static final String UI_VALIDATE = "Validate";

    private static final String SESSION_CAFE = "cafe.instance";
    private static final String SESSION_UPDATE_MODE = "cafe.mode";
    private static final String UI_CAFE = "cafe";

    private static final String NO_CURRENT_REC = "Cafe Tutorial: no current record";

    /**
     * Build the normal state content for this portlet.
     *
     * @param portlet The velocity-based portlet that is being built.
     * @param context The velocity context for this request.
     * @param rundata The turbine rundata context for this request.
     */

    protected void buildNormalContext(VelocityPortlet portlet,
                                       Context context,
                                       RunData rundata)
    {
        String mode = null;
        Cafe cafe = null;

        try
        {
            mode = this.getQueryParameter(rundata, UI_MODE, UI_REFRESH);
            cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);

            // refresh mode - simply keep state of fields from session, 
            // the request is for another portlet or simply a refresh
            //
            if (mode.equalsIgnoreCase(UI_REFRESH))
            {
                if (cafe != null)
                {
                    rundata.getParameters().setProperties(cafe);
                }                
            }
            else if (mode.equalsIgnoreCase(UI_EDIT) ||
                     mode.equalsIgnoreCase(UI_DELETE))
            {
                int rowid = Integer.parseInt(this.getQueryParameter(rundata, UI_ROW_ID));

                context.put(UI_ROW_ID, String.valueOf(rowid));

                // get the primary key and put the object in the context
                Criteria criteria = new Criteria();
                criteria.add( CafePeer.CAFE_ID, rowid);
                List cafes = CafePeer.doSelect(criteria);
                if (cafes != null && cafes.size() > 0)
                {
                    cafe = (Cafe)cafes.get(0);
                }
                else
                {
                    throw new Exception("Cafe for id="+rowid+" not found.");
                }

                rundata.getUser().setTemp(SESSION_CAFE, cafe);

                rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode);

            }
            else if (mode.equalsIgnoreCase(UI_ADD))
            {
                cafe = new Cafe(); 
                cafe.setCafeName("");
                rundata.getUser().setTemp(SESSION_CAFE, cafe);
                rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode);
            }

            context.put(UI_CAFE, cafe);
            context.put(UI_MODE, mode);


        }
        catch (Exception e)
        {
            Log.error(e);
            rundata.setMessage(e.toString());
        }
    }



    /**
     * Update the portlet state from the form.
     *
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     */
    public void doUpdate(RunData rundata, Context context) throws Exception
    {
        Cafe cafe = null;
        Connection con = null;

        try
        {
            con = Torque.getConnection();

            cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);

            if(cafe == null)
            {
                Log.error(NO_CURRENT_REC);
                rundata.setMessage(NO_CURRENT_REC);
                return;
            }
        
            rundata.getParameters().setProperties(cafe);

            validate(cafe);

            cafe.save(con);

            con.commit();

            returnToBrowser(rundata, true);
            
        }
        catch (Exception e)
        {
            Log.error("error in saving cafe: " + e);
            rundata.setMessage(e.toString());
            if (con != null)
            {
                con.rollback();
            }

        }
        finally
        {
            try
            {
                Torque.closeConnection(con);
            }
            catch (Exception e)
            {}
        }

    }

    /**
     * Delete current record
     *
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     */
    public void doDelete(RunData rundata, Context context) throws Exception
    {
        Cafe cafe = null;
        Connection con = null;

        try
        {
            con = Torque.getConnection();

            cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE);

            if(cafe == null)
            {
                Log.error(NO_CURRENT_REC);
                rundata.setMessage(NO_CURRENT_REC);
                return;
            }
        

            CafePeer.doDelete(cafe, con);

            con.commit();

            returnToBrowser(rundata, true);
            
        }
        catch (Exception e)
        {
            Log.error("error in deleting cafe: " + e);
            rundata.setMessage(e.toString());

            if (con != null)
            {
                con.rollback();
            }

        }
        finally
        {
            try
            {
                Torque.closeConnection(con);
            }
            catch (Exception e)
            {}
        }

    }

    /**
     * Cancel editing or deletion
     *
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     */
    public void doCancel(RunData rundata, Context context) throws Exception
    {
        returnToBrowser(rundata, false);
    }

    /** 
     * Helper function to get query param from request
     *
     */
    public static String getQueryParameter(RunData rundata, String attrName)
    {
        String str = rundata.getParameters().getString(attrName);
        if(str != null && str.length() > 0 && str.trim().length() > 0 )
            return str;
        return null;
    }

    /** 
     * Helper function to get query param from request with a default value
     *
     */
    public static String getQueryParameter(RunData rundata, String attrName, String defaultValue)
    {
        String str = getQueryParameter(rundata, attrName);
        if(str == null)
            return defaultValue;
        else
            return str;
    }


    /**
     *   validate that its a valid record
     *
     */
    private void validate(Cafe cafe) throws Exception
    {

        if(isEmpty(cafe.getCafeName()))
        {
            throw new Exception("Cafe Name must be entered.");
        }
    }

    /**
     * Check for string bein empty or null
     * this should be put in some utility package
     *
     */
    public static boolean isEmpty(String str)
    {
        if(!(str != null && str.trim().length() > 0))
        {
            return true;
        }
        return false;
    }

    /** 
     * redirect back to browser
     *
     */
    private void returnToBrowser(RunData rundata, boolean refresh)
    {
        try
        {
            JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
            DynamicURI duri = link.getPaneByName("TutorialCafeBrowser");
            if (refresh)
            {
                duri.addQueryData(CafeBrowserAction.BROWSER_COMMAND, CafeBrowserAction.BROWSER_REFRESH);
            }
            rundata.setRedirectURI(duri.toString());
            JetspeedLinkFactory.putInstance(link);
        }
        catch (Exception e)
        {
            Log.error(e);
            rundata.setMessage(e.toString());
        }
    }

}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us