Monday, May 7, 2007

Serializing and deserializing objects with FDS

I was running into a problem with synchronizing objects which include child objects. The log files in the java classes returned fully filled object including child objects. However, when the sames objects came over to the Actionscript client side, the child object were in there initial state as shallow objects.

Several tries later and with the help of John Zhao from Abode, it turned out that the java classes need to be implemented as Serializable.


package com.grendel.java.data;
import java.io.Serializable

public class Person inplements Serializable
{
public int personId;
public Contact contact;

public Person () { super();}
}
package com.grendel.java.data;
import java.io.Serializable;

public class Contact inplements Serializable
{
public int contactId;
public String contact;
public Contact () { super();}
}

If you instantiate the Person class in the PersonAssembler or on the client the Contact child class will be serialized and deserialized during synchronization.

Friday, May 4, 2007

A wrapper class for DataService methods

Having multiple DataServices, each with their own getItem method (and createItem, deleteItem, fill and updateItem) means handling each event with an ItemReference. Since really the only things changing are the identity object, initial Object and the result handler, it is a perfect candidate for a wrapper class.

public class Services{
private var _personDS:DataService;
private var _workDS:DataService;
private var _instance:Services;

public static function getInstance(){
if (_instance == null) new Services();
return _instance;
}
public function Services(){
if (_instance == null) {
_instance = this;
_personDS = new DataService("my.person");
_workDS = new DataService("my.work");
}
else {
throw new Error("singleton class Services cannot accept multiple instances");
}
}
public function dsPerson():DataService{
return _personDS;
}
public function dsWork():DataService{
return _workDS;
}
public function getItem(ds:DataService,
identity:Object,
initObject:Object,
resFunction:Function) {
var token:ItemReference = ds.getItem(identity, initObject);
token.addResponder(new ItemResponder(resFunction, faultHandler));
}
private function faultHandler(fe:FaultHandler, token:Object=null):void{
Alert.show(fe.fault.faultString(), "Error in DataService");
}
}
You call the wrapped getItem method as follows. I really like it that in Flex 2 you do not need the Delegates anymore if you parameterize a function in a wrapper method, so calling these methods will be even easier:

private function getPerson(personId:int):void{
Services.getInstance().getItem(
Services.getInstance().dsPerson(),
{personId:personId},
new Person(),
personResHandler);
}
public function personResHandler(res:ResultEvent, token:Object=null):void{
var person:Person = Person(res.result);
Alert.show("Person retrieved is " + person.name);
}