Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 6.83 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.Serializable;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6.  
  7. /**
  8.  * Oferece suporte para lidar com paginas em telas a pesquisa fica a cargo do
  9.  * programador.
  10.  *
  11.  * @author carlosr
  12.  *
  13.  * @param <T>
  14.  */
  15.  
  16. public class ListaPaginada<T> implements List<T>, Serializable {
  17.  
  18.         private static final long serialVersionUID = 1L;
  19.  
  20.         private List<T> listaInterna;
  21.         private Integer totalRegistros;
  22.  
  23.         public ListaPaginada(List<T> listaInterna, Integer totalRegistros) {
  24.                 this.listaInterna = listaInterna;
  25.                 this.totalRegistros = totalRegistros;
  26.         }
  27.  
  28.         public void add(int arg0, T arg1) {
  29.                 listaInterna.add(arg0, arg1);
  30.         }
  31.  
  32.         public boolean add(T arg0) {
  33.                 return listaInterna.add(arg0);
  34.         }
  35.  
  36.         public boolean addAll(Collection<? extends T> arg0) {
  37.                 return listaInterna.addAll(arg0);
  38.         }
  39.  
  40.         public boolean addAll(int arg0, Collection<? extends T> arg1) {
  41.                 return listaInterna.addAll(arg0, arg1);
  42.         }
  43.  
  44.         public void clear() {
  45.                 listaInterna.clear();
  46.         }
  47.  
  48.         public boolean contains(Object arg0) {
  49.                 return listaInterna.contains(arg0);
  50.         }
  51.  
  52.         public boolean containsAll(Collection<?> arg0) {
  53.                 return listaInterna.containsAll(arg0);
  54.         }
  55.  
  56.         public boolean equals(Object arg0) {
  57.                 return listaInterna.equals(arg0);
  58.         }
  59.  
  60.         public T get(int arg0) {
  61.                 return listaInterna.get(arg0);
  62.         }
  63.  
  64.         public int hashCode() {
  65.                 return listaInterna.hashCode();
  66.         }
  67.  
  68.         public int indexOf(Object arg0) {
  69.                 return listaInterna.indexOf(arg0);
  70.         }
  71.  
  72.         public boolean isEmpty() {
  73.                 return listaInterna.isEmpty();
  74.         }
  75.  
  76.         public Iterator<T> iterator() {
  77.                 return listaInterna.iterator();
  78.         }
  79.  
  80.         public int lastIndexOf(Object arg0) {
  81.                 return listaInterna.lastIndexOf(arg0);
  82.         }
  83.  
  84.         public ListIterator<T> listIterator() {
  85.                 return listaInterna.listIterator();
  86.         }
  87.  
  88.         public ListIterator<T> listIterator(int arg0) {
  89.                 return listaInterna.listIterator(arg0);
  90.         }
  91.  
  92.         public T remove(int arg0) {
  93.                 return listaInterna.remove(arg0);
  94.         }
  95.  
  96.         public boolean remove(Object arg0) {
  97.                 return listaInterna.remove(arg0);
  98.         }
  99.  
  100.         public boolean removeAll(Collection<?> arg0) {
  101.                 return listaInterna.removeAll(arg0);
  102.         }
  103.  
  104.         public boolean retainAll(Collection<?> arg0) {
  105.                 return listaInterna.retainAll(arg0);
  106.         }
  107.  
  108.         public T set(int arg0, T arg1) {
  109.                 return listaInterna.set(arg0, arg1);
  110.         }
  111.  
  112.         public int size() {
  113.                 return listaInterna.size();
  114.         }
  115.  
  116.         public List<T> subList(int arg0, int arg1) {
  117.                 return listaInterna.subList(arg0, arg1);
  118.         }
  119.  
  120.         public Object[] toArray() {
  121.                 return listaInterna.toArray();
  122.         }
  123.  
  124.         @SuppressWarnings("hiding")
  125.         public <T> T[] toArray(T[] arg0) {
  126.                 return listaInterna.toArray(arg0);
  127.         }
  128.  
  129.         public Integer getTotalRegistros() {
  130.                 return totalRegistros;
  131.         }
  132.  
  133.         public void setTotalRegistros(Integer totalRegistros) {
  134.                 this.totalRegistros = totalRegistros;
  135.         }
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. import java.io.IOException;
  147. import java.io.Serializable;
  148. import java.util.ArrayList;
  149. import java.util.HashMap;
  150. import java.util.List;
  151. import java.util.Map;
  152.  
  153. import javax.faces.context.FacesContext;
  154.  
  155. import org.ajax4jsf.model.DataVisitor;
  156. import org.ajax4jsf.model.Range;
  157. import org.ajax4jsf.model.SequenceRange;
  158. import org.ajax4jsf.model.SerializableDataModel;
  159. import org.jboss.seam.core.Expressions;
  160.  
  161. import br.gov.capes.framework.entity.BaseEntity;
  162.  
  163. /**
  164.  * DataModel que adiciona suporte de paginação
  165.  *
  166.  * para usar a paginação use:
  167.  *
  168.  * PaginacaoDataModel clientes = new PaginacaoDataModel(#{metodo que retorna uma
  169.  * listaPaginada para o componente de paginacao}}
  170.  *
  171.  * @param <T>
  172.  */
  173.  
  174. public class PaginacaoDataModel<T> extends SerializableDataModel {
  175.  
  176.         private static final long serialVersionUID = 2954923950179861809L;
  177.  
  178.         protected Serializable currentPk;
  179.         protected boolean detached = false;
  180.         protected List<Serializable> wrappedKeys = new ArrayList<Serializable>();
  181.         protected Map<Serializable, T> wrappedData = new HashMap<Serializable, T>();
  182.  
  183.         @Override
  184.         public Object getRowKey() {
  185.                 return currentPk;
  186.         }
  187.  
  188.         @Override
  189.         public void setRowKey(final Object key) {
  190.                 this.currentPk = (Serializable) key;
  191.         }
  192.  
  193.         @Override
  194.         public void update() {
  195.                 detached = false;
  196.         }
  197.  
  198.         @Override
  199.         public SerializableDataModel getSerializableModel(final Range range) {
  200.                 if (wrappedKeys != null) {
  201.                         detached = true;
  202.                         return this;
  203.                 }
  204.                 return null;
  205.         }
  206.  
  207.         @Override
  208.         public void setRowIndex(final int rowIndex) {
  209.                 throw new UnsupportedOperationException();
  210.  
  211.         }
  212.  
  213.         @Override
  214.         public void setWrappedData(final Object data) {
  215.                 throw new UnsupportedOperationException();
  216.  
  217.         }
  218.  
  219.         @Override
  220.         public int getRowIndex() {
  221.                 throw new UnsupportedOperationException();
  222.         }
  223.  
  224.         @Override
  225.         public Object getWrappedData() {
  226.                 throw new UnsupportedOperationException();
  227.         }
  228.  
  229.         private int firstRow;
  230.         private int numberOfRows;
  231.  
  232.        
  233.         @Override
  234.         public void walk(final FacesContext context, final DataVisitor visitor,
  235.                         final Range range, final Object argument) throws IOException {
  236.                 final int firstRow = ((SequenceRange) range).getFirstRow();
  237.                 final int numberOfRows = ((SequenceRange) range).getRows();
  238.  
  239.                 if (!(firstRow == this.firstRow && numberOfRows == this.numberOfRows)) {
  240.                         detached = false;
  241.                         this.firstRow = firstRow;
  242.                         this.numberOfRows = numberOfRows;
  243.                 } else {
  244.                         detached = true;
  245.                 }
  246.                
  247.                
  248.  
  249.                 if (detached) {
  250.                         for (final Serializable key : wrappedKeys) {
  251.                                 setRowKey(key);
  252.                                 visitor.process(context, key, argument);
  253.                         }
  254.                 } else {
  255.                         wrappedKeys = new ArrayList<Serializable>();
  256.                         for (final T object : findObjects(firstRow, numberOfRows)) {
  257.                                 wrappedKeys.add(getId(object));
  258.                                 wrappedData.put(getId(object), object);
  259.                                 visitor.process(context, getId(object), argument);
  260.                         }
  261.                 }
  262.         }
  263.  
  264.         @Override
  265.         public boolean isRowAvailable() {
  266.                 if (currentPk == null) {
  267.                         return false;
  268.                 }
  269.                 if (wrappedKeys.contains(currentPk)) {
  270.                         return true;
  271.                 }
  272.                 if (wrappedData.entrySet().contains(currentPk)) {
  273.                         return true;
  274.                 }
  275.                 return false;
  276.         }
  277.  
  278.         @Override
  279.         public Object getRowData() {
  280.                 if (currentPk == null) {
  281.                         return null;
  282.                 }
  283.                 T object = wrappedData.get(currentPk);
  284.                 return object;
  285.         }
  286.  
  287.         @Override
  288.         public int getRowCount() {
  289.                 return getNumRecords();
  290.         }
  291.  
  292.         public Serializable getId(T object) {
  293.                 return (Serializable) ((BaseEntity) object).getId();
  294.         }
  295.  
  296.         private String expression;
  297.         private int numRecords;
  298.  
  299.         @SuppressWarnings("unchecked")
  300.         public List<T> findObjects(Integer firstRow, Integer numberOfRows) {
  301.  
  302.                 ListaPaginada<T> listaPaginada = Expressions.instance().createMethodExpression(expression, ListaPaginada.class,Integer.class, Integer.class).invoke(firstRow, numberOfRows);
  303.  
  304.                 numRecords = listaPaginada.getTotalRegistros();
  305.                
  306.                 return listaPaginada;
  307.         }
  308.  
  309.         public PaginacaoDataModel(String expression) {
  310.                 this.expression = expression;
  311.         }
  312.  
  313.         public int getNumRecords() {
  314.                 return numRecords;
  315.         }
  316. }