KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > TypeCreateAction


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 package olstore.action;
28
29 import java.util.Vector;
30
31 import javax.servlet.http.*;
32
33
34 import olstore.dto.PropertyValue;
35 import olstore.form.CreateTypeForm;
36
37 import org.apache.struts.action.*;
38
39
40 public class TypeCreateAction extends DemoBaseAction {
41     
42     /**
43      * Acts as a relay for all item related tasks with a default action
44      *
45      */

46     public ActionForward execute ( ActionMapping mapping,
47             ActionForm form,
48             HttpServletRequest request,
49             HttpServletResponse response
50     ) throws Exception {
51         
52         //Throw an error if it doesn't equal of any of these?
53
try {
54             String action = ((CreateTypeForm)form).getSubmitType();
55             if ( action == null || action.equals("") ) {
56                 CreateNewType ( mapping, form, request, response );
57             } else if ( action.equals ( "updateProperties" ) ) {
58                 updateProperties ( mapping, form, request, response );
59             } else if ( action.equals ( "saveType" ) ) {
60                 return mapping.findForward ( "saveType" );
61             }
62             
63             return mapping.findForward ( "updateType" );
64             
65         } catch ( Exception e ) {
66             //Logging code here
67
ActionErrors errors = new ActionErrors();
68             errors.add("error", new ActionError("errors.type.load", e.getMessage() ));
69             saveErrors(request, errors);
70             // Return to same page
71
return (new ActionForward(mapping.getInput()));
72         }
73         
74     }
75     
76     
77     public void CreateNewType ( ActionMapping mapping,
78             ActionForm form,
79             HttpServletRequest request,
80             HttpServletResponse response
81     ) throws Exception {
82         
83         // Add a few empty Properties as defaults
84
Vector props = new Vector();
85         for ( int i = 0; i <5 ; i++ ) {
86             props.add ( new PropertyValue() );
87         }
88         ((CreateTypeForm)form).setProperties ( props );
89     }
90     
91     
92     /**
93      * This method updates the number of properties
94      * for display. The only validation that should take place in this
95      * particular action is that the total number of fields is a valid
96      * integer
97      */

98     public ActionForward updateProperties ( ActionMapping mapping,
99             ActionForm form,
100             HttpServletRequest request,
101             HttpServletResponse response
102     ) throws Exception {
103         try {
104             Vector propValueArr = ((CreateTypeForm)form).getProperties();
105             String numPropsStr = ((CreateTypeForm)form).getNumProps();
106             int numProps = Integer.parseInt ( numPropsStr );
107             
108             // if the number is smaller than the present length, then truncate
109
// the existing array
110
if ( numProps < propValueArr.size() ) {
111                 for ( int i = propValueArr.size() -1 ; i > numProps - 1 ; i-- ) {
112                     propValueArr.remove ( i );
113                 }
114             }
115             
116             // If the number is larger, then grow the ArrayList
117
if ( numProps > propValueArr.size() ) {
118                 for ( int i = propValueArr.size() ; i < numProps ; i++ ) {
119                     propValueArr.add ( new PropertyValue() );
120                 }
121             }
122             
123             ((CreateTypeForm)form).setProperties ( propValueArr ) ;
124             return mapping.findForward ( "updateType" );
125         }
126         
127         catch (NumberFormatException n){
128             ActionErrors errors = new ActionErrors();
129             errors.add ("error", new ActionError("errors.not.int", n.getMessage() ));
130             saveErrors(request,errors);
131             return (new ActionForward(mapping.getInput()));
132         }
133         
134         
135         catch (Exception e){
136             //Logging code here
137
ActionErrors errors = new ActionErrors();
138             errors.add("error", new ActionError("errors.item.load", e.getMessage() ));
139             saveErrors(request, errors);
140             // Return to same page
141
return (new ActionForward(mapping.getInput()));
142         }
143         
144         
145         
146     }
147     
148 }
149
Popular Tags