Data Management service of LiveCycle DS (LCDS) is amazing. It gives developers enormous power to create data driven applications.
I created a sample application which will retrieve data from a database using LCDS. In this sample I used SQLAssembler packaged with LCDS. SQLAssembler allows you to create CRUD applications by just creating SQL queries in an XML file. Isn’t that amazing? I just have to mention SQL queries in an XML file and everything else is handled by the assembler. You can also create custom assemblers, but that is not part of this discussion 🙂
mx:DataService is the component used in the Flex application to communicate with data management service of LCDS on the server. This component is packaged in fds.swc.
You might be thinking why I should use Data Management service when I can retrieve data from a database using Remoting service. But that is not all it does, when you use Data Management service of LCDS. It will also automatically synchronize data on the client with data on the server. Imagine if there are two clients are accessing same data and one client changes data and other client still is looking at the old data. Data management service of LCDS will solve this by pushing the changes to all other clients. Isn’t that amazing. 🙂 It’s got more. Check out this URL for more features you get: http://www.adobe.com/products/livecycle/dataservices/features.html
Let’s jump into the code 🙂
Sample application
We will create one MXML file and add a destination to the data-management-config.xml. In this sample I am using the samples web application packaged with LCDS.
You have to configure your Flex application to access services-config.xml of the samples web application. This configuration is very similar to configuring your Flex application to use BlazeDS. Please check this URL for details on how to configure your Flex application to point to your BlazeDS folder. All the steps are same except that the folder you point should be wither samples web application of LCDS.
Flex Application
You can see that we are invoking initApp() method on creationComplete event of the application. in the initApp() method we are invoking fill() method of the DataService component. fill() method is the one which will connect to the server, get data and fill the array collection. First parameter to the fill() method is reference to the array collection and second parameter is used by the SQlAssembler to execute appropriate fill operation. Another worth mentioning in the code below is the DataService component. You can observe that we just gave the name of the destination to the DataService.
.mxml file
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical” creationComplete=”initApp();”>
<mx:Script>
<![CDATA[
import mx.events.CollectionEvent;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var companies:ArrayCollection = new ArrayCollection();
private var product:Product;
private function initApp():void
{
dsCompany.fill(companies, “all”);
}
]]>
</mx:Script>
<mx:DataService id=”dsCompany” destination=”sql-product”/>
<mx:DataGrid id=”companiesDg” dataProvider=”{companies}” editable=”true” width=”100%” height=”100%”/>
</mx:Application>
Adding destination to data-management-config.xml
I used the destination which is available in the samples application packaged with LCDS. If you are using samples web application of LCDS then please check of there is a destination with id sql-product in the data-management-config.xml. If this destination is not available in the configuration file, please add destination in this xml file to your data-management-config.xml file under <service> tag. For just retrieving data, the content in the destination you have to worry about is the <fill> tag. The SQL query inside the <fill> tag is the one which is executed when you invoke fill() method in the Flex application. The second parameter we passed “all” is used to select the appropriate query inside the <fill> tag. You can find the <fill> tag extracted from the destination tag below. Observe the <name> tag and the <sql> tag.
Please find more details on configuring SQLAssembler destination at this URL
<fill>
<name>all</name>
<sql>SELECT * FROM PRODUCT ORDER BY NAME</sql>
</fill>
That is all you need to do. 🙂 You are all set to go. Just restart your server and the run the Flex application. You will be able to view the data in the DataGrid. We did not discuss the updating part here, but when we update the data from the Flex application you can see that all other clients viewing this data will get the latest data. 🙂