Guest User

PageableDatamodel.java

a guest
Apr 2nd, 2011
1,103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.luca.jsf.model.rf;
  2.  
  3. import java.io.Serializable;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import javax.faces.context.FacesContext;
  9.  
  10. import org.ajax4jsf.model.DataVisitor;
  11. import org.ajax4jsf.model.ExtendedDataModel;
  12. import org.ajax4jsf.model.Range;
  13. import org.ajax4jsf.model.SequenceRange;
  14. import org.luca.domain.Identifiable;
  15.  
  16.  
  17. /**
  18.  * Scrollable datamodel based on:
  19.  * http://gochev.blogspot.com/2009/08/richfaces-server-side-paging-with.html
  20.  *
  21.  * @author luc4
  22.  */
  23. public abstract class PageableDatamodel<T extends Identifiable> extends ExtendedDataModel<T>
  24.         implements Serializable
  25. {
  26.  
  27.     private Integer currentId;
  28.  
  29.     private Integer rowCount;
  30.  
  31.     private Map<Integer, T> wrappedData = new HashMap<Integer, T>();
  32.  
  33.  
  34.     /**
  35.      * Returns an item by id.
  36.      *
  37.      * @param start
  38.      * @param numberOfRows
  39.      * @return
  40.      */
  41.     protected abstract T getItemById( Integer id );
  42.  
  43.  
  44.     /**
  45.      * Returns a data page.
  46.      *
  47.      * @param start
  48.      * @param numberOfRows
  49.      * @return
  50.      */
  51.     protected abstract List<T> getItemsRanged( int start, int numberOfRows );
  52.  
  53.  
  54.     /**
  55.      * Returns the total count for items.
  56.      *
  57.      * @return
  58.      */
  59.     protected abstract int getItemsTotalCount();
  60.  
  61.  
  62.     /**
  63.      * This method never called from framework. (non-Javadoc)
  64.      *
  65.      * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
  66.      */
  67.     @Override
  68.     public Object getRowKey()
  69.     {
  70.         return currentId;
  71.     }
  72.  
  73.  
  74.     /**
  75.      * This method normally called by Visitor before request Data Row.
  76.      */
  77.     @Override
  78.     public void setRowKey( Object key )
  79.     {
  80.         this.currentId = (Integer) key;
  81.     }
  82.  
  83.  
  84.     /**
  85.      * This is main part of Visitor pattern. Method called by framework many times during request
  86.      * processing.
  87.      */
  88.     @Override
  89.     public void walk( FacesContext context, DataVisitor visitor, Range range, Object argument )
  90.     {
  91.         int firstRow = ((SequenceRange) range).getFirstRow();
  92.         int numberOfRows = ((SequenceRange) range).getRows();
  93.  
  94.         for ( T item : getItemsRanged( firstRow, numberOfRows ) )
  95.         {
  96.             wrappedData.put( item.getId(), item );
  97.             visitor.process( context, item.getId(), argument );
  98.         }
  99.     }
  100.  
  101.  
  102.     @Override
  103.     public int getRowCount()
  104.     {
  105.         if ( null == rowCount )
  106.         {
  107.             rowCount = getItemsTotalCount();
  108.             return rowCount;
  109.         }
  110.         else
  111.         {
  112.             return rowCount;
  113.         }
  114.     }
  115.  
  116.  
  117.     /**
  118.      * This is main way to obtain data row. It is intensively used by framework. We strongly
  119.      * recommend use of local cache in that method.
  120.      */
  121.     @Override
  122.     public T getRowData()
  123.     {
  124.         if ( currentId == null )
  125.         {
  126.             return null;
  127.         }
  128.         else
  129.         {
  130.             T ret = wrappedData.get( currentId );
  131.             if ( null == ret )
  132.             {
  133.                 ret = getItemById( currentId );
  134.                 wrappedData.put( currentId, ret );
  135.                 return ret;
  136.             }
  137.             else
  138.             {
  139.                 return ret;
  140.             }
  141.         }
  142.     }
  143.  
  144.  
  145.     /**
  146.      * Never called by framework.
  147.      */
  148.     @Override
  149.     public boolean isRowAvailable()
  150.     {
  151.         if ( null == currentId )
  152.         {
  153.             return false;
  154.         }
  155.         else
  156.         {
  157.             return (null != wrappedData.get( currentId ));
  158.         }
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Unused rudiment from old JSF staff.
  164.      */
  165.     @Override
  166.     public int getRowIndex()
  167.     {
  168.         return -1;
  169.     }
  170.  
  171.  
  172.     /**
  173.      * Unused rudiment from old JSF staff.
  174.      */
  175.     @Override
  176.     public void setRowIndex( int rowIndex )
  177.     {
  178.         throw new UnsupportedOperationException();
  179.     }
  180.  
  181.  
  182.     /**
  183.      * Unused rudiment from old JSF staff.
  184.      */
  185.     @Override
  186.     public Object getWrappedData()
  187.     {
  188.         throw new UnsupportedOperationException();
  189.     }
  190.  
  191.  
  192.     /**
  193.      * Unused rudiment from old JSF staff.
  194.      */
  195.     @Override
  196.     public void setWrappedData( Object data )
  197.     {
  198.         throw new UnsupportedOperationException();
  199.     }
  200.  
  201. }
RAW Paste Data