1 15 16 package com.jdon.strutsutil; 17 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 21 import org.apache.struts.action.ActionForm; 22 import org.apache.struts.action.ActionForward; 23 import org.apache.struts.action.ActionMapping; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 27 import com.jdon.controller.events.EventModel; 28 import com.jdon.controller.model.ModelIF; 29 import com.jdon.model.ModelForm; 30 import com.jdon.model.ModelHandler; 31 import com.jdon.model.ModelKey; 32 import com.jdon.model.config.ModelMapping; 33 import com.jdon.util.Debug; 34 35 61 public class ModelSaveAction extends ModelBaseAction { 62 63 private final static String module = ModelSaveAction.class.getName(); 64 65 public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) 66 throws Exception { 67 68 intContext(this.getServlet().getServletContext()); 69 70 checkConfigName(actionMapping); 71 72 EventModel em = null; 73 String formName = FormBeanUtil.getFormName(actionMapping); 74 75 ModelHandler modelHandler = modelManager.borrowtHandlerObject(formName); 76 try { 77 ModelForm form = getModelForm(modelHandler, actionForm, request); 78 ModelIF model = makeModel(actionMapping, actionForm, request, modelHandler); 79 modelHandler.formCopyToModelIF(form, model); 80 81 em = new EventModel(); 82 em.setActionName(form.getAction()); 83 em.setModelIF(model); 84 em.setActionType(FormBeanUtil.actionTransfer(form.getAction())); 85 86 Debug.logVerbose("[JdonFramework] save data to database ... ", module); 87 modelHandler.serviceAction(em, request); 89 90 modelHandler.modelIFCopyToForm(em.getModelIF(), form); 91 92 } catch (Exception ex) { 93 Debug.logError("[JdonFramework]please check your service � model or form :" + ex, module); 94 throw new Exception ("System error! please call system Admin." + ex); 95 } finally { 96 modelManager.returnHandlerObject(modelHandler); 97 } 98 99 if (em.getErrors() != null) { 100 Debug.logError("[JdonFramework] save error!! " + em.getErrors(), module); 101 ActionMessages errors = new ActionMessages(); 102 ActionMessage error = new ActionMessage(em.getErrors()); 103 errors.add(ActionMessages.GLOBAL_MESSAGE, error); 104 saveErrors(request, errors); 105 106 ActionForward af = actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME); 107 if (af != null) 108 return af; 109 else 110 return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME); 111 } else { 112 Debug.logVerbose("[JdonFramework] save successfully ... ", module); 113 return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME); 114 } 115 116 } 117 118 121 protected ModelForm getModelForm(ModelHandler modelHandler, ActionForm actionForm, HttpServletRequest request) throws Exception { 122 123 if (actionForm == null) { 124 throw new Exception (" must define form-bean as 'action' name in struts-config.xml "); 125 } 126 ModelForm strutsForm = (ModelForm) actionForm; 127 String action = strutsForm.getAction(); 128 if ((action == null) || (action.length() == 0)) 129 throw new Exception (" Need a field : <html:hidden property=action /> in jsp's form! "); 130 131 return strutsForm; 132 133 } 134 135 144 protected ModelIF makeModel(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, ModelHandler modelHandler) 145 throws Exception { 146 ModelIF model = null; 147 try { 148 String formName = actionMapping.getName(); 149 if (formName == null) 150 throw new Exception ("no define the FormName in struts_config.xml"); 151 152 ModelMapping modelMapping = modelHandler.getModelMapping(); 153 String keyName = modelMapping.getKeyName(); 154 String keyValue = request.getParameter(keyName); 155 if (keyValue == null) { 156 Debug.logError("[JdonFramework]Need a model's key field : <html:hidden property=MODEL EKY /> in jsp's form! ", module); 157 } 158 159 ModelKey modelKey = new ModelKey(keyValue, formName); 160 model = modelManager.getCache(modelKey); 161 if (model != null) { modelManager.removeCache(keyValue); 163 } else { Debug.logVerbose("[JdonFramework] no model cache, keyName is " + keyName, module); 165 model = modelManager.getModelObject(formName); 166 } 167 } catch (Exception e) { 168 Debug.logError("[JdonFramework] makeModel error: " + e); 169 throw new Exception (e); 170 } 171 return model; 172 } 173 174 } 175 | Popular Tags |