You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2011/04/06 21:16:01 UTC

WorkingWithLargeTables reverted to revision 11 on Myfaces Wiki

Dear wiki user,

You have subscribed to a wiki page "Myfaces Wiki" for change notification.

The page WorkingWithLargeTables has been reverted to revision 11 by JakobKorherr.
The comment on this change is: SPAM.
http://wiki.apache.org/myfaces/WorkingWithLargeTables?action=diff&rev1=12&rev2=13

--------------------------------------------------

- ÿþ**You have received the software as part of the Visual Studio 9.0 Beta Program. 
- 
- The terms and conditions of the Visual Studio 9.0, Beta 1 license agreement 
- 
- applies to your use of the software.** MICROSOFT PRE-RELEASE SOFTWARE LICENSE 
- 
- TERMS MICROSOFT VISUAL STUDIO 9.0 PROFESSIONAL, BETA 1 These license terms are 
- 
- an agreement between Microsoft Corporation (or based on where you live, one of 
- 
- its affiliates) and you. Please read them. They apply to the pre-release 
- 
- software named above, which includes the media on which you received it, if any. 
- 
- The terms also apply to any Microsoft "  updates, "  supplements, "  Internet-based 
- 
- services, and "  support services for this software, unless other terms accompany 
- 
- those items. If so, those terms apply. BY USING THE SOFTWARE, YOU ACCEPT THESE 
- 
- TERMS. IF YOU DO NOT ACCEPT THEM, DO NOT USE THE SOFTWARE. If you comply with 
- 
- these license terms, you have the rights below. 1. INSTALLATION AND USE RIGHTS. 
- 
- "  General. You may install and use one copy of the software on your premises to 
- 
- design, develop and test your programs for use with the software. "  Evaluation 
- 
- Only. The software is for evaluation purposes only. You may not distribute any 
- 
- program you develop with the software. "  Included Microsoft Programs. These 
- 
- license terms apply to all Microsoft programs included with the software. "  
- 
- Third Party Programs. The software contains third party programs. The license 
- 
- terms with those programs apply to your use of them. 2. INTERNET-BASED SERVICES. 
- 
- Microsoft provides Internet-based services with the software. It may change or 
- 
- cancel them at any time. 3. TIME-SENSITIVE SOFTWARE. The software will stop 
- 
- running on 15 March 2008. You will not receive any other notice. You may not be 
- 
- able to access data used with the software when it stops running. 4. PRE-RELEASE 
- 
- SOFTWARE. This software is a pre-release version. It may not work the way a 
- 
- final version of the software will. We may change it for the final, commercial 
- 
- version. We also may not release a commercial version. 5. FEEDBACK. If you give 
- 
- feedback about the software to Microsoft, you give to Microsoft, without charge, 
- 
- the right to use, share and commercialize your feedback in any way and for any 
- 
- purpose. You also give to third parties, without charge, any patent rights 
- 
- needed for their products, technologies and services to use or interface with 
- 
- any specific parts of a Microsoft software or service that includes the 
- 
- feedback. You will not give feedback that is subject to a license that requires 
- 
- Microsoft to license its software or documentation to third parties because we 
- 
- include your feedback in them. These rights survive this agreement. 6. SCOPE OF 
- 
- LICENSE. The software is licensed, not sold. This agreement only gives you some 
- 
- rights to use the software. Microsoft reserves all other rights. Unless 
- 
- applicable law gives you more rights despite this limitation, you may use the 
- 
- software only as expressly permitted in this agreement. In doing so, you must 
- 
- comply with any technical limitations in the software that only allow you to use 
- 
- it in certain ways. You may not "  disclose the results of any benchmark tests of 
- 
- the software to any third party without Microsoft s prior written approval; "  
- 
- work around any technical limitations in the software; "  reverse engineer, 
- 
- decompile or disassemble the software, except and only to the extent that 
- 
- applicable law expressly permits, despite this limitation; "  make more copies of 
- 
- the software than specified in this agreement or allowed by applicable law, 
- 
- despite this limitation; "  publish the software for others to copy; "  rent, 
- 
- lease or lend the software; "  transfer the software or this agreement to any 
- 
- third party; or "  use the software for commercial software hosting services. 7. 
- 
- EXPORT RESTRICTIONS. The software is subject to United States export laws and 
- 
- regulations. You must comply with all domestic and international export laws and 
- 
- regulations that apply to the software. These laws include restrictions on 
- 
- destinations, end users and end use. For additional information, see 
- 
- www.microsoft.com/exporting. 8. SUPPORT SERVICES. Because this software is 
-  as 
- 
- is,
-   we may not provide support services for it. 9. ENTIRE AGREEMENT. This 
- 
- agreement, and the terms for supplements, updates, Internet-based services and 
- 
- support services that you use, are the entire agreement for the software and 
- 
- support services. 10. APPLICABLE LAW. a. United States. If you acquired the 
- 
- software in the United States, Washington state law governs the interpretation 
- 
- of this agreement and applies to claims for breach of it, regardless of conflict 
- 
- of laws principles. The laws of the state where you live govern all other 
- 
- claims, including claims under state consumer protection laws, unfair 
- 
- competition laws, and in tort. b. Outside the United States. If you acquired the 
- 
- software in any other country, the laws of that country apply. 11. LEGAL EFFECT. 
- 
- This agreement describes certain legal rights. You may have other rights under 
- 
- the laws of your country. You may also have rights with respect to the party 
- 
- from whom you acquired the software. This agreement does not change your rights 
- 
- under the laws of your country if the laws of your country do not permit it to 
- 
- do so. 12. DISCLAIMER OF WARRANTY. THE SOFTWARE IS LICENSED 
-  AS-IS.
-   YOU BEAR 
- 
- THE RISK OF USING IT. MICROSOFT GIVES NO EXPRESS WARRANTIES, GUARANTEES OR 
- 
- CONDITIONS. YOU MAY HAVE ADDITIONAL CONSUMER RIGHTS UNDER YOUR LOCAL LAWS WHICH 
- 
- THIS AGREEMENT CANNOT CHANGE. TO THE EXTENT PERMITTED UNDER YOUR LOCAL LAWS, 
- 
- MICROSOFT EXCLUDES THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
- 
- PARTICULAR PURPOSE AND NON-INFRINGEMENT. 13. LIMITATION ON AND EXCLUSION OF 
- 
- REMEDIES AND DAMAGES. YOU CAN RECOVER FROM MICROSOFT AND ITS SUPPLIERS ONLY 
- 
- DIRECT DAMAGES UP TO U.S. $5.00. YOU CANNOT RECOVER ANY OTHER DAMAGES, INCLUDING 
- 
- CONSEQUENTIAL, LOST PROFITS, SPECIAL, INDIRECT OR INCIDENTAL DAMAGES. This 
- 
- limitation applies to "  anything related to the software, services, content 
- 
- (including code) on third party Internet sites, or third party programs; and "  
- 
- claims for breach of contract, breach of warranty, guarantee or condition, 
- 
- strict liability, negligence, or other tort to the extent permitted by 
- 
- applicable law. It also applies even if Microsoft knew or should have known 
- 
- about the possibility of the damages. The above limitation or exclusion may not 
- 
- apply to you because your country may not allow the exclusion or limitation of 
- 
- incidental, consequential or other damages. Please note: As this software is 
- 
- distributed in Quebec, Canada, some of the clauses in this agreement are 
- 
- provided below in French. Remarque : Ce logiciel étant distribué au Québec, 
- 
- Canada, certaines des clauses dans ce contrat sont fournies ci-dessous en 
- 
- français. EXONÉRATION DE GARANTIE. Le logiciel visé par une licence est offert « 
- 
- tel quel ». Toute utilisation de ce logiciel est à votre seule risque et péril. 
- 
- Microsoft n accorde aucune autre garantie expresse. Vous pouvez bénéficier de 
- 
- droits additionnels en vertu du droit local sur la protection des consommateurs, 
- 
- que ce contrat ne peut modifier. La ou elles sont permises par le droit locale, 
- 
- les garanties implicites de qualité marchande, d adéquation à un usage 
- 
- particulier et d absence de contrefaçon sont exclues. LIMITATION DES 
- 
- DOMMAGES-INTÉRÊTS ET EXCLUSION DE RESPONSABILITÉ POUR LES DOMMAGES. Vous pouvez 
- 
- obtenir de Microsoft et de ses fournisseurs une indemnisation en cas de dommages 
- 
- directs uniquement à hauteur de 5,00 $ US. Vous ne pouvez prétendre à aucune 
- 
- indemnisation pour les autres dommages, y compris les dommages spéciaux, 
- 
- indirects ou accessoires et pertes de bénéfices. Cette limitation concerne : "  
- 
- tout ce qui est relié au logiciel, aux services ou au contenu (y compris le 
- 
- code) figurant sur des sites Internet tiers ou dans des programmes tiers ; et "  
- 
- les réclamations au titre de violation de contrat ou de garantie, ou au titre de 
- 
- responsabilité stricte, de négligence ou d une autre faute dans la limite 
- 
- autorisée par la loi en vigueur. Elle s applique également, même si Microsoft 
- 
- connaissait ou devrait connaître l éventualité d un tel dommage. Si votre pays 
- 
- n autorise pas l exclusion ou la limitation de responsabilité pour les dommages 
- 
- indirects, accessoires ou de quelque nature que ce soit, il se peut que la 
- 
- limitation ou l exclusion ci-dessus ne s appliquera pas à votre égard. EFFET 
- 
- JURIDIQUE. Le présent contrat décrit certains droits juridiques. Vous pourriez 
- 
- avoir d autres droits prévus par les lois de votre pays. Le présent contrat ne 
- 
- modifie pas les droits que vous confèrent les lois de votre pays si celles-ci ne 
- 
- le permettent pas. 
+ Components t:dataModel and t:dataScroller work together nicely to allow a user to "page" through a set of a few dozen to a few hundred records.
+ However the implementation does assume that the entire set of available records are in memory and wrapped up inside a !ListDataModel or !ArrayDataModel.
+ 
+ When the available dataset is quite large, and the application can have many users, this can lead to memory usage problems.
+ 
+ This page contains discussions on how to handle this scenario.
+ 
+ === On-demand loading ===
+ 
+ A custom DataModel can be used to allow data to be loaded "on demand".
+ 
+ First, a class needs to be defined which your "business methods" (eg EJBs) can use to pass "pages" of data back to the UI.
+ This class needs to be defined in your project, as the "business" level shouldn't be extending !MyFaces classes:
+ 
+ {{{
+ package example;
+ 
+ import java.util.List;
+ 
+ /**
+  * A simple class that represents a "page" of data out of a longer set, ie
+  * a list of objects together with info to indicate the starting row and
+  * the full size of the dataset. EJBs can return instances of this type
+  * when returning subsets of available data.
+  */
+ public class DataPage<T> {
+ 
+   private int datasetSize;
+   private int startRow;
+   private List<T> data;
+ 	
+   /**
+    * Create an object representing a sublist of a dataset.
+    * 
+    * @param datasetSize is the total number of matching rows
+    * available.
+    * 
+    * @param startRow is the index within the complete dataset
+    * of the first element in the data list.
+    * 
+    * @param data is a list of consecutive objects from the
+    * dataset.
+    */
+   public DataPage(int datasetSize, int startRow, List<T> data) {
+     this.datasetSize = datasetSize;
+     this.startRow = startRow;
+     this.data = data;
+   }
+ 
+   /**
+    * Return the number of items in the full dataset.
+    */
+   public int getDatasetSize() {
+     return datasetSize;
+   }
+ 
+   /**
+    * Return the offset within the full dataset of the first
+    * element in the list held by this object.
+    */
+   public int getStartRow() {
+     return startRow;
+   }
+ 
+   /**
+    * Return the list of objects held by this object, which
+    * is a continuous subset of the full dataset.
+    */
+   public List<T> getData() {
+     return data;
+   }
+ }
+ }}}
+ 
+ Now a custom !DataModel can use this !DataPage stuff. Again, it is recommended that you copy this code into your project and change the package name appropriately. This class can't go in the !MyFaces libraries as it depends on !DataPage, and as noted above, !DataPage is accessed by your business level code so it really can't be in the !MyFaces libs:
+ 
+ {{{
+ package example;
+ 
+ import javax.faces.model.DataModel;
+ 
+ import example.DataPage;
+ 
+ /**
+  * A special type of JSF DataModel to allow a datatable and datascroller
+  * to page through a large set of data without having to hold the entire
+  * set of data in memory at once.
+  * <p>
+  * Any time a managed bean wants to avoid holding an entire dataset,
+  * the managed bean should declare an inner class which extends this
+  * class and implements the fetchData method. This method is called
+  * as needed when the table requires data that isn't available in the
+  * current data page held by this object.
+  * <p>
+  * This does require the managed bean (and in general the business
+  * method that the managed bean uses) to provide the data wrapped in
+  * a DataPage object that provides info on the full size of the dataset.
+  */
+ public abstract class PagedListDataModel<T> extends DataModel {
+ 
+ 	private int pageSize;
+ 	private int rowIndex;
+ 	private DataPage<T> page;
+ 	
+ 	private int lastStartRow = -1;
+ 	private DataPage<T> lastPage;
+ 
+ 	/*
+ 	 * Create a datamodel that pages through the data showing the specified
+ 	 * number of rows on each page.
+ 	 */
+ 	public PagedListDataModel(int pageSize) {
+ 		super();
+ 		this.pageSize = pageSize;
+ 		this.rowIndex = -1;
+ 		this.page = null;
+ 	}
+ 
+ 	/**
+ 	 * Not used in this class; data is fetched via a callback to the fetchData
+ 	 * method rather than by explicitly assigning a list.
+ 	 */
+ 	@Override
+ 	public void setWrappedData(Object o) {
+ 		throw new UnsupportedOperationException("setWrappedData");
+ 	}
+ 
+ 	@Override
+ 	public int getRowIndex() {
+ 		return rowIndex;
+ 	}
+ 
+ 	/**
+ 	 * Specify what the "current row" within the dataset is. Note that the
+ 	 * UIData component will repeatedly call this method followed by getRowData
+ 	 * to obtain the objects to render in the table.
+ 	 */
+ 	@Override
+ 	public void setRowIndex(int index) {
+ 		rowIndex = index;
+ 	}
+ 
+ 	/**
+ 	 * Return the total number of rows of data available (not just the number of
+ 	 * rows in the current page!).
+ 	 */
+ 	@Override
+ 	public int getRowCount() {
+ 		return getPage().getDatasetSize();
+ 	}
+ 
+ 	/**
+ 	 * Return a DataPage object; if one is not currently available then fetch
+ 	 * one. Note that this doesn't ensure that the datapage returned includes
+ 	 * the current rowIndex row; see getRowData.
+ 	 */
+ 	private DataPage<T> getPage() {
+ 		if (page != null)
+ 			return page;
+ 
+ 		int rowIndex = getRowIndex();
+ 		int startRow = rowIndex;
+ 		if (rowIndex == -1) {
+ 			// even when no row is selected, we still need a page
+ 			// object so that we know the amount of data available.
+ 			startRow = 0;
+ 		}
+ 
+ 		// invoke method on enclosing class
+ 		page = suggestFetchPage(startRow, pageSize);
+ 		return page;
+ 	}
+ 
+ 	/**
+ 	 * Return the object corresponding to the current rowIndex. If the DataPage
+ 	 * object currently cached doesn't include that index then fetchPage is
+ 	 * called to retrieve the appropriate page.
+ 	 */
+ 	@Override
+ 	public Object getRowData() {
+ 		if (rowIndex < 0) {
+ 			throw new IllegalArgumentException("Invalid rowIndex for PagedListDataModel; not within page");
+ 		}
+ 
+ 		// ensure page exists; if rowIndex is beyond dataset size, then
+ 		// we should still get back a DataPage object with the dataset size
+ 		// in it...
+ 		if (page == null) {
+ 			page = suggestFetchPage(rowIndex, pageSize);
+ 		}
+ 
+ 		// Check if rowIndex is equal to startRow,
+ 		// useful for dynamic sorting on pages
+ 
+ 		if (rowIndex == page.getStartRow()) {
+ 			page = suggestFetchPage(rowIndex, pageSize);
+ 		}
+ 
+ 		int datasetSize = page.getDatasetSize();
+ 		int startRow = page.getStartRow();
+ 		int nRows = page.getData().size();
+ 		int endRow = startRow + nRows;
+ 
+ 		if (rowIndex >= datasetSize) {
+ 			throw new IllegalArgumentException("Invalid rowIndex");
+ 		}
+ 
+ 		if (rowIndex < startRow) {
+ 			page = suggestFetchPage(rowIndex, pageSize);
+ 			startRow = page.getStartRow();
+ 		}
+ 		else if (rowIndex >= endRow) {
+ 			page = suggestFetchPage(rowIndex, pageSize);
+ 			startRow = page.getStartRow();
+ 		}
+ 
+ 		return page.getData().get(rowIndex - startRow);
+ 	}
+ 
+ 	@Override
+ 	public Object getWrappedData() {
+ 		return page.getData();
+ 	}
+ 
+ 	/**
+ 	 * Return true if the rowIndex value is currently set to a value that
+ 	 * matches some element in the dataset. Note that it may match a row that is
+ 	 * not in the currently cached DataPage; if so then when getRowData is
+ 	 * called the required DataPage will be fetched by calling fetchData.
+ 	 */
+ 	@Override
+ 	public boolean isRowAvailable() {
+ 		DataPage<T> page = getPage();
+ 		if (page == null)
+ 			return false;
+ 
+ 		int rowIndex = getRowIndex();
+ 		if (rowIndex < 0) {
+ 			return false;
+ 		}
+ 		else if (rowIndex >= page.getDatasetSize()) {
+ 			return false;
+ 		}
+ 		else {
+ 			return true;
+ 		}
+ 	}
+ 
+ 	/**
+ 	 * the jsf framework can be funky.  It could ask for the same start row multiple times within the
+ 	 * same cycle.   Therefore, we cache the results for the startRow and make sure that if the framework
+ 	 * asks for them again in very short order that we simply returned the cached value.   What the
+ 	 * extended class could do in fetchPage could be costly, so we make sure it is called only when needed!
+ 	 * 
+ 	 * @param startRow
+ 	 * @param pageSize
+ 	 * @return
+ 	 */
+ 	public DataPage<T> suggestFetchPage(int startRow, int pageSize) {
+ 		if(this.lastStartRow == startRow) {
+ 			return this.lastPage;
+ 		}
+ 		
+ 		this.lastStartRow = startRow;
+ 		this.lastPage = fetchPage(startRow, pageSize);
+ 		return this.lastPage;
+ 	}
+ 
+ 	/**
+ 	 * Method which must be implemented in cooperation with the managed bean
+ 	 * class to fetch data on demand.
+ 	 */
+ 	public abstract DataPage<T> fetchPage(int startRow, int pageSize);
+ }
+ 
+ 
+ Finally, the managed bean needs to provide a simple inner class that provides the fetchPage implementation:
+ 
+ {{{
+   public SomeManagedBean {
+     ....
+ 
+ 
+     private DataPage<SomeRowObject> getDataPage(int startRow, int pageSize) {
+       // access database here, or call EJB to do so
+     }
+ 
+     public DataModel getDataModel() {
+         if (dataModel == null) {
+             dataModel = new LocalDataModel(getRowsPerPage());
+         }
+ 
+         return dataModel;
+     }
+ 
+     private class LocalDataModel extends PagedListDataModel {
+         public LocalDataModel(int pageSize) {
+             super(pageSize);
+         }
+         
+         public DataPage<SomeRowObject> fetchPage(int startRow, int pageSize) {
+             // call enclosing managed bean method to fetch the data
+             return getDataPage(startRow, pageSize);
+         }
+     }
+ 
+ }}}
+ 
+ The jsp pages are then trivial; by default the t:dataScroller will update the t:dataTable's "first" property, and that's all that is needed because when the table
+ asks the custom !DataModel for the necessary rows callbacks to fetchPage will be made which will fetch exactly the data required. No event listeners, action methods,
+ or anything else is required as glue.
+ 
+ === Other approaches ===
+ 
+ In an email thread on this topic, an alternative was decribed:
+    http://sebcsaba.fw.hu/scrollablelist.html
+ 
+ So was this:
+   http://www.mail-archive.com/users%40myfaces.apache.org/msg12597.html
+ 
+ And another example with the DataModel approach
+   http://www.jroller.com/page/cagataycivici?entry=jsf_datatable_with_custom_paging
+ 
+ And a sortable and scrollable demo with 1.1.1 jars included:
+   [[attachment:myfaces-cars.zip]]
+ 
+ Paging large data sets with a !LazyList
+   http://www.ilikespam.com/blog/paging-large-data-sets-with-a-lazylist
+ 
+ Paged datatable with a GenericDataTableHandler:
+   http://jroller.com/page/jurgenlust?anchor=genericcrudhandler_for_jsf
+