Ruby on Rails extension for Flash Builder 4

October 13, 2009

Adobe Flash Builder4 beta 2 is available on http://labs.adobe.com for downloading. One of the themes of Flash Builder 4 is to make data-centric development a lot more easier. You can find articles on developing Flex application for various server technologies using Flash Builder 4 at this URL http://sujitreddyg.wordpress.com/flash-builder-4

The Data Centric work flows in Flash Builder 4 are designed to be extensible i.e. you can add a new plugin to Flash Builder by implementing the interfaces exposed by Flash Builder and add support for any back end of your choice.

Gaurav Priyadarshi from Adobe Flex team extended to add support for consuming AMF(Remoting) based services running on a Ruby on Rails driven server.

You need Flash Builder 4 beta 2 to use this plugin, download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/ and then follow instructions below to set up Ruby on Rails plugin.

Please download the plugin and install from this URL http://dcdror.riaforge.org/

Details on installing the plugin and setting up Ruby on Rails project can be found at this URL http://dcdror.riaforge.org/blog/index.cfm/2009/10/9/Using-Ruby-On-Rails-plugin-for-DCD

Details on using the plugin can be found at this URL http://dcdror.riaforge.org/blog/index.cfm/2009/10/9/Using-the-plugin-from-FlashBuilder4


Building Flex and Java based CRUD application using Flash Builder 4

October 12, 2009

In this article we will create a CRUD application using Adobe Flex and Java. Flash Builder 4 allows you to build Flex front end for Java classes on the server with just couple of clicks, so we will use Flash Builder 4 to develop the application.

BlazeDS is a Java based server technology from Adobe which lets you invoke methods in Java class on server from your Flex applications. BlazeDS is the best solution for integrating Flex with Java so obviously we will use this in our sample project.

Below are the steps we will follow to complete our application

  1. Set up BlazeDS (copying files :) )
  2. Expose Java class as Remoting service destination
  3. Use Flash Builder to generate Flex code to invoke methods in Java class on the server
  4. Retrieve data from server and display in the application
  5. Allow user to add/update/delete entries in a database table from the application

Article allows you to skip any of steps mentioned above and copy the code :) Let’s get started :)

You can download the completed project from here http://sujitreddyg.com/fb4articles/beta2/BlazeDSCRUD.zip

Set up BlazeDS

Download BlazeDS from this URL http://flexorg.wip3.adobe.com/blazeds/trunk/11835/blazeds-bin-4.0.0.11835.zip follow instructions at this URL to setup BlazeDS http://sujitreddyg.wordpress.com/2009/04/07/setting-up-blazeds/

Once BlazeDS is setup you need to expose the Java class as Remoting destination, visit this URL to know how to expose a Java class as Remoting destination http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/

After exposing your Java class as Remoting destination using BlazeDS, follow steps below to build the Flex application.

In this sample we will expose SimpleCustomerService.Java as Remoting destination. SimpleCustomerService communicates with database and returns SimpleCustomer objects.

Setting up BlazeDS RDSDispatchServlet

After setting up BlazeDS, you have to enable RDSDisptachServlet, which is used by Flash Builder 4 to get destination details. This is disabled by default. It’s very simple all you have to do is to un-comment/add the Servlet mapping in the web.xml.

Copy the Servlet URL mapping and declaration for the RDSDispatchServlet below and copy it into your web application web.xml under <web-app> node.

<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id=”RDS_DISPATCH_MAPPING”>
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Creating new Flex project with BlazeDS server configurations

Create new Flex project

Create a new Flex project from the File menu and enter project details as shown below.

1

In this screen:

  1. Enter project name
  2. set “Web (runs in Adobe Flash Player)” as the application type
  3. Set the Flex SDK to Flex 3.4
  4. Set the application server type to J2EE
  5. Select BlazeDS
  6. Click on Next to continue

Configure J2EE server settings

2

In this screen:

  1. Set the Root folder to the root folder of your web application with BlazeDS configured. Its C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\demo in this sample
  2. Set the Root URL to root URL of your BlazeDS enabled web application
  3. Set the Context root to context root of your BlazeDS enabled web application
  4. Leave the output folder to default value
  5. Click on validate configuration button
  6. Click finish to continue

Using DCD feature to connect to consume Remoting Service

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4 which lets you develop data centric applications very easily. In this sample we will see how to create a Flex application which consumes BlazeDS Remoting service. Same steps can be followed for LCDS Remoting services also.

Creating Service using DCD

3

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

4

Select BlazeDS and click on Next. Flash Builder will display a window asking for RDS credentials. Since we turned off security for our RDSServlet in the web.xml, select “No password required” and click on OK to continue.

5

Selecting Remoting destination

You can see that Flash Builder listed all service destinations exposed from your web application as shown in the screen below.

6

In this screen we are selecting the destination the newly created service should use. Select a destination (SimpleCustomerServiceDestination in this sample) and click on Finish to continue.

Code for invoking the Remoting service will be generated, you can see the service and its operations (methods exposed by the Java class) being displayed in the “Data/Services” window and in the package explorer.

7

Flash Builder 4 introspects return types for the Java class methods and creates AS3 classes for any custom Java classes.  We have the service class ready, let’s create UI.

Creating UI

Please copy the code from this URL http://sujitreddyg.com/fb4articles/beta2/BlazeDSCRUD_1.mxml.txt into your main application file (BlazeDSCRUD.mxml in this sample). Code in the URL will add UI controls required for this sample application. After copying the code, your Flash Builder design view should look as shown in the image below.

Note: In this article to refer to a control, we will use value set for the id property of the controls.

8

Create Form for the Customer data type

