1 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 54 public ActionForward execute ( ActionMapping mapping, 55 ActionForm form, 56 HttpServletRequest request, 57 HttpServletResponse response 58 ) throws Exception { 59 60 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 ActionErrors errors = new ActionErrors(); 95 errors.add("error", new ActionError("errors.item.load", e.getMessage() )); 96 saveErrors(request, errors); 97 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 Context context = new InitialContext(); 112 String type = (String) context.lookup ( "java:comp/env/DEFAULT_TYPE" ); 113 114 ((CreateItemForm)form).getPic ( 0 ); 118 119 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 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 ( numProps < propValueArr.size() ) { 150 for ( int i = propValueArr.size() -1 ; i > numProps - 1 ; i-- ) { 151 propValueArr.remove ( i ); 152 } 153 } 154 155 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 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 throw new Exception("The number of Picture Fields must be a postive interger") ; 187 } 188 189 if ( numPics < propValueArr.size() ) { 192 for ( int i = propValueArr.size() -1 ; i > numPics - 1 ; i-- ) { 193 propValueArr.remove ( i ); 194 } 195 } 196 197 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 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 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 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 |