KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > ItemCreateAction


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 import javax.naming.Context;
33 import javax.naming.InitialContext;
34
35 import olstore.session.helper.ItemHelperLocal;
36 import olstore.session.helper.ItemHelperLocalHome;
37
38 import olstore.dto.ItemValue;
39 import olstore.dto.PictureValue;
40 import olstore.dto.PropertyValue;
41 import olstore.form.CreateItemForm;
42 import olstore.framework.EJBHomeFactory;
43
44 import org.apache.struts.action.*;
45 import org.apache.commons.beanutils.BeanUtils;
46
47
48 public class ItemCreateAction extends DemoBaseAction {
49     
50     /**
51      * Acts as a relay for all item related tasks with a default action
52      *
53      */

54     public ActionForward execute ( ActionMapping mapping,
55             ActionForm form,
56             HttpServletRequest request,
57             HttpServletResponse response
58     ) throws Exception {
59         
60         //Throw an error if it doesn't equal of any of these?
61
try {
62             String action = ((CreateItemForm)form).getSubmitType();
63             if ( action == null || action.equals("") ) {
64                 String itemId = ((CreateItemForm)form).getItemId();
65                 if ( itemId != null && ! itemId.equals("") ) {
66                     loadItem ( mapping, form, request, response );
67                 } else {
68                     CreateNewItem ( mapping, form, request, response );
69                 }
70             } else if ( action.equals ( "updateProperties" ) ) {
71                 updateProperties ( mapping, form, request, response );
72             } else if ( action.equals ( "updatePictures" ) ) {
73                 updatePictures ( mapping, form, request, response );
74             } else if ( action.equals ( "changeType" ) ) {
75                 changeType ( mapping, form, request, response );
76             } else if ( action.equals ( "saveItem" ) ) {
77                 return mapping.findForward ( "saveItem" );
78             }
79             
80             return mapping.findForward ( "updateItem" );
81             
82             
83         }
84         
85         catch (NumberFormatException n){
86             ActionErrors errors = new ActionErrors();
87             errors.add ("error", new ActionError("errors.not.int", n.getMessage() ));
88             saveErrors(request,errors);
89             return (new ActionForward(mapping.getInput()));
90         }
91         
92         catch ( Exception e ) {
93             //Logging code here
94
ActionErrors errors = new ActionErrors();
95             errors.add("error", new ActionError("errors.item.load", e.getMessage() ));
96             saveErrors(request, errors);
97             // Return to same page
98
return (new ActionForward(mapping.getInput()));
99         }
100         
101     }
102     
103     
104     public void CreateNewItem ( ActionMapping mapping,
105             ActionForm form,
106             HttpServletRequest request,
107             HttpServletResponse response
108     ) throws Exception {
109         
110         //Get the default selected type
111
Context context = new InitialContext();
112         String type = (String) context.lookup ( "java:comp/env/DEFAULT_TYPE" );
113         
114         //Add an empty default Picture
115
//This get method creates the specified index element, see form
116
//for details
117
((CreateItemForm)form).getPic ( 0 );
118         
119         //Load up the default properties for default type
120
EJBHomeFactory factory = EJBHomeFactory.getInstance();
121         ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
122         ItemHelperLocal itemHelper = itemHelperHome.create();
123         Vector propValueArr = itemHelper.getPropertiesForType ( type );
124         
125         ((CreateItemForm)form).setProperties ( propValueArr );
126     }
127     
128     
129     /**
130      * This method updates the number of properties
131      * for display. The only validation that should take place in this
132      * particular action is that the total number of fields is a valid
133      * integer
134      */

135     public void updateProperties ( ActionMapping mapping,
136             ActionForm form,
137             HttpServletRequest request,
138             HttpServletResponse response
139     ) throws Exception {
140         
141         Vector propValueArr = ((CreateItemForm)form).getProperties();
142         String numPropsStr = ((CreateItemForm)form).getNumProps();
143         int numProps = Integer.parseInt ( numPropsStr );
144         
145         
146         
147         // if the number is smaller than the present length, then truncate
148
// the existing array
149
if ( numProps < propValueArr.size() ) {
150             for ( int i = propValueArr.size() -1 ; i > numProps - 1 ; i-- ) {
151                 propValueArr.remove ( i );
152             }
153         }
154         
155         // If the number is larger, then grow the ArrayList
156
if ( numProps > propValueArr.size() ) {
157             for ( int i = propValueArr.size() ; i < numProps ; i++ ) {
158                 propValueArr.add ( new PropertyValue() );
159             }
160         }
161         
162         ((CreateItemForm)form).setProperties ( propValueArr ) ;
163         
164     }
165     
166     /**
167      * This method updates the number of properties
168      * for display. The only validation that should take place in this
169      * particular action is that the total number of fields is a valid
170      * integer
171      */

172     public void updatePictures ( ActionMapping mapping,
173             ActionForm form,
174             HttpServletRequest request,
175             HttpServletResponse response
176     ) throws Exception {
177         
178         Vector propValueArr = ((CreateItemForm)form).getPictures();
179         String numPicsStr = ((CreateItemForm)form).getNumPics();
180         int numPics = Integer.parseInt ( numPicsStr );
181         
182         if (numPics<=0){
183             //ActionErrors errors = new ActionErrors();
184
//errors.add ("error", new ActionError("errors.not.int", "The Number of Fields for Pictures " ));
185
//saveErrors(request,errors);
186
throw new Exception("The number of Picture Fields must be a postive interger") ;
187         }
188         
189         // if the number is smaller than the present length, then truncate
190
// the existing array
191
if ( numPics < propValueArr.size() ) {
192             for ( int i = propValueArr.size() -1 ; i > numPics - 1 ; i-- ) {
193                 propValueArr.remove ( i );
194             }
195         }
196         
197         // If the number is larger, then grow the ArrayList
198
if ( numPics > propValueArr.size() ) {
199             for ( int i = propValueArr.size() ; i < numPics ; i++ ) {
200                 propValueArr.add ( new PictureValue() );
201             }
202         }
203         
204         ((CreateItemForm)form).setPictures ( propValueArr ) ;
205         
206     }
207     
208     
209     /**
210      * This action loads up the default properties for a given type
211      * for an existing form. (It preserves the values that the
212      * user has entered)
213      */

214     public void changeType ( ActionMapping mapping,
215             ActionForm form,
216             HttpServletRequest request,
217             HttpServletResponse response
218     ) throws Exception {
219         
220         CreateItemForm createForm = (CreateItemForm)form;
221         String newType = createForm.getTypeId();
222         EJBHomeFactory factory = EJBHomeFactory.getInstance();
223         ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
224         ItemHelperLocal helperLocal = itemHelperHome.create();
225         Vector newProps = helperLocal.getPropertiesForType ( newType );
226         
227         //change the oldProps to have the new prop names (preserve values)
228
int i = 0;
229         for (; i < newProps.size() ; i++ ) {
230             PropertyValue prop = createForm.getProp ( i );
231             prop.setName ( ((PropertyValue)newProps.get(i)).getName() );
232         }
233         
234         // erase any extras that might be left
235
Vector oldProps = createForm.getProperties();
236         int size = oldProps.size();
237         for ( int j=0; j < size - i; j++ ) {
238             oldProps.remove ( oldProps.size() - 1 );
239         }
240     }
241     
242     
243     public void loadItem ( ActionMapping mapping,
244             ActionForm form,
245             HttpServletRequest request,
246             HttpServletResponse response
247     ) throws Exception {
248         
249         CreateItemForm createForm = (CreateItemForm)form;
250         String itemId = createForm.getItemId();
251         EJBHomeFactory factory = EJBHomeFactory.getInstance();
252         ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
253         ItemHelperLocal itemHelper = itemHelperHome.create();
254         
255         ItemValue itemVal = itemHelper.getItemValueForId ( itemId );
256         BeanUtils.copyProperties ( createForm, itemVal );
257     }
258     
259     
260 }
261
Popular Tags