Integrating Flex with JBoss Seam - Flamingo Goodies

On top of providing the gluing between Seam and Flex, Flamingo ships with a couple of goodies that can’t be ignored. SeamBinding automatically synchronizes Seam and Flex object and SeamValidator performs Flex validation based on server side rules (all the code samples are taken from this demo).

Validation

When you create database tables you specify a set of rules for each field. Say the field can’t be null, it must have more than 5 characters, etc. These rules must be enforced on the front end as well. In Flex we’d have to create validators for each field and for each rule to make sure that we don’t break any of the database constraints. Flamingo’s SeamValidator automatically validates Flex elements based on server side constraints.

On the server side the database management is done by Hibernate so we just need to specify rules for each field (CommandItem.java):

@Length(min=3, max=40)
private String command;

Flamingo automatically checks against Hibernate rules and performs the validation directly on Flex objects (silvafug.mxml):

<flamingo:SeamValidator id="validator" destination="commandItem"
        source="{commandInput}" validationTarget="command"
        property="text" />
<mx:TextInput id="commandInput"/>
<mx:Button label="Validate Command" click="validator.validate()"/>

That’s all what it takes for the magic to happen. Pretty neat, isn’t it?

Binding

Binding a Flex and a Java Object can be done automatically using Flamingo’s SeamBinding. Note that we can get the same results with RemoteObjects (pass the object to the server as an argument - see the post on Remote Calls) but SeamBinding simplifies our lifes a bit by removing some extra steps.

Say we have a Java Object (CommandItem.java):

@Name("commandItem")
@Entity
@Scope(SESSION)
public class CommandItem implements Serializable
{
	private static final long serialVersionUID = 1892843745280885007L;

	@Id @GeneratedValue
	private Integer dbID;

	@Length(min=3, max=40)
	private String command;

	private Integer distance;

	public Integer getDbID() {
		return dbID;
	}
	public void setDbID(Integer dbID) {
		this.dbID = dbID;
	}
	public String getCommand() {
		return command;
	}
	public void setCommand(String command) {
		this.command = command;
	}
	public Integer getDistance() {
		return distance;
	}
	public void setDistance(Integer distance) {
		this.distance = distance;
	}
}

and a Flex object (CommandItem.as):

[RemoteClass(alias="com.java.package.CommandItem")]
[Bindable]
public class CommandItem
{
	public var dbID:int;
	public var command:String;
	public var distance:int;
}

then SeamBinding seamlessly keeps the 2 objects synchronized. All we need to do is connect the 2 objects and call on commit or update depending on which direction we want to perform the synchronization (silvafug.mxml):

<flamingo:SeamBinding destination="commandItem"
        id="bindingService" source="{commandItem}"/>
<mx:Button label="Do Something" click="bindingService.commit()"/>

Tada! When bindingService.commit() is called we got our client side object automatically saved in the server session. If we want it stored in the database all we’d have to do is to persist the server side object.

4 comments so far

Hey there!

I’m actually (attempting) to use Flamingo with my Flex app because of this series of blog entries, it sounds very promising. I’m trying to convert my Flex apps backend from RoR to Seam, and so far it’s all been a learning curve. Anyway.

I’ve got Flamingo mostly working, but something I can’t quite find an example of is doing a bean call like:

getPlayer(id)

and have it return a mapped Player object.. I’ve done the whole thing with having a proxy object on the Flex side with the proper metadata, etc - the Seam side works fine, it finds the object and hucks it over the connection, but then Flex chokes with a Type Coercion error - it seems that the thing it’s getting back is an AsyncToken - you have this issue? Your example does it a little differently, so I can’t really apply what you are doing.

Anyway, if you’ve got a word of wisdom or two to share, I’d love to hear em.

Cheers,
Jason

Jason
June 11th, 2008 at 4:01 pm

@ Jason

My guess is that you are passing in a field that actionscript doesn’t understand. I only had some problems before when mapping a long (since there is no long in actionscript). Are you able to get the object in javascript (take a look at view/doSomething.xhtml on the demo code to see how to get the object in javascript)? Also, what are you using in Flex to make the remote call? Is it a Flamingo specific component or is it a RemoteObject?

darius
June 12th, 2008 at 11:45 am

what to do if I want to have two components with same mapping and need to bind them with different bean parameters? I always have the same result (the last one executed)! I mean, having something similar to:

the reason of my issue is that my bean takes a parameter, on which it returns a different value. Since it seems that the only way to use flamingo is this “lastResult” thing, I always get both name and surname fields stuck to the name or the surname, depending on which is called later. How may I solve this?

paolo
June 16th, 2008 at 4:26 am

@ paolo

Unfortunately your code didn’t come through.

SeamBinding is meant to synchronize two java/flex data objects. It is not meant to do function calls. You are right, if you want to do a call with a parameter your best shot would be to use lastResult. After each call you need to store the result in some variable because lastResult always shows the result from the latest server call. To make it short, I would set the result parameter on the RemoteObject to call a function that saves the return value (maybe push it on an array?)

darius
June 17th, 2008 at 12:58 am

Leave a Comment

Name (required)

Mail (will not be published) (required)

Website

Comment