Accessibility

Table of Contents

Automatically generating code for Flex 2 data access

A sample application from start to finish

This section walks you through the entire process of creating an application that lets you display and modify the Employee data. See a step-by-step video of the process.

This sample application uses the MySQL database and deploys the application under the JRun application server. This tutorial assumes that you have the Eclipse IDE with the Flex Builder plug-in installed.

Step 1: Get the DaoFlex package

The complete source for the DaoFlex package and sample application is included in DaoFlex.zip (ZIP, 9.11MB).

If you unzip it to the c:\TheRiaBook\tools\DaoFlex folder, you shouldn't need to modify any settings. If you unzipped the file in a different directory, modify the build script file at examples\build.xml.

Step 2: Start code generation and deployment for Flex Data Services

To start the code generation of a sample Employee application, open Eclipse IDE and create a new project in the Java perspective. Eclipse gives you an option to create a new project based on the preexisting Ant build script. DAOFlex includes this script in the examples subfolder of DaoFlex package.

To run the build.xml file from the examples directory:

  1. Right-click on the build.xml in the root of the project.
  2. Select the Run As | Ant Build menu.
  3. Select the Ant task build-original.

    The DaoFlex doclet automatically retrieves the database metadata related to the result of the query and combines it with the metadata provided by the Employee.java file.

    By using this combined metadata and running prewritten XSLT stylesheets against it, DaoFlex will generate the following:

    • Java and ActionScript versions of the EmployeeDTO class
    • Implementation of the abstract data access object (DAO) for remoting
    • Implementation of DAO and Assembler for data services
    • Configuration files for remoting and data services
    • A test MXML application

The entire process of generating the required files should not take more than a few seconds.

The examples folder

Next, let's examine the content of the examples folder, which contains the following subfolders and files:

  • src: The directory that contains the source code for the Employee.java class
  • jdbc: The theriabook.properties file that defines the JDBC driver and the data source. For example, the test database on localhost would have the following properties:
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test...
  • generated\metadata: This folder contains the XML for the metadata files generated by DaoFlex
  • generated\java: This folder contains the Java files generated by DAOFlex: Java DTO, DAO, Assemblers
  • generated\web: This folder contains the MXML, ActionScript and services configuration files.
  • build.xml: The Ant script file for our sample application. This file contains the "build-original" task that we'll run later.

The Employee.java file

The Employee.java file is located in the src directory and contains the source for the Employee.java class:

  • Employee.java has an abstract method, getEmployees(). Because getEmployees() is declared as an abstract method, it must be implemented in an Employee.java class descendent.
  • getEmployees() has only one parameter, startDate. Notice (in Listing 1) that this parameter maps to a binding variable with the same name in the tag @daoflex:sql.
  • getEmployees() returns a collection of DTO objects with a specified type of transferType=EmployeeDTO[]. The definition of this DTO is generated automatically.
  • The key columns are specified as keyColumns=EMP_ID, which performs the same role as <identity property="EMP_ID"/> in the FDS descriptor.
  • To add update capabilities to the generated application, we used the tablName=Employee attribute.
  • The data source that represents the database connection pool is annotated on the class level as pool=jdbc/theriabook. The name of the properties file must match the name used in the pool parameter. Individual methods can override this setting, and can use different connection pools.

The build.xml script

The build.xml script creates two .jar files:

  • The first jar file includes the generated DAOs, DTOs, and Assemblers (daoflex-examples-generated.jar).
  • The second jar file contains abstract DAO classes (daoflex-examples.jar).

The following code snippet from build.xml describes the location of the deployment directory within the JRun server that came with Flex Data Services:

<property name="deployment.web.root"     
                value="C:/fds/jrun4/servers/default/theriabook"/>
<property name="original.jar.destination" 
     value="${deployment.web.root}/WEB-INF/lib/daoflex-examples.jar"/>
<property name="generated.jar.destination" 
     value="${deployment.web.root}/WEB-INF/lib/daoflex-examples-
     generated.jar"/>