Skip to content

Commit

Permalink
Refactored to make more generic interface - now it is more clear that…
Browse files Browse the repository at this point in the history
… any document can be loaded. Also, renamed to CouchMover as an attempt at humour (furnisher moving).
  • Loading branch information
Kevin Malakoff committed Jun 15, 2011
1 parent 533da0c commit 19a66ac
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 68 deletions.
Expand Up @@ -41,8 +41,8 @@
0A59445A13A806F500402C46 /* NSData_Base64Extensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A59445613A806F500402C46 /* NSData_Base64Extensions.m */; };
0A5FF58513A7C0C70087D7FD /* mycouchapp.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A5FF58313A7C0C70087D7FD /* mycouchapp.json */; };
0A5FF58613A7C0C70087D7FD /* mycouchapp.version in Resources */ = {isa = PBXBuildFile; fileRef = 0A5FF58413A7C0C70087D7FD /* mycouchapp.version */; };
0A5FF58913A7C2610087D7FD /* CouchAppManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A5FF58713A7C2610087D7FD /* CouchAppManager.h */; };
0A5FF58A13A7C2610087D7FD /* CouchAppManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A5FF58813A7C2610087D7FD /* CouchAppManager.m */; };
0A5FF58913A7C2610087D7FD /* CouchMover.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A5FF58713A7C2610087D7FD /* CouchMover.h */; };
0A5FF58A13A7C2610087D7FD /* CouchMover.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A5FF58813A7C2610087D7FD /* CouchMover.m */; };
0AC7E6FD13A6A1BA00CCB232 /* libCouchbase.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0AC7E6FC13A6A1BA00CCB232 /* libCouchbase.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -97,8 +97,8 @@
0A59445613A806F500402C46 /* NSData_Base64Extensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSData_Base64Extensions.m; path = Vendor/TouchFoundation_MinimalExtraction/NSData_Base64Extensions.m; sourceTree = "<group>"; };
0A5FF58313A7C0C70087D7FD /* mycouchapp.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = mycouchapp.json; path = Resources/mycouchapp.json; sourceTree = "<group>"; };
0A5FF58413A7C0C70087D7FD /* mycouchapp.version */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = mycouchapp.version; path = Resources/mycouchapp.version; sourceTree = "<group>"; };
0A5FF58713A7C2610087D7FD /* CouchAppManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CouchAppManager.h; path = Classes/CouchAppManager.h; sourceTree = "<group>"; };
0A5FF58813A7C2610087D7FD /* CouchAppManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CouchAppManager.m; path = Classes/CouchAppManager.m; sourceTree = "<group>"; };
0A5FF58713A7C2610087D7FD /* CouchMover.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CouchMover.h; path = Classes/CouchMover.h; sourceTree = "<group>"; };
0A5FF58813A7C2610087D7FD /* CouchMover.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CouchMover.m; path = Classes/CouchMover.m; sourceTree = "<group>"; };
0AC7E6FC13A6A1BA00CCB232 /* libCouchbase.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libCouchbase.a; path = "phonegap-couchbase-ios/Vendor/Couchbase/libCouchbase.a"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -236,8 +236,8 @@
children = (
0A1A732A13A4D09F00850912 /* AppDelegate.h */,
0A1A732C13A4D09F00850912 /* AppDelegate.m */,
0A5FF58713A7C2610087D7FD /* CouchAppManager.h */,
0A5FF58813A7C2610087D7FD /* CouchAppManager.m */,
0A5FF58713A7C2610087D7FD /* CouchMover.h */,
0A5FF58813A7C2610087D7FD /* CouchMover.m */,
);
name = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -285,7 +285,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
0A5FF58913A7C2610087D7FD /* CouchAppManager.h in Headers */,
0A5FF58913A7C2610087D7FD /* CouchMover.h in Headers */,
0A59445813A806F500402C46 /* Base64Transcoder.h in Headers */,
0A59445913A806F500402C46 /* NSData_Base64Extensions.h in Headers */,
);
Expand Down Expand Up @@ -385,7 +385,7 @@
files = (
0A1A731513A4D09F00850912 /* main.m in Sources */,
0A1A732D13A4D09F00850912 /* AppDelegate.m in Sources */,
0A5FF58A13A7C2610087D7FD /* CouchAppManager.m in Sources */,
0A5FF58A13A7C2610087D7FD /* CouchMover.m in Sources */,
0A59445713A806F500402C46 /* Base64Transcoder.c in Sources */,
0A59445A13A806F500402C46 /* NSData_Base64Extensions.m in Sources */,
);
Expand Down
@@ -1,6 +1,6 @@
//
// AppDelegate.h
// CouchAppManager
// couchMover
//
// Created by Kevin Malakoff on 6/11/11.
// Copyright 2011 None.
Expand All @@ -26,7 +26,7 @@

