/
SQLContactService.as
164 lines (130 loc) · 4.87 KB
/
SQLContactService.as
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.robotlegs.examples.addressbook.service
{
import com.probertson.data.QueuedStatement;
import com.probertson.data.SQLRunner;
import flash.data.SQLResult;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import mx.collections.ArrayCollection;
import org.robotlegs.examples.addressbook.model.ContactsModel;
import org.robotlegs.examples.addressbook.model.vo.Contact;
import org.robotlegs.examples.addressbook.service.events.ContactServiceEvent;
import org.robotlegs.mvcs.Actor;
public class SQLContactService extends Actor implements IContactService
{
[Inject]
public var model:ContactsModel;
[Inject]
public var sqlRunner:SQLRunner;
protected var newContact:Contact;
public function load():void
{
sqlRunner.execute(LOAD_CONTACTS_SQL, null, load_result, Contact);
}
private function load_result(result:SQLResult):void
{
model.list = new ArrayCollection(result.data);
dispatch(new ContactServiceEvent(ContactServiceEvent.LOADED));
}
public function save(contact:Contact):void
{
if(!model.list.contains(contact))
{
addNew(contact);
}
else
{
update(contact);
}
}
private function addNew(contact:Contact):void
{
var params:Object = new Object();
params["firstName"] = contact.firstName;
params["lastName"] = contact.lastName;
params["address"] = contact.address;
params["city"] = contact.city;
params["state"] = contact.state;
params["zip"] = contact.zip;
params["phone"] = contact.phone;
params["email"] = contact.email;
newContact = contact;
sqlRunner.executeModify(Vector.<QueuedStatement>([new QueuedStatement(ADD_CONTACT_SQL, params)]), addNew_result, database_error);
}
private function addNew_result(results:Vector.<SQLResult>):void
{
var result:SQLResult = results[0];
if (result.rowsAffected > 0)
{
var contactId:Number = result.lastInsertRowID;
loadNewContact(contactId);
}
else
{
dispatch(new ContactServiceEvent(ContactServiceEvent.SAVED));
}
}
private function loadNewContact(contactId:int):void
{
sqlRunner.execute(LOAD_CONTACT_SQL, {contactId:contactId}, loadNewContact_result, Contact);
}
private function loadNewContact_result(result:SQLResult):void
{
if (result.data != null && result.data.length > 0)
{
var contact:Contact = result.data[0];
newContact.update(contact)
model.list.addItem(newContact);
model.selected = newContact;
newContact = null;
}
dispatch(new ContactServiceEvent(ContactServiceEvent.SAVED));
}
private function update(contact:Contact):void
{
var params:Object = new Object();
params["contactId"] = contact.contactId;
params["firstName"] = contact.firstName;
params["lastName"] = contact.lastName;
params["address"] = contact.address;
params["city"] = contact.city;
params["state"] = contact.state;
params["zip"] = contact.zip;
params["phone"] = contact.phone;
params["email"] = contact.email;
sqlRunner.executeModify(Vector.<QueuedStatement>([new QueuedStatement(UPDATE_CONTACT_SQL, params)]), update_result, database_error);
}
private function update_result(results:Vector.<SQLResult>):void
{
dispatch(new ContactServiceEvent(ContactServiceEvent.SAVED));
}
public function remove(contact:Contact):void
{
model.remove(contact);
sqlRunner.executeModify(Vector.<QueuedStatement>([new QueuedStatement(DELETE_CONTACT_SQL, {contactId:contact.contactId})]), remove_result, database_error);
}
private function remove_result(results:Vector.<SQLResult>):void
{
}
private function database_error(error:SQLError):void
{
dispatch(new SQLErrorEvent(SQLErrorEvent.ERROR, false, false, error));
}
// ------- SQL statements -------
[Embed(source="sql/LoadContacts.sql", mimeType="application/octet-stream")]
private static const LoadContactsStatementText:Class;
private static const LOAD_CONTACTS_SQL:String = new LoadContactsStatementText();
[Embed(source="sql/LoadContact.sql", mimeType="application/octet-stream")]
private static const LoadContactStatementText:Class;
private static const LOAD_CONTACT_SQL:String = new LoadContactStatementText();
[Embed(source="sql/AddContact.sql", mimeType="application/octet-stream")]
private static const AddContactStatementText:Class;
private static const ADD_CONTACT_SQL:String = new AddContactStatementText();
[Embed(source="sql/UpdateContact.sql", mimeType="application/octet-stream")]
private static const UpdateContactStatementText:Class;
private static const UPDATE_CONTACT_SQL:String = new UpdateContactStatementText();
[Embed(source="sql/DeleteContact.sql", mimeType="application/octet-stream")]
private static const DeleteContactStatementText:Class;
private static const DELETE_CONTACT_SQL:String = new DeleteContactStatementText();
}
}