KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > ModelHandler


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.model;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19
20 import org.apache.commons.beanutils.PropertyUtils;
21
22 import com.jdon.controller.events.EventModel;
23 import com.jdon.controller.model.Model;
24 import com.jdon.controller.model.ModelIF;
25 import com.jdon.model.config.ModelMapping;
26 import com.jdon.strutsutil.util.CreateViewPageUtil;
27 import com.jdon.util.Debug;
28
29
30 /**
31  * Presentation layer delegateion class
32  * Presentation layer need three common works collaboration with service layer:
33  *
34  * 1. Presentation layer need a ModelForm instance that include
35  * some initially data, these data must obtain from service.
36  *
37  * 2. Presentation layer must obtain a existed ModelForm instance from service
38  *
39  * 3. Presentation layer submit the data ModelForm instance that user created or
40  * update, service layer must persistence them
41  *
42  * Framework's user can extends this ModelHandler, and
43  * configure your jdonframework.xml, so ask framework active it:
44  * <model ...>
45       <handler class="your ModelHandler concrete class" />
46     </model>
47  *
48  * @author banq
49  */

50
51 public abstract class ModelHandler implements ServiceHandler{
52
53   private final static String JavaDoc module = ModelHandler.class.getName();
54
55   protected ModelMapping modelMapping;
56   public void setModelMapping(ModelMapping modelMapping) {
57     this.modelMapping = modelMapping;
58   }
59
60   public ModelMapping getModelMapping() {
61     return modelMapping;
62   }
63
64   /**
65    *
66    *Presentation layer need a ModelForm instance that include
67    * some initially data, these data must obtain from service.
68    * this method implements these functions in presentation layer's
69    * ModelHanlder concrete class;
70    *
71    * this method is available in pushing create or edit view page
72    *
73    * @param request
74    * @return ModelForm instance
75    * @throws java.lang.Exception
76    *
77    */

78   public ModelForm initForm(HttpServletRequest JavaDoc request) throws
79       Exception JavaDoc{
80       return null;
81   }
82
83  /**
84   * for old version below 1.4
85   */

86   public Model initModel(HttpServletRequest JavaDoc request) throws Exception JavaDoc{
87       return null;
88   }
89
90   /**
91    *Presentation layer need a ModelForm instance that include
92    * some initially data, these data must obtain from service.
93    * this method implements these functions by deleagating
94    * service.
95    *
96    * this mehtod is only availabe in pushing create view page
97    * in pushing editable view page, the model is obtained
98    * by it's key, not from this method;
99    *
100    * @param request
101    * @return Model instance
102    * @throws Exception
103    * @see CreateViewPageUtil
104    */

105   public ModelIF initModelIF(EventModel em, ModelForm form, HttpServletRequest JavaDoc request) throws Exception JavaDoc{
106       return initModel(request);
107   }
108
109   /**
110    * for old version below 1.4
111    */

112   public Model findModelByKey(String JavaDoc keyValue, HttpServletRequest JavaDoc request) throws
113       Exception JavaDoc{
114       return null;
115       
116   }
117   
118   /**
119    * obtain a existed Model instance by his primtive key.
120    *
121    *
122    * @param keyValue primtive key
123    * @param request
124    * @return Model
125    * @throws java.lang.Exception
126    */

127   public ModelIF findModelIF(Object JavaDoc keyValue, HttpServletRequest JavaDoc request) throws Exception JavaDoc{
128       return findModelByKey((String JavaDoc)keyValue, request); //for old version
129
}
130   
131
132   /**
133    * package the Model instance that has user's input data to EventModel object,
134    * and submit them to servier layer;
135    * @param em package Model instance
136    * @param request
137    * @throws java.lang.Exception
138    */

139   public abstract void serviceAction(EventModel em, HttpServletRequest JavaDoc request) throws
140       Exception JavaDoc;
141
142
143   /**
144    * for old version below 1.4
145    */

146   public void modelCopyToForm(Model model, ModelForm form) throws Exception JavaDoc {
147       try {
148           PropertyUtils.copyProperties(form, model);
149        } catch (Exception JavaDoc e) {
150           String JavaDoc error = " Model:" + model.getClass().getName() +" copy To ModelForm:"+ form.getClass().getName() +" error:" + e;
151           Debug.logError(error, module);
152           throw new Exception JavaDoc(error);
153        }
154   }
155   
156   /**
157    * Model object's data transfer to ModelForm object
158    *
159    * default implemention is copy mapping between with them;
160    * another implemention is :
161    * PropertyUtils.setProperty
162    * extends this class , and override this method
163    *
164    * @param model
165    * @param form
166    * @throws java.lang.Exception
167    */

168   public void modelIFCopyToForm(ModelIF model, ModelForm form) throws Exception JavaDoc{
169       if (model instanceof Model){ //for below 1.4 version
170
modelCopyToForm((Model)model, form);
171           return;
172       }
173       try {
174           PropertyUtils.copyProperties(form, model);
175         } catch (Exception JavaDoc e) {
176           String JavaDoc error = " Model:" + model.getClass().getName() +" copy To ModelForm:"+ form.getClass().getName() +" error:" + e;
177           Debug.logError(error, module);
178           throw new Exception JavaDoc(error);
179         }
180   }
181
182   /**
183    * for old version below 1.4
184    */

185   public void formCopyToModel(ModelForm form, Model model) throws Exception JavaDoc {
186         try {
187             PropertyUtils.copyProperties(model, form);
188         } catch (Exception JavaDoc e) {
189             String JavaDoc error = " ModelForm:" + form.getClass().getName()
190                     + " copy To Model:" + model.getClass().getName()
191                     + " error:" + e;
192             Debug.logError(error, module);
193             throw new Exception JavaDoc(error);
194         }
195     }
196   
197   /**
198      * ModelForm object's data transfer to Model object
199      *
200      * default implemention is copy mapping between with them;
201      *
202      * another implemention: String propertyName =
203      * StringUtil.getLastString(model.getClass().getName()); Model hasDataModel =
204      * PropertyUtils.getProperty(form, propertyName); model = hasDataModel;
205      *
206      * extends this class , and override this method
207      *
208      * @param model
209      * @param form
210      * @throws java.lang.Exception
211      */

212   public void formCopyToModelIF(ModelForm form, ModelIF model) throws Exception JavaDoc{
213       if (model instanceof Model){ //for below 1.4 version
214
formCopyToModel(form, (Model)model);
215           return;
216       }
217       try {
218           PropertyUtils.copyProperties(model, form);
219         } catch (Exception JavaDoc e) {
220           String JavaDoc error = " ModelForm:"+ form.getClass().getName() +" copy To Model:"+model.getClass().getName() +" error:" + e;
221           Debug.logError(error, module);
222           throw new Exception JavaDoc(error);
223         }
224   }
225
226 }
227
Popular Tags