Skip to content

Commit

Permalink
Update for making requests on design views for documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd Anderson committed Mar 26, 2010
1 parent 4696657 commit 494e78e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 18 deletions.
Binary file modified bin/as3couchdb.swc
Binary file not shown.
15 changes: 14 additions & 1 deletion src/com/custardbelly/as3couchdb/core/CouchDatabase.as
Expand Up @@ -143,11 +143,24 @@ package com.custardbelly.as3couchdb.core

/**
* Invokes action mediator to return all documents related to database resolved as the supplied class type.
* @param documentClass String
* @param documentClass String The fully qualified Class name. This is used to resolve results to a specified type of model.
*/
public function getAllDocuments( documentClass:String ):void
{
_actionMediator.handleGetAllDocuments( documentClass );
}

/**
* Invokes action mediator to return all documents returned from a design document view, filter by optional key value.
* @param documentClass String The fully qualified Class name. This is used to resolve results to a sepcified type of model.
* @param designDocumentName String The design document name within the database.
* @param viewName String The view name to query on which the map/reduce methods reside.
* @param keyValue String Optional filter on results by key type.
*
*/
public function getDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String = null ):void
{
_actionMediator.handleGetDocumentsFromView( documentClass, designDocumentName, viewName, keyValue );
}
}
}
2 changes: 1 addition & 1 deletion src/com/custardbelly/as3couchdb/enum/CouchActionType.as
Expand Up @@ -37,6 +37,6 @@ package com.custardbelly.as3couchdb.enum
public static const UPDATE:String = "update";
public static const DELETE:String = "delete";

public static const READ_ALL:String = "readAll";
public static const READ_DOCUMENTS:String = "readDocuments";
}
}
Expand Up @@ -39,6 +39,7 @@ package com.custardbelly.as3couchdb.mediator
import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
import com.custardbelly.as3couchdb.responder.ReadAllDocumentsResponder;
import com.custardbelly.as3couchdb.responder.ReadDatabaseResponder;
import com.custardbelly.as3couchdb.responder.ReadDocumentsFromViewResponder;
import com.custardbelly.as3couchdb.service.CouchDatabaseService;
import com.custardbelly.as3couchdb.service.ICouchDatabaseService;
import com.custardbelly.as3couchdb.service.ICouchRequest;
Expand Down Expand Up @@ -187,12 +188,25 @@ package com.custardbelly.as3couchdb.mediator

/**
* Invokes the ICouchDatabaseService to retrieve all documents from target CouchDatabase and resolve each return as a type of CouchDocument class.
* @param documentClass String
* @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
*/
public function handleGetAllDocuments( documentClass:String ):void
{
var serviceResponder:ReadAllDocumentsResponder = new ReadAllDocumentsResponder( documentClass, _responder );
_service.getAllDocuments( _database.db_name, true, serviceResponder );
}

/**
* Invokes service to request documents based on design view map/reduce with optional key value filter.
* @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
* @param designDocumentName String
* @param viewName String
* @param keyValue String
*/
public function handleGetDocumentsFromView( 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 );
}
}
}
Expand Up @@ -34,8 +34,16 @@ package com.custardbelly.as3couchdb.mediator
function handleCompact( cleanup:Boolean ):void;
/**
* Invokes service to request all documents and resolve each document as a type of supplied class.
* @param documentClass String
* @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
*/
function handleGetAllDocuments( documentClass:String ):void;
/**
* Invokes service to request documents based on design view map/reduce with optional key value filter.
* @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
* @param designDocumentName String
* @param viewName String
* @param keyValue String
*/
function handleGetDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String ):void;
}
}
Expand Up @@ -27,21 +27,28 @@ package com.custardbelly.as3couchdb.responder

