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...
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
TaskDS.java
GwtSearchParameter.java
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
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); } }
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) });
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()) ); }
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; } }
Any ideas as to what I have done wrong?
TIA
Dale
SmartGWT 2.2
GWT 2.0.4
Comment