1 16 package com.blandware.atleap.webapp.taglib.core.grid; 17 18 import com.blandware.atleap.common.util.PartialCollection; 19 import com.blandware.atleap.common.util.QueryInfo; 20 import com.blandware.atleap.webapp.taglib.core.grid.util.EntityFieldComparator; 21 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid; 22 import com.blandware.atleap.webapp.taglib.core.grid.util.SortField; 23 import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.springframework.context.ApplicationContext; 27 import org.springframework.web.context.support.WebApplicationContextUtils; 28 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.JspTagException ; 31 import javax.servlet.jsp.PageContext ; 32 import javax.servlet.jsp.tagext.JspFragment ; 33 import javax.servlet.jsp.tagext.SimpleTagSupport ; 34 import java.io.IOException ; 35 import java.lang.reflect.Method ; 36 import java.util.*; 37 38 121 public class RowsIteratorTag extends SimpleTagSupport { 122 123 protected transient final Log log = LogFactory.getLog(RowsIteratorTag.class); 124 protected transient ApplicationContext applicationCtx = null; 125 126 129 public static final String IS_ODD_ROW_KEY = "com.blandware.atleap.webapp.taglib.core.grid.IS_ODD_ROW"; 130 131 134 protected String beanId; 135 136 144 protected String method; 145 146 150 protected Object collection; 151 152 155 protected String tagId; 156 157 160 protected String var; 161 162 165 protected String oddClass = new String (); 166 167 170 protected String evenClass = new String (); 171 172 180 protected Boolean firstRowEven = Boolean.TRUE; 181 182 185 protected String cssVar; 186 187 191 protected String indexId; 192 193 197 private Iterator rowsIterator = null; 198 199 202 private boolean isOddRow = true; 203 204 205 208 private int loopCount = 0; 209 210 220 public String getBeanId() { 221 return beanId; 222 } 223 224 230 public void setBeanId(String beanId) { 231 this.beanId = beanId; 232 } 233 234 244 public String getMethod() { 245 return method; 246 } 247 248 254 public void setMethod(String method) { 255 this.method = method; 256 } 257 258 268 public Object getCollection() { 269 return collection; 270 } 271 272 278 public void setCollection(Object collection) { 279 this.collection = collection; 280 } 281 282 292 public String getTagId() { 293 return tagId; 294 } 295 296 302 public void setTagId(String tagId) { 303 this.tagId = tagId; 304 } 305 306 316 public String getVar() { 317 return var; 318 } 319 320 326 public void setVar(String var) { 327 this.var = var; 328 } 329 330 340 public String getOddClass() { 341 return oddClass; 342 } 343 344 350 public void setOddClass(String oddClass) { 351 this.oddClass = oddClass; 352 } 353 354 364 public String getEvenClass() { 365 return evenClass; 366 } 367 368 374 public void setEvenClass(String evenClass) { 375 this.evenClass = evenClass; 376 } 377 378 388 public Boolean getFirstRowEven() { 389 return firstRowEven; 390 } 391 392 398 public void setFirstRowEven(Boolean firstRowEven) { 399 this.firstRowEven = firstRowEven; 400 } 401 402 412 public String getCssVar() { 413 return cssVar; 414 } 415 416 422 public void setCssVar(String cssVar) { 423 this.cssVar = cssVar; 424 } 425 426 436 public String getIndexId() { 437 return indexId; 438 } 439 440 446 public void setIndexId(String indexId) { 447 this.indexId = indexId; 448 } 449 450 456 public void doTag() throws JspException , IOException { 457 458 PageContext pageContext = (PageContext ) getJspContext(); 459 460 GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class); 461 462 if ( parentGridTag == null ) { 463 JspTagException e = new JspTagException ("Parent tag is invalid! This tag is only valid when nested within 'grid' tag"); 464 throw e; 465 } 466 467 if ( (beanId == null || method == null) && collection == null ) { 469 String errorMessage = "Either collection or beanId with method attributes must be specified"; 471 JspTagException e = new JspTagException (errorMessage); 472 throw e; 473 } 474 475 Integer pageSize = parentGridTag.getPageSize(); 477 478 Grid grid = parentGridTag.getGrid(); 479 480 if ( cssVar == null && ((oddClass != null && oddClass.trim().length() != 0) || (evenClass != null && evenClass.trim().length() != 0)) ) { 481 cssVar = TaglibConstants.ROW_CLASS_ATTR; 482 } 483 484 if ( indexId == null ) { 485 indexId = TaglibConstants.INDEX_ID_ATTR; 486 } 487 488 Integer pageNumber = parentGridTag.getCurrentPageNumber(); 489 490 int gridTotal = parentGridTag.getGrid().getTotal().intValue(); 492 int pn = pageNumber.intValue(); 493 int ps = pageSize.intValue(); 494 495 int displayedCount = ps * (pn - 1); 497 498 Integer offset = new Integer (displayedCount > gridTotal ? displayedCount - gridTotal : 0); 499 Integer limit = new Integer (displayedCount > gridTotal ? ps : displayedCount + ps - gridTotal); 500 501 if ( limit.intValue() < 0 ) { 502 limit = new Integer (0); 503 } 504 505 if ( beanId == null || method == null ) { 506 Collection c = null; 507 if ( collection instanceof Collection ) { 509 c = (Collection) collection; 510 } else if ( collection instanceof Object [] ) { 511 c = Arrays.asList((Object []) collection); 512 } else if ( collection instanceof Map ) { 513 c = ((Map) collection).entrySet(); 514 } else { 515 JspTagException e = new JspTagException ("Only instances of java.util.Collection, java.lang.Object[] and java.util.Map are supported in collection attribute of RowsIteratorTag"); 516 throw e; 517 } 518 519 if ( !grid.getSortFields().isEmpty() ) { 521 SortField sortField = grid.getSortFieldByIndex(0, tagId); 522 if ( sortField != null ) { 523 String property = sortField.getProperty(); 524 EntityFieldComparator comparator = new EntityFieldComparator(property); 525 if ( !sortField.isOrderAscending() ) { 526 comparator.reverseOrder(); 527 } 528 if ( !(collection instanceof Map) ) { 529 List list = new ArrayList(c); 530 Collections.sort(list, comparator); 531 c = list; 532 } else { 533 SortedMap sortedMap = new TreeMap(comparator); 534 sortedMap.putAll((Map) collection); 535 c = new LinkedHashSet(sortedMap.entrySet()); 536 } 537 } 538 } 539 540 List subList = new ArrayList(); 541 542 if ( c instanceof PartialCollection ) { 543 PartialCollection partialCollection = (PartialCollection) c; 544 grid.addTotal(partialCollection.getTotal()); 545 subList = partialCollection.asList(); 546 } else { 547 grid.addTotal(c.size()); 548 List list = new LinkedList(c); 549 550 if ( gridTotal + list.size() <= displayedCount ) { 551 subList = new ArrayList(); 552 } else { 553 int fromIndex = offset.intValue(); 554 int toIndex = offset.intValue() + limit.intValue() < list.size() ? offset.intValue() + limit.intValue() : list.size(); 555 556 if ( fromIndex < list.size() ) { 557 subList = list.subList(fromIndex, toIndex); 558 } 559 } 560 } 561 rowsIterator = subList.iterator(); 562 563 } else { 564 565 PartialCollection rows = new PartialCollection(); 566 567 569 if ( applicationCtx == null ) { 570 applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()); 571 } 572 573 Object bean = applicationCtx.getBean(beanId); 574 if ( bean == null ) { 575 String errorMessage = "No bean with ID=" + beanId + " could be found in application context"; 576 if ( log.isErrorEnabled() ) { 577 log.error(errorMessage); 578 } 579 JspTagException e = new JspTagException (errorMessage); 580 throw e; 581 } 582 if ( log.isDebugEnabled() ) { 583 log.debug("Bean with ID=" + beanId + " has been succefully found in application context"); 584 } 585 586 Method method = null; 587 try { 588 method = bean.getClass().getMethod(this.method, new Class []{QueryInfo.class}); 589 } catch ( NoSuchMethodException e ) { 590 if ( log.isErrorEnabled() ) { 591 log.error(e.getMessage()); 592 } 593 throw new JspTagException (e); 594 } 595 596 if ( log.isDebugEnabled() ) { 597 log.debug("Method '" + this.method + "' was found on bean with ID=" + beanId); 598 } 599 600 601 603 try { 604 605 String whereClause = grid.getWhereClause(tagId); 606 String orderByClause = grid.getOrderByClause(tagId); 607 QueryInfo queryInfo = new QueryInfo(whereClause, orderByClause, limit, offset); 608 queryInfo.setQueryParameters(grid.getQueryParameters()); 609 rows = (PartialCollection) method.invoke(bean, new Object []{queryInfo}); 610 611 parentGridTag.getGrid().addTotal(rows.getTotal().intValue()); 612 613 618 } catch ( Exception e ) { 619 throw new JspTagException (e); 620 } 621 rowsIterator = rows.iterator(); 622 } 623 624 if ( firstRowEven == null ) { 625 firstRowEven = Boolean.TRUE; 626 } 627 628 if ( pageContext.getAttribute(IS_ODD_ROW_KEY) != null ) { 629 isOddRow = ((Boolean ) pageContext.getAttribute(IS_ODD_ROW_KEY)).booleanValue(); 630 } else { 631 isOddRow = !firstRowEven.booleanValue(); 633 } 634 635 while ( rowsIterator.hasNext() ) { 636 Object element = rowsIterator.next(); 638 if ( element == null ) { 639 pageContext.removeAttribute(var); 640 } else { 641 pageContext.setAttribute(var, element); 642 } 643 644 if ( cssVar != null ) { 645 String rowClass = isOddRow ? oddClass : evenClass; 646 isOddRow = !isOddRow; 647 pageContext.setAttribute(cssVar, rowClass); 648 } 649 650 pageContext.setAttribute(indexId, new Integer (loopCount++)); 651 652 JspFragment body = getJspBody(); 654 if ( body != null ) { 655 body.invoke(null); 656 } 657 } 658 659 if ( loopCount > 0 ) { 660 pageContext.setAttribute(IS_ODD_ROW_KEY, Boolean.valueOf(isOddRow)); 662 } 663 664 } 665 } 666 | Popular Tags |