We will display the details of the customer entry selected in the listCustomers control (created in previous step) in formSelectedCustomer. Using Flash Builder you can bind a Form control to an entity instance.  Right click on the formSelectedCustomer and select “Bind to Data” as shown in the image below.

9

Flash Builder will display window as shown in the image below.

10

In this screen:

  1. We want the Form items to be generated based on a data type (SimpleCustomer) so select “Data type”
  2. Select SimpleCustomer from the list of data types
  3. We want the form to be editable, so select the “Make form editable” check box
  4. Flash Builder will display the list of properties in the selected data type as shown in the image below. Flash Builder will also assign a UI control which it will use to display the property value as shown in the image below.

11

In this window, you can select the items which you want to be included in the form and UI control in which you want the property value to be displayed. You can also arrange the controls in the order in which you want them to be displayed. Arrange the properties and modify the controls as shown in the image above.

Flash Builder will add FormItems to formSelectedCustomer and binds the values of the controls under formSelectedCustomer to SimpleCustomer instance. In this sample Flash Builder generated a SimpleCustomer instance with “simpleCustomer” as its id. So we will refer to the SimpleCustomer instance bound to the items in formSelectedCustomer as simpleCustomer.

Binding data to UI Controls

Now that we have our UI is ready, let’s get the data from the server and display it in the application.

Right click on the List and click on “Bind to Data ..” as shown in the image below.

13

Flash Builder will display window as shown in the image below.

14

In this screen:

  1. Select New service call
  2. Set Service to SimpleCustomerServiceDestination
  3. Set Operation to getAllCustomers(): SimpleCustomer[]
  4. Set Bing to field to customerName
  5. Click on OK to continue

In previous step we bound the service call result to listCustomers component, listCustomers will send a request to the getAllCustomers() function on the server and displays the result.

listCustomers is displaying only the name of the customer, so let’s display complete details of the  customer in formSelectedCustomer. Right click on the listCustomers and select “Generate Change Handler” as shown in the image below.

15

Flash Builder will generate a function which will be invoked when user selects a different item in the listCustomers. Flash Builder will also switch the view to Source view as shown in the image below, so that you can write code in the handler function generated.

16

Add the code below to the generated change handler function as shown in the image above.

simpleCustomer = listCustomers.selectedItem as SimpleCustomer;

In the code snippet above, simpleCustomer is the instance of SimpleCustomer which was bound to the formSelectedCustomer. In the code above, we are passing the reference of the selected SimpleCustomer instance in listCustomers to the SimpleCustomer instance bound to formSelectedCustomer. This line of code will populate formSelectedCustomer with details of the selected customer in listCustomers control.

Now, let’s keep formSelectedCustomer populated with values of the first customer entry in listCustomers as soon as the customer data is loaded from the server.

Understanding the CallResponder

How will you know when the data is returned from the server? For each service call generated in the application there will be a CallResponder class instance associated with it. CallResponder dispatches result event when the call to the service is successful and the data returned from the server can be accessed using a property called lastResult in CallResponder instance. To map the CallResponder instance with the service call, you have to pass the reference of the AsyncToken returned by the service call to the token property of the CallResponder.  You can see service call AsyncToken reference passed to the CallResponder in listCustomers_creationCompleteHandler function as shown below.

protected function listCustomers_creationCompleteHandler(event:FlexEvent):void

{

getAllCustomersResult.token = simpleCustomerServiceDestination.getAllCustomers();

}

CallResponder class will dispatch result event when the data is retrieved successfully from the server. You can add a handler to the result event to perform any logic when the server response is successful. In our case we will keep the first Customer in the listCustomers to be selected and its value to be displayed in formSelectedCustomer as soon as the data from the server is loaded on to the client.

Add a result event handler to the getAllCustomersResult CallResponder as shown in the image below.

17

In the generated result handler add the following code. In the code below list is the List instance added in previous steps.

if(listCustomers != null)

{

listCustomers.selectedIndex = 0;

simpleCustomer = listCustomers.selectedItem as SimpleCustomer;

}

You can see the BlazeDSCRUD.mxml file with code till this step at this URL http://sujitreddyg.com/fb4articles/beta2/BlazeDSCRUD_2.mxml.txt

At this point you can save and run the application. Application will launch in a browser and displays the data retrieved from the server as shown in the image below. You can view details of the customs by selecting them from the list.

19

Performing Add/Update/Delete operations

In the SimpleCustomerService Java class on the server we have methods which can perform Add/Update/Delete operations in the database. If you observe Flash Builder generated AS3 functions in SimpleCustomerServiceDestination.as class for each public method of the selected Remoting destination including the addCustomer() , updateCustomer() and deleteCustomer(). We just have to invoke these AS3 function in order to invoke corresponding Java method on the server.

Let’s invoke functions in the SimpleCustomerDestination AS3 class when user clicks on Add/Update/Delete buttons created earlier.

Right click on the button labeled “Add” (id = “btnAdd”) and select “Generate Service Call ..” as shown in the image below.

20

Flash Builder displays window as shown in the image below, in which you can select the service call which you want to invoke when user clicks on the Button.

21

In this screen:

  1. Set the Service to SimpleCustomerServiceDestination
  2. Set the Operation to addCustomer(arg0:SimpleCustomer)
  3. Click on OK to continue

The addCustomer function selected expects an argument of the type SimpleCustomer . So Flash Builder 4 will switch to the source view and lets you enter the argument as shown in the image below.

22

simpleCustomer is the object which is bound to formSelectedCustomer and so any changes made by the user in formSelectedCustomer control will reflect in the simpleCustomer instance so pass simpleCustomer as an argument to the addCustomer function as shown in the image above. Code in the button click handler function will look as shown below. In the code snippet below we are passing the AsyncToken returned by addCustomer function to addCustomerResult, so that addCustomerResult object will dispatch result event when the service call is successful.

