KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > IterateSectionWidget


1 /*
2  * $Id: IterateSectionWidget.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.screen;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.GeneralException;
41 import org.ofbiz.base.util.UtilHttp;
42 import org.ofbiz.base.util.UtilValidate;
43 import org.ofbiz.base.util.UtilXml;
44 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
45 import org.ofbiz.base.util.collections.MapStack;
46 import org.ofbiz.base.util.string.FlexibleStringExpander;
47 import org.ofbiz.webapp.control.RequestHandler;
48 import org.w3c.dom.Element JavaDoc;
49
50
51 /**
52  * Widget Library - Screen model HTML class
53  *
54  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
55  * @version $Rev: 5462 $
56  * @since 3.1
57  */

58 public class IterateSectionWidget extends ModelScreenWidget {
59     public static final String JavaDoc module = IterateSectionWidget.class.getName();
60     
61     protected ModelScreenWidget childWidget;
62     protected List JavaDoc sectionList;
63     protected FlexibleMapAccessor listNameExdr;
64     protected FlexibleStringExpander entryNameExdr;
65     protected FlexibleStringExpander keyNameExdr;
66     protected FlexibleStringExpander paginateTarget;
67     protected boolean paginate = true;
68     
69     public static int DEFAULT_PAGE_SIZE = 100;
70     protected int viewIndex = 0;
71     protected int viewSize = DEFAULT_PAGE_SIZE;
72     protected int lowIndex = -1;
73     protected int highIndex = -1;
74     protected int listSize = 0;
75     protected int actualPageSize = 0;
76     
77
78     public IterateSectionWidget(ModelScreen modelScreen, Element JavaDoc iterateSectionElement) {
79         super(modelScreen, iterateSectionElement);
80         listNameExdr = new FlexibleMapAccessor(iterateSectionElement.getAttribute("list-name"));
81         entryNameExdr = new FlexibleStringExpander(iterateSectionElement.getAttribute("entry-name"));
82         keyNameExdr = new FlexibleStringExpander(iterateSectionElement.getAttribute("key-name"));
83         if (this.paginateTarget == null || iterateSectionElement.hasAttribute("paginate-target"))
84             this.paginateTarget = new FlexibleStringExpander(iterateSectionElement.getAttribute("paginate-target"));
85          
86         paginate = "true".equals(iterateSectionElement.getAttribute("paginate"));
87         if (iterateSectionElement.hasAttribute("view-size"))
88             setViewSize(iterateSectionElement.getAttribute("view-size"));
89         sectionList = new ArrayList JavaDoc();
90         List JavaDoc childElementList = UtilXml.childElementList(iterateSectionElement);
91         Iterator JavaDoc childElementIter = childElementList.iterator();
92         while (childElementIter.hasNext()) {
93             Element JavaDoc sectionElement = (Element JavaDoc) childElementIter.next();
94             ModelScreenWidget.Section section = new ModelScreenWidget.Section(modelScreen, sectionElement);
95             sectionList.add(section);
96         }
97     }
98
99     public void renderWidgetString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) throws GeneralException {
100     
101         boolean isEntrySet = false;
102             if (!(context instanceof MapStack)) {
103                 context = MapStack.create(context);
104             }
105             
106             MapStack contextMs = (MapStack) context;
107             contextMs.push();
108
109             // create a standAloneStack, basically a "save point" for this SectionsRenderer, and make a new "screens" object just for it so it is isolated and doesn't follow the stack down
110
String JavaDoc entryName = this.entryNameExdr.expandString(context);
111         String JavaDoc keyName = this.keyNameExdr.expandString(context);
112         Object JavaDoc obj = listNameExdr.get(context);
113         if (obj == null) {
114             Debug.logError("No object found for listName:" + listNameExdr.toString(), module);
115             return;
116         }
117         List JavaDoc theList = null;
118         if (obj instanceof Map JavaDoc ) {
119             Set JavaDoc entrySet = ((Map JavaDoc)obj).entrySet();
120             Object JavaDoc [] a = entrySet.toArray();
121             theList = Arrays.asList(a);
122             isEntrySet = true;
123         } else if (obj instanceof List JavaDoc ) {
124             theList = (List JavaDoc)obj;
125         } else {
126             Debug.logError("Object not list or map type", module);
127             return;
128         }
129         getListLimits(context, theList);
130         int rowCount = 0;
131         Iterator JavaDoc iter = theList.iterator();
132         int itemIndex = -1;
133         while (iter.hasNext()) {
134             itemIndex++;
135             if (itemIndex >= highIndex) {
136                 break;
137             }
138             Object JavaDoc item = iter.next();
139             if (itemIndex < lowIndex) {
140                 continue;
141             }
142             if (isEntrySet) {
143                 contextMs.put(entryName, ((Map JavaDoc)item).get("value"));
144                 contextMs.put(keyName, ((Map JavaDoc)item).get("key"));
145             } else {
146                 contextMs.put(entryName, item);
147             }
148             contextMs.put("itemIndex", new Integer JavaDoc(itemIndex));
149             
150             rowCount++;
151             Iterator JavaDoc sectionIter = this.sectionList.iterator();
152             while (sectionIter.hasNext()) {
153                 ModelScreenWidget.Section section = (ModelScreenWidget.Section)sectionIter.next();
154                 section.renderWidgetString(writer, contextMs, screenStringRenderer);
155             }
156         }
157         if ((itemIndex + 1) < highIndex) {
158             setHighIndex(itemIndex + 1);
159         }
160         setActualPageSize(highIndex - lowIndex);
161         if (paginate) {
162             try {
163                 renderNextPrev(writer, context);
164             } catch(IOException JavaDoc e) {
165                 Debug.logError(e, module);
166                 throw new RuntimeException JavaDoc(e.getMessage());
167             }
168         }
169         contextMs.pop();
170
171     }
172     /*
173      * @return
174      */

