KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28
29 import com.jdon.controller.model.Model;
30 import com.jdon.controller.model.ModelIF;
31 import com.jdon.controller.model.PageIterator;
32 import com.jdon.model.ModelKey;
33 import com.jdon.util.Debug;
34
35 /**
36  * batch query action
37  *
38  * @author banq
39  */

40 public abstract class ModelListAction extends ModelBaseAction {
41
42   private final static String JavaDoc module = ModelListAction.class.getName();
43
44   public ActionForward execute(ActionMapping actionMapping,
45                                ActionForm actionForm,
46                                HttpServletRequest JavaDoc request,
47                                HttpServletResponse JavaDoc response) throws
48       Exception JavaDoc {
49
50     intContext(this.getServlet().getServletContext());
51    
52
53     int start = 0;
54     int count = 30;
55
56     String JavaDoc startStr = request.getParameter("start");
57     if ( (startStr != null) && (startStr.length() != 0)) {
58       start = Integer.parseInt(startStr);
59     }
60     String JavaDoc countStr = request.getParameter("count");
61     if ( (countStr != null) && (countStr.length() != 0)) {
62       count = Integer.parseInt(countStr);
63     }
64     
65
66     ModelListForm listForm = getModelListForm(actionMapping, actionForm, request);
67     listForm.setStart(start);
68
69     PageIterator pageIterator = getPageIterator(request, start, count);
70     if (pageIterator != null) {// by pageIterator
71
if (pageIterator.isElementsTypeIsKey()){
72             setModellistByKey(listForm, pageIterator, request);
73         }else{
74             setModellistByModel(listForm, pageIterator, request);
75         }
76     } else {
77         Debug.logError("getPageIterator not be implemented, you must implement either of them ", module);
78         listForm.setList(new ArrayList JavaDoc());
79     }
80     
81     listForm.setOneModel(setupOneModelIF(request));
82     customizeListForm(actionMapping, actionForm, request, listForm);
83
84     if (actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME) == null)
85       Debug.logError(
86           "not found the forward name '" + FormBeanUtil.FORWARD_SUCCESS_NAME +
87           "' in struts-config.xml", module);
88
89     return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME);
90   }
91   
92   private void setModellistByKey(ModelListForm listForm,
93             PageIterator pageIterator, HttpServletRequest JavaDoc request) {
94         Collection JavaDoc c = null;
95         try {
96             listForm.setAllCount(pageIterator.getAllCount());
97             if (pageIterator.getCount() != 0)
98                 listForm.setCount(pageIterator.getCount());// for block
99
// pageIterator
100
c = getModelList(request, pageIterator);
101             Debug.logVerbose(
102                     "[JdonFramework] listForm 's property: getList size is "
103                             + c.size(), module);
104             pageIterator.reset();
105         } catch (Exception JavaDoc e) {
106             Debug.logError(" setModellistByKey error " + e, module);
107             c = new ArrayList JavaDoc();
108         }
109         listForm.setList(c);
110   }
111   
112   /* the elements in pageIterator is Model type
113   * we directly add them in result
114   */

115   private void setModellistByModel(ModelListForm listForm,
116             PageIterator pageIterator, HttpServletRequest JavaDoc request) {
117         Collection JavaDoc c = null;
118         try {
119             listForm.setAllCount(pageIterator.getAllCount());
120             if (pageIterator.getCount() != 0)
121                 listForm.setCount(pageIterator.getCount());
122             c = new ArrayList JavaDoc(pageIterator.getSize());
123             while (pageIterator.hasNext()) {
124                 Object JavaDoc o = pageIterator.next();
125                 if ( o != null)
126                     c.add(o);
127             }
128             Debug.logVerbose(
129                     "[JdonFramework] listForm 's property: getList size is "
130                             + c.size(), module);
131             pageIterator.reset();
132         } catch (Exception JavaDoc e) {
133             Debug.logError(" setModellistByModel error " + e, module);
134             c = new ArrayList JavaDoc();
135         }
136         listForm.setList(c);
137   }
138
139   /**
140    * 获得ModelListForm实例
141    *
142    * @param actionMapping
143    * @param actionForm
144    * @param request
145    * @return
146    * @throws java.lang.Exception
147    */

