Search

 

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

Tags

Archives


« | Main | »

AIR Embedded Database Code Exerpts 2

By Rich Tretola | June 13, 2007
2,793 views

Here is today’s excerpt. This is the data access class that is used to save and retrieve the userid from the SQLite database. Here is the flow:

When the application launches, the PreferencesDAO class is instantiated. If a userid can not be retrieved from the local database, a Cairngorm event is dispatched to get a new userid from a remote SQL server via ColdFusion. The userid is then saved to the local database using the savePreferences() function.

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
package com.everythingflex.employeeDirectory.dao
{
    import com.adobe.cairngorm.control.CairngormEventDispatcher;
    import com.everythingflex.employeeDirectory.control.GetUserIdEvent;
    import com.everythingflex.employeeDirectory.model.ModelLocator;

    import flash.data.SQLStatement;
    import flash.events.SQLErrorEvent;
    import flash.events.SQLEvent;
    import flash.data.SQLResult;

    public class PreferencesDAO
    {

        private var model:ModelLocator = ModelLocator.getInstance();

        private var savePrefSQL:SQLStatement;
        private var getUserIdSQL:SQLStatement;

        private function statusHandler(event:SQLEvent):void {
        //  model.log += event;
        }

        // save preferences data
        public function savePreferences(userId:int):void{
            var sqlText:String = "INSERT INTO Preferences" +
            " (USER_ID)" +
            " VALUES(" + "'" + userId + "')"
            savePrefSQL = new SQLStatement();
            savePrefSQL.sqlConnection = model.db;
            savePrefSQL.addEventListener(SQLEvent.RESULT, savePreferencesResult);
            savePrefSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
            savePrefSQL.text = sqlText;
            savePrefSQL.execute();
            model.log += sqlText + " \n";
        }

        private function savePreferencesResult(event:SQLEvent):void{
            model.log += "add successful \n";
        }

        // get userid from local data
        public function getUserId():void{
            var sqlText:String = "SELECT USER_ID FROM Preferences";
            getUserIdSQL = new SQLStatement();
            getUserIdSQL.sqlConnection = model.db;
            getUserIdSQL.addEventListener(SQLEvent.RESULT, getUserIdResult);
            getUserIdSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
            getUserIdSQL.text = sqlText;
            getUserIdSQL.execute();
            model.log +=  sqlText + " \n";
        }

        // if local userid does not exist, retieve a new userid from remote server
        private function getUserIdResult(event:SQLEvent):void{
            var result:SQLResult = getUserIdSQL.getResult();
            if(result.data.length > 0){
                model.userId = result.data[0]["USER_ID"];
            } else {
                getRemoteUserId();
            }
        }

        // if connected, retrieve a new userid from remote server
        private function getRemoteUserId():void{
            var getUserIdEvent :GetUserIdEvent = new GetUserIdEvent();
            CairngormEventDispatcher.getInstance().dispatchEvent( getUserIdEvent );
        }
    }


}

Topics: Adobe AIR, Tutorials | No Comments »

Comments