1 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 46 public ActionForward execute ( ActionMapping mapping, 47 ActionForm form, 48 HttpServletRequest request, 49 HttpServletResponse response 50 ) throws Exception { 51 52 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 ActionErrors errors = new ActionErrors(); 68 errors.add("error", new ActionError("errors.type.load", e.getMessage() )); 69 saveErrors(request, errors); 70 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 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 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 ( numProps < propValueArr.size() ) { 111 for ( int i = propValueArr.size() -1 ; i > numProps - 1 ; i-- ) { 112 propValueArr.remove ( i ); 113 } 114 } 115 116 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 ActionErrors errors = new ActionErrors(); 138 errors.add("error", new ActionError("errors.item.load", e.getMessage() )); 139 saveErrors(request, errors); 140 return (new ActionForward(mapping.getInput())); 142 } 143 144 145 146 } 147 148 } 149 | Popular Tags |