2 Replies Latest reply on Jul 17, 2010 11:03 PM by Yasser Hawari

    Seam with richfaces extended data table

    Tanya Ruttenberg Expert

      I would really like to implement an extended data table in my seam app.  I have been poring over the example from the richfaces demo but I cannot understand what is going on there at all.


      I don't understand what a SerializableDataModel is supposed to do. How does it tie in to my EJBs?  How does it tie in with my view?


      Does someone have an example?  Or can someone who knows how to explain things to neophytes please take a stab at explaining this to me (and probably others)?


      I have 2 entities joined via OneToOne, a device and its asset tag.  I have a table. Each row contains information about the device and the asset tag if the latter exists.  I want to be able to edit the asset tag data and save it. 


      It seems that the extended data table is the neatest way to do this if I could simply understand how it's done.


      Thanks in advance,
      TDR

        • 1. Re: Seam with richfaces extended data table
          Herberson Silva Newbie

          Hi,


          I already faced the same issue and found a solution.


          First you have understand that data passed from Seam to rich:extendedDataTable must be a DataModel class and unfortunately we can't use a java.util.List annoted with @DataModel.


          After a couple of hours of debuging RichFaces and Seam source codes I discover that I have to use a object of class org.richfaces.model.ExtendedTableDataModel and to instantiate that I too have to use a object and this one must be a intance of interface org.richfaces.model.DataProvider.


          So to instantiate a ExtendedTableDataModel i had to provide a implementation of class DataProvider.


          Here a example code of DataProvider implementation:



          import java.lang.reflect.InvocationTargetException;
          import java.util.Collections;
          import java.util.Comparator;
          import java.util.List;
          
          import org.apache.commons.beanutils.PropertyUtils;
          import org.richfaces.model.DataProvider;
          
          public class MyDataProvider<T> implements DataProvider<T> {
          
               private static final long serialVersionUID = -4599358104308291613L;
               
               private List<T> data;
               private String keyProperty;
               
               public MyDataProvider(List<T> data, String keyProperty) {
                    this.data = data;
                    this.keyProperty = keyProperty;
               }
               
               public T removeItem(T item) {
                    return removeItemByKey(getKey(item));
               }
               
               public T removeItemByKey(Object key) {
                    T removedItem = null;
                    T iterItem = null;
                    
                    for (int iItem = 0; iItem < getRowCount(); iItem++) {
                         iterItem = data.get(iItem);
                         if ( getKey(iterItem).equals(key) ) {
                              removedItem = iterItem;
                              data.remove(iItem);
                              break;
                         }
                    }
                    
                    return removedItem;
               }
               
               public void orderItemsBy(final String propertyName) {
                    Collections.sort(data, new Comparator<T>() {
                         @SuppressWarnings("unchecked")
                         public int compare(T o1, T o2) {
                              int comp = -1;
                              if (o1 == null || o2 == null) {
                                   comp = 0;
                              } else {
                                   Object valueO1 = getPropertyValue(o1, propertyName);
                                   Object valueO2 = getPropertyValue(o2, propertyName);
                                   if (valueO1 == null || valueO2 == null) {
                                        comp = 0;
                                   } else if (valueO1 instanceof Comparable && valueO2 instanceof Comparable) {
                                        comp = ((Comparable)valueO1).compareTo(valueO2);
                                   } else {
                                        comp = valueO1.toString().compareTo(valueO2.toString());
                                   }//end if
                              }//end if
                              return comp;
                         }//end method
                    });
               }
               
               public void clear() {
                    data.clear();
               }
               
               public boolean isEmpty() {
                    return (data == null || data.isEmpty());
               }
               
               public void addItem(T item) {
                    data.add(item);
               }
               
               private Object getPropertyValue(T item, String propertyName) {
                    Object k;
                    try {
                         k = PropertyUtils.getProperty(item, getKeyProperty());
                    } catch (IllegalAccessException e) {
                         k = null;
                    } catch (InvocationTargetException e) {
                         k = null;
                    } catch (NoSuchMethodException e) {
                         k = null;
                    }
                    return k;
               }
          
               public T getItemByKey(Object key) {
                    for (T item : data) {
                         if (key.equals(getKey(item))) {
                              return item;
                         }
                    }
                    return null;
               }
          
               public List<T> getItemsByRange(int firstRow, int endRow) {
                    return data.subList(firstRow, endRow);
               }
          
               public Object getKey(T item) {
                    return getPropertyValue(item, getKeyProperty());
               }
               
               public int getRowCount() {
                    return data.size();
               }
          
               public List<T> getData() {
                    return data;
               }
          
               public void setData(List<T> data) {
                    this.data = data;
               }
          
               public String getKeyProperty() {
                    return keyProperty;
               }
          
               public void setKeyProperty(String keyProperty) {
                    this.keyProperty = keyProperty;
               }
          
          }
          



          Herberson

          • 2. Re: Seam with richfaces extended data table
            Yasser Hawari Newbie

            u need to test the performance of this component. richfaces extendedDataTable and tree components should be used with care. they DO have performance problems.