175     public String JavaDoc getPaginateTarget(Map JavaDoc context) {
176         return this.paginateTarget.expandString(context);
177     }
178     
179     public boolean getPaginate() {
180         return this.paginate;
181     }
182     
183     public void setPaginate(boolean val) {
184         paginate = val;
185     }
186     
187     public void setViewIndex(int val) {
188         viewIndex = val;
189     }
190
191     public void setViewSize(int val) {
192         viewSize = val;
193     }
194
195     public void setViewSize(String JavaDoc val) {
196         try {
197             Integer JavaDoc sz = new Integer JavaDoc(val);
198             viewSize = sz.intValue();
199         } catch(NumberFormatException JavaDoc e) {
200             viewSize = DEFAULT_PAGE_SIZE;
201         }
202     }
203
204     public void setListSize(int val) {
205         listSize = val;
206     }
207
208     public void setLowIndex(int val) {
209         lowIndex = val;
210     }
211
212     public void setHighIndex(int val) {
213         highIndex = val;
214     }
215     public void setActualPageSize(int val) {
216         actualPageSize = val;
217     }
218
219     public int getViewIndex() {
220         return viewIndex;
221     }
222
223     public int getViewSize() {
224         return viewSize;
225     }
226
227     public int getListSize() {
228         return listSize;
229     }
230
231     public int getLowIndex() {
232         return lowIndex;
233     }
234
235     public int getHighIndex() {
236         return highIndex;
237     }
238     
239     public int getActualPageSize() {
240         return actualPageSize;
241     }
242     
243     public void getListLimits(Map JavaDoc context, List JavaDoc items) {
244         listSize = items.size();
245         
246        if (paginate) {
247             try {
248                 Map JavaDoc params = (Map JavaDoc)context.get("parameters");
249                 String JavaDoc viewIndexString = (String JavaDoc) params.get("VIEW_INDEX");
250                 viewIndex = Integer.parseInt(viewIndexString);
251             } catch (Exception JavaDoc e) {
252                 try {
253                     viewIndex = ((Integer JavaDoc) context.get("viewIndex")).intValue();
254                 } catch (Exception JavaDoc e2) {
255                     viewIndex = 0;
256                 }
257             }
258             context.put("viewIndex", new Integer JavaDoc(this.viewIndex));
259     
260             try {
261                 viewSize = ((Integer JavaDoc) context.get("viewSize")).intValue();
262             } catch (Exception JavaDoc e) {
263                 //viewSize = DEFAULT_PAGE_SIZE;
264
}
265             lowIndex = viewIndex * viewSize;
266             highIndex = (viewIndex + 1) * viewSize;
267     
268     
269         } else {
270             viewIndex = 0;
271             viewSize = DEFAULT_PAGE_SIZE;
272             lowIndex = 0;
273             highIndex = DEFAULT_PAGE_SIZE;
274         }
275     }
276     
277
278     public void renderNextPrev(Writer JavaDoc writer, Map JavaDoc context) throws IOException JavaDoc {
279         String JavaDoc targetService = this.getPaginateTarget(context);
280         if (targetService == null) {
281             targetService = "${targetService}";
282         }
283         
284         if (UtilValidate.isEmpty(targetService)) {
285             Debug.logWarning("TargetService is empty.", module);
286             return;
287         }
288
289         int viewIndex = -1;
290         try {
291             viewIndex = ((Integer JavaDoc) context.get("viewIndex")).intValue();
292         } catch (Exception JavaDoc e) {
293             viewIndex = 0;
294         }
295
296         int viewSize = -1;
297         try {
298             viewSize = ((Integer JavaDoc) context.get("viewSize")).intValue();
299         } catch (Exception JavaDoc e) {
300             viewSize = this.getViewSize();
301         }
302
303         int listSize = -1;
304         try {
305             listSize = this.getListSize();
306         } catch (Exception JavaDoc e) {
307             listSize = -1;
308         }
309
310 /*
311         int highIndex = -1;
312         try {
313             highIndex = modelForm.getHighIndex();
314         } catch (Exception e) {
315             highIndex = 0;
316         }
317
318         int lowIndex = -1;
319         try {
320             lowIndex = modelForm.getLowIndex();
321         } catch (Exception e) {
322             lowIndex = 0;
323         }
324 */

325         
326         int lowIndex = viewIndex * viewSize;
327         int highIndex = (viewIndex + 1) * viewSize;
328         int actualPageSize = this.getActualPageSize();
329         // if this is all there seems to be (if listSize < 0, then size is unknown)
330
if (actualPageSize >= listSize && listSize > 0) {
331             return;
332         }
333
334         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) context.get("request");
335         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) context.get("response");
336
337         String JavaDoc str = (String JavaDoc) context.get("queryString");
338         String JavaDoc queryString = UtilHttp.stripViewParamsFromQueryString(str);
339         ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
340         RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
341
342         writer.write("<table border=\"0\" width=\"100%\" cellpadding=\"2\">\n");
343         writer.write(" <tr>\n");
344         writer.write(" <td align=\"right\">\n");
345         writer.write(" <b>\n");
346         if (viewIndex > 0) {
347             writer.write(" <a HREF=\"");
348             String JavaDoc linkText = targetService;
349             if (linkText.indexOf("?") < 0) linkText += "?";
350             else linkText += "&amp;";
351             //if (queryString != null && !queryString.equals("null")) linkText += queryString + "&";
352
linkText += "VIEW_SIZE=" + viewSize + "&amp;VIEW_INDEX=" + (viewIndex - 1) + "\"";
353
354             // make the link
355
writer.write(rh.makeLink(request, response, linkText, false, false, false));
356             writer.write(" class=\"buttontext\">[Previous]</a>\n");
357
358         }
359         if (listSize > 0) {
360             writer.write(" <span class=\"tabletext\">" + (lowIndex + 1) + " - " + (lowIndex + actualPageSize) + " of " + listSize + "</span> \n");
361         }
362         if (highIndex < listSize) {
363             writer.write(" <a HREF=\"");
364             String JavaDoc linkText = targetService;
365             if (linkText.indexOf("?") < 0) linkText += "?";
366             else linkText += "&amp;";
367             linkText += "VIEW_SIZE=" + viewSize + "&amp;VIEW_INDEX=" + (viewIndex + 1) + "\"";
368
369             // make the link
370
writer.write(rh.makeLink(request, response, linkText, false, false, false));
371             writer.write(" class=\"buttontext\">[Next]</a>\n");
372
373         }
374         writer.write(" </b>\n");
375         writer.write(" </td>\n");
376         writer.write(" </tr>\n");
377         writer.write("</table>\n");
378
379     }
380
381     public String JavaDoc rawString() {
382         // TODO: something more than the empty tag
383
return "<iterate-section/>";
384     }
385 }
386
387
Popular Tags