148   protected ModelListForm getModelListForm(ActionMapping actionMapping,
149                                         ActionForm actionForm,
150                                         HttpServletRequest JavaDoc request) throws
151       Exception JavaDoc {
152     ModelListForm modelListForm = null;
153     if (actionForm == null){
154       modelListForm = new ModelListForm();
155       FormBeanUtil.saveActionForm(modelListForm, actionMapping, request);
156     }else if (actionForm instanceof ModelListForm)
157       modelListForm = (ModelListForm) actionForm;
158
159     if (modelListForm == null)
160       throw new Exception JavaDoc(
161           "not found the bean of com.jdon.strutsutil.ModelListForm");
162     else
163       return modelListForm;
164
165   }
166
167   /**
168    * æ ¹æ?®PageIterator中ID集å?ˆï¼ŒèŽ·å¾—相应的Model集å?ˆ
169    * @param request
170    * @param pageIterator
171    * @return Model集å?ˆ
172    * @throws java.lang.Exception
173    */

174   protected List JavaDoc getModelList(HttpServletRequest JavaDoc request,
175                             PageIterator pageIterator) throws Exception JavaDoc {
176     List JavaDoc list = new ArrayList JavaDoc(pageIterator.getSize());
177     ModelIF model = null;
178     Class JavaDoc modelClass = null;
179     while (pageIterator.hasNext()) {
180       Object JavaDoc dataKey = pageIterator.next();
181       if ( (modelClass == null) || (!isEnableCache())) {
182         Debug.logVerbose("[JdonFramework] getCache from db. " , module);
183         model = fetchModel(request, dataKey);
184         if (model != null)
185           modelClass = model.getClass();
186         else
187           continue;
188       } else {
189         //首先从缓存中获å?–
190
ModelKey modelKey = new ModelKey(dataKey, modelClass);
191         model = modelManager.getCache(modelKey);
192         if (model == null) {
193           model = fetchModel(request, dataKey);
194           modelManager.addCache(modelKey, model);
195         }
196       }
197       if (model != null)
198           list.add(model);
199     }
200     return list;
201   }
202   
203
204   /**
205    * 定制ModelListForm
206    *
207    * 缺çœ?ModelListForm是å?ªæœ‰ä¸€ä¸ªList,包å?«ä¸€ç§?Model集å?ˆ
208    * 有的应用å?¯èƒ½æ˜¯ä¸¤ç§?Model集å?ˆï¼Œå?¯ä»¥ç»§æ‰¿ModelListForm实现Map-backed ActionForms
209    * å†?继承本Action,实现本方法。
210    *
211    * @param actionMapping
212    * @param actionForm
213    * @param request
214    * @param modelListForm
215    * @throws java.lang.Exception
216    */

217   public void customizeListForm(ActionMapping actionMapping,
218                                     ActionForm actionForm,
219                                HttpServletRequest JavaDoc request,
220                               ModelListForm modelListForm ) throws Exception JavaDoc{
221
222
223   }
224
225
226   /**
227    *
228    * decide if enable cacche?
229    * the sub-class can unable cache by overrid this method
230    * @param enable
231    */

232   protected boolean isEnableCache() {
233     return true;
234   }
235
236   private ModelIF fetchModel(HttpServletRequest JavaDoc request, Object JavaDoc dataKey) throws
237       Exception JavaDoc {
238     if (dataKey == null) return null;
239     ModelIF model = null;
240     try {
241       model = findModelIFByKey(request, dataKey);
242       if (model == null){
243           Debug.logWarning("[JdonFramework] the model is null for the primary key=" + dataKey+" and data type=" + dataKey.getClass().getName()+ ", reasons:", module);
244           Debug.logWarning("[JdonFramework] 1. maybe the data in database wae deleted ", module);
245           Debug.logWarning("[JdonFramework] 2. a error occured in the findModelByKey mehtod of the class "+ this.getClass().getName(), module);
246       }
247     } catch (Exception JavaDoc ex) {
248       Debug.logError(ex, module);
249     }
250     return model;
251   }
252
253   protected ModelIF setupOneModelIF(HttpServletRequest JavaDoc request) {
254       return setupOneModel(request);
255    }
256   
257   protected Model setupOneModel(HttpServletRequest JavaDoc request){
258       return null;
259   }
260
261
262   /**
263    * get a PageIterator from the service
264    *
265    * @param request HttpServletRequest
266    * @param start int
267    * @param count int
268    * @return PageIterator
269    */

270   public abstract PageIterator getPageIterator(HttpServletRequest JavaDoc request,
271                                                int start,
272                                                int count);
273
274   /**
275    * obtain a Model instance from service layer
276    *
277    * @param request HttpServletRequest
278    * @param key Object the primary key of the Model
279    * @return ModelIF return the Model object from service layer
280    */

281   public ModelIF findModelIFByKey(HttpServletRequest JavaDoc request, Object JavaDoc key){
282       return findModelByKey(request, key);
283   }
284      
285   /**
286    * for old version
287    */

288   public Model findModelByKey(HttpServletRequest JavaDoc request, Object JavaDoc key){
289       return null;
290   }
291   
292   
293
294
295 }
296
Popular Tags