protected function btnAdd_clickHandler(event:MouseEvent):void

{

addCustomerResult.token = simpleCustomerServiceDestination.addCustomer(simpleCustomer);

}

Similarly generate service calls for Update and Delete buttons, select updateCustomer and deleteCustomer operations respectively and pass the same simpleCustomer object as argument. After generating service calls for other two buttons your application code in this URL http://sujitreddyg.com/fb4articles/beta2/BlazeDSCRUD_3.mxml.txt

Clicking on the Add/Update/Delete buttons will reflect the changes on the server. Let’s get the updated collection of customers from the server by invoking getAllCustomers function of the SimpleCustomerServiceDestination class if the Add/Update/Delete operations are successful.

You will have CallResponder instances created for Add, Update and Delete service calls in the application, invoke getAllCustomers() function in the result event of these call responders so that we will get updated customers list after Add, Update and Delete operation are performed successfully.

You can view/copy the completed BlazeDSCRUD.mxml file at this URL http://sujitreddyg.com/fb4articles/beta2/BlazeDSCRUD.mxml.txt

Save and run your application. You will see your application launched in a browser and ready for performing CRUD operations on the database entries.

Data centric applications can be developed very easily and effectively using Data Centric Development features in Flash Builder 4. Try enabling client side data management and client side data paging for the service used in this article, you can find the articles at the URL below. Please find more articles on using features in Flash Builder 4 at this URL http://sujitreddyg.wordpress.com/flash-builder-4/

Adobe Rocks :)


Building Flex applications for Java based HTTP Services using Flash Builder 4

October 5, 2009

Have a J2EE application and want to create Adobe Flex front end?  With Data Centric Development feature in Flash Builder 4 it’s very simple. This article explains communicating with Java classes on the server using HTTP Service.  Since its HTTP Service you will create a JSP/Sevlet which will invoke the required Java class and returns the data as either XML or JSON.

You can also invoke methods in Java classes on the server from Flex application and get objects as response instead of invoking a JSP/Servlet which returns XML/JSON. Please find more details on invoking Java methods from Flex application at this URL http://sujitreddyg.wordpress.com/2009/06/01/building-flex-application-for-blazeds-remoting-service-using-flash-builder-4/

In this article we will be using getallcustomers.jsp file which will invoke SimpleCustomerService.java class and returns the entries in the database in XML format. We will use Flash Builder DCD to create a Flex application which will consume the XML data returned by the server.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

1

Create a new Flex project as shown in the image above and explained below.

In this screen:

  1. Set the project name to “SampleHTTPJavaProject”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Click on finish to continue

Using DCD to consume data returned by the server

Data-Centric Development (DCD) is one of the advancements to the Flash Builder 4. Let’s see how DCD is making developers productive. We will use Flash Builder DCD to consume the data from the Java server.

Creating a service

2

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

3

Since we are using HTTP Service to communicate with the server, select HTTPService from the options and click on next button to continue.

Setting service properties

A service can have any number of operations. Each service will be a class and each operation of a service will be a function inside the class. Each operation has an URL associated with it, when the operation is invoked a HTTP request is sent to the corresponding URL. We will create a service and an operation to consume the data from the getallcustomers.jsp.

4

In this screen:

  1. Change the operation name to “getAllCustomers”
  2. Set the URL of the operation to http://<server-name>:<server-port>/<webapp-context>/getallcustomers.jsp in this sample it is http://localhost:9191/demo/getallcustomers.jsp
  3. Since we are not expecting any parameters in the JSP file we will skip the parameters section
  4. Set the service name to “CustomerService”
  5. Click on finish button to create the service

Service will be created and listed in the services explorer as shown in the image below.

5

Configuring return type

We will configure service to create an AS3 VO class based on nodes in the XML data returned by the JSP file. The VO class will be the return type for the operation. This will make coding easier, since it is easier to deal with strong typed objects than parsing the XML data.

Right click on the “getAllCustomers” operation and select “Configure return type” as shown in the image below.

6

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

7

Entering parameters required by the server

8

Our JSP file is not expecting any parameters, so we don’t have to pass any parameters in this step.

Flash Builder also allows you to give sample response rather than sending a request to the server to get the data. This will be useful when the server side code is not ready but you want to continue creating the Flex application without waiting for the server. Since we have the server ready in our case, let’s send a request to the server and configure return type. Click on next to continue.

Flash Builder parses the XML data returned by the server and displays the same as shown in the image below. You can select the XML node based on which you want the return type AS3 class to be generated. In this step, you can choose to create a new data type or merge the properties to existing AS3 class.

9

In this screen:

  1. Select the “customer” node
  2. Click on finish to continue

You can see the return type of the operation in the “data/services” panel changed as shown in the image below. When we invoke the “getAllCustomers” operation, response will be object of the type “Customer” will be created based on the response from the server and returned.

10

Binding data/service to UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to just bind a service response to a control.

11

In this screen:

  1. Switch to design view
  2. Change the Application layout to “vertical” using the properties panel
  3. Drag and drop a “DataGrid” from the “components” panel on to the design area
  4. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below

12

A window as shown in the image below will be launched.

13

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getAllCustomers():Customer[]” from the operations list
  4. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

14

Save and run the application, you can see the data retrieved from the Remoting service and displayed in the DataGrid as shown in the image below.

15

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for a JSP file using DCD features in Flash Builder. Flash Builder 4 rocks :)


Client Side Data Management using Flash Builder 4

June 8, 2009

Updated for Flash Builder 4 Beta 2 :)

Flash Builder 4 has Client side Data Management feature which lets you do the following

  1. Track changes to the local data
  2. Undo operations on the local data
  3. Automate common CRUD, instead of hand coding
  4. Automatically/Manually commit all local changes to server
  5. and more ……

