j2flex - CRUD operations
In this article I'll talk about the j2flex client side API, more specifically how to read and modify data.
One of the main goals for j2flex was to keep things simple. For a default installation this means, that you do not have to code a single line of server side code to persist you date (of course you can do this if you want by using the server side Java API, but basic CRUD functionality is already inplemented for you). So let's see how this works.
j2flex comes as a compiled ActionScript libraray (SWC) that has to be added to the project's build path. The main class you're working with in the client side code is called J2FlexService. To create an instance of the service class in your Flex application you just do:
-
<j2flex:J2FlexService id="j2flex" destination="j2flex.service"/>
If you've been working with RemoteObjects or DataServices in Flex before this should look familiar. Actually, j2flex 1.0 requires an existing installation of Adobe's LiveCycle Data Services (LCDS) product so under the hood it uses the same messaging and transport infrastructure as LCDS (i.e. AMF3 over HTTP and/or RTMP).
Next, say we want to fetch a list of products from the database. Lets assume the products are stored inside a virtual table called products in the meta database. A list of objects is typically wrapped by an ArrayCollection in Flex, so just create one (in MXML in this case, could be ActionScript 3 as well, of course):
-
<mx:ArrayCollection id="products"/>
To display the list of products lets create a DataGrid control and bind it to products ArrayCollection:
-
<mx:DataGrid id="productsGrid" dataProvider="{products}"/>
Now, the final bits are to send a j2flex SQL statement to j2flex and populate the ArrayCollection. To execute a query you use the getObjects() method of the J2FlexService class. You can also pass in a ListCollectionView instance which will be automatically filled with the data returned by the server:
-
// invoke the getObjects method on the j2flex service instance
-
j2flex.getObjects("select * from products order by price ASC", products);
That's it - Flex will contact the server side J2FlexService class, fetch the data from the meta database and return the data as a list of objects to Flex. The ArrayCollection will update automatically, thus the DataGrid will display the records.
If you want to update existing data this is as simple as picking an object from the ArrayCollection, update properties of the object and finally send the object back to j2flex by calling the updateItem() method on the J2FlexService instance:
-
// e.g. pick the 5th product from the ArrayCollection
-
var product:Object = products.getItemAt(4);
-
-
// update the price
-
product.price = 1.99;
-
-
// update the name
-
product.name = "New Name";
-
-
// and persist it
-
j2flex.updateObject(product);
To create a new product you simply create a new Object instance (or Product subclass if you use typed Value Objects), set the object type (which is a static ID actually provided by j2flex in the moment you created the server side object definition), populate it with data and persist it:
-
// create a new product
-
var product:Object = new Object();
-
-
// set the type of the Object - this relates to a ID created by j2flex
-
product.obj_type = 1234;
-
-
// set the price
-
product.price = 1.99;
-
-
// set the name
-
product.name = "New Product";
-
-
// and persist it
-
j2flex.createObject(product);
Deleting objects works exactly the same - just pass the object that should get deleted to the deleteObject() method and you're done.
Important to note here: these operations will only execute successfully, if the current user is allowed to do it - so for every action, by default j2flex checks the effective user's rights against the object rights set. This is true for all read and write operations by default!
These are the basic CRUD operations directly built into j2flex. More details on additional features will be posted soon!
Dirk.


Hi, just curious - is this available to the public now? Or when do you expect that it might be? Will you be releasing it for free or selling it?
Hi,
we’ll be putting version 1.0 online next week so stay tuned.
Dirk.
Hi Drik,
Very good work done. Flex as well as Java developers will like it.
I want to use j2flex in my project, from where i will get swc and server side lib.
Good Work, keep it up.
Regards,
Parag