1 17 package org.alfresco.web.ui.common.component.data; 18 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIComponentBase; 25 import javax.faces.context.FacesContext; 26 import javax.faces.el.ValueBinding; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.web.app.Application; 30 import org.alfresco.web.bean.repository.Repository; 31 import org.alfresco.web.config.ViewsConfigElement; 32 import org.alfresco.web.data.IDataContainer; 33 import org.alfresco.web.ui.common.renderer.data.IRichListRenderer; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 40 public class UIRichList extends UIComponentBase implements IDataContainer 41 { 42 45 48 public UIRichList() 49 { 50 setRendererType("org.alfresco.faces.RichListRenderer"); 51 52 ViewsConfigElement viewsConfig = (ViewsConfigElement)Application.getConfigService( 54 FacesContext.getCurrentInstance()).getConfig("Views"). 55 getConfigElement(ViewsConfigElement.CONFIG_ELEMENT_ID); 56 List <String > views = viewsConfig.getViews(); 57 58 for (String view : views) 60 { 61 try 62 { 63 Class clazz = Class.forName(view); 64 IRichListRenderer renderer = (IRichListRenderer)clazz.newInstance(); 65 UIRichList.viewRenderers.put(renderer.getViewModeID(), renderer); 66 67 if (logger.isDebugEnabled()) 68 logger.debug("Added view '" + renderer.getViewModeID() + 69 "' to UIRichList"); 70 } 71 catch (Exception e) 72 { 73 if (logger.isWarnEnabled()) 74 { 75 logger.warn("Failed to create renderer: " + view, e); 76 } 77 } 78 } 79 } 80 81 82 85 88 public String getFamily() 89 { 90 return "org.alfresco.faces.Data"; 91 } 92 93 96 public void restoreState(FacesContext context, Object state) 97 { 98 Object values[] = (Object [])state; 99 super.restoreState(context, values[0]); 101 this.currentPage = ((Integer )values[1]).intValue(); 102 this.sortColumn = (String )values[2]; 103 this.sortDescending = ((Boolean )values[3]).booleanValue(); 104 this.value = values[4]; this.dataModel = (IGridDataModel)values[5]; this.viewMode = (String )values[6]; 107 this.pageSize = ((Integer )values[7]).intValue(); 108 this.initialSortColumn = (String )values[8]; 109 this.initialSortDescending = ((Boolean )values[9]).booleanValue(); 110 } 111 112 115 public Object saveState(FacesContext context) 116 { 117 Object values[] = new Object [10]; 118 values[0] = super.saveState(context); 120 values[1] = Integer.valueOf(this.currentPage); 121 values[2] = this.sortColumn; 122 values[3] = (this.sortDescending ? Boolean.TRUE : Boolean.FALSE); 123 values[4] = this.value; 124 values[5] = this.dataModel; 125 values[6] = this.viewMode; 126 values[7] = Integer.valueOf(this.pageSize); 127 values[8] = this.initialSortColumn; 128 values[9] = (this.initialSortDescending ? Boolean.TRUE : Boolean.FALSE); 129 130 return (values); 131 } 132 133 138 public Object getValue() 139 { 140 if (this.value == null) 141 { 142 ValueBinding vb = getValueBinding("value"); 143 if (vb != null) 144 { 145 this.value = vb.getValue(getFacesContext()); 146 } 147 } 148 return this.value; 149 } 150 151 156 public void setValue(Object value) 157 { 158 this.dataModel = null; 159 this.value = value; 160 } 161 162 165 public void clearSort() 166 { 167 this.sortColumn = null; 168 this.sortDescending = true; 169 this.initialSortColumn = null; 170 this.initialSortDescending = false; 171 } 172 173 178 public String getViewMode() 179 { 180 ValueBinding vb = getValueBinding("viewMode"); 181 if (vb != null) 182 { 183 this.viewMode = (String )vb.getValue(getFacesContext()); 184 } 185 186 return this.viewMode; 187 } 188 189 194 public void setViewMode(String viewMode) 195 { 196 this.viewMode = viewMode; 197 } 198 199 204 public UIComponent getEmptyMessage() 205 { 206 return getFacet("empty"); 207 } 208 209 210 213 218 public String getCurrentSortColumn() 219 { 220 return this.sortColumn; 221 } 222 223 226 public boolean isCurrentSortDescending() 227 { 228 return this.sortDescending; 229 } 230 231 234 public String getInitialSortColumn() 235 { 236 return this.initialSortColumn; 237 } 238 239 242 public void setInitialSortColumn(String initialSortColumn) 243 { 244 this.initialSortColumn = initialSortColumn; 245 } 246 247 250 public boolean isInitialSortDescending() 251 { 252 return this.initialSortDescending; 253 } 254 255 258 public void setInitialSortDescending(boolean initialSortDescending) 259 { 260 this.initialSortDescending = initialSortDescending; 261 } 262 263 266 public int getPageSize() 267 { 268 ValueBinding vb = getValueBinding("pageSize"); 269 if (vb != null) 270 { 271 int pageSize = ((Integer )vb.getValue(getFacesContext())).intValue(); 272 if (pageSize != this.pageSize) 273 { 274 setPageSize(pageSize); 276 } 277 } 278 279 return this.pageSize; 280 } 281 282 287 public void setPageSize(int val) 288 { 289 if (val >= -1) 290 { 291 this.pageSize = val; 292 setCurrentPage(0); 293 } 294 } 295 296 299 public int getPageCount() 300 { 301 return this.pageCount; 302 } 303 304 309 public int getCurrentPage() 310 { 311 return this.currentPage; 312 } 313 314 317 public void setCurrentPage(int index) 318 { 319 this.currentPage = index; 320 } 321 322 327 public boolean isDataAvailable() 328 { 329 return this.rowIndex < this.maxRowIndex; 330 } 331 332 337 public Object nextRow() 338 { 339 Object rowData = getDataModel().getRow(this.rowIndex + 1); 341 342 String var = (String )getAttributes().get("var"); 345 if (var != null) 346 { 347 Map requestMap = getFacesContext().getExternalContext().getRequestMap(); 348 if (isDataAvailable() == true) 349 { 350 requestMap.put(var, rowData); 351 } 352 else 353 { 354 requestMap.remove(var); 355 } 356 } 357 358 this.rowIndex++; 359 360 return rowData; 361 } 362 363 370 public void sort(String column, boolean descending, String mode) 371 { 372 this.sortColumn = column; 373 this.sortDescending = descending; 374 375 UserTransaction tx = null; 378 try 379 { 380 if (getDataModel().size() > 64) 381 { 382 FacesContext context = FacesContext.getCurrentInstance(); 383 tx = Repository.getUserTransaction(context, true); 384 tx.begin(); 385 } 386 387 getDataModel().sort(column, descending, mode); 388 389 if (tx != null) 391 { 392 tx.commit(); 393 } 394 } 395 catch (Throwable err) 396 { 397 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 398 } 399 } 400 401 402 405 408 public void bind() 409 { 410 int rowCount = getDataModel().size(); 411 int pageSize = getPageSize(); 413 if (pageSize != -1) 414 { 415 this.rowIndex = (this.currentPage * pageSize) - 1; 417 418 this.pageCount = (rowCount / this.pageSize) + 1; 420 if (rowCount % pageSize == 0 && this.pageCount != 1) 421 { 422 this.pageCount--; 423 } 424 425 this.maxRowIndex = this.rowIndex + pageSize; 427 if (this.maxRowIndex >= rowCount) 428 { 429 this.maxRowIndex = rowCount - 1; 430 } 431 } 432 else 434 { 435 this.rowIndex = -1; 436 this.pageCount = 1; 437 this.maxRowIndex = (rowCount - 1); 438 } 439 if (logger.isDebugEnabled()) 440 logger.debug("Bound datasource: PageSize: " + pageSize + "; CurrentPage: " + this.currentPage + "; RowIndex: " + this.rowIndex + "; MaxRowIndex: " + this.maxRowIndex + "; RowCount: " + rowCount); 441 } 442 443 446 public IRichListRenderer getViewRenderer() 447 { 448 IRichListRenderer renderer = null; 450 if (getViewMode() != null) 451 { 452 renderer = (IRichListRenderer)UIRichList.viewRenderers.get(getViewMode()); 453 } 454 return renderer; 455 } 456 457 462 private IGridDataModel getDataModel() 463 { 464 if (this.dataModel == null) 465 { 466 Object val = getValue(); 468 if (val instanceof List ) 469 { 470 this.dataModel = new GridListDataModel((List )val); 471 } 472 else if ( (java.lang.Object [].class).isAssignableFrom(val.getClass()) ) 473 { 474 this.dataModel = new GridArrayDataModel((Object [])val); 475 } 476 else 477 { 478 throw new IllegalStateException ("UIRichList 'value' attribute binding should specify data model of a supported type!"); 479 } 480 481 if (this.sortColumn == null) 483 { 484 String initialSortColumn = getInitialSortColumn(); 485 if (initialSortColumn != null && initialSortColumn.length() != 0) 486 { 487 boolean descending = isInitialSortDescending(); 488 489 this.sortColumn = initialSortColumn; 491 this.sortDescending = descending; 492 } 493 } 494 if (this.sortColumn != null) 495 { 496 this.dataModel.sort(this.sortColumn, this.sortDescending, IDataContainer.SORT_CASEINSENSITIVE); 498 } 499 500 this.currentPage = 0; 502 } 503 504 return this.dataModel; 505 } 506 507 508 511 512 private final static Map <String , IRichListRenderer> viewRenderers = new HashMap <String , IRichListRenderer>(5); 513 514 private int currentPage = 0; 516 private String sortColumn = null; 517 private boolean sortDescending = true; 518 private Object value = null; 519 private IGridDataModel dataModel = null; 520 private String viewMode = null; 521 private int pageSize = -1; 522 private String initialSortColumn = null; 523 private boolean initialSortDescending = false; 524 525 private int rowIndex = -1; 527 private int maxRowIndex = -1; 528 private int pageCount = 1; 529 530 private static Log logger = LogFactory.getLog(IDataContainer.class); 531 } 532 | Popular Tags |