We saw how we can point Flash Builder 4 to a Remoting destination and build Flex UI to it by just few clicks here http://sujitreddyg.wordpress.com/2009/06/01/building-flex-application-for-blazeds-remoting-service-using-flash-builder-4/

In this article we will see how we can enable client side Data Management for a Remoting destination. Client side Data Management is not specific to BlazeDS, it will work with any type of service including HTTP Service.

In this sample we will expose SimpleCustomerService.Java as Remoting destination. SimpleCustomerService communicates with database and returns SimpleCustomer objects.

To know how to expose Java classes as Remoting destination visit this URL http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/

Setting up BlazeDS RDSDispatchServlet

After setting up BlazeDS, you have to enable RDSDisptachServlet, which is used by Flash Builder 4 to get destination details. This is disabled by default. It’s very simple all you have to do is to un-comment/add the Servlet mapping in the web.xml.

Copy the Servlet URL mapping and declaration for the RDSDispatchServlet from the web.xml in the blazeds.war file and add to your web application web.xml file. Else copy it from below

<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id=”RDS_DISPATCH_MAPPING”>
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>

To use Client side data management, we need to create functions/methods on the server which will perform the CRUD operations on the server. Function/Method signatures on the server side should be as follows

  1. Add item – accepts entity type object and returns either ID of the added item or added entity object “public int addCustomer(SimpleCustomer customer) or public Customer addCustomer(Customer customer)
  2. Update item – accepts entity type object and returns nothing “public void updateCustomer(SimpleCustomer customer)
  3. Delete item – accepts either ID of the item or the entity object to delete and returns Boolean or nothing “public void deleteCustomer(int customerId) or public void deleteCustomer(SimpleCustomer customer)
  4. Get item – accepts ID of the item and returns entity object “public SimpleCustomer getCustomer(int customerId)

Now that we have our server side code ready, we will use Flash Builder to generate AS3 code to consume the Remoting service and enable data management on the client.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create new Flex project

1

Enter project details

2

In this screen:

  1. Enter project name
  2. set “Web (runs in Adobe Flash Player)” as the application type
  3. change the Flex SDK to Flex 3.4
  4. set the application server type to J2EE
  5. select BlazeDS
  6. click on Next

Configure J2EE server settings

3

In this screen:

  1. Set the Root folder to the root folder of your web application with BlazeDS configured. In this sample its C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\demo
  2. Enter the URL to your web application in Root URL
  3. Set the context root to context root of your web application
  4. Leave the output folder to default value
  5. Click on validate configuration button, you can see validation message on top of the screen
  6. Click finish to continue

Using DCD to generate code to consume service

4

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Creating new service

5

In this sample we are consuming a BlazeDS Remoting service so select BlazeDS and click on Next.

6

A Window asking if RDS server requires authentication will be displayed, just select “No password required” as we configured our RDS sever to not use authentication. Click OK to continue.

Selecting Remoting destination

7

In this screen we are selecting the destination the newly created service should use. All destinations available will be displayed, select the destination which points to the SimpleCustomerService.java. In this sample SimpleCustomerService is exposed as Remoting destination service with id – SimpleCustomerServiceDestination.

Code for invoking the Remoting service will be generated. You can also see the service and its operations (methods exposed by the Java class) being displayed in the “Data/Services” window and in the package explorer.

8

Configuring Client Side Data Management

Right click on the getAllCustomers() operation and select “Enable Data Mangement” as shown in the image below.

9

A window as shown in the image below will be launched.

10

Data Management needs unique identifier to manage the objects. In this screen, we should select the unique identifier for the data type, which is usually the primary key. Select the unique key (customerId in this sample) and click on next.

11

We need to map the Data Management operations to functions/methods from your service class to use for performing CRUD operations. Select appropriate operations as show in the image above. Operations should have signatures as explained in this article.

In this screen:

  1. Select the operations which have to be invoked for respective data management operations
  2. Click finish to continue.

Binding data to UI controls

Switch to design view as shown in the image below.

12

In this screen:

  1. Change the Application layout to vertical
  2. Drag and drop a DataGrid on to the design view
  3. Set the editable property of the DataGrid to true
  4. Select the DataGrid
  5. Right click on the DataGrid and select “Bind to Data”
  6. You will see a window with options to bind to a service as shown in the image below

14

In this screen:

  1. Select “New service call”, because we want to create a new service call
  2. Select “SimpleCustomerService” from services
  3. Select “getAllCustomers()” operation from the list
  4. Click on OK to continue

We will add a button which will commit all changes to the server. Add a Button control as shown in the image below.

17

In this screen:

  1. Add a Button control below DataGrid
  2. Set the label of the Button to “Commit”

Let’s generate a click handler for the button.

18

In this screen:

  1. Select the button and right click on the button
  2. Select “Generate click handler”
  3. Flash Builder will shift to the code view so that you can add code to the generated click handler as shown in the image below

20

In this screen, we will be invoking commit() function on the SimpleCustomerServiceDestination service so that any changes made to the data on the client will be saved on the server when the commit button is clicked.

Add “simpleCustomerServiceDestination.commit();” to the generated button click handler function as shown in the image above.

Save and run the application. Application will be launched in the browser as shown in the image below. Make changes to the data in the DataGrid and click on “commit” button. You will see all changes made to the data will be saved on the server.

Based on the changes to the data on the client, respective operations will be invoked. For example if you modified an entry, then the update operation will be invoked and on the server your class will have logic to update the same in the database.

23

You can perform all CRUD operations with just one line of code. Check the attached sample code to see how to add/delete an item. Please download MXML file with CRUD functionality implemented at this URL http://sujitreddyg.com/fb4articles/beta2/DMSample.mxml.txt

