Announcement

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

    bug: ResultSet.valueMap

    SC_SNAPSHOT-2011-06-06/Pro Deployment 2011-06-06

    resultSet.getValueMap always returns a String -> String map.

    Observed: "id" field is of type Sequence, yet when valueMap.get(1) is called, there is no result. Instead, if valueMap.get("1") is called, the value is found.

    Expected: valueMap returns the correct type of map.

    Code:
    	Record record = new Record();
    		record.setAttribute("id", 1);
    		record.setAttribute("value", "one");
    		Record[] testData = { record };
    
    		final DataSource dataSource = new DataSource();
    		dataSource.addField(new DataSourceField("id", FieldType.SEQUENCE));
    		dataSource.addField(new DataSourceField("value", FieldType.TEXT));
    		dataSource.setClientOnly(true);
    		dataSource.setTestData(testData);
    
    		dataSource.fetchData(null, new DSCallback() {
    			@Override
    			public void execute(DSResponse response, Object rawData, DSRequest request) {
    				final ResultSet resultSet = new ResultSet(dataSource);
    				resultSet.setInitialData(response.getData());
    				Map valueMap = resultSet.getValueMap("id", "value");
    				Object result = valueMap.get(1);
    				Object result2 = valueMap.get("1");
    				SC.say("result=" + result + "<br>" + "result2=" + result2);
    			}
    		});

    #2
    It looks like valueMap on a ListGridField is supposed to always have a String key (!?!) - based on this crash after I tried using my non-String key map:

    Code:
    java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    	at com.smartgwt.client.util.JSOHelper.convertMapToJavascriptObject(JSOHelper.java:859)
    	at com.smartgwt.client.util.JSOHelper.setAttribute(JSOHelper.java:851)
    	at com.smartgwt.client.core.DataClass.setAttribute(DataClass.java:153)
    	at com.smartgwt.client.widgets.grid.ListGridField.setValueMap(ListGridField.java:3073)
    Since JSOHelper presumes a valueMap is indexed by String keys

    Code:
     public static JavaScriptObject convertMapToJavascriptObject(Map valueMap) {    	
            if(valueMap == null) return null;
            JavaScriptObject valueJS = JSOHelper.createObject();
            for (Iterator iterator = valueMap.keySet().iterator(); iterator.hasNext();) {
                [b]String key = (String) iterator.next();[/b]
                if (key == null) {
                    SC.logWarn("JSO::convertMapToJavascriptObject : Map contains null key - dropping this entry.");
                    continue;
                }

    Comment


      #3
      JavaScript Objects are always string-keyed and that's what we're using as it's the lightest weight representation. It works for everything valueMaps are used for. So the question is really: what probably are you trying to solve?

      Comment


        #4
        Ah. That makes more sense. So a javascript map will treat get("1") and get(1) as having the same key (since, as you said, all keys will be strings). This fails when I've grabbed a java version of the map which is typed, and get(1) get(1L) and get("1") are all different keys.

        What I was trying to do was dynamically change the valueMap for each row. It's really useful to know why the keys are strings.

        Comment


          #5
          I guess I could insulate myself from this map difference by using ResultSet.find(KEY_FIELD,key).getAttribute(VALUE_FIELD) - would that be as efficient as a map? (the word find makes me think probably not).

          Comment

          Working...
          X