PageRenderTime 2103ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/flex/DocumentsFlexExample/src/com/custardbelly/couchdb/example/view/ContactsForm.as

http://github.com/bustardcelly/as3couchdb
ActionScript | 454 lines | 236 code | 44 blank | 174 comment | 13 complexity | 849502f898cb9bc3cd90ad838e235d4d MD5 | raw file
  1. package com.custardbelly.couchdb.example.view
  2. {
  3. import com.custardbelly.as3couchdb.core.CouchDatabase;
  4. import com.custardbelly.as3couchdb.core.CouchServiceResult;
  5. import com.custardbelly.as3couchdb.enum.CouchActionType;
  6. import com.custardbelly.as3couchdb.event.CouchEvent;
  7. import com.custardbelly.couchdb.example.event.ContactEvent;
  8. import com.custardbelly.couchdb.example.model.ContactDatabase;
  9. import com.custardbelly.couchdb.example.model.ContactDocument;
  10. import com.custardbelly.couchdb.example.serialize.ContactDocumentReader;
  11. import flash.display.Sprite;
  12. import flash.events.Event;
  13. import flash.events.MouseEvent;
  14. import flash.utils.getQualifiedClassName;
  15. import mx.binding.utils.BindingUtils;
  16. import mx.collections.ArrayCollection;
  17. import mx.core.FlexGlobals;
  18. import mx.events.StateChangeEvent;
  19. import mx.managers.PopUpManager;
  20. import mx.states.State;
  21. import spark.components.Button;
  22. import spark.components.List;
  23. import spark.components.VGroup;
  24. /**
  25. * ContactsForm is a main view controller for working with Database and Documents of a CouchDB instance using the contact models.
  26. * @author toddanderson
  27. */
  28. public class ContactsForm extends VGroup
  29. {
  30. public var loadDatabaseButton:Button;
  31. public var deleteDatabaseButton:Button;
  32. public var infoDatabaseButton:Button;
  33. public var contactList:List;
  34. public var addContactButton:Button;
  35. protected var database:CouchDatabase;
  36. protected var contactReader:ContactDocumentReader;
  37. protected var contactPanel:ContactEditPanel;
  38. protected var deletePanel:ContactDeletePanel;
  39. [Bindable] public var contacts:ArrayCollection;
  40. public static const AVAILABLE_STATE:String = "availableState";
  41. public static const UNAVAILABLE_STATE:String = "unavailableState";
  42. /**
  43. * Constructor.
  44. */
  45. public function ContactsForm()
  46. {
  47. super();
  48. contacts = new ArrayCollection();
  49. contactReader = new ContactDocumentReader();
  50. var availableState:State = new State();
  51. availableState.name = ContactsForm.AVAILABLE_STATE;
  52. var unavailableState:State = new State();
  53. unavailableState.name = ContactsForm.UNAVAILABLE_STATE;
  54. states = [availableState, unavailableState];
  55. addEventListener( StateChangeEvent.CURRENT_STATE_CHANGE, handleStateChange, false, 0, true );
  56. currentState = ContactsForm.UNAVAILABLE_STATE;
  57. }
  58. /**
  59. * @inherit
  60. */
  61. override protected function childrenCreated():void
  62. {
  63. super.childrenCreated();
  64. loadDatabaseButton.addEventListener( MouseEvent.CLICK, loadDatabase, false, 1, true );
  65. deleteDatabaseButton.addEventListener( MouseEvent.CLICK, deleteDatabase, false, 0, true );
  66. infoDatabaseButton.addEventListener( MouseEvent.CLICK, infoDatabase, false, 0, true );
  67. contactList.addEventListener( ContactEvent.DELETE, handleDeleteContact );
  68. contactList.addEventListener( ContactEvent.EDIT, handleEditContact );
  69. addContactButton.addEventListener( MouseEvent.CLICK, handleAddClick, false, 0, true );
  70. BindingUtils.bindProperty( contactList, "dataProvider", this, "contacts" );
  71. }
  72. /**
  73. * @private
  74. *
  75. * Loads a new database based on contact database model.
  76. * @param evt Event
  77. */
  78. protected function loadDatabase( evt:Event = null ):void
  79. {
  80. database = new ContactDatabase();
  81. database.addEventListener( CouchActionType.CREATE, handleCouchDatabaseReady, false, 0, true );
  82. database.addEventListener( CouchActionType.DELETE, handleCouchDatabaseDelete, false, 0, true );
  83. database.addEventListener( CouchEvent.RESULT, handleServiceResult, false, 0, true );
  84. database.addEventListener( CouchEvent.FAULT, handleServiceFault, false, 0, true );
  85. database.createIfNotExist();
  86. }
  87. /**
  88. * @private
  89. *
  90. * Deletes a database.
  91. * @param evt Event
  92. */
  93. protected function deleteDatabase( evt:Event = null ):void
  94. {
  95. database.remove();
  96. }
  97. /**
  98. * @private
  99. *
  100. * Queries info about a database.
  101. * @param evt Event
  102. */
  103. protected function infoDatabase( evt:Event = null ):void
  104. {
  105. database.info();
  106. }
  107. /**
  108. * @private
  109. *
  110. * Loads all documents from a database and resolves them to a contact document.
  111. * @param evt Event
  112. */
  113. protected function loadContacts( evt:Event = null ):void
  114. {
  115. database.addEventListener( CouchActionType.READ_DOCUMENTS, handleReadAllDocuments );
  116. // Grab all the documents.
  117. database.getAllDocuments();
  118. // Access by design view.
  119. // For example, a this design doc resides in the contacts DB:
  120. // {
  121. // "_id" : "_design/contacts",
  122. // "views" : {
  123. // "all" : {
  124. // "map" : "function(doc){ emit(doc._id, doc) }"
  125. // },
  126. // "lastNames" : {
  127. // "map" : "function(doc){ emit(doc.lastName, doc) }"
  128. // }
  129. // }
  130. // }
  131. // The following request will return a list of contacts only with the lastName property equal to Anderson by hitting this url:
  132. // http://127.0.0.1:5984/contacts/_design/contacts/_view/lastNames?key="Anderson"
  133. // database.getDocumentsFromView( "com.custardbelly.couchdb.model.ContactDocument", "contacts", "lastNames", "Anderson" );
  134. }
  135. /**
  136. * @private
  137. *
  138. * Resolves a generic object returned from _all_docs to a ContactDocument instance.
  139. * @param value Object
  140. * @return ContactDocument
  141. */
  142. protected function resolveToContact( value:Object ):ContactDocument
  143. {
  144. var contact:ContactDocument;
  145. try
  146. {
  147. // Try and fill document from result.
  148. // If faulted, document returned is not related to a ContactDocument.
  149. contact = contactReader.createDocumentFromResult( value, getQualifiedClassName( ContactDocument ) ) as ContactDocument;
  150. }
  151. catch( e:Error )
  152. {
  153. // Could not resolve generic object returned from _all_docs as a ContactDocument.
  154. }
  155. return contact;
  156. }
  157. /**
  158. * @private
  159. *
  160. * Adds the edit panel to the PopUpManager for editing a new or exisiting contact.
  161. * @param contact ContactDocument
  162. * @param title String
  163. */
  164. protected function showEditPanel( contact:ContactDocument, title:String ):void
  165. {
  166. if( contactPanel == null )
  167. {
  168. contactPanel = new ContactEditPanel();
  169. contactPanel.addEventListener( ContactEvent.SAVE, handleContactSave, false, 0, true );
  170. contactPanel.addEventListener( ContactEvent.CANCEL, handleCancelEditContact, false, 0, true );
  171. }
  172. contactPanel.title = title;
  173. contactPanel.contact = contact;
  174. PopUpManager.addPopUp( contactPanel, Sprite(FlexGlobals.topLevelApplication), true );
  175. PopUpManager.centerPopUp( contactPanel );
  176. }
  177. /**
  178. * @private
  179. *
  180. * Removes the edit panel from the PopUpManager.
  181. */
  182. protected function hideEditPanel():void
  183. {
  184. PopUpManager.removePopUp( contactPanel );
  185. }
  186. /**
  187. * @private
  188. *
  189. * Adds the delete panel to the PopUpManager for deleting an existing contact.
  190. * @param contact ContactDocument
  191. */
  192. protected function showDeletePanel( contact:ContactDocument ):void
  193. {
  194. if( deletePanel == null )
  195. {
  196. deletePanel = new ContactDeletePanel();
  197. deletePanel.addEventListener( ContactEvent.DELETE, handleContactDelete, false, 0, true );
  198. deletePanel.addEventListener( ContactEvent.CANCEL, handleCancelDeleteContact, false, 0, true );
  199. }
  200. deletePanel.contact = contact;
  201. PopUpManager.addPopUp( deletePanel, Sprite(FlexGlobals.topLevelApplication), true );
  202. PopUpManager.centerPopUp( deletePanel );
  203. }
  204. /**
  205. * @private
  206. *
  207. * Removes the delete panel from the PopUpManager.
  208. */
  209. protected function hideDeletePanel():void
  210. {
  211. PopUpManager.removePopUp( deletePanel );
  212. }
  213. protected function handleStateChange( evt:StateChangeEvent ):void
  214. {
  215. addContactButton.enabled = currentState == ContactsForm.AVAILABLE_STATE;
  216. deleteDatabaseButton.enabled = currentState == ContactsForm.AVAILABLE_STATE;
  217. infoDatabaseButton.enabled = currentState == ContactsForm.AVAILABLE_STATE;
  218. loadDatabaseButton.enabled = currentState != ContactsForm.AVAILABLE_STATE;
  219. if( currentState == ContactsForm.AVAILABLE_STATE )
  220. loadContacts();
  221. else
  222. contacts = new ArrayCollection();
  223. }
  224. /**
  225. * @private
  226. *
  227. * Event handler for read in of database, whether existant or newly created.
  228. * @param evt CouchEvent
  229. */
  230. protected function handleCouchDatabaseReady( evt:CouchEvent ):void
  231. {
  232. currentState = ContactsForm.AVAILABLE_STATE;
  233. }
  234. /**
  235. * @private
  236. *
  237. * Event handle for delete of database.
  238. * @param evt CouchEvent
  239. */
  240. protected function handleCouchDatabaseDelete( evt:CouchEvent ):void
  241. {
  242. currentState = ContactsForm.UNAVAILABLE_STATE;
  243. }
  244. /**
  245. * @private
  246. *
  247. * Eevnt handler for generic service result.
  248. * @param evt CouchEvent
  249. */
  250. protected function handleServiceResult( evt:CouchEvent ):void
  251. {
  252. trace( evt.data );
  253. }
  254. /**
  255. * @private
  256. *
  257. * Event handler for generic service fault.
  258. * @param evt CouchEvent
  259. */
  260. protected function handleServiceFault( evt:CouchEvent ):void
  261. {
  262. trace( evt.data );
  263. }
  264. /**
  265. * @private
  266. *
  267. * Event handler for success of read in all documents related to a database.
  268. * @param evt CouchEvent
  269. */
  270. protected function handleReadAllDocuments( evt:CouchEvent ):void
  271. {
  272. database.removeEventListener( CouchActionType.READ_DOCUMENTS, handleReadAllDocuments, false );
  273. contacts = new ArrayCollection();
  274. var contactList:Array = ( evt.data as CouchServiceResult ).data as Array;
  275. var i:int = contactList.length;
  276. var contact:ContactDocument;
  277. while( --i > -1 )
  278. {
  279. contact = resolveToContact( contactList[i] );
  280. if( contact )
  281. {
  282. contact.addEventListener( CouchActionType.CREATE, handleContactSaveResult, false, 0, true );
  283. contact.addEventListener( CouchActionType.UPDATE, handleContactSaveResult, false, 0, true );
  284. contact.addEventListener( CouchActionType.DELETE, handleContactDeleteResult, false, 0, true );
  285. contact.addEventListener( CouchEvent.FAULT, handleContactFault, false, 0, true );
  286. contacts.addItem( contact );
  287. }
  288. }
  289. }
  290. /**
  291. * @private
  292. *
  293. * Event handler for add request of a new contact.
  294. * @param evt MouseEvent
  295. */
  296. protected function handleAddClick( evt:MouseEvent ):void
  297. {
  298. var contact:ContactDocument = new ContactDocument();
  299. contact.addEventListener( CouchActionType.CREATE, handleContactSaveResult, false, 0, true );
  300. contact.addEventListener( CouchEvent.FAULT, handleContactFault, false, 0, true );
  301. showEditPanel( contact, "Add Contact:" );
  302. }
  303. /**
  304. * @private
  305. *
  306. * Event handler for delete request of contact.
  307. * @param evt ContactEvent
  308. */
  309. protected function handleDeleteContact( evt:ContactEvent ):void
  310. {
  311. var contact:ContactDocument = evt.contact;
  312. showDeletePanel( contact );
  313. }
  314. /**
  315. * @private
  316. *
  317. * Event handler for edit request of a contact.
  318. * @param evt ContactEvent
  319. */
  320. protected function handleEditContact( evt:ContactEvent ):void
  321. {
  322. var contact:ContactDocument = evt.contact;
  323. showEditPanel( contact, "Edit Contact:" );
  324. }
  325. /**
  326. * @private
  327. *
  328. * Event handler for save request of a contact.
  329. * @param evt ContactEvent
  330. */
  331. protected function handleContactSave( evt:ContactEvent ):void
  332. {
  333. var contact:ContactDocument = evt.contact;
  334. contact.addEventListener( CouchActionType.CREATE, handleContactSaveResult );
  335. contact.addEventListener( CouchActionType.UPDATE, handleContactSaveResult );
  336. contact.update();
  337. hideEditPanel();
  338. }
  339. /**
  340. * @private
  341. *
  342. * Event handler for cancel of edit/save of contact.
  343. * @param evt ContactEvent
  344. */
  345. protected function handleCancelEditContact( evt:ContactEvent ):void
  346. {
  347. hideEditPanel();
  348. }
  349. /**
  350. * @private
  351. *
  352. * Event handle for delete request of contact.
  353. * @param evt ContactEvent
  354. */
  355. protected function handleContactDelete( evt:ContactEvent ):void
  356. {
  357. var contact:ContactDocument = evt.contact;
  358. contact.remove();
  359. }
  360. /**
  361. * @private
  362. *
  363. * Event handler for cancel of delete of contact.
  364. * @param evt ContactEvent
  365. */
  366. protected function handleCancelDeleteContact( evt:ContactEvent ):void
  367. {
  368. hideDeletePanel();
  369. }
  370. /**
  371. * @private
  372. *
  373. * Event handler for successful save of contact document.
  374. * @param evt CouchEvent
  375. */
  376. protected function handleContactSaveResult( evt:CouchEvent ):void
  377. {
  378. loadContacts();
  379. }
  380. /**
  381. * @private
  382. *
  383. * Event handler for successful delete of contact document.
  384. * @param evt CouchEvent
  385. */
  386. protected function handleContactDeleteResult( evt:CouchEvent ):void
  387. {
  388. hideDeletePanel();
  389. var result:CouchServiceResult = evt.data as CouchServiceResult;
  390. var data:ContactDocument = result.data as ContactDocument;
  391. var i:int = contacts.length;
  392. while( --i > -1 )
  393. {
  394. if( contacts.getItemAt( i ) == data )
  395. break;
  396. }
  397. contacts.removeItemAt( i );
  398. }
  399. /**
  400. * @private
  401. *
  402. * Event handler for generic fault in couch request related to a contact document.
  403. * @param evt CouchEvent
  404. */
  405. protected function handleContactFault( evt:CouchEvent ):void
  406. {
  407. trace( evt.type );
  408. }
  409. }
  410. }