More article on Flash Builder 4 at this URL http://sujitreddyg.wordpress.com/flash-builder-4/

Flash Builder 4 rocks :)


Consuming JSON using Data Centric Development (DCD) feature in Flash Builder 4

June 5, 2009

Updated for Flash Builder 4 Beta 2 :)

Consuming a HTTP based service returning data in JSON format is very simple using Flash Builder 4. Flash Builder 4 understands JSON format and generates code to invoke the service and also create AS3 data types for the JSON response. To know how, continue reading this article.

In this sample we will create a Flex application which allows users to search using Yahoo search APIs.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

2

In this screen:

  1. Set the project name to “HTTPJSONSample”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Click on finish to continue

Using DCD to consume data returned by the server

Data-Centric Development (DCD) is one of the advancements to the Flash Builder 4. Let’s see how DCD is making developers productive. We will use Flash Builder DCD to consume the data from the Java server.

Creating a service

3

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

4

Since we are using HTTP Service to communicate with the server, select HTTPService from the options and click on next button to continue.

Setting service properties

A service can have any number of operations. Each service will be a class and each operation of a service will be a function inside the class. Each operation has an URL associated with it, when the operation is invoked a HTTP request is sent to the corresponding URL. We will create a service which will communicate with Yahoo search APIs.

5

In this screen:

  1. Set the operation name to “search”
  2. Set the URL of the operation to http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=India&output=json
  3. You can see the query string parameters in the URL are added to the “Parameters” section of the operation.
  4. Set the Service name to YahooSearchService
  5. Click on finish button to create the service

Service will be created and listed in the services explorer as shown in the image below.

6

Configuring return type

We will configure service to create an AS3 VO class based on the JSON data returned by the server. The VO class will be the return type for the operation. This will make coding easier, since it is easier to deal with strong typed objects.

Right click on the “searchWeb” operation in the services explorer and select “Configure return type” as shown in the image below.

7

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

8

Click Next to continue.

Entering parameters required by the server

9

In this screen:

  1. Select “Enter parameter values and call the operation”
  2. Enter “YahooDemo” for parameter “appId”
  3. Enter “India” for parameter “query”
  4. Enter “json” for parameter “output”

Flash Builder also allows you to give sample response rather than sending a request to the server to get the data. This will be useful when the server side code is not ready but you want to continue creating the Flex application without waiting for the server. Since we have the server ready in our case, let’s send a request to the server and configure return type. Click on next to continue.

Flash Builder parses the JSON data returned by the server and displays the same as shown in the image below. In this step, you can choose to create a new data type or merge the properties to existing AS3 class.

You can see the response from the server in the window as shown in the image below.

10

Set the name of the new data type to “SearchResultSet” and click on finish to continue.

You can see the return type of the operation changed as shown in the image below. When we invoke the “search” operation, response will be object of the type “SearchResultSet” will be created based on the response from the server and returned.

11

Generating form or displaying service results

Flash Builder 4 can generate form based on the response from the server or for the arguments for an operation. Right click on the “search” operation and select “Generate form” as shown in the image below.

12

You will see a window launched as shown in the image below. Select the service to invoke and the form items to create.

13

In this screen:

  1. Select “New service call”
  2. Select “YahooSearchService”
  3. Select “search” operation
  4. We need form to take input parameters which the service is expecting, so select “Form for input parameters”
  5. We will not generate form for return type, so deselect “For for return type”
  6. Click on next to continue

You can select the fields for which the form items have to be generated. You will see a window as shown in the image below. Select the fields for which you want the UI controls to be generated and click on finish.

14

In this screen we are selecting only “query” because that’s the only input we want from the user. We will be hard coding values for other two parameters. Select only “query” and click on finish to continue.

Flash Builder will switch to the source view so that you can enter the parameters to the operation.

15

In this screen:

  1. Change the arguments to the “search” function as shown in the image above
  2. Set the first argument to “YahooDemo” which is the application id for Yahoo
  3. Set the last argument to “json”, which is the response format

Displaying search result in DataGrid

Switch to design view as shown in the image below. You will see the user input forms generated in previous steps.

16

In this screen:

  1. Set the application layout to vertical
  2. Drag and drop a DataGrid from the components panel to the design view
  3. Select the DataGrid
  4. Set the width of the DataGrid to 100%

Right click on the DataGrid and select “Bind to Data” as shown in the image below.

17

A window as shown in the image below will be launched.

18

In this screen, select existing call result. This was the call we created for the input for created in previous steps. We are binding the result of the generated service to the DataGrid. Click ok to continue.

19

ResultSet was the return type for the “search” operation so the DataGrid is expecting SearchResultSet.ResultSet object from the service call. But the search results are available in the SearchResultSet.ResultSet.Result property as an array of Result objects. Image below shows the data types.

20

Lets modify DataGrid to show the title and URL of the Result objects.

22

In this screen:

  1. Switch to source view
  2. Change the dataProvider property of the DataGrid to “searchWebResult.lastResult.ResultSet.Result”
  3. Copy the code below under <mx:columns> of the DataGrid

<mx:DataGridColumn headerText=”Title” dataField=”Title” />

<mx:DataGridColumn headerText=”Url” dataField=”Url”/>

Save and run the application. Application will be launched in the browser enter the string you want to search in the TextInput and click on “Search Web” button. You will see request will be sent and the response being displayed as shown in the image below.

23

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for a JSON response from the Yahoo search API using DCD features in Flash Builder. Flash Builder 4 rocks :)


Building Flex applications for PHP based HTTP Services using Flash Builder 4

June 4, 2009

Updated for Flash Builder 4 Beta 2 :)

