Binding to server-side context variable from JavaFX

In addition to calling methods and Hibernate Validator, it’s also very simple to bind to a context variable and update it from JavaFX. A context variable is any variable inside a scope on the server.

Server

Seam component:

@Name ("shop")
@Scope (ScopeType.SESSION)
public class Store {

   IceCream iceCream; // getter and setter

   @Create
   public void init (){
	iceCream = new IceCream();
	iceCream.setName("Best ice cream");
	iceCream.setFlavors(2);
   }
}

Model:

@Entity
@Name ("icecream")
public class IceCream implements java.io.Serializable{

   @Max(value=5, message="Sorry, you can't have more than {value} flavors")
   private Integer flavors;

   private String name;

   // getters and setters
}

Client

Model:

public class IceCream implements java.io.Serializable{
   private String name;
   private Integer flavors;
   // setters and getters
}

BindingManager interface:

public interface BindingManager {
   public String commit(String componentName, Object value);
   public Object getObject(String name);
}
public class IceCreamServiceFactory {
   public static BindingManager getBindingManager() {
	return (BindingManager) FXServiceFactory.getService(BindingManager.class,
		"com.exadel.flamingo.service.binding.bindingManager");
   }
   public static String commit (String componentName, Object value){
	return getBindingManager().commit(componentName, value);
   }
   public static IceCream getIceCream (String componentName){
	return (IceCream)getBindingManager().getObject(componentName);
   }
}

JavaFX script:

CookieHandler.setDefault(new CookieManager(null,CookiePolicy.ACCEPT_ALL));
FXServiceFactory.URL = "http://localhost:8080/server-javafx/seam/resource/hessian/";

FX.println("Getting ice cream from server");
var iceCream = IceCreamServiceFactory.getIceCream('shop.iceCream');
FX.println ("  {iceCream}");

iceCream.setName ("Gelato");
iceCream.setFlavors(4);

FX.println("Starting commit");
IceCreamServiceFactory.commit("shop.iceCream", iceCream);
FX.println("Getting ice cream from the server after update");
FX.println ("   {IceCreamServiceFactory.getIceCream('shop.iceCream')}");

Line 5: Getting ice cream from the server (binding to context variable)
Line 8-9: Making changes to ice cream
Line 12: Committing changes to the server
Line 14: Getting the updated object from the server

Output:

Getting ice cream from server
  Name: Best ice cream, flavors: 2
Starting commit
Getting ice cream from the server after update
   Name: Gelato, flavors: 4

That’s it.

2 thoughts on “Binding to server-side context variable from JavaFX

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s