KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > util > EditeViewPageUtil


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.util;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19
20 import org.apache.commons.beanutils.ConvertUtils;
21 import org.apache.struts.action.ActionMapping;
22
23 import com.jdon.controller.model.ModelIF;
24 import com.jdon.model.ModelForm;
25 import com.jdon.model.ModelHandler;
26 import com.jdon.model.ModelKey;
27 import com.jdon.model.ModelManager;
28 import com.jdon.model.config.ModelMapping;
29 import com.jdon.strutsutil.FormBeanUtil;
30 import com.jdon.util.Debug;
31
32 /**
33  * prepare for push a editable jsp page. this class will call the service to get
34  * a model that has datas. this work will delegate the ModelHandler class
35  *
36  *
37  * @author banq
38  */

39 public class EditeViewPageUtil {
40     private final static String JavaDoc module = EditeViewPageUtil.class.getName();
41
42     protected ModelManager modelManager;
43
44
45     public EditeViewPageUtil(ModelManager modelManager) {
46         this.modelManager = modelManager;
47     }
48
49     /**
50      * two things: 1. create a ModelForm null instance 2. obtain a existed Model
51      * instance copy the Model instance to the ModelForm instance
52      *
53      */

54     public ModelIF getModelForEdit(ActionMapping actionMapping, ModelForm modelForm, HttpServletRequest JavaDoc request)
55             throws Exception JavaDoc {
56         ModelIF model = null;
57         ModelHandler modelHandler = null;
58         try {
59             String JavaDoc formName = FormBeanUtil.getFormName(actionMapping);
60             modelHandler = modelManager.borrowtHandlerObject(formName);
61
62             ModelForm form = modelHandler.initForm(request);
63             if (form != null) {
64                 form.setAction(ModelForm.EDIT_STR);
65                 FormBeanUtil.saveActionForm(form, actionMapping, request);
66             } else {
67                 form = modelForm;
68             }
69             Debug.logVerbose("[JdonFramework] got a ModelForm ... ", module);
70
71             Debug.logVerbose("[JdonFramework] prepare to fetch a Model from service layer", module);
72             model = fetchModel(request, formName, modelHandler);
73             Debug.logVerbose("[JdonFramework] got the Model data successfully..", module);
74
75             modelHandler.modelIFCopyToForm(model, form);
76
77         } catch (Exception JavaDoc ex) {
78             Debug.logError("[JdonFramework]please check your service ã€? model or form, error is: " + ex, module);
79         } finally {
80             modelManager.returnHandlerObject(modelHandler); //返回modelhandlerå†?用
81
}
82         return model;
83     }
84
85     protected ModelIF fetchModel(HttpServletRequest JavaDoc request, String JavaDoc formName, ModelHandler modelHandler) throws Exception JavaDoc {
86         ModelIF model = null;
87         try {
88             Object JavaDoc keyValue = getParamKeyValue(request, modelHandler);
89             clearModelCache(formName, keyValue, modelHandler);
90             model = modelHandler.findModelIF(keyValue, request);
91             if (model == null) {
92                 Debug.logError("[JdonFramework] Error: got a NULL Model..", module);
93                 throw new Exception JavaDoc("got a NULL Model");
94             } else {
95                 addModelCache(formName, keyValue, modelHandler, model);
96             }
97         } catch (Exception JavaDoc ex) {
98             Debug.logError("[JdonFramework] the method 'findModelByKey' of your handler or 'getMethod' of service happened error: " + ex, module);
99             throw new Exception JavaDoc(ex);
100         }
101         return model;
102     }
103     
104    
105     private void clearModelCache(String JavaDoc formName, Object JavaDoc keyValue, ModelHandler modelHandler) {
106         ModelIF model = null;
107         try {
108             ModelKey modelKey = new ModelKey(keyValue, formName);
109             model = modelManager.getCache(modelKey);
110             if (model != null) {//clear the cache
111
modelManager.removeCache(keyValue);
112             }
113         } catch (Exception JavaDoc e) {
114             Debug.logError("[JdonFramework] clearModelCache error: " + e);
115         }
116     }
117
118     private void addModelCache(String JavaDoc formName, Object JavaDoc keyValue, ModelHandler modelHandler, ModelIF model) {
119         ModelKey modelKey = new ModelKey(keyValue, formName);
120         modelManager.addCache(modelKey, model);
121     }
122
123     /**
124      * 获得å?‚æ•°key值 例如: /admin/productAction.do?action=edit&productId=1721
125      * 缺çœ?:productId为productçš„modelmapping.xml中key定义值
126      *
127      * 对于如下调用: /admin/productAction.do?action=edit&userId=16
128      * userIdä¸?是modelmapping.xml中key定义值,则需è¦?override本方法,
129      *
130      *
131      * @param actionMapping
132      * @param request
133      * @return å?‚æ•°key值
134      * @throws java.lang.Exception
135      */

136     public Object JavaDoc getParamKeyValue(HttpServletRequest JavaDoc request, ModelHandler modelHandler) {
137
138         Object JavaDoc keyValue = null;
139         try {
140             ModelMapping modelMapping = modelHandler.getModelMapping();
141             String JavaDoc keyName = modelMapping.getKeyName();
142             Debug.logVerbose("[JdonFramework] the keyName is " + keyName, module);
143             String JavaDoc keyValueS = request.getParameter(keyName);
144             Debug.logVerbose("[JdonFramework] got the keyValue is " + keyValueS, module);
145             if (keyValueS == null) {
146                 Debug.logVerbose("[JdonFramework]the keyValue is null", module);
147             }
148             Class JavaDoc keyClassType = modelMapping.getKeyClassType();
149             if (keyClassType.isAssignableFrom(String JavaDoc.class)) {
150                 keyValue = keyValueS;
151             }else{
152                 Debug.logVerbose("[JdonFramework] convert String keyValue to" + keyClassType.getName(), module);
153                 keyValue = ConvertUtils.convert(keyValueS, keyClassType);
154             }
155         } catch (Exception JavaDoc e) {
156             Debug.logError("[JdonFramework] getParamKeyValue error: " + e);
157         }
158         return keyValue;
159     }
160
161 }
162
Popular Tags