public function handleResult( value:CouchServiceResult ):void
{
// TODO: Check for fault.
var documents:Array = _databaseReader.getDocumentListFromResult( value.data );

var i:int;
var document:CouchDocument;
var documentList:Array = [];
for( i = 0; i < documents.length; i++ )
var result:Object = value.data;
if( _databaseReader.isResultAnError( result ) )
{
// Documents are returned form /_all_docs as {doc:Object, id:String, key:String, value:Object}
// Supply the doc property to the reader.
document = _documentReader.createDocumentFromResult( _documentClass, documents[i].doc );
documentList.push( document );
handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
}
else
{
var documents:Array = _databaseReader.getDocumentListFromResult( result );

var i:int;
var document:CouchDocument;
var documentList:Array = [];
for( i = 0; i < documents.length; i++ )
{
// Documents are returned from /_all_docs as {doc:Object, id:String, key:String, value:Object}
// Supply the doc property to the reader.
document = _documentReader.createDocumentFromResult( _documentClass, documents[i].doc );
documentList.push( document );
}

if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_DOCUMENTS, documentList ) );
}

if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_ALL, documentList ) );
}

public function handleFault( value:CouchServiceFault ):void
Expand Down
@@ -0,0 +1,40 @@
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.enum.CouchActionType;

public class ReadDocumentsFromViewResponder extends ReadAllDocumentsResponder
{
public function ReadDocumentsFromViewResponder(documentClass:String, responder:ICouchServiceResponder)
{
super(documentClass, responder);
}

override public function handleResult( value:CouchServiceResult ):void
{
var result:Object = value.data;
if( _databaseReader.isResultAnError( result ) )
{
handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
}
else
{
var documents:Array = _databaseReader.getDocumentListFromResult( result );

var i:int;
var document:CouchDocument;
var documentList:Array = [];
for( i = 0; i < documents.length; i++ )
{
// Documents are assume to be returned from map/reduce as ( key:id, value:doc ).
document = _documentReader.createDocumentFromResult( _documentClass, documents[i].value );
documentList.push( document );
}

if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_DOCUMENTS, documentList ) );
}
}
}
}
17 changes: 17 additions & 0 deletions src/com/custardbelly/as3couchdb/service/CouchDatabaseService.as
Expand Up @@ -24,6 +24,8 @@
* <p>Licensed under The MIT License</p>
* <p>Redistributions of files must retain the above copyright notice.</p>
*/
// TODO: Support for upload of design documents.
// TODO: Support for upload of attachements.
package com.custardbelly.as3couchdb.service
{
import com.custardbelly.as3couchdb.enum.CouchContentType;
Expand Down Expand Up @@ -250,6 +252,21 @@ package com.custardbelly.as3couchdb.service
makeRequest( request, CouchRequestMethod.GET, responder );
}

/**
* Makes request on design document in CouchDB instance.
* @param id String
* @param responder ICouchServiceResponder
*/
public function getDocumentsFromView( databaseName:String, documentName:String, viewName:String, byKeyValue:String = null, responder:ICouchServiceResponder = null ):void
{
var request:URLRequest = new URLRequest();
request.contentType = CouchContentType.JSON;
request.url = _baseUrl + "/" + databaseName + "/_design/" + documentName + "/_view/" + viewName;
if( byKeyValue ) request.url += "?key=\"" + byKeyValue + "\"";

makeRequest( request, CouchRequestMethod.GET, responder );
}

/**
* Access an instance of a ICouchDatabaseService based on the url of the CouchDb instance.
* @param baseUrl String
Expand Down
Expand Up @@ -102,7 +102,12 @@ package com.custardbelly.as3couchdb.service
* @param responder ICouchServiceResponder
*/
function getAllDocumentsBySequence( databaseName:String, includeDocs:Boolean = false, responder:ICouchServiceResponder = null ):void;

/**
* Makes request on design document in CouchDB instance.
* @param id String
* @param responder ICouchServiceResponder
*/
function getDocumentsFromView( databaseName:String, documentName:String, viewName:String, byKeyValue:String = null, responder:ICouchServiceResponder = null ):void;
/**
* Requests the list of databases in the target CouchDB instance.
* @param responder ICouchServiceResponder
Expand Down

0 comments on commit 494e78e

Please sign in to comment.