KickJava   Java API By Example, From Geeks To Geeks.

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


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.struts.action.ActionMapping;
21
22 import com.jdon.controller.events.EventModel;
23 import com.jdon.controller.model.ModelIF;
24 import com.jdon.model.ModelForm;
25 import com.jdon.model.ModelHandler;
26 import com.jdon.model.ModelManager;
27 import com.jdon.strutsutil.FormBeanUtil;
28 import com.jdon.util.Debug;
29
30 /**
31  * prepare for push a a create/insert view page, we need do some ready works.
32  *
33  *
34  * @author banq
35  */

36 public class CreateViewPageUtil {
37     private final static String JavaDoc module = CreateViewPageUtil.class.getName();
38
39     private ModelManager modelManager;
40
41
42     public CreateViewPageUtil(ModelManager modelManager) {
43         this.modelManager = modelManager;
44     }
45
46     /**
47      * create a ModelForm object
48      *
49      * two things: 1. create a ModelForm null instance 2. initial ModelForm
50      * instance value, obtain a inital Model instance copy the Model instance to
51      * the ModelForm instance
52      *
53      * obtaining datas two ways: 1.call ModelHandler initForm method; 2.call
54      * Modelhandler initModel method;
55      *
56      */

57     public void doCreate(ActionMapping actionMapping, ModelForm modelForm, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
58         Debug.logVerbose("[JdonFramework] enter doCreate... ", module);
59         String JavaDoc formName = FormBeanUtil.getFormName(actionMapping);
60         ModelHandler modelHandler = modelManager.borrowtHandlerObject(formName);
61         try {
62
63             ModelForm form = modelHandler.initForm(request);
64             if (form != null) {
65                 form.setAction(ModelForm.CREATE_STR);
66                 FormBeanUtil.saveActionForm(form, actionMapping, request);
67             } else {
68                 form = modelForm;
69             }
70             Debug.logVerbose("[JdonFramework] got a ModelForm ... " , module);
71
72             ModelIF modelDTO = modelManager.getModelObject(formName);
73             modelHandler.formCopyToModelIF(form, modelDTO);
74             EventModel em = new EventModel();
75             em.setActionName(form.getAction());
76             em.setModelIF(modelDTO);
77
78             modelDTO = initModel(em, request, modelHandler, form);
79             //copy initial model to form , intial form;
80
if (modelDTO != null) { // the model can be null
81
Debug.logVerbose("[JdonFramework] got a Model From the 'initModel' method of the service", module);
82                 modelHandler.modelIFCopyToForm(modelDTO, form);
83             }
84
85         } catch (Exception JavaDoc ex) {
86             Debug.logError("[JdonFramework]please check your service ã€? model or form, error is: " + ex, module);
87             throw new Exception JavaDoc("System error! please call system admin. " + ex);
88         } finally {
89             modelManager.returnHandlerObject(modelHandler); //返回modelhandlerå†?用
90
}
91     }
92
93     private ModelIF initModel(EventModel em, HttpServletRequest JavaDoc request, ModelHandler modelHandler, ModelForm form) throws Exception JavaDoc {
94         ModelIF model = null;
95         try {
96             model = modelHandler.initModelIF(em, form, request);
97         } catch (Exception JavaDoc ex) {
98             Debug.logError("[JdonFramework] the method 'initForm' of your handler or 'initMethod' of your service happened error: " + ex, module);
99             throw new Exception JavaDoc(ex);
100         }
101         return model;
102     }
103
104 }
105
Popular Tags