Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update to support attachment requests.

  • Loading branch information...
commit 34abec6288b062e9e6332fdb1be80fd888ea71e8 1 parent c7a0f5f
Todd Anderson authored
Showing with 1,343 additions and 372 deletions.
  1. BIN  bin/as3couchdb.swc
  2. +1 −1  src/com/custardbelly/as3couchdb/as3couchdb_internal.as
  3. +146 −0 src/com/custardbelly/as3couchdb/command/CouchRequestCommand.as
  4. +32 −18 src/com/custardbelly/as3couchdb/command/{PendingRequestCommand.as → CreateDatabaseCommand.as}
  5. +7 −1 src/com/custardbelly/as3couchdb/command/IRequestCommand.as
  6. +151 −0 src/com/custardbelly/as3couchdb/core/CouchAttachment.as
  7. +5 −5 src/com/custardbelly/as3couchdb/core/CouchDatabase.as
  8. +20 −7 src/com/custardbelly/as3couchdb/core/CouchDocument.as
  9. +1 −1  src/com/custardbelly/as3couchdb/core/CouchModel.as
  10. +1 −1  src/com/custardbelly/as3couchdb/core/CouchModelEntity.as
  11. +12 −2 src/com/custardbelly/as3couchdb/core/CouchServiceFault.as
  12. +1 −1  src/com/custardbelly/as3couchdb/core/CouchServiceResult.as
  13. +6 −4 src/com/custardbelly/as3couchdb/core/CouchSession.as
  14. +1 −1  src/com/custardbelly/as3couchdb/core/CouchUser.as
  15. +1 −1  src/com/custardbelly/as3couchdb/enum/CouchActionType.as
  16. +2 −2 src/com/custardbelly/as3couchdb/enum/CouchContentType.as
  17. +1 −1  src/com/custardbelly/as3couchdb/enum/CouchFaultType.as
  18. +1 −1  src/com/custardbelly/as3couchdb/enum/CouchRequestMethod.as
  19. +1 −1  src/com/custardbelly/as3couchdb/enum/CouchRequestStatus.as
  20. +1 −1  src/com/custardbelly/as3couchdb/event/CouchEvent.as
  21. +20 −48 src/com/custardbelly/as3couchdb/mediator/CouchDatabaseActionMediator.as
  22. +36 −5 src/com/custardbelly/as3couchdb/mediator/CouchDocumentActionMediator.as
  23. +10 −14 src/com/custardbelly/as3couchdb/mediator/CouchSessionActionMediator.as
  24. +1 −1  src/com/custardbelly/as3couchdb/mediator/ICouchDatabaseActionMediator.as
  25. +5 −1 src/com/custardbelly/as3couchdb/mediator/ICouchDocumentActionMediator.as
  26. +6 −7 src/com/custardbelly/as3couchdb/mediator/ICouchSessionActionMediator.as
  27. +1 −1  src/com/custardbelly/as3couchdb/mediator/IServiceMediator.as
  28. +229 −0 src/com/custardbelly/as3couchdb/mediator/helper/AttachmentRequestQueue.as
  29. +115 −0 src/com/custardbelly/as3couchdb/responder/AbstractDatabaseResponder.as
  30. +116 −0 src/com/custardbelly/as3couchdb/responder/AbstractDocumentResponder.as
  31. +1 −1  src/com/custardbelly/as3couchdb/responder/BasicCouchResponder.as
  32. +3 −10 src/com/custardbelly/as3couchdb/responder/CreateDatabaseResponder.as
  33. +3 −8 src/com/custardbelly/as3couchdb/responder/CreateDocumentResponder.as
  34. +2 −7 src/com/custardbelly/as3couchdb/responder/DeleteDatabaseResponder.as
  35. +3 −8 src/com/custardbelly/as3couchdb/responder/DeleteDocumentResponder.as
  36. +1 −1  src/com/custardbelly/as3couchdb/responder/ICouchServiceResponder.as
  37. +67 −4 src/com/custardbelly/as3couchdb/responder/ReadAllDocumentsResponder.as
  38. +6 −45 src/com/custardbelly/as3couchdb/responder/ReadDatabaseResponder.as
  39. +5 −10 src/com/custardbelly/as3couchdb/responder/ReadDocumentResponder.as
  40. +39 −5 src/com/custardbelly/as3couchdb/responder/ReadDocumentsFromViewResponder.as
  41. +6 −45 src/com/custardbelly/as3couchdb/responder/UpdateDocumentResponder.as
  42. +1 −1  src/com/custardbelly/as3couchdb/serialize/CouchDatabaseReader.as
  43. +1 −1  src/com/custardbelly/as3couchdb/serialize/CouchDatabaseWriter.as
  44. +28 −2 src/com/custardbelly/as3couchdb/serialize/CouchDocumentReader.as
  45. +1 −1  src/com/custardbelly/as3couchdb/serialize/CouchDocumentWriter.as
  46. +1 −1  src/com/custardbelly/as3couchdb/serialize/ICouchDatabaseReader.as
  47. +1 −1  src/com/custardbelly/as3couchdb/serialize/ICouchDatabaseWriter.as
  48. +1 −1  src/com/custardbelly/as3couchdb/serialize/ICouchDocumentReader.as
  49. +1 −1  src/com/custardbelly/as3couchdb/serialize/ICouchDocumentWriter.as
  50. +3 −3 src/com/custardbelly/as3couchdb/service/AbstractCouchRequest.as
  51. +63 −27 src/com/custardbelly/as3couchdb/service/CouchDatabaseService.as
  52. +50 −10 src/com/custardbelly/as3couchdb/service/CouchDocumentService.as
  53. +3 −3 src/com/custardbelly/as3couchdb/service/CouchRequest.as
  54. +37 −12 src/com/custardbelly/as3couchdb/service/CouchService.as
  55. +2 −2 src/com/custardbelly/as3couchdb/service/ExInCouchRequest.as
  56. +17 −9 src/com/custardbelly/as3couchdb/service/HTTPCouchRequest.as
  57. +2 −2 src/com/custardbelly/as3couchdb/service/HTTPSessionRequest.as
  58. +28 −14 src/com/custardbelly/as3couchdb/service/ICouchDatabaseService.as
  59. +32 −7 src/com/custardbelly/as3couchdb/service/ICouchDocumentService.as
  60. +1 −1  src/com/custardbelly/as3couchdb/service/ICouchRequest.as
  61. +3 −2 src/com/custardbelly/as3couchdb/service/ICouchService.as
  62. +1 −1  src/com/custardbelly/as3couchdb/util/UUID.as
