KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > ModelSaveAction


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package com.jdon.strutsutil;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
36  * Accept the datas that user submited, and handle them to service layer,
37  * service maybe persistence them. it is just like a handler. all functions
38  * delegate Modelhandler
39  *
40  *
41  * configure in jdonframework.xml:
42  *
43  * <action name="productForm" type="com.jdon.strutsutil.ModelSaveAction"
44  * input="/admin/product.jsp" scope="request" path="/admin/saveProductAction">
45  * <forward name="success" path="/admin/productOk.jsp" /> <forward
46  * name="failure" path="/admin/productOk.jsp" /> </action>
47  *
48  * notie: the action parameter of the ModelForm(ActionForm) must be: 1. create ,
49  * this class will call the create method defined in jdonframework.xml 2. edit ,
50  * this class will call the edit method defined in jdonframework.xml 3. delete,
51  * this class will call the delete method defined in jdonframework.xml if not
52  * above, this class will call the action's value method of the service
53  * interface. example: action's value : deleteAll so, your service interface
54  * must havs a method: public void deleteAll(EventModel em);
55  *
56  * this class will directly call the deleteAll method of the service.
57  *
58  *
59  * @author banq
60  */

61 public class ModelSaveAction extends ModelBaseAction {
62
63     private final static String JavaDoc module = ModelSaveAction.class.getName();
64
65     public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
66             throws Exception JavaDoc {
67
68         intContext(this.getServlet().getServletContext());
69
70         checkConfigName(actionMapping);
71
72         EventModel em = null;
73         String JavaDoc 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             //deleagte the Modelhandler's serviceAction
88
modelHandler.serviceAction(em, request);
89
90             modelHandler.modelIFCopyToForm(em.getModelIF(), form);
91
92         } catch (Exception JavaDoc ex) {
93             Debug.logError("[JdonFramework]please check your service ã€? model or form :" + ex, module);
94             throw new Exception JavaDoc("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     /**
119      * get a ModelForm or create it.
120      */

121     protected ModelForm getModelForm(ModelHandler modelHandler, ActionForm actionForm, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
122
123         if (actionForm == null) {
124             throw new Exception JavaDoc(" must define form-bean as 'action' name in struts-config.xml ");
125         }
126         ModelForm strutsForm = (ModelForm) actionForm;
127         String JavaDoc action = strutsForm.getAction();
128         if ((action == null) || (action.length() == 0))
129             throw new Exception JavaDoc(" Need a field : <html:hidden property=action /> in jsp's form! ");
130
131         return strutsForm;
132
133     }
134
135     /**
136      * create a Model from the jdonframework.xml
137      *
138      * @param actionMapping
139      * @param actionForm
140      * @param request
141      * @return Model
142      * @throws java.lang.Exception
143      */

144     protected ModelIF makeModel(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, ModelHandler modelHandler)
145             throws Exception JavaDoc {
146         ModelIF model = null;
147         try {
148             String JavaDoc formName = actionMapping.getName();
149             if (formName == null)
150                 throw new Exception JavaDoc("no define the FormName in struts_config.xml");
151
152             ModelMapping modelMapping = modelHandler.getModelMapping();
153             String JavaDoc keyName = modelMapping.getKeyName();
154             String JavaDoc 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) {//clear the cache
162
modelManager.removeCache(keyValue);
163             } else {//
164
Debug.logVerbose("[JdonFramework] no model cache, keyName is " + keyName, module);
165                 model = modelManager.getModelObject(formName);
166             }
167         } catch (Exception JavaDoc e) {
168             Debug.logError("[JdonFramework] makeModel error: " + e);
169             throw new Exception JavaDoc(e);
170         }
171         return model;
172     }
173
174 }
175
Popular Tags