1 24 package org.ofbiz.widget.screen; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 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 ; 49 50 51 58 public class IterateSectionWidget extends ModelScreenWidget { 59 public static final String module = IterateSectionWidget.class.getName(); 60 61 protected ModelScreenWidget childWidget; 62 protected List 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 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 (); 90 List childElementList = UtilXml.childElementList(iterateSectionElement); 91 Iterator childElementIter = childElementList.iterator(); 92 while (childElementIter.hasNext()) { 93 Element sectionElement = (Element ) childElementIter.next(); 94 ModelScreenWidget.Section section = new ModelScreenWidget.Section(modelScreen, sectionElement); 95 sectionList.add(section); 96 } 97 } 98 99 public void renderWidgetString(Writer writer, Map 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 String entryName = this.entryNameExdr.expandString(context); 111 String keyName = this.keyNameExdr.expandString(context); 112 Object obj = listNameExdr.get(context); 113 if (obj == null) { 114 Debug.logError("No object found for listName:" + listNameExdr.toString(), module); 115 return; 116 } 117 List theList = null; 118 if (obj instanceof Map ) { 119 Set entrySet = ((Map )obj).entrySet(); 120 Object [] a = entrySet.toArray(); 121 theList = Arrays.asList(a); 122 isEntrySet = true; 123 } else if (obj instanceof List ) { 124 theList = (List )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 iter = theList.iterator(); 132 int itemIndex = -1; 133 while (iter.hasNext()) { 134 itemIndex++; 135 if (itemIndex >= highIndex) { 136 break; 137 } 138 Object item = iter.next(); 139 if (itemIndex < lowIndex) { 140 continue; 141 } 142 if (isEntrySet) { 143 contextMs.put(entryName, ((Map )item).get("value")); 144 contextMs.put(keyName, ((Map )item).get("key")); 145 } else { 146 contextMs.put(entryName, item); 147 } 148 contextMs.put("itemIndex", new Integer (itemIndex)); 149 150 rowCount++; 151 Iterator 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 e) { 165 Debug.logError(e, module); 166 throw new RuntimeException (e.getMessage()); 167 } 168 } 169 contextMs.pop(); 170 171 } 172 175 public String getPaginateTarget(Map 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 val) { 196 try { 197 Integer sz = new Integer (val); 198 viewSize = sz.intValue(); 199 } catch(NumberFormatException 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 context, List items) { 244 listSize = items.size(); 245 246 if (paginate) { 247 try { 248 Map params = (Map )context.get("parameters"); 249 String viewIndexString = (String ) params.get("VIEW_INDEX"); 250 viewIndex = Integer.parseInt(viewIndexString); 251 } catch (Exception e) { 252 try { 253 viewIndex = ((Integer ) context.get("viewIndex")).intValue(); 254 } catch (Exception e2) { 255 viewIndex = 0; 256 } 257 } 258 context.put("viewIndex", new Integer (this.viewIndex)); 259 260 try { 261 viewSize = ((Integer ) context.get("viewSize")).intValue(); 262 } catch (Exception e) { 263 } 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 writer, Map context) throws IOException { 279 String 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 ) context.get("viewIndex")).intValue(); 292 } catch (Exception e) { 293 viewIndex = 0; 294 } 295 296 int viewSize = -1; 297 try { 298 viewSize = ((Integer ) context.get("viewSize")).intValue(); 299 } catch (Exception e) { 300 viewSize = this.getViewSize(); 301 } 302 303 int listSize = -1; 304 try { 305 listSize = this.getListSize(); 306 } catch (Exception e) { 307 listSize = -1; 308 } 309 310 325 326 int lowIndex = viewIndex * viewSize; 327 int highIndex = (viewIndex + 1) * viewSize; 328 int actualPageSize = this.getActualPageSize(); 329 if (actualPageSize >= listSize && listSize > 0) { 331 return; 332 } 333 334 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 335 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 336 337 String str = (String ) context.get("queryString"); 338 String queryString = UtilHttp.stripViewParamsFromQueryString(str); 339 ServletContext ctx = (ServletContext ) 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 linkText = targetService; 349 if (linkText.indexOf("?") < 0) linkText += "?"; 350 else linkText += "&"; 351 linkText += "VIEW_SIZE=" + viewSize + "&VIEW_INDEX=" + (viewIndex - 1) + "\""; 353 354 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 linkText = targetService; 365 if (linkText.indexOf("?") < 0) linkText += "?"; 366 else linkText += "&"; 367 linkText += "VIEW_SIZE=" + viewSize + "&VIEW_INDEX=" + (viewIndex + 1) + "\""; 368 369 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 rawString() { 382 return "<iterate-section/>"; 384 } 385 } 386 387 | Popular Tags |