View
BIN  bin/as3couchdb.swc
Binary file not shown
View
2  src/com/custardbelly/as3couchdb/as3couchdb_internal.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: as2couchdb_internal.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
146 src/com/custardbelly/as3couchdb/command/CouchRequestCommand.as
@@ -0,0 +1,146 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: CouchRequestCommand.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: CouchService.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.command
+{
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.responder.BasicCouchResponder;
+ import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
+ import com.custardbelly.as3couchdb.service.ICouchRequest;
+
+ import flash.net.URLRequest;
+
+ /**
+ * CouchRequestCommand is generic wrapper for invoking a ICouchRequest with held arguments.
+ * @author toddanderson
+ */
+ public class CouchRequestCommand implements IRequestCommand
+ {
+ protected var _couchRequest:ICouchRequest;
+ protected var _urlRequest:URLRequest;
+ protected var _requestType:String;
+ protected var _responder:ICouchServiceResponder;
+
+ protected var _nextCommand:IRequestCommand;
+
+ /**
+ * Constructor.
+ * @param couchRequest ICouchRequest
+ * @param urlRequest URLRequest
+ * @param type String
+ * @param responder ICouchServiceResponder
+ */
+ public function CouchRequestCommand( couchRequest:ICouchRequest, urlRequest:URLRequest, type:String, responder:ICouchServiceResponder = null )
+ {
+ _couchRequest = couchRequest;
+ _urlRequest = urlRequest;
+ _requestType = type;
+ _responder = responder;
+ }
+
+ /**
+ * @private
+ *
+ * Executes the next command (if available)in the chain.
+ */
+ protected function executeNextCommand():void
+ {
+ if( _nextCommand ) _nextCommand.execute();
+ }
+
+ /**
+ * @private
+ *
+ * Responder method for successful result in service request.
+ * @param result CouchServiceResult
+ */
+ protected function handleRequestResult( result:CouchServiceResult ):void
+ {
+ if( _responder ) _responder.handleResult( result );
+ executeNextCommand();
+ }
+
+ /**
+ * @private
+ *
+ * Responder method for fault in service request.
+ * @param fault CouchServiceFault
+ */
+ protected function handleRequestFault( fault:CouchServiceFault ):void
+ {
+ if( _responder ) _responder.handleFault( fault );
+ executeNextCommand();
+ }
+
+ /**
+ * Accessor/Modifier to optional next command in chain.
+ * @return IRequestCommand
+ */
+ public function get nextCommand():IRequestCommand
+ {
+ return _nextCommand;
+ }
+ public function set nextCommand( value:IRequestCommand ):void
+ {
+ _nextCommand = value;
+ }
+
+ /**
+ * Invokes wrapped ICouchRequest with passed arguments.
+ */
+ public function execute():void
+ {
+ _couchRequest.execute( _urlRequest, _requestType, new BasicCouchResponder( handleRequestResult, handleRequestFault ) );
+ }
+ }
+}
View
50 ...belly/as3couchdb/command/PendingRequestCommand.as → ...belly/as3couchdb/command/CreateDatabaseCommand.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
- * <p>Class File: PendingRequestCommand.as</p>
- * <p>Version: 0.3</p>
+ * <p>Class File: CreateDatabaseCommand.as</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,22 +26,20 @@
*/
package com.custardbelly.as3couchdb.command
{
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.enum.CouchFaultType;
+ import com.custardbelly.as3couchdb.enum.CouchRequestStatus;
import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
import com.custardbelly.as3couchdb.service.ICouchRequest;
import flash.net.URLRequest;
-
+
/**
- * PendingRequestCommand is generic wrapper for invoking a ICouchRequest with held arguments.
+ * CreateDatabaseCommand is a CouchRequestCommand extensions to properly handle the response of database already existant in CouchDB instance.
* @author toddanderson
*/
- public class PendingRequestCommand implements IRequestCommand
+ public class CreateDatabaseCommand extends CouchRequestCommand
{
- protected var _couchRequest:ICouchRequest;
- protected var _urlRequest:URLRequest;
- protected var _requestType:String;
- protected var _responder:ICouchServiceResponder;
-
/**
* Constructor.
* @param couchRequest ICouchRequest
@@ -49,20 +47,36 @@ package com.custardbelly.as3couchdb.command
* @param type String
* @param responder ICouchServiceResponder
*/
- public function PendingRequestCommand( couchRequest:ICouchRequest, urlRequest:URLRequest, type:String, responder:ICouchServiceResponder = null )
+ public function CreateDatabaseCommand( couchRequest:ICouchRequest, urlRequest:URLRequest, type:String, responder:ICouchServiceResponder=null )
{
- _couchRequest = couchRequest;
- _urlRequest = urlRequest;
- _requestType = type;
- _responder = responder;
+ super( couchRequest, urlRequest, type, responder );
}
/**
- * Invokes wrapped ICouchRequest with passed arguments.
+ * @private
+ *
+ * Override to check if fault do to already existant database. If so, move on to any subsewuent chain commands. Most likely it is a read command.
+ * @param fault CouchServiceFault
*/
- public function execute():void
+ override protected function handleRequestFault( fault:CouchServiceFault ):void
{
- _couchRequest.execute( _urlRequest, _requestType, _responder );
+ // If a 412 has happened, customize the fault response.
+ if( fault.status == CouchRequestStatus.HTTP_ALREADY_EXISTS )
+ {
+ if( _nextCommand != null )
+ {
+ executeNextCommand();
+ }
+ else
+ {
+ var fault:CouchServiceFault = new CouchServiceFault( CouchFaultType.DATABASE_ALREADY_EXISTS, fault.status, "Database already exists." );
+ super.handleRequestFault( fault );
+ }
+ }
+ else
+ {
+ super.handleRequestFault( fault );
+ }
}
}
}
View
8 src/com/custardbelly/as3couchdb/command/IRequestCommand.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: IRequestCommand.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -32,6 +32,12 @@
public interface IRequestCommand
{
/**
+ * Accessor/Modifier to optional next command in chain.
+ * @return IRequestCommand
+ */
+ function get nextCommand():IRequestCommand;
+ function set nextCommand( value:IRequestCommand ):void;
+ /**
* Executes command.
*/
function execute():void;
View
151 src/com/custardbelly/as3couchdb/core/CouchAttachment.as
@@ -0,0 +1,151 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: CouchAttachment.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.core
+{
+ import com.custardbelly.as3couchdb.as3couchdb_internal;
+
+ import flash.utils.ByteArray;
+
+ /**
+ * CouchAttachment is a respresentation of the JSON object of an attachment associated with a document.
+ * @author toddanderson
+ */
+ public class CouchAttachment
+ {
+ protected var _fileName:String;
+ protected var _contentType:String;
+ protected var _data:*;
+ protected var _revisionPosition:String;
+ protected var _document:CouchDocument;
+ protected var _isDirty:Boolean;
+ protected var _isDeleted:Boolean;
+
+ /**
+ * Constructor.
+ * @param fileName String
+ * @param contentType String
+ */
+ public function CouchAttachment( fileName:String = "", contentType:String = "", data:* = null )
+ {
+ _fileName = fileName;
+ _contentType = contentType;
+ _data = data;
+ }
+
+ /**
+ * Returns flag of CouchAttachment instance having changed.
+ * @return Boolean
+ */
+ public function isDirty():Boolean
+ {
+ return _isDirty || ( _document == null );
+ }
+
+ /**
+ * Accessor/Modifier of the fileName.
+ * @return String
+ */
+ public function get fileName():String
+ {
+ return _fileName;
+ }
+ public function set fileName(value:String):void
+ {
+ _fileName = value;
+ _isDirty = true;
+ }
+
+ /**
+ * Accessor/Modifier of the content type for the data.
+ * @return String
+ */
+ public function get contentType():String
+ {
+ return _contentType;
+ }
+ public function set contentType(value:String):void
+ {
+ _contentType = value;
+ _isDirty = true;
+ }
+
+ /**
+ * Accessor/Modifier for the byte array of data to send to CouchDB.
+ * @return *
+ */
+ public function get data():*
+ {
+ return _data;
+ }
+ public function set data(value:*):void
+ {
+ _data = value;
+ _isDirty = true;
+ }
+
+ /**
+ * Accessor/Modifier for the revision position of the document that relates to this attachment.
+ * @return String
+ */
+ public function get revisionPosition():String
+ {
+ return _revisionPosition;
+ }
+ public function set revisionPosition( value:String ):void
+ {
+ _revisionPosition = value;
+ }
+
+ /**
+ * Accessor/Modifier to denote the attachement as having been deleted from the attachment list of an associated document.
+ * @return Boolean
+ */
+ public function get isDeleted():Boolean
+ {
+ return ( _isDeleted && _document != null );
+ }
+ public function set isDeleted( value:Boolean ):void
+ {
+ _isDeleted = value;
+ }
+
+ /**
+ * Internal Accessor/Modifier for related document. This is internal and is only set in parsing JSON object returned from COuchDB.
+ * To add attachments to a CouchDocument instance, use the CouchDocument#attachments modifier. When a CouchDocument is saved,
+ * it will look to see if a correlating CouchDocument is associated with this CouchAttachment. If not it will push the document as well.
+ * @return CouchDocument
+ */
+ as3couchdb_internal function get document():CouchDocument
+ {
+ return _document;
+ }
+ as3couchdb_internal function set document( value:CouchDocument ):void
+ {
+ _document = value;
+ }
+ }
+}
View
10 src/com/custardbelly/as3couchdb/core/CouchDatabase.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchDatabase.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -33,19 +33,19 @@ package com.custardbelly.as3couchdb.core
/**
* Dispatched upon successful create of a database instance in CouchDB.
*/
- [Event(name="create", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="create", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* Dispatched upon successful read in and application of attributes from a database instance in CouchDB.
*/
- [Event(name="read", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="read", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* Dispatched upon successful creation and save of a database in CouchDB.
*/
- [Event(name="save", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="save", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* Dispatched upon successful deletion of database in CouchDB.
*/
- [Event(name="delete", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="delete", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* <p>CouchDatabase is a base model representing an instance of a Couch Database. To effectively use a CouchDatabase and its methods,
View
27 src/com/custardbelly/as3couchdb/core/CouchDocument.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchDocument.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -33,15 +33,19 @@ package com.custardbelly.as3couchdb.core
/**
* Dispatched upon successful read in and application of attributes for a document instance in CouchDB.
*/
- [Event(name="read", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="read", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* Dispatched upon successful creation and update of a document in CouchDB.
*/
- [Event(name="save", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="create", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
+ /**
+ * Dispatched upon successful update and update of a document in CouchDB.
+ */
+ [Event(name="update", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* Dispatched upon successful deletion of document in CouchDB.
*/
- [Event(name="delete", type="com.custardbelly.as3couchdb.event.CouchEvent")]
+ [Event(name="delete", type="com.custardbelly.as3couchdb.enum.CouchActionType")]
/**
* <p>CouchDocument is a base model representing an instance of a document within a databse of CouchDB. To effectively use a CouchDocument and its methods,
@@ -63,7 +67,7 @@ package com.custardbelly.as3couchdb.core
{
protected var _id:String;
protected var _revision:String;
- protected var _attachments:Object; // Generic map of key/value.
+ protected var _attachments:Vector.<CouchAttachment>;
protected var _actionMediator:ICouchDocumentActionMediator;
@@ -74,6 +78,7 @@ package com.custardbelly.as3couchdb.core
{
super();
_actionMediator = _mediator as ICouchDocumentActionMediator;
+ _attachments = new Vector.<CouchAttachment>();
}
/**
@@ -111,6 +116,14 @@ package com.custardbelly.as3couchdb.core
}
/**
+ * Invokes the action mediator to push attachments that are either modified or new to the COuchDB database related to this docuent instance.
+ */
+ public function saveAttachments():void
+ {
+ _actionMediator.doSaveAttachments();
+ }
+
+ /**
* Returns the unique identifier for this document.
* @return String
*/
@@ -140,11 +153,11 @@ package com.custardbelly.as3couchdb.core
* Returns a generic object describing attachments associated with this document.
* @return Object
*/
- public function get attachments():Object
+ public function get attachments():Vector.<CouchAttachment>
{
return _attachments;
}
- public function set attachments( value:Object ):void
+ public function set attachments( value:Vector.<CouchAttachment> ):void
{
_attachments = value;
}
View
2  src/com/custardbelly/as3couchdb/core/CouchModel.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchModel.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
2  src/com/custardbelly/as3couchdb/core/CouchModelEntity.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchModelEntity.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
14 src/com/custardbelly/as3couchdb/core/CouchServiceFault.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchServiceFault.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -33,6 +33,7 @@ package com.custardbelly.as3couchdb.core
public class CouchServiceFault
{
protected var _type:String;
+ protected var _status:int;
protected var _message:String;
/**
@@ -40,9 +41,10 @@ package com.custardbelly.as3couchdb.core
* @param type String
* @param message String
*/
- public function CouchServiceFault( type:String, message:String )
+ public function CouchServiceFault( type:String, status:int, message:String )
{
_type = type;
+ _status = status;
_message = message;
}
/**
@@ -54,6 +56,14 @@ package com.custardbelly.as3couchdb.core
return _type;
}
/**
+ * Returns the status code related to the fault.
+ * @return int
+ */
+ public function get status():int
+ {
+ return _status;
+ }
+ /**
* Returns a description of the fault encountered.
* @return String
*/
View
2  src/com/custardbelly/as3couchdb/core/CouchServiceResult.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchServiceResult.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
10 src/com/custardbelly/as3couchdb/core/CouchSession.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchSession.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -68,7 +68,6 @@ package com.custardbelly.as3couchdb.core
_headers = [];
_headers.push( new URLRequestHeader( "Cookie", _cookie ) );
_headers.push( new URLRequestHeader( "X-CouchDB-WWW-Authenticate", "Cookie" ) );
- _headers.push( new URLRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ) );
}
/**
@@ -83,11 +82,14 @@ package com.custardbelly.as3couchdb.core
/**
* @private
+ *
* Renews the cookie for a session.
+ * @param pendingCommand The pending command in chain after renewal.
+ * @return IRequestCommand
*/
- as3couchdb_internal function renew( command:IRequestCommand ):void
+ as3couchdb_internal function renew():IRequestCommand
{
- _actionMediator.doRenew( _user, command );
+ return _actionMediator.createRenewRequest( _user );
}
/**
View
2  src/com/custardbelly/as3couchdb/core/CouchUser.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchUser.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
2  src/com/custardbelly/as3couchdb/enum/CouchActionType.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchActionType.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
4 src/com/custardbelly/as3couchdb/enum/CouchContentType.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchContentType.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -33,6 +33,6 @@ package com.custardbelly.as3couchdb.enum
public class CouchContentType
{
public static const JSON:String = "application/json";
- public static const JPEG:String = "images/jpg";
+ public static const JPEG:String = "image/jpeg";
}
}
View
2  src/com/custardbelly/as3couchdb/enum/CouchFaultType.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchFaultType.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
2  src/com/custardbelly/as3couchdb/enum/CouchRequestMethod.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchRequestMethod.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
2  src/com/custardbelly/as3couchdb/enum/CouchRequestStatus.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchRequestStatus.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
2  src/com/custardbelly/as3couchdb/event/CouchEvent.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchEvent.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
68 src/com/custardbelly/as3couchdb/mediator/CouchDatabaseActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchDatabaseActionMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,6 +26,7 @@
*/
package com.custardbelly.as3couchdb.mediator
{
+ import com.custardbelly.as3couchdb.command.IRequestCommand;
import com.custardbelly.as3couchdb.core.CouchDatabase;
import com.custardbelly.as3couchdb.core.CouchModel;
import com.custardbelly.as3couchdb.core.CouchServiceFault;
@@ -56,7 +57,7 @@ package com.custardbelly.as3couchdb.mediator
* @private
* A basic responder to do result and fault from service operations.
*/
- protected var _responder:BasicCouchResponder;
+ protected var _serviceResponder:BasicCouchResponder;
/**
* Constructor.
@@ -77,7 +78,7 @@ package com.custardbelly.as3couchdb.mediator
{
_database = target as CouchDatabase;
_service = CouchDatabaseService.getDatabaseService( baseUrl, request );
- _responder = new BasicCouchResponder( handleServiceResult, handleServiceFault );
+ _serviceResponder = new BasicCouchResponder( handleServiceResult, handleServiceFault );
}
/**
@@ -105,46 +106,17 @@ package com.custardbelly.as3couchdb.mediator
}
/**
- * @private
- *
- * Responder method for the sucess of database creation. Forwards on to reading in values for the database.
- * @param result CouchServiceResult
- */
- protected function doCreateResult( result:CouchServiceResult ):void
- {
- doRead( CouchActionType.CREATE );
- }
-
- /**
- * @private
- *
- * Responder method for fault in database creation. Could return in CouchFaulttype.DATABASE_ALREADY_EXISTS. IF so, read in the database.
- * @param fault
- *
- */
- protected function doCreateFault( fault:CouchServiceFault ):void
- {
- if( fault.type == CouchFaultType.DATABASE_ALREADY_EXISTS )
- {
- doRead( CouchActionType.CREATE );
- }
- else
- {
- handleServiceFault( fault );
- }
- }
-
- /**
* Invokes the ICouchDatabaseService to create a database based on current CouchDatabase target.
*/
public function doCreateIfNotExist():void
{
- // Create internal responder for creation operation.
- var createResponder:ICouchServiceResponder = new BasicCouchResponder( doCreateResult, doCreateFault );
// Create responder for creation operation with basic responder.
- var serviceResponder:ICouchServiceResponder = new CreateDatabaseResponder( _database, createResponder );
+ var readResponder:ICouchServiceResponder = new ReadDatabaseResponder( _database, CouchActionType.CREATE, _serviceResponder )
// Invoke service to create database.
- _service.createDatabase( _database.db_name, serviceResponder );
+ var createRequestCommand:IRequestCommand = _service.createDatabase( _database.db_name );
+ var readRequestCommand:IRequestCommand = _service.readDatabase( _database.db_name, readResponder );
+ createRequestCommand.nextCommand = readRequestCommand;
+ createRequestCommand.execute();
}
/**
@@ -153,8 +125,8 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doRead( action:String = CouchActionType.READ ):void
{
- var serviceResponder:ICouchServiceResponder = new ReadDatabaseResponder( _database, action, _responder );
- _service.readDatabase( _database.db_name, serviceResponder );
+ var serviceResponder:ICouchServiceResponder = new ReadDatabaseResponder( _database, action, _serviceResponder );
+ _service.readDatabase( _database.db_name, serviceResponder ).execute();
}
/**
@@ -162,8 +134,8 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doDelete():void
{
- var serviceResponder:ICouchServiceResponder = new DeleteDatabaseResponder( _database, _responder );
- _service.deleteDatabase( _database.db_name, serviceResponder );
+ var serviceResponder:ICouchServiceResponder = new DeleteDatabaseResponder( _database, _serviceResponder );
+ _service.deleteDatabase( _database.db_name, serviceResponder ).execute();
}
/**
@@ -171,7 +143,7 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doInfo():void
{
- _service.getDatabaseInfo( _database.db_name, _responder );
+ _service.getDatabaseInfo( _database.db_name, _serviceResponder ).execute();
}
/**
@@ -179,7 +151,7 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doGetChanges():void
{
- _service.getDatabaseChanges( _database.db_name, _responder );
+ _service.getDatabaseChanges( _database.db_name, _serviceResponder ).execute();
}
/**
@@ -188,7 +160,7 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doCompact( cleanup:Boolean ):void
{
- _service.compactDatabase( _database.db_name, cleanup, _responder );
+ _service.compactDatabase( _database.db_name, cleanup, _serviceResponder ).execute();
}
/**
@@ -197,8 +169,8 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doGetAllDocuments( documentClass:String ):void
{
- var serviceResponder:ReadAllDocumentsResponder = new ReadAllDocumentsResponder( documentClass, _responder );
- _service.getAllDocuments( _database.db_name, true, serviceResponder );
+ var serviceResponder:ReadAllDocumentsResponder = new ReadAllDocumentsResponder( documentClass, _serviceResponder );
+ _service.getAllDocuments( _database.db_name, true, serviceResponder ).execute();
}
/**
@@ -210,8 +182,8 @@ package com.custardbelly.as3couchdb.mediator
*/
public function doGetDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String ):void
{
- var serviceResponder:ReadDocumentsFromViewResponder = new ReadDocumentsFromViewResponder( documentClass, _responder );
- _service.getDocumentsFromView( _database.db_name, designDocumentName, viewName, keyValue, serviceResponder );
+ var serviceResponder:ReadDocumentsFromViewResponder = new ReadDocumentsFromViewResponder( documentClass, _serviceResponder );
+ _service.getDocumentsFromView( _database.db_name, designDocumentName, viewName, keyValue, serviceResponder ).execute();
}
}
}
View
41 src/com/custardbelly/as3couchdb/mediator/CouchDocumentActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchDocumentActionMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -26,12 +26,14 @@
*/
package com.custardbelly.as3couchdb.mediator
{
+ import com.custardbelly.as3couchdb.core.CouchAttachment;
import com.custardbelly.as3couchdb.core.CouchDocument;
import com.custardbelly.as3couchdb.core.CouchModel;
import com.custardbelly.as3couchdb.core.CouchServiceFault;
import com.custardbelly.as3couchdb.core.CouchServiceResult;
import com.custardbelly.as3couchdb.enum.CouchActionType;
import com.custardbelly.as3couchdb.event.CouchEvent;
+ import com.custardbelly.as3couchdb.mediator.helper.AttachmentRequestQueue;
import com.custardbelly.as3couchdb.responder.BasicCouchResponder;
import com.custardbelly.as3couchdb.responder.CreateDocumentResponder;
import com.custardbelly.as3couchdb.responder.DeleteDocumentResponder;
@@ -51,6 +53,7 @@ package com.custardbelly.as3couchdb.mediator
protected var _document:CouchDocument;
protected var _service:ICouchDocumentService;
protected var _serviceResponder:BasicCouchResponder;
+ protected var _attachmentRequestQueue:AttachmentRequestQueue;
/**
* Constructor.
@@ -106,8 +109,8 @@ package com.custardbelly.as3couchdb.mediator
public function doRead( id:String ):void
{
_document.id = id;
- var responder:ICouchServiceResponder = new ReadDocumentResponder( _document, _serviceResponder );
- _service.readDocument( id, responder );
+ var responder:ICouchServiceResponder = new ReadDocumentResponder( _document, CouchActionType.READ, _serviceResponder );
+ _service.readDocument( id, responder ).execute();
}
/**
@@ -120,7 +123,8 @@ package com.custardbelly.as3couchdb.mediator
var responder:ICouchServiceResponder = ( _document.id )
? new UpdateDocumentResponder( _document, CouchActionType.UPDATE, _serviceResponder )
: new CreateDocumentResponder( _document, _serviceResponder );
- _service.saveDocument( _document, responder );
+
+ _service.saveDocument( _document, responder ).execute();
}
/**
@@ -129,7 +133,34 @@ package com.custardbelly.as3couchdb.mediator
public function doDelete():void
{
var responder:ICouchServiceResponder = new DeleteDocumentResponder( _document, _serviceResponder )
- _service.deleteDocument( _document.id, _document.revision, responder );
+ _service.deleteDocument( _document.id, _document.revision, responder ).execute();
}
+
+ /**
+ * Invokes service to save changed, unsaved, or deleted attachments associate with the document.
+ */
+ public function doSaveAttachments():void
+ {
+ // Create attachment request queue if not already existant.
+ if( _attachmentRequestQueue == null )
+ {
+ _attachmentRequestQueue = new AttachmentRequestQueue( _document, _service, _serviceResponder );
+ }
+
+ // Run through attachments and mark those needed requests.
+ var attachments:Vector.<CouchAttachment> = new Vector.<CouchAttachment>();
+ var i:int;
+ var attachment:CouchAttachment;
+ for( i = 0; i < _document.attachments.length; i++ )
+ {
+ attachment = _document.attachments[i];
+ if( attachment.isDirty() || attachment.isDeleted )
+ {
+ _attachmentRequestQueue.addAttachment( attachment );
+ }
+ }
+ // Start request queue.
+ _attachmentRequestQueue.start();
+ }
}
}
View
24 src/com/custardbelly/as3couchdb/mediator/CouchSessionActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CouchSessionActionMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -122,31 +122,27 @@ package com.custardbelly.as3couchdb.mediator
var cookie:String = result.data.toString();
// Update session.
updateSession( cookie );
- // Execute pending command request.
- if( _pendingCommandRequest )
- _pendingCommandRequest.execute();
}
/**
- * Invokes service to create a new session based on user credentials.
+ * Invokes service to renew session and complete pending request.
* @param user CouchUser
+ * @param pendingCommand IRequestCommand The pending command to execute after successful renewal.
+ * @return IRequestCommand
*/
- public function doCreate( user:CouchUser ):void
+ public function createRenewRequest( user:CouchUser ):IRequestCommand
{
- _service.createSession( user, _serviceResponder );
+ // Request new session cookie.
+ return _service.createSession( user, new BasicCouchResponder( handleRenewResult, handleServiceFault ) );
}
/**
- * Invokes service to renew session and complete pending request.
+ * Invokes service to create a new session based on user credentials.
* @param user CouchUser
- * @param commandRequest IRequestCommand
*/
- public function doRenew( user:CouchUser, commandRequest:IRequestCommand ):void
+ public function doCreate( user:CouchUser ):void
{
- // Store pending request.
- _pendingCommandRequest = commandRequest;
- // Request new session cookie.
- _service.createSession( user, new BasicCouchResponder( handleRenewResult, handleServiceFault ) );
+ _service.createSession( user, _serviceResponder ).execute();
}
}
}
View
2  src/com/custardbelly/as3couchdb/mediator/ICouchDatabaseActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ICouchDatabaseMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
6 src/com/custardbelly/as3couchdb/mediator/ICouchDocumentActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ICouchDocumentActionMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -45,5 +45,9 @@ package com.custardbelly.as3couchdb.mediator
* Invokes service to delete the document.
*/
function doDelete():void;
+ /**
+ * Invokes service to save changed or unsaved attachments associate with the document.
+ */
+ function doSaveAttachments():void;
}
}
View
13 src/com/custardbelly/as3couchdb/mediator/ICouchSessionActionMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ICouchSessionActionMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -36,16 +36,15 @@ package com.custardbelly.as3couchdb.mediator
public interface ICouchSessionActionMediator extends IServiceMediator
{
/**
- * Invokes service to create a new session based on user credentials.
+ * Invokes service to renew session and complete pending request.
* @param user CouchUser
+ * @return IRequestCommand
*/
- function doCreate( user:CouchUser ):void;
-
+ function createRenewRequest( user:CouchUser ):IRequestCommand;
/**
- * Invokes service to renew session and complete pending request.
+ * Invokes service to create a new session based on user credentials.
* @param user CouchUser
- * @param requestCommand IRequestCommand
*/
- function doRenew( user:CouchUser, requestCommand:IRequestCommand ):void;
+ function doCreate( user:CouchUser ):void;
}
}
View
2  src/com/custardbelly/as3couchdb/mediator/IServiceMediator.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: IServiceMediator.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
229 src/com/custardbelly/as3couchdb/mediator/helper/AttachmentRequestQueue.as
@@ -0,0 +1,229 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: AttachmentRequestQueue.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.mediator.helper
+{
+ import com.custardbelly.as3couchdb.as3couchdb_internal;
+ import com.custardbelly.as3couchdb.command.IRequestCommand;
+ import com.custardbelly.as3couchdb.core.CouchAttachment;
+ import com.custardbelly.as3couchdb.core.CouchDocument;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.enum.CouchActionType;
+ import com.custardbelly.as3couchdb.responder.BasicCouchResponder;
+ import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
+ import com.custardbelly.as3couchdb.serialize.CouchDocumentReader;
+ import com.custardbelly.as3couchdb.serialize.ICouchDocumentReader;
+ import com.custardbelly.as3couchdb.service.ICouchDocumentService;
+
+ /**
+ * AttachmentRequestQueue is a request queue for saving and deleting attachments related to a document.
+ * @author toddanderson
+ */
+ public class AttachmentRequestQueue
+ {
+ protected var _document:CouchDocument;
+ protected var _service:ICouchDocumentService;
+ protected var _faultIsFatal:Boolean;
+ protected var _finalResponder:ICouchServiceResponder;
+ protected var _requestResponder:ICouchServiceResponder;
+
+ protected var _reader:ICouchDocumentReader;
+ protected var _attachments:Vector.<CouchAttachment>;
+ protected var _isRunning:Boolean;
+ protected var _currentAttachment:CouchAttachment;
+
+ /**
+ * Constructor.
+ * @param document CouchDocument
+ * @param service ICouchDocumentService
+ * @param faultIsFatal Boolean Optional lag to stop queue request if a fault is receieved.
+ * @param responder ICouchServiceResponder
+ */
+ public function AttachmentRequestQueue( document:CouchDocument, service:ICouchDocumentService, responder:ICouchServiceResponder = null, faultIsFatal:Boolean = false ):void
+ {
+ _document = document;
+ _service = service;
+ _finalResponder = responder;
+ _faultIsFatal = faultIsFatal;
+
+ _requestResponder = new BasicCouchResponder( handleAttachmentRequestResult, handleAttachmentRequestFault );
+ _reader = new CouchDocumentReader();
+ _attachments = new Vector.<CouchAttachment>();
+ }
+
+ /**
+ * @private
+ *
+ * Performs request on next attachment in queue if available. If empty, notifies optional responder.
+ */
+ protected function loadNext():void
+ {
+ // If we have attachments left in queue, begin new request.
+ if( _attachments.length > 0 )
+ {
+ var request:IRequestCommand;
+ _isRunning = true;
+ _currentAttachment = _attachments.shift();
+ if( _currentAttachment.isDirty() )
+ {
+ request = _service.saveAttachment( _document.id, _document.revision, _currentAttachment.fileName, _currentAttachment.data, _currentAttachment.contentType, _requestResponder );
+ request.execute();
+ }
+ else if( _currentAttachment.isDeleted )
+ {
+ request = _service.deleteAttachment( _document.id, _document.revision, _currentAttachment.fileName, _requestResponder );
+ request.execute();
+ }
+ else loadNext();
+ }
+ // Else we are done.
+ else
+ {
+ _isRunning = false;
+ _currentAttachment = null;
+ notifyOfResult();
+ }
+ }
+
+ /**
+ * @private
+ *
+ * Notifies final responder of result if available.
+ */
+ protected function notifyOfResult():void
+ {
+ if( _finalResponder )
+ _finalResponder.handleResult( new CouchServiceResult( CouchActionType.UPDATE, _document ) );
+ }
+
+ /**
+ * @private
+ *
+ * Notifies final responder of fault if available. Only invoked if determined as fault being fatal.
+ * @param errorType String
+ * @param status int
+ * @param message String
+ */
+ protected function notifyOfFault( errorType:String, status:int, message:String ):void
+ {
+ // Clear.
+ _isRunning = false;
+ _currentAttachment = null;
+ _attachments = new Vector.<CouchAttachment>();
+ // Notify responder.
+ if( _finalResponder )
+ {
+ _finalResponder.handleFault( new CouchServiceFault( errorType, status, message ) );
+ }
+ }
+
+ /**
+ * @private
+ *
+ * Returns flag of successful result containing an error. Notifies final responder if necessary.
+ * @param value CouchServiceResult
+ * @return Boolean
+ */
+ protected function handleResultAsError( value:CouchServiceResult ):Boolean
+ {
+ var result:Object = value.data;
+ // If we find data related to error and we are told to treat faults as fatal to queue.
+ if( _reader.isResultAnError( result ) && _faultIsFatal )
+ {
+ notifyOfFault( result["error"], 0, result["reason"] );
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @private
+ *
+ * Responder method for result in attachment request.
+ * @param result CouchServiceResult
+ */
+ protected function handleAttachmentRequestResult( result:CouchServiceResult ):void
+ {
+ if( !handleResultAsError( result ) )
+ {
+ use namespace as3couchdb_internal;
+ // Update document based on result.
+ var data:Object = result.data;
+ _reader.updateDocumentFromResult( _document, data );
+
+ // Update attachment in relation to document.
+ _currentAttachment.document = _document;
+ _currentAttachment.revisionPosition = _document.revision;
+
+ // Remove attachment from list on document if delete.
+ if( _currentAttachment.isDeleted )
+ _document.attachments.splice( _document.attachments.indexOf( _currentAttachment ), 1 );
+ }
+ loadNext();
+ }
+
+ /**
+ * @private
+ *
+ * Responder method for fault in attachment request.
+ * @param fault CouchServiceFault
+ */
+ protected function handleAttachmentRequestFault( fault:CouchServiceFault ):void
+ {
+ // If we aren't using a fault as fatal to the request queue, move on.
+ if( !_faultIsFatal )
+ {
+ loadNext();
+ }
+ // Else notify of fault if responder available.
+ else
+ {
+ notifyOfFault( fault.type, fault.status, fault.message );
+ }
+ }
+
+ /**
+ * Adds a attachment to the request queue and optionally begins the request process.
+ * @param attachment CouchAttachment
+ * @param autostart Boolean Optional flag to start the request queue.
+ */
+ public function addAttachment( attachment:CouchAttachment, autostart:Boolean = false ):void
+ {
+ _attachments.push( attachment );
+ if( !_isRunning && autostart )
+ loadNext();
+ }
+
+ /**
+ * Starts the request queue.
+ */
+ public function start():void
+ {
+ if( !_isRunning ) loadNext();
+ }
+ }
+}
View
115 src/com/custardbelly/as3couchdb/responder/AbstractDatabaseResponder.as
@@ -0,0 +1,115 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: AbstractDatabaseResponder.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.responder
+{
+ import com.custardbelly.as3couchdb.core.CouchDatabase;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.serialize.CouchDatabaseReader;
+ import com.custardbelly.as3couchdb.serialize.ICouchDatabaseReader;
+
+ import flash.errors.IllegalOperationError;
+ import flash.utils.getQualifiedClassName;
+
+ /**
+ * AbstractDatabaseResponder is an abstract responder to requests made on a database.
+ * @author toddanderson
+ */
+ public class AbstractDatabaseResponder implements ICouchServiceResponder
+ {
+ protected var _database:CouchDatabase;
+ protected var _responder:ICouchServiceResponder;
+ protected var _status:int;
+
+ /**
+ * @private
+ * The reader that is knowledgable of the data type and attributes returned from the service.
+ */
+ protected var _reader:ICouchDatabaseReader;
+
+ /**
+ * Constructor.
+ */
+ public function AbstractDatabaseResponder( database:CouchDatabase, responder:ICouchServiceResponder )
+ {
+ _database = database;
+ _responder = responder;
+ // Create a new reader object that understands the returned data.
+ _reader = new CouchDatabaseReader();
+ }
+
+ /**
+ * @private
+ *
+ * Validates the result as being a successful error return or not.
+ * @param result CouchServicResult
+ * @return Boolean
+ */
+ protected function handleAsResultError( value:CouchServiceResult ):Boolean
+ {
+ var result:Object = value.data;
+ if( _reader.isResultAnError( result ) )
+ {
+ handleFault( new CouchServiceFault( result["error"], 0, result["reason"] ) );
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Responder method to success in service request.
+ * @param value CouchServiceResult
+ */
+ public function handleResult( value:CouchServiceResult ):void
+ {
+ // abstract.
+ throw new IllegalOperationError( "[" + getQualifiedClassName( this ) + "::handleResult] - Method needs to be overridden in concrete class." );
+ }
+
+ /**
+ * Responder method to fault in service request.
+ * @param value CouchServiceFault
+ */
+ public function handleFault( value:CouchServiceFault ):void
+ {
+ if( _responder ) _responder.handleFault( value );
+ }
+
+ /**
+ * Returns the current HTTP status code of the request.
+ * @return int
+ */
+ public function get status():int
+ {
+ return _status;
+ }
+ public function set status(value:int):void
+ {
+ _status = value;
+ }
+ }
+}
View
116 src/com/custardbelly/as3couchdb/responder/AbstractDocumentResponder.as
@@ -0,0 +1,116 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: AbstractDocumentResponder.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
+package com.custardbelly.as3couchdb.responder
+{
+ import com.custardbelly.as3couchdb.core.CouchDocument;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.serialize.CouchDocumentReader;
+ import com.custardbelly.as3couchdb.serialize.ICouchDocumentReader;
+
+ import flash.errors.IllegalOperationError;
+ import flash.utils.getQualifiedClassName;
+
+ /**
+ * AbtstractDocumentResponder is a abstract responder to service requests related to a couch document.
+ * @author toddanderson
+ */
+ public class AbstractDocumentResponder implements ICouchServiceResponder
+ {
+ protected var _document:CouchDocument;
+ protected var _responder:ICouchServiceResponder;
+ /**
+ * @private
+ *
+ * The reader that is knowledgable about the returned data and its attributes in relation to documents.
+ */
+ protected var _reader:ICouchDocumentReader;
+ protected var _status:int;
+
+ /**
+ * Constructor.
+ * @param document CouchDocument
+ * @param responder ICouchServiceResponder
+ */
+ public function AbstractDocumentResponder( document:CouchDocument, responder:ICouchServiceResponder )
+ {
+ _document = document;
+ _responder = responder;
+ _reader = new CouchDocumentReader();
+ }
+
+ /**
+ * @private
+ *
+ * Responds to successful response to determine if the result is an error.
+ * @param value CouchServicResult
+ * @return Boolean
+ */
+ protected function handleResultAsError( value:CouchServiceResult ):Boolean
+ {
+ var result:Object = value.data;
+ if( _reader.isResultAnError( result ) )
+ {
+ handleFault( new CouchServiceFault( result["error"], 0, result["reason"] ) );
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Abstract responder method for the service request.
+ * @param value CouchServicResult
+ */
+ public function handleResult(value:CouchServiceResult):void
+ {
+ // abstract.
+ throw new IllegalOperationError( "[" + getQualifiedClassName( this ) + "::handleResult] - Method needs to be overridden in concrete class." );
+ }
+
+ /**
+ * Responder method for the service request fault.
+ * @param value CouchServiceFault
+ */
+ public function handleFault(value:CouchServiceFault):void
+ {
+ if( _responder ) _responder.handleFault( value );
+ }
+
+ /**
+ * Returns the current HTTP status code of the request.
+ * @return int
+ */
+ public function get status():int
+ {
+ return _status;
+ }
+ public function set status(value:int):void
+ {
+ _status = value;
+ }
+ }
+}
View
2  src/com/custardbelly/as3couchdb/responder/BasicCouchResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: BasicCouchResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
13 src/com/custardbelly/as3couchdb/responder/CreateDatabaseResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CreateDatabaseResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -54,14 +54,7 @@ package com.custardbelly.as3couchdb.responder
*/
override public function handleResult( value:CouchServiceResult ):void
{
- // determine if error.
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- // else notify any responder.
- else
+ if( !handleAsResultError( value ) )
{
if( _responder ) _responder.handleResult( new CouchServiceResult( _action, _database ) );
}
@@ -76,7 +69,7 @@ package com.custardbelly.as3couchdb.responder
// If a 412 has happened, customize the fault response.
if( _status == CouchRequestStatus.HTTP_ALREADY_EXISTS )
{
- var fault:CouchServiceFault = new CouchServiceFault( CouchFaultType.DATABASE_ALREADY_EXISTS, "Database with name [" + _database.db_name + "] already exists at " + _database.baseUrl + "." );
+ var fault:CouchServiceFault = new CouchServiceFault( CouchFaultType.DATABASE_ALREADY_EXISTS, _status, "Database with name [" + _database.db_name + "] already exists at " + _database.baseUrl + "." );
super.handleFault( fault );
}
// else pass along the super fault handler.
View
11 src/com/custardbelly/as3couchdb/responder/CreateDocumentResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: CreateDocumentResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -53,15 +53,10 @@ package com.custardbelly.as3couchdb.responder
*/
override public function handleResult( value:CouchServiceResult ):void
{
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- else
+ if( !handleResultAsError( value ) )
{
// update the target document based on returned value as a creation result.
- _reader.updateDocumentFromCreation( _document, result );
+ _reader.updateDocumentFromCreation( _document, value.data );
if( _responder ) _responder.handleResult( new CouchServiceResult( _action, _document ) );
}
}
View
9 src/com/custardbelly/as3couchdb/responder/DeleteDatabaseResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: DeleteDatabaseResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -54,12 +54,7 @@ package com.custardbelly.as3couchdb.responder
*/
override public function handleResult( value:CouchServiceResult ):void
{
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- else
+ if( !handleAsResultError( value ) )
{
// Mark the database target as being deleted.
_database.isDeleted = true;
View
11 src/com/custardbelly/as3couchdb/responder/DeleteDocumentResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: DeleteDocumentResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -52,15 +52,10 @@ package com.custardbelly.as3couchdb.responder
*/
override public function handleResult( value:CouchServiceResult ):void
{
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- else
+ if( !handleResultAsError( value ) )
{
// Update the document base on the result.
- _reader.updateDocumentFromResult( _document, result );
+ _reader.updateDocumentFromResult( _document, value.data );
// Mark the document as being deleted.
_document.isDeleted = true;
if( _responder ) _responder.handleResult( new CouchServiceResult( _action, _document ) );
View
2  src/com/custardbelly/as3couchdb/responder/ICouchServiceResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ICouchServiceResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
71 src/com/custardbelly/as3couchdb/responder/ReadAllDocumentsResponder.as
@@ -1,3 +1,29 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: ReadAllDocumentsResponder.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
package com.custardbelly.as3couchdb.responder
{
import com.custardbelly.as3couchdb.core.CouchDocument;
@@ -7,6 +33,10 @@ package com.custardbelly.as3couchdb.responder
import com.custardbelly.as3couchdb.serialize.CouchDatabaseReader;
import com.custardbelly.as3couchdb.serialize.CouchDocumentReader;
+ /**
+ * ReadAllDocumentsResponder is a service responder to read in the payload of all documents related to a database in the CouchDB instance.
+ * @author toddanderson
+ */
public class ReadAllDocumentsResponder implements ICouchServiceResponder
{
protected var _documentClass:String;
@@ -16,6 +46,11 @@ package com.custardbelly.as3couchdb.responder
protected var _databaseReader:CouchDatabaseReader;
protected var _documentReader:CouchDocumentReader;
+ /**
+ * Constructor.
+ * @param documentClass String The fully-qualified class name of the document instance to resolve results to.
+ * @param responder ICouchServiceResponder
+ */
public function ReadAllDocumentsResponder( documentClass:String, responder:ICouchServiceResponder )
{
_documentClass = documentClass;
@@ -25,14 +60,34 @@ package com.custardbelly.as3couchdb.responder
_documentReader = new CouchDocumentReader();
}
- public function handleResult( value:CouchServiceResult ):void
+ /**
+ * @private
+ *
+ * Determines if successful result returned is related to an error.
+ * @param value CouchServiceResult
+ * @return Boolean
+ */
+ protected function handleResultAsError( value:CouchServiceResult ):Boolean
{
var result:Object = value.data;
+ // If error, notify.
if( _databaseReader.isResultAnError( result ) )
{
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
+ handleFault( new CouchServiceFault( result["error"], 0, result["reason"] ) );
+ return true;
}
- else
+ return false;
+ }
+
+ /**
+ * Responder method to handle the successful response form the request.
+ * @param value CouchServiceResult
+ */
+ public function handleResult( value:CouchServiceResult ):void
+ {
+ var result:Object = value.data;
+ // If error, notify.
+ if( !handleResultAsError( value ) )
{
var documents:Array = _databaseReader.getDocumentListFromResult( result );
@@ -51,16 +106,24 @@ package com.custardbelly.as3couchdb.responder
}
}
+ /**
+ * Responder method to handle a fault in the service request.
+ * @param value CouchServiceFault
+ */
public function handleFault( value:CouchServiceFault ):void
{
if( _responder ) _responder.handleFault( value );
}
+ /**
+ * Accessor/Modifier of the current HTTP status code of the request.
+ * @return
+ *
+ */
public function get status():int
{
return _status;
}
-
public function set status(value:int):void
{
_status = value;
View
51 src/com/custardbelly/as3couchdb/responder/ReadDatabaseResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ReadDatabaseResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -36,18 +36,9 @@ package com.custardbelly.as3couchdb.responder
* ReadDatabaseResponder is an ICouchServiceResponder implementation that handle result and fault from a service operation with regards to accessing and reading a database instance from CouchDB.
* @author toddanderson
*/
- public class ReadDatabaseResponder implements ICouchServiceResponder
+ public class ReadDatabaseResponder extends AbstractDatabaseResponder
{
- protected var _status:int;
- protected var _database:CouchDatabase;
protected var _action:String;
- protected var _responder:ICouchServiceResponder;
- /**
- * @private
- * The reader that is knowledgable of the data type and attributes returned from the service.
- */
- protected var _reader:ICouchDatabaseReader;
-
/**
* Constructor.
* @param database CouchDatabase The target database to read in and apply attributes to.
@@ -56,51 +47,21 @@ package com.custardbelly.as3couchdb.responder
*/
public function ReadDatabaseResponder( database:CouchDatabase, action:String, responder:ICouchServiceResponder )
{
- _database = database;
+ super( database, responder );
_action = action;
- _responder = responder;
- // Create a new reader object that understands the returned data.
- _reader = new CouchDatabaseReader();
}
/**
* Result handler for response from service. Parses result to determine if the data relates to an error or success.
* @param value CouchServiceResult
*/
- public function handleResult( value:CouchServiceResult ):void
+ override public function handleResult( value:CouchServiceResult ):void
{
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- else
+ if( !handleAsResultError( value ) )
{
- _reader.updateFromResult( _database, result );
+ _reader.updateFromResult( _database, value.data );
if( _responder ) _responder.handleResult( new CouchServiceResult( _action, _database ) );
}
}
-
- /**
- * Fault handle for response from service.
- * @param value CouchServiceFault
- */
- public function handleFault( value:CouchServiceFault ):void
- {
- if( _responder ) _responder.handleFault( value );
- }
-
- /**
- * Returns the current HTTP status of the service operation.
- * @return int
- */
- public function get status():int
- {
- return _status;
- }
- public function set status( value:int ):void
- {
- _status = value;
- }
}
}
View
15 src/com/custardbelly/as3couchdb/responder/ReadDocumentResponder.as
@@ -1,7 +1,7 @@
/**
* <p>Original Author: toddanderson</p>
* <p>Class File: ReadDocumentResponder.as</p>
- * <p>Version: 0.3</p>
+ * <p>Version: 0.4</p>
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -42,9 +42,9 @@ package com.custardbelly.as3couchdb.responder
* @param document CouchDocument
* @param responder ICouchServiceResponder
*/
- public function ReadDocumentResponder(document:CouchDocument, responder:ICouchServiceResponder)
+ public function ReadDocumentResponder( document:CouchDocument, action:String, responder:ICouchServiceResponder )
{
- super(document, CouchActionType.READ, responder);
+ super(document, action, responder);
}
/**
@@ -52,15 +52,10 @@ package com.custardbelly.as3couchdb.responder
*/
override public function handleResult( value:CouchServiceResult ):void
{
- var result:Object = value.data;
- if( _reader.isResultAnError( result ) )
- {
- handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
- }
- else
+ if( !handleResultAsError( value ) )
{
// use reader to update target document based on result.
- _reader.updateDocumentFromRead( _document, result );
+ _reader.updateDocumentFromRead( _document, value.data );
if( _responder ) _responder.handleResult( new CouchServiceResult( _action, _document ) );
}
}
View
44 src/com/custardbelly/as3couchdb/responder/ReadDocumentsFromViewResponder.as
@@ -1,3 +1,29 @@
+/**
+ * <p>Original Author: toddanderson</p>
+ * <p>Class File: ReadDocumentsFromViewResponder.as</p>
+ * <p>Version: 0.4</p>
+ *
+ * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:</p>
+ *
+ * <p>The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.</p>
+ *
+ * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.</p>
+ *
+ * <p>Licensed under The MIT License</p>
+ * <p>Redistributions of files must retain the above copyright notice.</p>
+ */
package com.custardbelly.as3couchdb.responder
{
import com.custardbelly.as3couchdb.core.CouchDocument;
@@ -5,21 +31,29 @@ package com.custardbelly.as3couchdb.responder
import com.custardbelly.as3couchdb.core.CouchServiceResult;
import com.custardbelly.as3couchdb.enum.CouchActionType;
+ /**
+ * ReadDocumentsFromViewResponder is a responder to a request make on a view document in the database