ValueMap problem...

Hi all,

I have a small problem with
ValueMap

I have created a custom widget & I have passed a
HashMap<Long, Object[]>

as data to the
client
side using target.addAttribute("my map name", my map); .

Then I try to retrieve that same
map
from my client class as uidl.getMapAttribute("my map name") .
But what I get is a
ValueMap
, which I’m unable to iterate through it or get the values inside it. Also there’s no proper documentation in that API page too.

Can anyone help me in reading my values from the
ValueMap
.

Thanks in advance.
Asela.

Hi,

You can iterate through the ValueMap either by using the key set or by using the key array: Here is an example of both:

ValueMap map = uidl.getMapAttribute("myMap");

for (String key : map.getKeySet()) {
    Long lkey = Long.valueOf(key);
    String[] value = map.getStringArray(key);
}

JsArrayString keyArray = map.getKeyArray();
for (int i = 0; i < keyArray.length(); i++) {
    Long lkey = Long.valueOf(keyArray.get(i));
    String[] value = map.getStringArray(keyArray.get(i));
}

As you might notice everything will be a string as it passes to the client side since all objects get serialized on the way. So if you have your own objects you want to pass you should write your own serialization for them.

Hi John,

Thanks for the reply. But I have no clue of doing the following.

Is there a documentation or an example where I can refer to this?

Thanks & regards,
Asela.

Hi,

By serializing I mean that you should override the toString() method on the POJO so that it can be converted to a string. Here is example:

class MyClass{
    private static final String DELIMITER = ":";
    private String someString;
    private String anotherString; 

    public String toString(){
       return someString+DELIMITER+anotherString;
   }
}

Now when you add an instance of this object to the PaintTarget the toString() method will get called and the string will be sent to the client side implementation. On the client side you then need to de-serialize the string back by parsing the string received from the server side and creating a new client side object of it. Here is an example of the client side counterpart:

class VMyClass{
    private static final String DELIMITER = ":";
    private String someString;
    private String anotherString; 

    public void fromString(String str){
       this.someString = str.substring(0, str.indexOf(DELIMITER));
       this.anotherString = str.substring(str.indexOf(DELIMITER));
   }
}