Search

 

June 2007
S M T W T F S
« May   Jul »
 12
3456789
10111213141516
17181920212223
24252627282930

Tags

Archives


« | Main | »

AIR Embedded Database Sample Application

By Rich Tretola | June 11, 2007
10,891 views

EmployeeDirectory (Download the AIR Installer)

This application utilized the SQLite embedded database and the network connection event listeners which are part of Apollo.

It also uses Cairngorm for interacting with the remote ColdFusion server for backup and restore functionality.

Upon first run, the local SQLIte database and tables are created.

Attempting to add a new Employee record validates the form

The Employee record passes validation and is saved locally.

Selecting the record from the grid will put form in edit mode.

If the application detects a network connection, the server interaction buttons are available. Clicking Back Up sends the data to a remote ColdFusion server.

While the record is highlighted in the grid, hit the delete button which removes the local record from the SQLite database.

Clicking Restore will prompt the user to overwrite any local data.

The record is retrieved from the remote ColdFusion server and saved back into the local SQLite database.

I spent the past few hours patching errors that occurred when the public beta was launched so the source code is kind of ugly. I will post it as soon as I feel that it is in a clean state.

Topics: Adobe AIR | 4 Comments »

4 Responses to “AIR Embedded Database Sample Application”

  1. EverythingFlex » Blog Archive » AIR Embedded Database Code Exerpts 1 Says:
    June 12th, 2007 at 11:49 am

    [...] are some excerpts for the AIR Embedded Database [...]

  2. Super-simple SQLite example for Adobe AIR 1 Beta « Flash Enabled - Get Ready With Flash… Says:
    June 15th, 2007 at 4:13 am

    [...] but this code can probably be reused by anyone wanting to get started with AIR+SQLite. There are a couple of other AIR+SQLite examples out there to check out and don’t forget the documentation on the [...]

  3. Tiago Says:
    October 16th, 2007 at 6:41 am

    Hi Rich, how are you synchronizing the data between the local and the remote DB?
    Do you have any down loadable example?
    Thanks, Tiago

    Reply to this comment

  4. everythingflex Says:
    October 16th, 2007 at 7:07 am

    In this case I am simply passing the Objects to a remote ColdFusion server over AMF. I point my local AS3 object to my remote CFC:
    Here is the sample connection:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <mx:RemoteObject
            id="coldfusionService"
            destination="ColdFusion"
            endpoint="http://mydomain.com/flex2gateway/"
            showBusyCursor="true"
            source="com.everythingflex.employeeDirectory.business.Delegate"
            concurrency="last"
            result="event.token.resultHandler( event )"
            fault="event.token.faultHandler( event )"/>

    Employee ActionScript Object

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.everythingflex.employeeDirectory.vo
    {
        [RemoteClass(alias="com.everythingflex.employeeDirectory.vo.Employee")]
        [Bindable]
        public class Employee
        {
            public var employeeId:Number;
            public var firstName:String;
            public var lastName:String;
            public var email:String;
            public var jobTitle:String;
            public var userId:Number;
        }
    }

    Employee ColdFusion CFC

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    <cfcomponent output="false">

        <cfproperty name="employeeId" type="numeric" required="true" default="">
        <cfproperty name="firstName" type="string" required="true" default="">
        <cfproperty name="lastName" type="string" required="true" default="">
        <cfproperty name="email" type="string" required="true" default="">
        <cfproperty name="jobTitle" type="string" required="true" default="">
        <cfproperty name="userId" type="numeric" required="true" default="">

            <!--- Initialize vars --->
        <cfscript>
            variables.instance["employeeId"]=0;
            variables.instance["firstName"]="";
            variables.instance["lastName"]="";
            variables.instance["email"]="";
            variables.instance["jobTitle"]="";
            variables.instance["userId"]=0;
        </cfscript>

        <!--- Populate data --->
        <cffunction name="init" output="false" access="public" returntype="com.everythingflex.employeeDirectory.vo.Employee">
            <cfargument name="employeeId" type="numeric" required="yes">
            <cfargument name="firstName" type="string" required="yes">
            <cfargument name="lastName" type="string" required="yes">
            <cfargument name="email" type="string" required="yes">
            <cfargument name="jobTitle" type="string" required="yes">
            <cfargument name="userId" type="numeric" required="yes">

            <!--- Save it all --->
            <cfscript>
                setEmployeeId(arguments.employeeId);
                setFirstName(arguments.firstName);
                setLastName(arguments.lastName);
                setEmail(arguments.email);
                setJobTitle(arguments.jobTitle);
                setUserId(arguments.userId);
            </cfscript>

            <cfreturn this>
        </cffunction>

        <cffunction name="getEmployeeId" output="false" access="public" returntype="numeric">
            <cfreturn variables.instance.employeeId>
        </cffunction>
        <cffunction name="setEmployeeId" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="numeric">
            <cfset variables.instance.employeeId=arguments.val>
        </cffunction>

        <cffunction name="getFirstName" output="false" access="public" returntype="string">
            <cfreturn variables.instance.firstName>
        </cffunction>
        <cffunction name="setFirstName" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="string">
            <cfset variables.instance.firstName=arguments.val>
        </cffunction>

        <cffunction name="getLastName" output="false" access="public" returntype="string">
            <cfreturn variables.instance.lastName>
        </cffunction>
        <cffunction name="setLastName" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="string">
            <cfset variables.instance.lastName=arguments.val>
        </cffunction>

        <cffunction name="getEmail" output="false" access="public" returntype="string">
            <cfreturn variables.instance.email>
        </cffunction>
        <cffunction name="setEmail" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="string">
            <cfset variables.instance.email=arguments.val>
        </cffunction>

        <cffunction name="getJobTitle" output="false" access="public" returntype="string">
            <cfreturn variables.instance.jobTitle>
        </cffunction>
        <cffunction name="setJobTitle" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="string">
            <cfset variables.instance.jobTitle=arguments.val>
        </cffunction>

        <cffunction name="getUserId" output="false" access="public" returntype="numeric">
            <cfreturn variables.instance.userId>
        </cffunction>
        <cffunction name="setUserId" output="false" access="public" returntype="void">
            <cfargument name="val" required="true" type="numeric">
            <cfset variables.instance.userId=arguments.val>
        </cffunction>

    </cfcomponent>

    I do also have a central delegate but it simply calls this DAO to manage the remote data:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    <cfcomponent>
        <!--- Define properties --->
        <cfproperty name="datasource" type="string">

        <!--- Initialize vars --->
        <cfset datasource="EmployeeDirectory">

        <cffunction name="getEmployees" returntype="Array">
            <cfargument name="userId" default="true" required="false">
            <cfset var getEmployees = "">
            <cfset var employees = ArrayNew(1)>
            <cfset var args = StructNew()>
            <cfquery name="getEmployees" datasource="#datasource#">
                SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL,
                       JOB_TITLE, USER_ID
                FROM Employees
                WHERE USER_ID = #arguments.userId#
            </cfquery>
            <cfloop query="getEmployees">
                <cfscript>
                    args = StructNew();
                    StructInsert(args, "employeeId", getEmployees.EMPLOYEE_ID);
                    StructInsert(args, "firstName", getEmployees.FIRST_NAME);
                    StructInsert(args, "lastName", getEmployees.LAST_NAME);
                    StructInsert(args, "email", getEmployees.EMAIL);
                    StructInsert(args, "jobTitle", getEmployees.JOB_TITLE);
                    StructInsert(args, "userId", getEmployees.USER_ID);
                </cfscript>
                <cfinvoke component="com.everythingflex.employeeDirectory.vo.Employee" method="init"
                        argumentcollection="#args#"
                        returnvariable="employee">
                <cfset ArrayAppend(employees, employee)>
            </cfloop>
            <cfreturn employees/>
        </cffunction>

        <cffunction name="deleteEmployees" returntype="Boolean">
            <cfargument name="userId" default="0" required="false">
            <cftry>
                <cfquery name="deleteEmployees" datasource="#datasource#">
                    DELETE
                    FROM Employees
                    WHERE USER_ID = #arguments.userId#
                </cfquery>
                <cfreturn true>
            <cfcatch>
                <cfreturn false>
            </cfcatch>
            </cftry>
        </cffunction>

        <cffunction name="saveEmployees" returntype="numeric">
            <cfargument name="employees" type="Array" required="true">
            <cfset var returnCount = 0>
            <cfset var args = "">
            <cfscript>
                deleteEmployees(arguments.employees[1].getUserId());
            </cfscript>

            <cfloop from="1" to="#ArrayLen(employees)#" index="i">
                <cfscript>
                    args = StructNew();
                    StructInsert(args, "employee", employees[i]);
                </cfscript>
                <cfinvoke component="com.everythingflex.employeeDirectory.business.Delegate" method="saveEmployee"
                        argumentcollection="#args#"
                        returnvariable="result">
                <cfif result>
                    <cfset returnCount = returnCount + 1>
                </cfif>
            </cfloop>
            <cfreturn returnCount/>
        </cffunction>

        <cffunction name="saveEmployee" returntype="Boolean">
            <cfargument name="employee" type="com.everythingflex.employeeDirectory.vo.Employee">
            <cfset var returnVar = true>
            <cfset var saveEmployee = "">
            <cftry>
                <cfquery name="saveEmployee" datasource="#datasource#">
                    INSERT INTO Employees
                        (FIRST_NAME,
                         LAST_NAME,
                         EMAIL,
                         JOB_TITLE,
                         USER_ID)
                    VALUES
                        ('#employee.getFirstName()#',
                         '#employee.getLastName()#',
                         '#employee.getEmail()#',
                         '#employee.getJobTitle()#',
                         #employee.getUserId()#)
                </cfquery>
            <cfcatch>
                <cfset returnVar = false>
            </cfcatch>
            </cftry>
            <cfreturn returnVar/>
        </cffunction>

    </cfcomponent>

    Reply to this comment

Comments