Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Odd behaviour with DSRequest.getSortBy()

    Hi all,

    I am experiencing a strange behaviour when trying to get sort criteria. I have recentrly started working for a company and the code which is used to process the DSRequest handled it in such a way as to allow only one sort condition...

    Code:
    public class GwtRpcDataSourceUtils {
        /**
         * Private constructor. Class should not be instantiated - all functionality is provided through static methods.
         */
        private GwtRpcDataSourceUtils() {
        }
        
        ...
        ...
        
        public static SearchParameter getSearchParameter(DSRequest request) {
            int startIndex = (request.getStartRow() == null || request.getStartRow() < 0) ? 0 : request.getStartRow();
            int endIndex = (request.getEndRow() == null) ? -1 : request.getEndRow();
            int pageSize = -1;
            if (endIndex != -1) {
                pageSize = (endIndex - startIndex) + 1;
            }
    
            boolean ascending = true;
            String sortBy = null;
            if (request.getSortBy()[0] != null) {
                sortBy = request.getSortBy()[0].getField();
                ascending = request.getSortBy()[0].getSortDirection().equals(SortDirection.ASCENDING);
    
            }
            @SuppressWarnings("unchecked")
            Map<String, Serializable> criteria = request.getCriteria() != null ? request.getCriteria().getValues() : null;
            return new GwtSearchParameter(startIndex, pageSize, sortBy, ascending, criteria);
        }
    }
    This code I cannot change but as you can see, if just takes the first instance from the returned sort data. This code works fine. However I have written a new version of GwtSearchParameter to take its place to handle multiple search, however it does not work. This is the new code...

    TaskTabPage.java snippet
    Code:
    // Setup the data grid configuration
    listGrid.setWidth100();
    listGrid.setHeight(1);
    listGrid.setAlternateRecordStyles(true);
    listGrid.setBodyOverflow(Overflow.VISIBLE);
    listGrid.setOverflow(Overflow.VISIBLE);
    listGrid.setAutoFetchData(true);
    listGrid.setDataSource(TaskDS.getInstance(false));
    listGrid.setCriteria(getTaskCriteria());
    listGrid.setFixedRecordHeights(false);
    listGrid.setSelectionType(SelectionStyle.SINGLE);
    listGrid.setDataPageSize(20);
    
    listGrid.setFields(/** various fields **/);
    // Configure filter attributes
    listGrid.setCanSort(true);
    listGrid.setSort(new SortSpecifier[]{
        new SortSpecifier(TaskDS.STAGE, SortDirection.ASCENDING),
        new SortSpecifier(DataSourceConstants.SEQUENCE, SortDirection.ASCENDING)
    });
    TaskDS.java
    Code:
    protected void executeFetch(final String requestId, DSRequest request, final DSResponse response) {
    	GwtSearchParameter param = new GwtSearchParameter(request);
    
    	RpcServiceLocator.getTaskRpcService().getTasks(userTasks, param,
    		new FetchOperationAsyncCallback<TaskDto>(this, requestId, request, response, getMapper())
    	);
    }
    GwtSearchParameter.java
    Code:
    package com.serengetisystems.tcrm.client.util;
    
    import com.serengetisystems.srgutils.gwt.util.SearchParameter;
    import com.serengetisystems.srgutils.search.PagingParameter;
    import com.serengetisystems.srgutils.search.PojoPagingParameter;
    import com.smartgwt.client.data.Criteria;
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.SortSpecifier;
    import com.smartgwt.client.types.SortDirection;
    
    import java.io.Serializable;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    /**
     * Wrapper class for old GwtSearchParameter as old version does not allow for multiple sort criteria
     *
     * @author dale.ellis
     * @version 1.0
     * Version History
     * -----------------------------------------------------------------------------------
     * Changed By     When        Description
     * dale.ellis     07/04/11    Initial release
     */
    public class GwtSearchParameter implements SearchParameter, PagingParameter, Serializable {
    
        private LinkedHashMap<String, Boolean> multiSortData = new LinkedHashMap<String, Boolean>();
        private Map<String, Serializable> criteria;
        private int startIndex;
        private int pageSize;
    
        public GwtSearchParameter() {}
    
        /**
         *
         * @param criteria
         */
        public GwtSearchParameter(Map<String, Serializable> criteria) {
            this(0, Integer.MAX_VALUE, null, true, criteria);
        }
    
        /**
         *
         * @param request
         */
        public GwtSearchParameter(DSRequest request) {
            this.criteria = (request.getCriteria() != null ? request.getCriteria().getValues() : null);
            this.startIndex = (request.getStartRow() == null || request.getStartRow() < 0) ? 0 : request.getStartRow();
            this.pageSize = -1;
            int endIndex = (request.getEndRow() == null) ? -1 : request.getEndRow();
            if (endIndex != -1) {
                this.pageSize = (endIndex - startIndex) + 1;
            }
            try {
                SortSpecifier[] sorts = request.getSortBy();
                if (sorts != null) {
                    setSort(request.getSortBy());
                }
            } catch (Exception ex) {
                // request.getSortBy() causes java.lang.ClassCastException: java.lang.String cannot be cast to com.google.gwt.core.client.JavaScriptObject
            }
        }
    
        /**
         *
         * @param startIndex
         * @param pageSize
         * @param sortBy
         * @param ascending
         * @param criteria
         */
        public GwtSearchParameter(int startIndex, int pageSize, String sortBy, boolean ascending, Map<String, Serializable> criteria) {
            this.startIndex = startIndex;
            this.pageSize = pageSize;
            this.criteria = criteria;
            if (sortBy != null) {
                multiSortData.put(sortBy, ascending);
            }
        }
    
        public int getStartIndex()
        {
            return startIndex;
        }
    
        /**
         * @inheritDoc
         */
        public int getPageSize()
        {
            return pageSize;
        }
    
        /**
         * Add a sort into the map
         * @param sortBy
         * @param ascending
         */
        public void addSort(String sortBy, boolean ascending) {
            multiSortData.put(sortBy, ascending);
        }
    
        public boolean isMultiSort() {
            return (multiSortData.size() > 1);
        }
    
        @Override
        public String getSortBy() {
            return multiSortData.isEmpty() ? null : multiSortData.keySet().iterator().next();
        }
    
        public Map<String, Boolean> getMultiSortData() {
            return multiSortData;
        }
    
        @Override
        public boolean isAscending() {
            if (multiSortData.isEmpty()) {
                return true;
            } else {
                return multiSortData.get(multiSortData.keySet().iterator().next());
            }
        }
    
        // populates multi sortd
        public void setSort(SortSpecifier[] sortBy) {
            for (SortSpecifier sort : sortBy) {
                addSort(sort.getField(), sort.getSortDirection().equals(SortDirection.ASCENDING));
            }
        }
    
        /**
         * Returns filter criteria.
         * @return filter criteria.
         */
        public Map<String, Serializable> getCriteria()
        {
            return criteria;
        }
    }
    The issue I have is that whenever I call request.getSortBy() from my new code, it throws a java.lang.ClassCastException: java.lang.String cannot be cast to com.google.gwt.core.client.JavaScriptObject yet the orininal code can call request.getSortBy() without issue and I'm totally baffled.

    Any ideas as to what I have done wrong?
    TIA
    Dale

    SmartGWT 2.2
    GWT 2.0.4

    #2
    Sorry should have searched forum before, I see there is a workaround..

    request.getAttribute("sortBy")

    Still very odd that the static method works though! oh well

    Cheers,
    Dale

    Comment


      #3
      For anyone interested....

      I'm basing the - prefix on what I read here, not tested yet

      Code:
              try {
                  SortSpecifier[] sorts = request.getSortBy();
                  if (sorts != null) {
                      setSort(request.getSortBy());
                  }
              } catch (Exception ex) {
                  // request.getSortBy() causes java.lang.ClassCastException: java.lang.String cannot be cast to com.google.gwt.core.client.JavaScriptObject
                  // Workaround as this is a bug in SmartGWT 2.2
                  // http://forums.smartclient.com/showthread.php?t=8935&highlight=getSortBy%28%29
                  String sortAsString = request.getAttribute("sortBy");
                  if (sortAsString != null) {
                      for (String sort : sortAsString.split(",")) {
                          if (sort.startsWith("-")) {
                              addSort(sort.substring(1), false);
                          } else {
                              addSort(sort, true);
                          }
                      }
                  }
              }

      Comment

      Working...
      X