Have a PHP application and want to create Adobe Flex front end?  With Data Centric Development feature in Flash Builder 4 it’s very simple. This article explains communicating with PHP classes on the server using HTTP Service.  Since its HTTP Service you will create a PHP file which will invoke the required PHP class and returns the data as either XML or JSON.

You can also invoke functions in PHP classes on the server from Flex application and get objects as response instead of invoking a JSP/Servlet which returns XML/JSON. Please find more details on invoking PHP methods from Flex application at this URL http://sujitreddyg.wordpress.com/2009/06/01/building-flex-application-for-a-php-class-using-flash-builder-4/

In this article we will be using getallcustomers.php file which will invoke CustomerService.php class and returns the entries in the database in XML format. We will use Flash Builder DCD to create a Flex application which will consume the XML data returned by the server.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

1

Create a Flex project as shown in the image above.

Enter project details

2

In this screen:

  1. Set the project name to “SampleHTTPPHPProject”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Click on finish to continue

Using DCD to consume data returned by the PHP file

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4. Let’s see how this is making developers productive. We will use DCD to consume the data from the PHP file.

Creating a service

4

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

5

Select HTTPService from the options and click on next button to continue.

Setting service properties

A service can have any number of operations. Each service will be a class and each operation of a service will be a function inside the class. There is an URL associated with an operation, when the operation is invoked a HTTP request is sent to the corresponding URL. We will create a service and a operation to consume the data from the PHP file created above.

6

In this screen:

  1. Change the operation name to “getAllCustomers”
  2. Set the URL of the operation to http://<server-name>:<server-port>/<webapp-context>/getallcustomers.php in this sample it is http://localhost:9292/fb4beta2/getallcustomers.php
  3. Since we are not expecting any parameters in the PHP file we will skip the parameters section
  4. Set the service name to “CustomerService”
  5. Click on finish button to create the service

Service will be created and listed in the services explorer as shown in the image below.

7

Configuring return type

We will configure service to create an AS3 VO class based on nodes in the XML data returned by the PHP file. The VO class will be the return type for the operation. This will make coding easier, since it is easier to deal with strong typed objects than parsing the XML data.

Right click on the “getAllCustomers” operation and select “Configure return type” as shown in the image below.

8

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

9

Entering parameters required by the server

10

Our PHP class is not expecting any parameters, so we don’t have to pass any parameters in this step.

Flash Builder also allows you to give sample response rather than sending a request to the server to get the data. This will be useful when the server side code is not ready but you want to continue creating the Flex application without waiting for the server. Since we have the server ready in our case, let’s send a request to the server and configure return type. Click on next to continue.

You can see the response from the server in the window as shown in the image below.

11

Flash Builder parses the XML data returned by the server and displays the same as shown in the image above. You can select the XML node based on which you want the return type AS3 class to be generated. In this step, you can choose to create a new data type or merge the properties to existing AS3 class.

12

In this screen:

  1. Select the “customer” node
  2. Click on finish to continue

You can see the return type of the operation in the “data/services” panel changed as shown in the image below. When we invoke the “getAllCustomers” operation, response will be object of the type “Customer” will be created based on the response from the server and returned.

13

Binding data/service to UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to just bind a service response to a control.

14

In this screen:

  1. Switch to design view
  2. Change the Application layout to “vertical” using the properties panel
  3. Drag and drop a “DataGrid” from the “components” panel on to the design area
  4. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below

15

A window as shown in the image below will be launched.

16

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getAllCustomers():Customer[]” from the operations list
  4. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

17

Save and run the application, you can see the data retrieved from the Remoting service and displayed in the DataGrid as shown in the image below.

18

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for a PHP file exposed as HTTP Service using DCD features in Flash Builder. Flash Builder 4 rocks :)


Built in Data Paging using Flash Builder 4

June 3, 2009

Updated for Flash Builder Beta 2 :)

Flash Builder 4 lets you enable data paging with just few clicks for any server technology. It can be enabled for any service type (Including HTTP Service). Continue reading this article to know how.

For data paging to work you just need to implement 2 functions on the server and leave it to the generated Flex code by Flash Builder to invoke these functions when data has to be fetched.

Functions to implement:

  1. Returns number of rows in the database “count()”
  2. Takes two input arguments and returns the data collection. The two input arguments are startIndex and numberOfItems “getItems_paged($startIndex, $numItems)”

Data paging can be enabled for any type of service with any server technology. In this sample we will be using CustomerService.php which has the 2 required functions implemented and retrieves data from customers table. You can enable paging for any service similarly.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

1

Enter project details as shown in the image above and explained below

In this screen:

  1. Set the project name to “PagingSample”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Set the Application server type to “PHP”
  5. Click next to continue

2

In this screen:

  1. Set the Web root to the root folder of your PHP server. Its c:\apacheserver\htdocs in this sample
  2. Set the root URL to the root URL of your PHP server. Its http://localhost:9292 in this sample
  3. Set the output folder to root folder of your web application where you your PHP file is deployed
  4. Click the “Validate Configuration” to validate the server details entered
  5. Click on finish to continue

Using DCD to consume data returned by the PHP file

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4. Let’s see how this is making developers productive. We will use DCD to generate Flex applications to consume the data from the PHP file.

Creating a service

3

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

4

Since we want to communicate with the PHP we created, select PHP and click on next to continue.

Selecting PHP file for the service

6

In this screen:

  1. Browse and select the CustomerService.php file or the PHP file which has the required functions implemented
  2. Click on next button to continue

You will see a window displayed, saying Zend AMF library will be installed as shown in the image below. Just click on OK and continue will the set up. Flash Builder will deploy the Zend AMF on your PHP server.

ZendInstallScreen

Once the installation completes, Flash Builder will display a message. Clicking OK will display the functions in the PHP class. Functions are referred as “operation”. Image below shows the window with operations exposed.

