1 2 Previous Next 16 Replies Latest reply on Mar 18, 2009 5:09 AM by Ilya Shaikovsky

    dataTable implement paging, sorting and filtering in backing

    rosdorff Newbie

      Hello,

      I want to use rich:dataTable but there will be 100.000+ records.
      So to keep the data in memory is considered to be no option.
      The idea is to make db calls with the paging, sorting and filtering parameters from the dataTable.
      My question is: how to go about this. Which DataModel should I implement. Is there some example code.

      Any hints greatly appreciated.

      Wilfred

        • 1. Re: dataTable implement paging, sorting and filtering in bac
          Nick Belaevski Master

          Hello Wilfred,

          Here is the RFC for that: https://jira.jboss.org/jira/browse/RF-4403. You can start with extending org.ajax4jsf.model.ExtendedDataModel class.

          • 2. Re: dataTable implement paging, sorting and filtering in bac
            rosdorff Newbie

            Thanks for the help guys.
            Also this post helped me a lot.
            http://www.jboss.org/index.html?module=bb&op=viewtopic&t=125952

            It all works now. I'll add the code here for anyone interested.

            • 3. Re: dataTable implement paging, sorting and filtering in bac
              rosdorff Newbie

              ContractsReport.xhtml

              <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              
              <ui:composition xmlns="http://www.w3.org/1999/xhtml"
               xmlns:s="http://jboss.com/products/seam/taglib"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:rich="http://richfaces.org/rich"
               xmlns:a4j="http://richfaces.org/a4j">
              
               <a4j:form id="contractsReportSearchForm" styleClass="edit" process="contractsReportPanel"
               ajaxSubmit="true" reRender="contractsReport" ajaxSingle="true" limitToList="true">
              
               <rich:simpleTogglePanel id="searchPanel" label="Search parameters"
               switchType="ajax" ajaxSingle="true" reRender="searchPanel" limitToList="true">
              
               <s:decorate template="/layout/display.xhtml">
               <ui:define name="label">Keyword</ui:define>
               <h:inputText id="keyword"
               value="#{contractsReportSearchForm.keyword}"/>
               </s:decorate>
              
               </rich:simpleTogglePanel>
              
               <div class="actionButtons"><a4j:commandButton id="search"
               value="Search" eventsQueue="placeHolder" ignoreDupResponses="true" />
               </div>
              
               </a4j:form>
              
               <rich:panel id="contractsReportPanel">
               <f:facet name="header">contract search results</f:facet>
               <div class="results" id="contractsReport"><h:outputText
               value="The contract search returned no results."
               rendered="#{empty contractsReportExtendedDataModel.rowCount}" />
               <h:form>
               <rich:dataTable id="contractsReport" var="contract"
               value="#{contractsReportExtendedDataModel}"
               rendered="#{not empty contractsReportExtendedDataModel.rowCount}"
               reRender="contractsReportDataScroller"
               rows="10">
               <rich:column id="id"
               sortBy="#{contractsReportExtendedDataModel.getColumnName(0)}"
               filterBy="#{contractsReportExtendedDataModel.getColumnName(0)}" filterEvent="onkeyup">
               <f:facet name="header">id</f:facet>
               <a4j:commandLink value="#{contract.id}"
               action="#{modalPanelPlaceHolder.setPage('/pages/include/SessionsReportModalPanel.xhtml')}"
               reRender="modalPanelPlaceHolder"
               oncomplete="#{rich:component('modalPanel')}.show()"
               eventsQueue="modalPanelPlaceHolder"
               ignoreDupResponses="true">
               <a4j:actionparam name="contractId" value="#{contract.id}" assignTo="#{sessionsReport.contractId}"/>
               </a4j:commandLink>
               </rich:column>
               <rich:column id="contractName"
               sortBy="#{contractsReportExtendedDataModel.getColumnName(1)}"
               filterBy="#{contractsReportExtendedDataModel.getColumnName(1)}" filterEvent="onkeyup">
               <f:facet name="header">contractName</f:facet>
               <h:outputText value="#{contract.contractName}"
               converter="binaryStringConverter" />
               </rich:column>
               <f:facet name="footer">
               <rich:datascroller for="contractsReport" maxPages="10"
               id="contractsReportDataScroller"
               renderIfSinglePage="false"
               ajaxSingle="true"
               reRender="contractsReport"
               limitToList="true"
               eventsQueue="placeHolder" ignoreDupResponses="true" />
               </f:facet>
               </rich:dataTable>
               </h:form>
               </div>
               </rich:panel>
              </ui:composition>
              


              • 4. Re: dataTable implement paging, sorting and filtering in bac
                rosdorff Newbie

                ContractsReportExtendedDataModel.java

                package com.foo.reporting.action.reports.contracts;
                
                import java.math.BigInteger;
                import java.util.List;
                import java.util.Map;
                
                import org.jboss.seam.ScopeType;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Logger;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Scope;
                import org.jboss.seam.log.Log;
                
                import com.foo.reporting.jsf.SearchForm;
                import com.foo.reporting.jsf.extendeddatamodel.BaseEdm;
                import com.foo.reporting.jsf.extendeddatamodel.DataAccessParameters;
                
                /**
                 * NB: The public methods in BaseEdm are called from facelets.
                 */
                @Name("contractsReportExtendedDataModel")
                @Scope(ScopeType.SESSION)
                public class ContractsReportExtendedDataModel extends BaseEdm<ContractsReportDto, BigInteger> {
                
                 @Logger
                 private Log log;
                
                 @In(value="contractsReportDaoDummy")
                 private ContractsReportDao contractsReportDao;
                
                 @In(value="contractsReportSearchForm")
                 private ContractsReportSearchForm searchForm;
                
                 private static final String[] columnsNames = {
                 "id", "contractName"
                 };
                
                 @Override
                 protected BigInteger getId(ContractsReportDto row) {
                 return row.getId();
                 }
                 @Override
                 public String getColumnName(Long columnNumber) {
                 int index = columnNumber.intValue();
                 return columnsNames[index];
                 }
                
                 @Override
                 protected List<ContractsReportDto> getListFromDao(Map<DataAccessParameters, String> map) {
                 log.info("Getting list for ContractsReport from Dao with range '{0}', search '{1}', sort '{2}', filter '{3}'.", map.get(DataAccessParameters.RANGE), map.get(DataAccessParameters.SEARCH), map.get(DataAccessParameters.SORT), map.get(DataAccessParameters.FILTER));
                 return contractsReportDao.getList(map.get(DataAccessParameters.RANGE), map.get(DataAccessParameters.SEARCH), map.get(DataAccessParameters.SORT), map.get(DataAccessParameters.FILTER));
                 }
                
                 @Override
                 protected Long getCountFromDao(Map<DataAccessParameters, String> map) {
                 log.info("Getting count for ContractsReport from Dao with search '{0}', filter '{1}'.", map.get(DataAccessParameters.SEARCH), map.get(DataAccessParameters.FILTER));
                 return contractsReportDao.getCount(map.get(DataAccessParameters.SEARCH), map.get(DataAccessParameters.FILTER));
                 }
                
                 @Override
                 protected SearchForm getSearchForm() {
                 return (SearchForm) searchForm;
                 }
                
                }
                


                • 5. Re: dataTable implement paging, sorting and filtering in bac
                  rosdorff Newbie

                  BaseEdm.java

                  package com.foo.reporting.jsf.extendeddatamodel;
                  
                  import java.io.IOException;
                  import java.io.Serializable;
                  import java.util.ArrayList;
                  import java.util.HashMap;
                  import java.util.List;
                  import java.util.Map;
                  
                  import javax.faces.context.FacesContext;
                  
                  import org.ajax4jsf.model.DataVisitor;
                  import org.ajax4jsf.model.ExtendedDataModel;
                  import org.ajax4jsf.model.Range;
                  import org.ajax4jsf.model.SequenceRange;
                  import org.jboss.seam.annotations.Logger;
                  import org.jboss.seam.log.Log;
                  import org.richfaces.model.FilterField;
                  import org.richfaces.model.Modifiable;
                  import org.richfaces.model.SortField2;
                  
                  import com.foo.reporting.jsf.SearchForm;
                  
                  /**
                   *
                   * For an example implementation {@link ExtendedDataModel} see http://livedemo.exadel.com/richfaces-demo/richfaces/dataTable.jsf?tab=dataModel&cid=1917084.
                   * For an example implementation {@link Modifiable} see https://jira.jboss.org/jira/browse/RF-4403.
                   *
                   * @param <T>
                   * @param <ID>
                   */
                  public abstract class BaseEdm<T, ID extends Serializable> extends
                   ExtendedDataModel implements Modifiable {
                  
                   /**
                   * Implement these methods in concrete classes.
                   */
                   protected abstract ID getId(T row);
                   protected abstract Long getCountFromDao(Map<DataAccessParameters, String> map);
                   protected abstract List<T> getListFromDao(Map<DataAccessParameters, String> map);
                   protected abstract SearchForm getSearchForm();
                   public abstract String getColumnName(Long columnNumber);
                  
                   @Logger
                   private Log log;
                  
                   private SearchForm cachedSearchFormForList;
                   private SearchForm cachedSearchFormForRowCount;
                   private Map<DataAccessParameters, String> dataAccessParameters = new HashMap<DataAccessParameters, String>();
                  
                   /**
                   * To override abstract methods in ExtendedDataModel.
                   */
                   private ID currentId;
                   private Map<ID, T> wrappedData = new HashMap<ID, T>();
                   private List<T> cachedItems;
                   private Long rowCount;
                  
                   private static final String SEARCHFORM_CACHE_FOR_LIST = "searchFormCacheForList";
                   private static final String SEARCHFORM_CACHE_FOR_ROW_COUNT = "searchFormCacheForRowCount";
                  
                   /**
                   * To implement Modifiable.
                   */
                   private SequenceRange cachedRange;
                   private List<FilterField> filterFields;
                   private List<SortField2> sortFields;
                  
                   /**
                   * Overrides DataModel.
                   * This is main way to obtain data row. It is intensively used by framework.
                   */
                   @Override
                   public Object getRowData() {
                   if (currentId == null) {
                   return null;
                   } else {
                   T ret = wrappedData.get(currentId);
                   if (ret == null) {
                   ret = this.findById(currentId);
                   wrappedData.put(currentId, ret);
                   return ret;
                   } else {
                   return ret;
                   }
                   }
                   }
                  
                   /**
                   * Overrides DataModel.
                   */
                   @Override
                   public int getRowCount() {
                  
                   if(!BaseEdmHelper.areEqualSearchForms(this.cachedSearchFormForRowCount, getSearchForm())) {
                   refreshRowCount();
                   updateCachedSearchForm(SEARCHFORM_CACHE_FOR_ROW_COUNT);
                   }
                  
                   if (rowCount == null) {
                   // prepare search to come
                   dataAccessParameters.put(DataAccessParameters.SEARCH, BaseEdmHelper.getDataAccessSearchParameters(getSearchForm()));
                   dataAccessParameters.put(DataAccessParameters.FILTER, BaseEdmHelper.getDataAccessFilters(filterFields, FacesContext.getCurrentInstance()));
                   return (rowCount = this.getCountFromDao(dataAccessParameters)).intValue();
                   } else {
                   return rowCount.intValue();
                   }
                   }
                  
                   /**
                   * Overrides ExtendedDataModel.
                   */
                   @Override
                   public void walk(FacesContext context, DataVisitor visitor, Range range,
                   Object argument) throws IOException {
                  
                   SequenceRange sequenceRange = (SequenceRange) range;
                   if(!BaseEdmHelper.areEqualRanges(this.cachedRange, sequenceRange)) {
                   refreshList();
                   this.cachedRange = sequenceRange;
                   }
                   if(!BaseEdmHelper.areEqualSearchForms(this.cachedSearchFormForList, getSearchForm())) {
                   refreshList();
                   updateCachedSearchForm(SEARCHFORM_CACHE_FOR_LIST);
                   }
                  
                   // prepare search to come
                   if(cachedItems == null) {
                   dataAccessParameters.put(DataAccessParameters.SEARCH, BaseEdmHelper.getDataAccessSearchParameters(getSearchForm()));
                   dataAccessParameters.put(DataAccessParameters.RANGE, BaseEdmHelper.getDataAccessRange(sequenceRange));
                   dataAccessParameters.put(DataAccessParameters.FILTER, BaseEdmHelper.getDataAccessFilters(filterFields, context));
                   dataAccessParameters.put(DataAccessParameters.SORT, BaseEdmHelper.getDataAccessSorts(sortFields, context));
                   }
                  
                   for (T item: getList()) {
                   visitor.process(context, getId(item), argument);
                   }
                   }
                  
                   /**
                   * Overrides ExtendedDataModel.
                   * This method normally called by Visitor before request Data Row.
                   */
                   @SuppressWarnings("unchecked")
                   @Override
                   public void setRowKey(Object key) {
                   this.currentId = (ID) key;
                   }
                  
                   /**
                   * Overrides ExtendedDataModel.
                   * This method never called from framework. (non-Javadoc)
                   *
                   * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
                   */
                   @Override
                   public Object getRowKey() {
                   return currentId;
                   }
                  
                   /**
                   * Overrides DataModel.
                   * Never called by framework.
                   */
                   @Override
                   public boolean isRowAvailable() {
                   if (currentId == null) {
                   return false;
                   } else {
                   return hasById(currentId);
                   }
                   }
                  
                   /**
                   * To implement Modifiable.
                   */
                   public void modify(List<FilterField> filterFields, List<SortField2> sortFields) {
                  
                   if(!BaseEdmHelper.areEqualFilterFields(this.filterFields, filterFields)) {
                   refreshList();
                   refreshRowCount();
                   this.filterFields = filterFields;
                   }
                  
                   if(!BaseEdmHelper.areEqualSortFields(this.sortFields, sortFields)) {
                   refreshList();
                   refreshRowCount();
                   this.sortFields = sortFields;
                   }
                  
                   }
                  
                  
                   private void refreshList() {
                   this.cachedItems = null;
                   }
                  
                   private void refreshRowCount() {
                   this.rowCount = null;
                   }
                  
                   /**
                   * Here not a component is created, or a reference to a component.
                   * Because components are subject to dis-injection.
                   */
                   private void updateCachedSearchForm(String cacheFor) {
                   try {
                   if(SEARCHFORM_CACHE_FOR_LIST.equals(cacheFor)) {
                   cachedSearchFormForList = (SearchForm) getSearchForm().clone();
                   } else if(SEARCHFORM_CACHE_FOR_ROW_COUNT.equals(cacheFor)) {
                   cachedSearchFormForRowCount = (SearchForm) getSearchForm().clone();
                   }
                   } catch (CloneNotSupportedException e) {
                   log.error("Something went wrong cloning the searchForm for {0}.", e, cacheFor);
                   }
                   }
                  
                   private boolean hasById(ID id) {
                   for (T row : getList()) {
                   ID rowId = getId(row);
                   if (rowId.equals(id))
                   return true;
                   }
                   return false;
                   }
                  
                   private List<T> getList() {
                   if (cachedItems != null) {
                   return cachedItems;
                   } else {
                   initData();
                   return cachedItems;
                   }
                   }
                  
                   private synchronized void initData() {
                   List<T> data = new ArrayList<T>();
                   data = getListFromDao(dataAccessParameters);
                   cachedItems = data;
                   wrapData();
                   }
                  
                   private void wrapData() {
                   wrappedData = new HashMap<ID, T>();
                   for (T row : cachedItems) {
                   ID id = getId(row);
                   wrappedData.put(id, row);
                   }
                   }
                  
                   private T findById(ID id) {
                   for (T item : getList()) {
                   if (getId(item).equals(id)) {
                   return item;
                   }
                   }
                   throw new RuntimeException("Item id=" + id.toString() + " not found.");
                   }
                  
                   /**
                   * Overrides DataModel.
                   * Unused rudiment from old JSF stuff.
                   */
                   @Override
                   public Object getWrappedData() {
                   throw new UnsupportedOperationException();
                   }
                  
                   /**
                   * Overrides DataModel.
                   * Unused rudiment from old JSF stuff.
                   */
                   @Override
                   public int getRowIndex() {
                   throw new UnsupportedOperationException();
                   }
                  
                   /**
                   * Overrides DataModel.
                   * Unused rudiment from old JSF stuff.
                   */
                   @Override
                   public void setRowIndex(int rowIndex) {
                   throw new UnsupportedOperationException();
                   }
                  
                   /**
                   * Overrides DataModel.
                   * Unused rudiment from old JSF stuff.
                   */
                   @Override
                   public void setWrappedData(Object data) {
                   throw new UnsupportedOperationException();
                   }
                  
                  }
                  


                  • 6. Re: dataTable implement paging, sorting and filtering in bac
                    rosdorff Newbie

                    BaseEdmHelper.java

                    package com.foo.reporting.jsf.extendeddatamodel;
                    
                    import java.util.List;
                    
                    import javax.el.ELException;
                    import javax.el.Expression;
                    import javax.el.ValueExpression;
                    import javax.faces.FacesException;
                    import javax.faces.context.FacesContext;
                    
                    import org.ajax4jsf.model.SequenceRange;
                    import org.apache.commons.lang.StringUtils;
                    import org.richfaces.model.ExtendedFilterField;
                    import org.richfaces.model.FilterField;
                    import org.richfaces.model.Ordering;
                    import org.richfaces.model.SortField2;
                    
                    import com.foo.reporting.jsf.SearchForm;
                    
                    public class BaseEdmHelper {
                    
                     protected static boolean areEqualRanges(SequenceRange range1, SequenceRange range2) {
                     if (range1 == null || range2 == null) {
                     return range1 == null && range2 == null;
                     } else {
                     return range1.getFirstRow() == range2.getFirstRow() && range1.getRows() == range2.getRows();
                     }
                     }
                    
                     protected static boolean areEqualSearchForms(SearchForm searchForm1, SearchForm searchForm2) {
                     if (searchForm1 == null || searchForm2 == null) {
                     return searchForm1 == null && searchForm2 == null;
                     } else {
                     return searchForm1.equals(searchForm2);
                     }
                     }
                    
                     // TODO optimize for performance.
                     protected static boolean areEqualFilterFields(List<FilterField> filterFields1, List<FilterField> filterFields2) {
                     if (filterFields1 == null || filterFields2 == null) {
                     return filterFields1 == null && filterFields2 == null;
                     } else {
                     return getDataAccessFilters(filterFields1, FacesContext.getCurrentInstance()).equals(getDataAccessFilters(filterFields2, FacesContext.getCurrentInstance()));
                     }
                     }
                    
                     // TODO optimize for performance.
                     protected static boolean areEqualSortFields(List<SortField2> sortFields1, List<SortField2> sortFields2) {
                     if (sortFields1 == null || sortFields2 == null) {
                     return sortFields1 == null && sortFields2 == null;
                     } else {
                     return getDataAccessSorts(sortFields1, FacesContext.getCurrentInstance()).equals(getDataAccessSorts(sortFields2, FacesContext.getCurrentInstance()));
                     }
                     }
                    
                     protected static String getPropertyName(FacesContext facesContext, Expression expression) {
                     try {
                     return (String) ((ValueExpression) expression).getValue(facesContext.getELContext());
                     } catch (ELException e) {
                     throw new FacesException(e.getMessage(), e);
                     }
                     }
                    
                     protected static String getDataAccessRange(SequenceRange sequenceRange) {
                     String ret = "";
                     if (sequenceRange != null) {
                     int first = sequenceRange.getFirstRow();
                     int rows = sequenceRange.getRows();
                    
                     ret += Integer.toString(first);
                     if (rows > 0) {
                     ret += "," + Integer.toString(rows);
                     }
                     }
                     return ret;
                     }
                    
                     protected static String getDataAccessSearchParameters(SearchForm searchForm) {
                     return searchForm.toDataAccessString();
                     }
                    
                     protected static String getDataAccessFilters(List<FilterField> filterFields, FacesContext context) {
                     String ret = "";
                     if (filterFields != null) {
                     for (FilterField filterField : filterFields) {
                     String propertyName = BaseEdmHelper.getPropertyName(context, filterField.getExpression());
                    
                     String filterValue = ((ExtendedFilterField) filterField).getFilterValue();
                     if (filterValue != null && filterValue.length() != 0) {
                    
                     // criteria.add(Restrictions.like(propertyName, filterValue, MatchMode.ANYWHERE).ignoreCase());
                    
                     filterValue = StringUtils.remove(filterValue, ",");
                     filterValue = StringUtils.remove(filterValue, ";");
                     ret += propertyName + "," + filterValue + ";";
                     }
                     }
                     }
                     return ret;
                     }
                    
                     protected static String getDataAccessSorts(List<SortField2> sortFields, FacesContext context) {
                     String ret = "";
                     if (sortFields != null) {
                     for (SortField2 sortField : sortFields) {
                     Ordering ordering = sortField.getOrdering();
                    
                     if (Ordering.ASCENDING.equals(ordering) || Ordering.DESCENDING.equals(ordering)) {
                     String propertyName = BaseEdmHelper.getPropertyName(context, sortField.getExpression());
                    
                     // Order order = Ordering.ASCENDING.equals(ordering) ? Order.asc(propertyName) : Order.desc(propertyName);
                     // criteria.addOrder(order.ignoreCase());
                    
                     ret += propertyName + "," + ordering + ";";
                     }
                     }
                     }
                     return ret;
                     }
                    
                    }
                    


                    • 7. Re: dataTable implement paging, sorting and filtering in bac
                      rosdorff Newbie

                      DataAccessParameters.java

                      package com.foo.reporting.jsf.extendeddatamodel;
                      
                      public enum DataAccessParameters {
                       SEARCH, RANGE, FILTER, SORT;
                      }
                      


                      • 8. Re: dataTable implement paging, sorting and filtering in bac
                        rosdorff Newbie

                        ContractsReportDto.java

                        package com.foo.reporting.action.reports.contracts;
                        import java.math.BigInteger;
                        public class ContractsReportDto {
                        
                         private BigInteger id;
                         private byte[] contractName;
                        
                         public BigInteger getId() {
                         return id;
                         }
                         public void setId(BigInteger id) {
                         this.id = id;
                         }
                         public byte[] getContractName() {
                         return contractName;
                         }
                         public void setContractName(byte[] contractName) {
                         this.contractName = contractName;
                         }
                        }
                        


                        • 9. Re: dataTable implement paging, sorting and filtering in bac
                          rosdorff Newbie

                          ContractsReportSDearchForm.java

                          package com.foo.reporting.action.reports.contracts;
                          
                          import org.apache.commons.lang.StringUtils;
                          import org.jboss.seam.ScopeType;
                          import org.jboss.seam.annotations.AutoCreate;
                          import org.jboss.seam.annotations.Name;
                          import org.jboss.seam.annotations.Scope;
                          
                          import com.foo.reporting.jsf.SearchForm;
                          
                          @Name("contractsReportSearchForm")
                          @AutoCreate
                          @Scope(ScopeType.SESSION)
                          public class ContractsReportSearchForm extends SearchForm {
                          
                           private String keyword = "";
                          
                           @Override
                           public boolean equals(Object obj) {
                           if (this == obj) return true;
                           // object must be ContractReportsSearchForm at this point
                           ContractsReportSearchForm form = (ContractsReportSearchForm) obj;
                           return keyword.equalsIgnoreCase(form.getKeyword());
                           }
                          
                           @Override
                           public int hashCode() {
                           return keyword.hashCode();
                           }
                          
                           @Override
                           public String toDataAccessString() {
                           String ret = "";
                           if(!"".equals(keyword)) {
                           keyword = StringUtils.remove(keyword, ",");
                           keyword = StringUtils.remove(keyword, ";");
                           ret = "keyword," + keyword + ";";
                           }
                           return ret;
                           }
                          
                           // getters and setters
                          
                           public String getKeyword() {
                           return keyword;
                           }
                          
                           public void setKeyword(String keyword) {
                           this.keyword = keyword;
                           }
                          
                          }
                          


                          • 10. Re: dataTable implement paging, sorting and filtering in bac
                            rosdorff Newbie

                            ContractsReportDao.java

                            package com.foo.reporting.action.reports.contracts;
                            
                            import java.util.List;
                            
                            public interface ContractsReportDao {
                             public List<ContractsReportDto> getList(String range, String search, String sort, String filter);
                            
                             /**
                             * The count for the paging does not need to take in to account the sort and the range.
                             * @param search
                             * @param filter
                             * @return Long
                             */
                             public Long getCount(String search, String filter);
                            }
                            


                            • 11. Re: dataTable implement paging, sorting and filtering in bac
                              rosdorff Newbie

                              ContractsReportDaoDummy.java

                              package com.foo.reporting.action.reports.contracts;
                              
                              import java.math.BigInteger;
                              import java.util.ArrayList;
                              import java.util.Collections;
                              import java.util.Comparator;
                              import java.util.HashMap;
                              import java.util.List;
                              import java.util.Map;
                              import java.util.StringTokenizer;
                              import java.util.Map.Entry;
                              
                              import org.apache.commons.lang.StringUtils;
                              import org.jboss.seam.annotations.AutoCreate;
                              import org.jboss.seam.annotations.In;
                              import org.jboss.seam.annotations.Name;
                              
                              import com.foo.reporting.converters.BinaryStringConverter;
                              
                              @Name("contractsReportDaoDummy")
                              @AutoCreate
                              public class ContractsReportDaoDummy implements ContractsReportDao {
                              
                               @In(value = "binaryStringConverter")
                               BinaryStringConverter converter;
                              
                               @In(value = "contractNameComparator")
                               ContractNameComparator contractNameComparator;
                              
                               public static Map<Integer, String> allContractsMap = new HashMap<Integer, String>();
                              
                               private static final String allContracts[] = {
                               "Contract 1",
                               "Contract 2",
                               "Contract 3",
                               "Contract 4",
                               "Contract 5",
                               "Contract 6",
                               "Digital temperature sensor IC LM75A SOIC8 +Free adapter",
                               "MC34063 Adjustable Power Supply DC-DC Converters 5x",
                               "100x Ultra Bright White LEDs, 5mm, Clear",
                               "100x Ultra Bright Blue LEDs, 5mm, Clear",
                               "100x Ultra Bright Yellow LEDs, 5mm, Clear",
                               "100x Ultra Bright Red LEDs, 5mm, Clear",
                               "High quality Universal Programmer Development Board",
                               "Lot of 10pcs 8x8 dot-matrix 3mm dia LED display bicolor",
                               "100x Ultra Bright Blue LEDs, 5mm, Clear",
                               "40pcs 74HC164 165 573 595 Shift register & latch IC kit",
                               "Electric Nail Manicure Drill File 4 Acrylic Polish Nail",
                               "LE-DS007 100x Ultra Bright VIOLET ( UV ) LED LEDs, 5mm",
                               "LE-DS007 100x Ultra Bright VIOLET ( UV ) LED LEDs, 5mm",
                               "Fantastic High brightness Cluster with 8pcs LED (Green)",
                               "100x Ultra Bright Yellow LEDs, 5mm, Clear",
                               "High brightness 1 pcs 3W 80 lm Lumen LED White",
                               "2X ATMEL ATMEGA128-16AU Microcontroller and 2x 64-TQFP",
                               "20pcs IRF530 & IRF9630 power mosfet kit",
                               "Electrolytic Capacitors Radial SMD SMT assorted kit",
                               "100 pcs Ultra Bright Mixed LEDs, 5mm, Clear",
                               "Lot of 80 pcs 8 values (1uH~1mH) color wheel inductors",
                               "Lot of 100pcs 4 values (4.7uH~220uH) DIP fixed inductor",
                               "10pcs 8x8 dot-matrix 3mm dia LED display bicolor",
                               "(SMD 0805) 50 Value Resistors + 32 Value Capacitors Kit",
                               "Fantastic High brightness Cluster with 24pcs LED (Red)",
                               "100x Ultra Bright Green LEDs, 5mm, Clear",
                               "100x Ultra Bright Blue LEDs, 5mm, Clear",
                               "Double-row Straight 20x male and 10x female pin header",
                               "NEW 30pcs HEAT SINKS ,50pcs insulation bushing and film",
                               "0.2% Class A Platinum Resistance Thermometers PT100"
                               };
                              
                               static {
                               try {
                               for (int i = 0; i < allContracts.length; i++) {
                               allContractsMap.put(i, allContracts);
                               }
                               } catch(Exception e) {
                               System.out.print("Error initializing allContractsList: " + e.getStackTrace());
                               }
                               }
                              
                               public Long getCount(String search, String filter) {
                               Map<Integer, String> searchResult = search(search);
                               Map<Integer, String> filterResult = filter(searchResult, filter);
                               return new Long(filterResult.size());
                               }
                              
                               public List<ContractsReportDto> getList(String range, String search, String sort, String filter) {
                               Map<Integer, String> searchResult = search(search);
                               Map<Integer, String> filterResult = filter(searchResult, filter);
                               List<ContractsReportDto> list = convertToList(filterResult);
                               sort(list, sort);
                               List<ContractsReportDto> truncatedList = range(list, range);
                               return truncatedList;
                               }
                              
                               private List<ContractsReportDto> range(List<ContractsReportDto> list, String range) {
                               if(range == null || "".equals(range)) return list;
                               List<ContractsReportDto> truncatedList = new ArrayList<ContractsReportDto>();
                              
                               Integer first = new Integer(getStringToken(range, 0));
                               Integer rows = new Integer(getStringToken(range, 1));
                              
                               for (int i = first; i < (first + rows) && i < list.size(); i++) {
                               truncatedList.add(list.get(i));
                               }
                              
                               return truncatedList;
                               }
                              
                               private String getStringToken(String string, int part) {
                               String ret = null;
                               List<String> tokens = new ArrayList<String>();
                               StringTokenizer st0 = new StringTokenizer(string,";");
                               while (st0.hasMoreTokens()) {
                               StringTokenizer st1 = new StringTokenizer(st0.nextToken(),",");
                               while (st1.hasMoreTokens()) {
                               tokens.add(st1.nextToken());
                               }
                               }
                               if(tokens.size() > part) ret = tokens.get(part);
                               return ret;
                               }
                              
                               private void sort(List<ContractsReportDto> list, String sort) {
                               if(sort == null || "".equals(sort)) return;
                               String propertyName = getStringToken(sort, 0);
                               String ordering = getStringToken(sort, 1);
                               Comparator<ContractsReportDto> comparator = null;
                               if("id".equals(propertyName)) {
                               comparator = new ContractIdComparator();
                               } else if("contractName".equals(propertyName)) {
                               comparator = contractNameComparator;
                               }
                               if(comparator == null) return;
                               Collections.sort(list, comparator);
                               if("descending".equalsIgnoreCase(ordering)) Collections.reverse(list);
                               }
                              
                               private List<ContractsReportDto> convertToList(Map<Integer, String> map) {
                               List<ContractsReportDto> list = new ArrayList<ContractsReportDto>();
                               for (Entry<Integer, String> entry : map.entrySet()) {
                               ContractsReportDto item = convertEntry(entry);
                               list.add(item);
                               }
                               return list;
                               }
                              
                               private ContractsReportDto convertEntry(Entry<Integer, String> entry) {
                               ContractsReportDto item = new ContractsReportDto();
                              
                               BigInteger id = BigInteger.valueOf(new Long(entry.getKey()));
                               byte[] contractName = "".getBytes();
                               Object contractNameObject = converter.transformStringToObject(entry.getValue());
                               if(contractNameObject != null) contractName = (byte[]) contractNameObject;
                              
                               item.setId(id);
                               item.setContractName(contractName);
                              
                               return item;
                               }
                              
                               private Map<Integer, String> filter(Map<Integer, String> map, String filter) {
                               if(filter == null || "".equals(filter)) return map;
                               String propertyName = getStringToken(filter, 0);
                               String filterValue = getStringToken(filter, 1);
                               Map<Integer, String> filterResult = new HashMap<Integer, String>();
                               for (Entry<Integer, String> entry : map.entrySet()) {
                               String entryValue = null;
                               if("id".equals(propertyName)) {
                               entryValue = entry.getKey().toString();
                               } else if("contractName".equals(propertyName)) {
                               entryValue = entry.getValue();
                               }
                               if(StringUtils.containsIgnoreCase(entryValue, filterValue)) {
                               filterResult.put(entry.getKey(), entry.getValue());
                               }
                               }
                               return filterResult;
                               }
                              
                               private Map<Integer, String> search(String search) {
                               if(search == null || "".equals(search)) return allContractsMap;
                               String keyword = getStringToken(search, 1);
                               Map<Integer, String> searchResult = new HashMap<Integer, String>();
                               for (Entry<Integer, String> entry : allContractsMap.entrySet()) {
                               String contractName = entry.getValue();
                               if(StringUtils.containsIgnoreCase(contractName, keyword)) {
                               searchResult.put(entry.getKey(), entry.getValue());
                               }
                               }
                               return searchResult;
                               }
                              
                               }
                              


                              • 12. Re: dataTable implement paging, sorting and filtering in bac
                                rosdorff Newbie

                                ContractNameComparator.java

                                package com.foo.reporting.action.reports.contracts;
                                
                                import java.io.Serializable;
                                import java.util.Comparator;
                                
                                import org.jboss.seam.ScopeType;
                                import org.jboss.seam.annotations.AutoCreate;
                                import org.jboss.seam.annotations.In;
                                import org.jboss.seam.annotations.Name;
                                import org.jboss.seam.annotations.Scope;
                                
                                import com.foo.reporting.converters.BinaryStringConverter;
                                
                                @Name("contractNameComparator")
                                @Scope(ScopeType.SESSION)
                                @AutoCreate
                                public class ContractNameComparator implements Comparator<ContractsReportDto>, Serializable {
                                
                                 private static final long serialVersionUID = 1L;
                                
                                 @In(value = "binaryStringConverter")
                                 BinaryStringConverter converter;
                                
                                 public int compare(ContractsReportDto o1, ContractsReportDto o2) {
                                 byte[] contractName1 = o1.getContractName();
                                 byte[] contractName2 = o2.getContractName();
                                
                                 String contractNameStr1 = converter.transformObjectToString((Object) contractName1);
                                 String contractNameStr2 = converter.transformObjectToString((Object) contractName2);
                                
                                 return contractNameStr1.compareToIgnoreCase(contractNameStr2);
                                 }
                                
                                }
                                


                                • 13. Re: dataTable implement paging, sorting and filtering in bac
                                  rosdorff Newbie

                                  ContractIdComparator.java

                                  package com.foo.reporting.action.reports.contracts;
                                  
                                  import java.util.Comparator;
                                  
                                  public class ContractIdComparator implements Comparator<ContractsReportDto> {
                                   public int compare(ContractsReportDto o1, ContractsReportDto o2) {
                                   return o1.getId().compareTo(o2.getId());
                                   }
                                  }
                                  


                                  • 14. Re: dataTable implement paging, sorting and filtering in bac
                                    rosdorff Newbie

                                    BinaryStringConverter.java

                                    package com.foo.reporting.converters;
                                    
                                    import java.io.Serializable;
                                    import java.io.UnsupportedEncodingException;
                                    
                                    import javax.faces.component.UIComponent;
                                    import javax.faces.context.FacesContext;
                                    
                                    import org.apache.commons.lang.StringUtils;
                                    import org.jboss.seam.ScopeType;
                                    import org.jboss.seam.annotations.AutoCreate;
                                    import org.jboss.seam.annotations.Logger;
                                    import org.jboss.seam.annotations.Name;
                                    import org.jboss.seam.annotations.Scope;
                                    import org.jboss.seam.annotations.faces.Converter;
                                    import org.jboss.seam.annotations.intercept.BypassInterceptors;
                                    import org.jboss.seam.log.Log;
                                    
                                    import com.foo.reporting.constants.Encoding;
                                    
                                    @Name("binaryStringConverter")
                                    @Scope(ScopeType.SESSION)
                                    @Converter
                                    @BypassInterceptors
                                    @AutoCreate
                                    public class BinaryStringConverter implements javax.faces.convert.Converter, Serializable {
                                    
                                     private static final long serialVersionUID = 1L;
                                    
                                     @Logger
                                     private static Log log;
                                    
                                     public Object getAsObject(FacesContext context, UIComponent component, String value) {
                                     return transformStringToObject(value);
                                     }
                                    
                                     public String getAsString(FacesContext context, UIComponent component, Object value) {
                                     return transformObjectToString(value);
                                     }
                                    
                                     public Object transformStringToObject(String value) {
                                     if (value == null || "".equals(value)) {
                                     return null;
                                     }
                                     String str = convertToUTF16(value);
                                     return str.getBytes();
                                     }
                                    
                                     public String transformObjectToString(Object value) {
                                     if (value == null) {
                                     return "";
                                     }
                                     String str = new String((byte[]) value);
                                     return convertToUTF8(str);
                                     }
                                    
                                     private String convertToUTF8(String str) {
                                     return convert(str, Encoding.UTF8, Encoding.UTF16);
                                     }
                                    
                                     private String convertToUTF16(String str) {
                                     return convert(str, Encoding.UTF16, Encoding.UTF8);
                                     }
                                    
                                     private String convert(String str, String fromEncoding, String toEncoding) {
                                     String returnValue = str;
                                     try {
                                     if (StringUtils.isNotEmpty(str) && StringUtils.isNotBlank(str)) {
                                     returnValue = new String(str.getBytes(fromEncoding), toEncoding);
                                     }
                                     } catch (UnsupportedEncodingException e) {
                                     log.error(e);
                                     }
                                     return returnValue;
                                     }
                                    
                                    }
                                    


                                    1 2 Previous Next