KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > examples > madlib > CreateActionHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.examples.madlib;
8
9 import com.inversoft.error.BasicError;
10 import com.inversoft.error.ErrorRegistry;
11 import com.inversoft.verge.mvc.controller.Action;
12
13
14 /**
15  * <p>
16  * This action handler is responsible for building the final
17  * MadLib from the users input.
18  * </p>
19  *
20  * @author Brian Pontarelli
21  */

22 public class CreateActionHandler {
23
24     /**
25      * The success action String return value
26      */

27     public static final String JavaDoc SUCCESS_ACTION = "success";
28
29     /**
30      * The failure action String return value
31      */

32     public static final String JavaDoc FAILURE_ACTION = "failure";
33
34     /**
35      * The cancel action String return value
36      */

37     public static final String JavaDoc CANCEL_ACTION = "cancel";
38
39
40     /**
41      * Construsts a new <code>CreateActionHandler</code>
42      */

43     public CreateActionHandler() {
44         // Empty
45
}
46
47
48     /**
49      * Handles the creation action which is generated with the user finishes
50      * entering all the fillins and submits that form
51      *
52      * @param action The action generated
53      * @return Either success of failure
54      */

55     public Object JavaDoc handleCreate(Action action){
56
57         // Check if the validation failed
58
if (action.getRequestContext().hasErrors()) {
59             return FAILURE_ACTION;
60         }
61         
62         // Retrieve the bean
63
MadLibBean mb = (MadLibBean) action.getHttpSession().getAttribute("madLibBean");
64         
65         // Build the MadLib and store it back in the MadLib bean
66
try {
67             String JavaDoc finalMadLib = MadLibBuilder.buildMadLib(mb);
68             mb.setFinalMadLib(finalMadLib);
69         } catch (MadLibException mle) {
70             BasicError error = ErrorRegistry.getBasicError(MadLibValidator.ERROR_BUNDLE,
71                 "creation", null, this);
72             action.getRequestContext().addError(error);
73             return FAILURE_ACTION;
74         }
75
76         return SUCCESS_ACTION;
77     }
78
79     /**
80      * Handles the cancel action and sends the user back to the select screen
81      *
82      * @param action The action generated
83      * @return Always cancel
84      */

85     public Object JavaDoc handleCancel(Action action) {
86         action.getRequestContext().clear();
87         return CANCEL_ACTION;
88     }
89 }
Popular Tags