Accessing Java objects with Flex remoting

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on September 10, 2009 · 5 mins read

This post talks about how we:

  1. Query the database through Flex by invoking a server side Java object (the ItemDao) by Flex remoting with Blaze DS.
  2. Displays the result, which is a java.util.List in a DataGrid and a ComboBox.

Now that we have successfully configured Spring with Flex, we would like to access server side Java objects, which we expose through Spring.
We will query Items from the database using Spring/Hibernate through the ItemDao.
This is how my Item.java looks like:

/**
 *
 * @author paawak
 */
@Entity
public class Item implements Serializable {
    private static final long serialVersionUID = 340952899861L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 20, unique = true)
    private String code;
    @Column(length = 15, unique = true)
    private String name;
    @Column(length = 50)
    private String description;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

And this is how my ItemDao.java looks like:

/**
 *
 * @author paawak
 */
public class ItemDao extends HibernateDaoSupport {
    @SuppressWarnings("unchecked")
    public List getAll() {
        return super.getHibernateTemplate().loadAll(Item.class);
    }
}

As is obvious, I configure the ItemDao through Spring and then expose it as below:

...

...

The one thing to remember here is that, Flex handles remote access through call back mechanism. That is, the call to the Java object, is asynchronous. The call returns immediately. However, the result (or the error) is got asynchronously by registering suitable listeners.
The following code snippet illustrates this:



    
        
    
	
		
			
			
			
		
	

This is how it looks like:
flex_remote_object-1
The most important thing to note here is that the result that is got back in the call back function, should be cast with the proper data type in Flex. This is the link which has the details under the head Converting data from Java to ActionScript. From here we get to know the correct casting. In this case, we are casting the result into mx.collections.ArrayCollection since it is of java.util.List type.
Also note that though each element in the array is of the type Item, it can be accessed by its bean attribute name. That is how we can access the id, name and code in the DataGrid. Note that you cannot invoke the getter method like getId() on the object, it will not work.
So far, we have used a DataGrid which can extract data based on the attribute names. What if we use a ComboBox which needs a two dimensional array with data and label attributes? This is the code snippet of the modified getItemListResultHandler():

...
           [Bindable]
            private var itemComboArray:Array = new Array();
...
            private function getItemListResultHandler(event:ResultEvent):void {
                itemsMultiArray = (ArrayCollection)(event.result);
                var count:int = 0;
                for each (var item:Object in itemsMultiArray) {
                    var content:Object = new Object();
                    content["label"] = item.name;
                    content["data"] = item.id;
                    itemComboArray[count++] = content;
                }
            }

And this is the code to add the ComboBox:

 ...

...

The screen looks like this now:
flex_remote_object-2

Sources

https://github.com/paawak/legacy-apps-from-desktop/tree/master/products/ims/ims-web/trunk