// Couchbase changes START
#import "Couchbase.h"
#import "CouchAppManager.h"
#import "couchMover.h"

@interface AppDelegate : PhoneGapDelegate<CouchbaseDelegate> {
// Couchbase changes END
Expand Down
@@ -1,6 +1,6 @@
//
// AppDelegate.m
// CouchAppManager
// CouchMover
//
// Created by Kevin Malakoff on 6/11/11.
// Copyright 2011 None.
Expand Down Expand Up @@ -33,7 +33,7 @@ - (void)couchbaseDidStart:(NSURL *)serverURL {
// TODO: how should user name and password be handled properly?
// For example, using the default user and password at initialization, and switching to the user and password selected by the actual user, and handling remote server connections.
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"admin" password:@"admin" persistence:NSURLCredentialPersistenceForSession];
CouchAppManager* couchAppManager = [[CouchAppManager alloc] init:serverURL serverCredential:credential databaseName:@"mycouchapp_db" documentName:@"mycouchapp"];
CouchMover* couchMover = [[CouchMover alloc] init:serverURL serverCredential:credential databaseName:@"mycouchapp_db"];

// put on a background thread because currently the manager uses async HTTP calls
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
Expand All @@ -42,15 +42,15 @@ - (void)couchbaseDidStart:(NSURL *)serverURL {
NSString *appVersion = [NSString stringWithContentsOfFile:pathToAppVersion usedEncoding:nil error:nil];

// load the coachapp if needed
[couchAppManager loadNewAppVersion:appVersion getAppAsJSONDataBlock:^(){
[couchMover loadDocument:@"_design/mycouchapp" version:appVersion getAppAsJSONDataBlock:^(){
NSString *pathToApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mycouchapp.json"];
return (NSData*) [NSData dataWithContentsOfFile:pathToApp];
}];

// go back to the main thread because that is where the webview is
dispatch_async(dispatch_get_main_queue(), ^{
[couchAppManager gotoAppPage:self.webView page:@"index.html"];
[couchAppManager release];
[couchMover gotoAppPage:@"_design/mycouchapp" webView:self.webView page:@"index.html"];
[couchMover release];
});
});
}
Expand Down
@@ -1,6 +1,6 @@
//
// CouchAppManager.h
// CouchAppManager
// CouchMover.h
// CouchMover
//
// Created by Kevin Malakoff on 6/11/11.
// Copyright 2011 None.
Expand All @@ -19,42 +19,41 @@

#import <Foundation/Foundation.h>

@interface CouchAppManager : NSObject {
NSURL* couchbaseServerURL; // pass the URL from "CouchbaseDelegate couchbaseDidStart"
@interface CouchMover : NSObject {
NSURL* serverURL; // pass the URL from "CouchbaseDelegate couchbaseDidStart"
NSURLCredential *couchappServerCredential; // required for authorization
NSString* couchappDatabaseName; // should be in the form of "mydatabase"
NSString* couchappDocumentName; // should be in the form of "myapp"
NSString* databaseName; // should be in the form of "mydatabase"

NSString* _credentialString;
}

@property (copy, readwrite) NSURL* couchbaseServerURL;
@property (copy, readwrite) NSURL* serverURL;
@property (copy, readwrite) NSURLCredential *couchappServerCredential;
@property (copy, readwrite) NSString* couchappDatabaseName;
@property (copy, readwrite) NSString* couchappDocumentName;
@property (copy, readwrite) NSString* databaseName;

@property (copy, readonly) NSString* _credentialString;

///////////////////////
// Public Interface
///////////////////////
-(CouchAppManager*)init:(NSURL*)serverURL serverCredential:(NSURLCredential*)serverCredential databaseName:(NSString*)databaseName documentName:(NSString*)documentName;
-(void)loadNewAppVersion:(NSString*)newVersion getAppAsJSONDataBlock:(NSData* (^)())getAppAsJSONDataBlock; // newVersionOverride allows for an optimization by the caller supplying a version string rather than parsing the appAsJSONString and using the _rev field (which is the default if newVersion is not supplied)
-(void)gotoAppPage:(UIWebView*)webView page:(NSString*)page;
-(CouchMover*)init:(NSURL*)inServerURL serverCredential:(NSURLCredential*)inServerCredential databaseName:(NSString*)inDatabaseName;
-(BOOL)documentHasChanged:(NSString*)documentName version:(NSString*)version; // for a couch app, pass the documentName in the format of @"_design/appname"
-(void)loadDocument:(NSString*)documentName version:(NSString*)version getAppAsJSONDataBlock:(NSData* (^)())getAppAsJSONDataBlock; // for a couch app, pass the documentName in the format of @"_design/appname"
-(void)gotoAppPage:(NSString*)appDocumentName webView:(UIWebView*)webView page:(NSString*)page;

///////////////////////
// Internal Flow
///////////////////////
-(BOOL)ensureAppDatabaseExists;
-(NSString*)getCurrentAppVersion;
-(void)setCurrentAppVersion:(NSString*)version;
-(NSString*)getCurrentAppVersion:(NSString*)documentName;
-(void)setCurrentAppVersion:(NSString*)documentName version:(NSString*)version;

///////////////////////
// URL Helpers
///////////////////////
-(NSString*)urlToAppDatabase;
-(NSString*)urlToAppDocument;
-(NSString*)urlToAppLoadedVerionDocument;
-(NSString*)urlToAppDocument:(NSString*)documentName;
-(NSString*)urlToLoadedDocumentVerionDocument:(NSString*)documentName;

///////////////////////
// HTTP Helpers
Expand Down
@@ -1,6 +1,6 @@
//
// CouchAppManager.m
// CouchAppManager
// CouchMover.m
// CouchMover
//
// Created by Kevin Malakoff on 6/11/11.
// Copyright 2011 None.
Expand All @@ -17,7 +17,7 @@
// License for the specific language governing permissions and limitations under
// the License.

#import "CouchAppManager.h"
#import "CouchMover.h"
#import "NSData_Base64Extensions.h" // for asBase64EncodedString for authenticaton

NSString * const CONTENT_TYPE_JSON = @"application/json";
Expand All @@ -28,46 +28,52 @@
#define RESPONSE_DATA_DB_EXISTS(data) ([self responseDataHasField:data field:@"db_name"])
#define RESPONSE_DATA_DOC_EXISTS(data) ([self responseDataHasField:data field:@"_id"])

@implementation CouchAppManager
@implementation CouchMover

@synthesize couchbaseServerURL;
@synthesize couchappDatabaseName;
@synthesize couchappDocumentName;
@synthesize serverURL;
@synthesize databaseName;

@synthesize _credentialString;

///////////////////////
// Public Interface
///////////////////////
-(CouchAppManager*)init:(NSURL*)serverURL serverCredential:(NSURLCredential*)serverCredential databaseName:(NSString*)databaseName documentName:(NSString*)documentName
-(CouchMover*)init:(NSURL*)inServerURL serverCredential:(NSURLCredential*)inServerCredential databaseName:(NSString*)inDatabaseName;
{
self = [super init];
if (self == nil)
return nil;

self.couchbaseServerURL = serverURL;
self.couchappServerCredential = serverCredential;
self.couchappDatabaseName = databaseName;
self.couchappDocumentName = documentName;
self.serverURL = inServerURL;
self.couchappServerCredential = inServerCredential;
self.databaseName = inDatabaseName;

return self;
}

-(void)loadNewAppVersion:(NSString*)newVersion getAppAsJSONDataBlock:(NSData* (^)())getAppAsJSONDataBlock
-(BOOL)documentHasChanged:(NSString*)documentName version:(NSString*)version
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// call this first because loadDocument calls this
[self ensureAppDatabaseExists];

// no version so always load
if(!version)
return TRUE;

// no change so do not upload
NSString *currentVersion = [self getCurrentAppVersion];
if(currentVersion && newVersion && ([currentVersion compare:newVersion] == NSOrderedSame))
{
// no change so do not upload
[pool release];
NSString *currentVersion = [self getCurrentAppVersion:documentName];
return(!currentVersion || ([currentVersion compare:version] != NSOrderedSame));
}

-(void)loadDocument:(NSString*)documentName version:(NSString*)version getAppAsJSONDataBlock:(NSData* (^)())getAppAsJSONDataBlock
{
// no change in the document
if(![self documentHasChanged:documentName version:version])
return;
}

NSString *documentURL = [self urlToAppDocument];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *documentURL = [self urlToAppDocument:documentName];
NSData *responseData;

/////////////////////////
Expand All @@ -91,13 +97,13 @@ -(void)loadNewAppVersion:(NSString*)newVersion getAppAsJSONDataBlock:(NSData* (^

// update the version -> if there is no revision, skip and it will be loaded again next time
else
[self setCurrentAppVersion:newVersion];
[self setCurrentAppVersion:documentName version:version];
[pool release];
}

-(void)gotoAppPage:(UIWebView*)webView page:(NSString*)page
-(void)gotoAppPage:(NSString*)appDocumentName webView:(UIWebView*)webView page:(NSString*)page
{
NSString *couchappApgeURL = [[NSString alloc] initWithFormat:@"window.location.href = \"%@/%@\"", [self urlToAppDocument], page];
NSString *couchappApgeURL = [[NSString alloc] initWithFormat:@"window.location.href = \"%@/%@\"", [self urlToAppDocument:appDocumentName], page];
[webView stringByEvaluatingJavaScriptFromString:couchappApgeURL];
[couchappApgeURL release];
}
Expand Down Expand Up @@ -163,9 +169,9 @@ -(BOOL)ensureAppDatabaseExists
return RESPONSE_DATA_OK(responseData);
}

-(NSString*)getCurrentAppVersion
-(NSString*)getCurrentAppVersion:(NSString*)documentName
{
NSString *documentURL = [self urlToAppLoadedVerionDocument];
NSString *documentURL = [self urlToLoadedDocumentVerionDocument:documentName];
NSData *responseData;

/////////////////////////
Expand All @@ -181,9 +187,9 @@ -(NSString*)getCurrentAppVersion
return RESPONSE_DATA_DOC_EXISTS(responseData)? [self responseDataExtractSimpleValue:responseData field:COUCHAPP_LOADED_VERSION_FIELD] : nil;
}

-(void)setCurrentAppVersion:(NSString*)version
-(void)setCurrentAppVersion:(NSString*)documentName version:(NSString*)version
{
NSString *documentURL = [self urlToAppLoadedVerionDocument];
NSString *documentURL = [self urlToLoadedDocumentVerionDocument:documentName];
NSData *responseData;

/////////////////////////
Expand Down Expand Up @@ -214,17 +220,17 @@ -(void)setCurrentAppVersion:(NSString*)version
///////////////////////
-(NSString*)urlToAppDatabase
{
return [NSString stringWithFormat:@"%@%@", [self.couchbaseServerURL absoluteString], self.couchappDatabaseName];
return [NSString stringWithFormat:@"%@%@", [self.serverURL absoluteString], self.databaseName];
}

-(NSString*)urlToAppDocument
-(NSString*)urlToAppDocument:(NSString*)documentName
{
return [NSString stringWithFormat:@"%@%@/_design/%@", [self.couchbaseServerURL absoluteString], self.couchappDatabaseName, self.couchappDocumentName];
return [NSString stringWithFormat:@"%@%@/%@", [self.serverURL absoluteString], self.databaseName, documentName];
}

-(NSString*)urlToAppLoadedVerionDocument
-(NSString*)urlToLoadedDocumentVerionDocument:(NSString*)documentName
{
return [NSString stringWithFormat:@"%@%@/_design/%@_%@", [self.couchbaseServerURL absoluteString], self.couchappDatabaseName, self.couchappDocumentName, COUCHAPP_LOADED_VERSION_DOC];
return [NSString stringWithFormat:@"%@%@/%@_%@", [self.serverURL absoluteString], self.databaseName, documentName, COUCHAPP_LOADED_VERSION_DOC];
}

///////////////////////
Expand Down Expand Up @@ -384,10 +390,9 @@ - (void)dealloc
{
[_credentialString release];

[couchbaseServerURL release];
[serverURL release];
[couchappServerCredential release];
[couchappDatabaseName release];
[couchappDocumentName release];
[databaseName release];
[super dealloc];
}

Expand Down
2 changes: 1 addition & 1 deletion phonegap-couchbase-ios/phonegap-couchbase-ios/main.m
@@ -1,6 +1,6 @@
//
// main.m
// CouchAppManager
// couchMover
//
// Created by Kevin Malakoff on 6/11/11.
// Copyright 2011 None.
Expand Down

0 comments on commit 19a66ac

Please sign in to comment.