7

Click on finish to continue. Service created above can be seen in the services explorer as shown in the image below.

8

Configuring return type

In this step we will test the operation and configure the return type on the client i.e. we will specify what type of object to create with the response from the server. This will make it easy for us to develop, since it is easier to deal with strong typed objects.

Right click on the “getItems_paged()” operation in the services explorer and select “Configure return type” as shown in the image below.

9

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

10

Invoking the operation and introspecting the response

Flash Builder sends request to the server and get the response and lets you configure the return type based on the response from server.

11

Operation requires two integer values as input arguments. Enter those two values as shown in the image above. You can see the response from the server in the window as shown in the image below.

12

Set the name of the data type to “Customer” as shown in the image above. Click on finish to continue.

You can see the return type of the operation changed as shown in the image below. When we invoke the “getItems_paged()” operation, response will be object of the type “Customer”, in our case ArrayCollection containing Customer objects.

Similarly configure return type for “count()” operation to “int” as shown in the image below.

13

“int” is an existing data type, so select the same from the existing data types list as shown in the image below.

14

Enabling paging

We have configured the two operations required for paging. To enable paging right click on “getItems_paged()” operation and select “Enable paging” as shown in the image below.

16

You will see a window as shown in the image below, where you need to select the unique identifier for the Customer class.

17

Select the unique identifier and click next to continue. customerId is the unique identifier in our case.

18

In this screen, select the operation to use for getting total count of records in the database and number of items to retrieve at a time. Click finish to continue.

Binding service results to UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to bind a service response to a UI control.

Switch to design view as shown in the image below.

20

In this screen:

  1. Change the Application layout to “vertical” using the properties panel
  2. Drag and drop a “DataGrid” from the “components” panel on to the design area
  3. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below.

21

A window as shown in the image below will be launched.

22

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getItems_Paged():Customer[]” from the operations list
  4. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

23

Save the application and run.

Paging is enabled and the data will be retrieved in chunks and only when it is required, for example when you scroll the DataGrid the data for the rows which are not retrieved will be retrieved.

More articles on using new features in Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we have data paging enabled with just few clicks. Flash Builder 4 rocks :)


Building a database based app using Flex and PHP with Flash Builder 4

June 1, 2009

Updated for Flash Builder 4 Beta 2 :)

You have a database table and want to create a Flex application using which you can perform CRUD operations on the database table? Flash Builder 4 makes it easy for you. Flash Builder generates PHP file with skeleton code to query data from the database. You just have to write your SQL queries for your database in the generated PHP file use Flash Builder 4 to generate Flex application. Continue reading this article to know how.

In this article will use Flash Builder Data Centric Development (DCD) feature to create a PHP file to query data from the database and Flex application which will consume the objects returned by the PHP class.

We will be using a table called customers in this sample. You can download the sql file from this URL http://sujitreddyg.com/fb4articles/beta2/customers.sql.txt

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

1

Enter project details as shown in the image above and explained below

In this screen:

  1. Set the project name to “NewPHPSample”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Set the Application server type to “PHP”
  5. Click next to continue

Configuring server settings

2

In this screen:

  1. Set the Web root to the root folder of your PHP server. Its c:\apacheserver\htdocs in this sample
  2. Set the root URL to the root URL of your PHP server. Its http://localhost:9292 in this sample
  3. Set the output folder to root folder of your web application where you want the PHP file to be deployed
  4. Click the “Validate Configuration” to validate the server details entered
  5. Click on finish to continue

Using DCD to consume data returned by the PHP file

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4. Let’s see how this is making developers productive. We will use DCD to consume the data from the PHP file.

Creating a service

3

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

4

Since we want to communicate with the PHP class, select PHP and click on next to continue.

Setting service properties

5

Since we don’t have the PHP class created and want Flash Builder to generate a sample PHP file. Click on the “click here to generate a sample” A window will be launched as shown in the image below.

6

Select the “Generate from template” option and click on OK to continue. Take a minute to read the message displayed by Flash Builder as shown in the image below.

7

Click OK to continue. You will observe that a sample PHP file will be create and the service settings will be updated to point the newly created PHP file as shown in the image below.

8

Click Next to continue.

You will see a window displayed, saying Zend AMF library will be installed as shown in the image below. Just click on OK and continue will the set up. Flash Builder will deploy the Zend AMF on your PHP server.

ZendInstallScreen

Once the installation completes, Flash Builder will display a message. Clicking OK will display the functions in the PHP class. Functions are referred as “operation”. Image below shows the window with operations exposed.

9

You can see that the generated sample PHP file has functions for all CRUD operations and more functions, which can be used for enabling client side data management using Flash Builder 4.  Click finish to continue. You can see the service created and its operations listed in the services explorer in the image below. Flash Builder will also try to open the created PHP file, we can view the file from the Flash Builder itself, you can just click on cancel.

10

Completing the PHP class generated

The PHP class generated by the Flash Builder contains skeleton code to connect to a database. You just have to change the SQL queries to query your database table. I updated the generated PHP file to retrieve data from the customers table. Download the updated the PHP file from this URL http://sujitreddyg.com/fb4articles/beta2/SamplePhp.php.txt and replace the generated file.

Configuring return type

In this step we will test the operation and configure the return type on the client i.e. we will specify what type of object to create with the response from the server. This will make it easy for us to develop, since it is easier to deal with strong typed objects.

Right click on the “getAllItems()” operation in the services explorer and select “Configure return type” as shown in the image below.

12

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

13

Click next to continue. You can see the response from the server in the window as shown in the image below.

14

You can see that Flash Builder introspected the objects returned from the server. Change the name of the data type to “Customer”. When we invoke the “getAllItems” operation, response will be object of the type “Customer”, in our case ArrayCollection containing Customer objects.

Click on finish to continue. You can see the return type of the operation changed as shown in the image below.

15

We will be using only getAllItems() so, we are not configure return types for other operations in this sample.

Displaying or binding service results in UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to bind a service response to a control with just a click.

16

In this screen:

  1. Switch to design view
  2. Change the Application layout to “vertical” using the properties panel
  3. Drag and drop a “DataGrid” from the “components” panel on to the design area
  4. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below.

17

A window as shown in the image below will be launched.

18

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getAllItems():Customer[]” from the operations list
  4. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

19

Save and run the application, you can see the data retrieved from the Remoting service and displayed in the DataGrid as shown in the image below.

20

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for a data base. Flash Builder 4 rocks :)


Building Flex application for a PHP class using Flash Builder 4

June 1, 2009

Updated for Flash Builder 4 Beta 2 :)

If you have a PHP class, Flash Builder 4 allows you to create a Flex front end for it with just few clicks. Continue reading this article to find out how.

In this article we will be creating Flex application which will invoke functions in CustomerService class which will return Customer class objects for each entry in the database. We will use Flash Builder DCD to create a Flex application which will consume the Customer objects returned by the CustomerService class.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://labs.adobe.com/technologies/flashbuilder4/

Create a new Flex project

1

Enter project details as shown in the image above and explained below

In this screen:

  1. Set the project name to “ExistingPHPSample”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Set the Flex SDK to “Flex 3.4”
  4. Set the Application server type to “PHP”
  5. Click next to continue

2

In this screen:

  1. Set the Web root to the root folder of your PHP server. Its c:\apacheserver\htdocs in this sample
  2. Set the Root URL to the root URL of your PHP server. Its http://localhost:9292 in this sample
  3. Leave the Output folder to default value
  4. Click the “Validate Configuration” to validate the server details entered
  5. Click on finish to continue

Using DCD to consume data returned by the PHP file

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4. Let’s see how this is making developers productive. We will use DCD to consume the data from the PHP file.

Creating a service

3

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

4

Since we want to communicate with the PHP we created, select PHP and click on next button.

Selecting PHP file for the service

5

In this screen:

  1. Browse and select the PHP file to use. Make sure the PHP file is deployed in your web application
  2. Click on next button to continue

You will see a window displayed, saying Zend AMF library will be installed as shown in the image below. Just click on OK and continue will the set up. Flash Builder will deploy the Zend AMF on your PHP server.

2.3

Once the installation completes, Flash Builder will display a message. Clicking OK will display the functions in the PHP class. PHP class functions are referred as “operation”. Image below shows the window with operations available.

6

Click on finish to continue. Service created above can be seen in the services explorer as shown in the image below.

7

Configuring return type

In this step we will test the operation and configure the return type on the client i.e. we will specify what type of object to create with the response from the server. This will make it easy for us to develop, since it is easier to deal with strong typed objects.

Right click on the “getAllCustomers” operation in the services explorer and select “Configure return type” as shown in the image below.

8

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

9

Click next to continue. You can see the response from the server in the window as shown in the image below.

10

Flash Builder introspected the Customer object returned from the server. Just click on finish. You can see the return type of the operation changed as shown in the image below. When we invoke the “getAllCustomers” operation, response will be object of the type “Customer”, in our case ArrayCollection containing Customer objects.

11

Displaying or binding service results in UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to just bind a service response to a control.

12

In this screen:

  1. Switch to design view
  2. Change the Application layout to “vertical” using the properties panel
  3. Drag and drop a “DataGrid” from the “components” panel on to the design area
  4. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below.

13

A window as shown in the image below will be launched.

14

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getAllCustomers():Customer[]” from the operations list
  4. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

15

Save and run the application, you can see the data retrieved from the Remoting service and displayed in the DataGrid as shown in the image below.

16

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for an existing PHP class using DCD features in Flash Builder 4 in just few minutes. Flash Builder 4 rocks :)


Using Flash Builder 4 for earlier BlazeDS builds

June 1, 2009

Flash Builder 4 has added Data-Centric Development features which will help you develop Flex applications for BlazeDS Remoting destinations very easily. You can create Flex front end for the Remoting destinations in just couple of clicks.

Flash Builder 4 uses the flex-rds.server.jar file which is added in BlazeDS 4. This jar file is used by Flash Builder 4 to retrieve details of the destinations exposed as Remoting services using BlazeDS. If you are using earlier builds of BlazeDS, you can still use new features in Flash Builder 4 by just copying the required jar file from BlazeDS 4 build.

Just follow the following simple steps to use Data-Centric Development features in Flash Builder 4

  1. Download BlazeDS 4 build from this URL http://flexorg.wip3.adobe.com/blazeds/trunk/11835/blazeds-bin-4.0.0.11835.zip
  2. Extract the downloaded zip file to a folder
  3. You will find blazeds.war file inside the extracted folder
  4. Extract the content from blazeds.war file so that we can use  the flex-rds-server.jar file
  5. Copy the flex-rds-server.jar file which will be found under WEB-INF/lib folder in blazeds.war to your web applications WEB-INF/lib folder
  6. Copy the Servlet URL mapping and declaration for the RDSDispatchServlet from the web.xml in the blazeds.war file and add to your web application web.xml file. Else copy it from below

<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id=”RDS_DISPATCH_MAPPING”>
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>

Restart your server and start using Flash Builder 4 for your BlazeDS projects as explained in this URL http://sujitreddyg.wordpress.com/2009/06/01/building-flex-application-for-blazeds-remoting-service-using-flash-builder-4/

More articles on how to use Flash Builder 4 – http://sujitreddyg.wordpress.com/flash-builder-4/

Flash Builder 4 is awesome :)