forked from couchbaselabs/CouchCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCouchRevision.m
268 lines (204 loc) · 7.79 KB
/
CouchRevision.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// CouchRevision.m
// CouchCocoa
//
// Created by Jens Alfke on 6/28/11.
// Copyright 2011 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "CouchRevision.h"
#import "CouchInternal.h"
@implementation CouchRevision
- (id) initWithDocument: (CouchDocument*)document revisionID: (NSString*)revisionID {
NSParameterAssert(document);
if (revisionID)
return [super initWithParent: document
relativePath: [@"?rev=" stringByAppendingString: revisionID]];
else
return [super initUntitledWithParent: document];
}
- (id) initWithDocument: (CouchDocument*)document properties: (NSDictionary*)properties {
NSParameterAssert(document);
NSParameterAssert(properties);
NSString* revisionID = $castIf(NSString, [properties objectForKey: @"_rev"]);
if (!revisionID) {
[self release];
return nil;
}
self = [self initWithDocument: document revisionID: revisionID];
if (self) {
[self setProperties: properties];
}
return self;
}
- (id) initWithOperation: (RESTOperation*)operation {
NSParameterAssert(operation.isGET);
// Have to block to find out the revision ID. :(
BOOL isDeleted = NO;
if (![operation wait]) {
// Check whether it's been deleted:
if (operation.httpStatus == 404 &&
[[operation.responseBody.fromJSON objectForKey: @"reason"] isEqualToString: @"deleted"]) {
isDeleted = YES;
} else {
if (operation.httpStatus != 404)
Warn(@"CouchRevision initWithOperation failed: %@ on %@", operation.error, operation);
[self release];
return nil;
}
}
self = [self initWithDocument: $castIf(CouchDocument, operation.resource)
properties: operation.responseBody.fromJSON];
if (self) {
_isDeleted = isDeleted;
}
return self;
}
- (id) initWithParent: (RESTResource*)parent relativePath: (NSString*)relativePath {
NSAssert(NO, @"Wrong initializer for CouchRevision");
return nil;
}
- (void)dealloc {
[_properties release];
[super dealloc];
}
- (NSURL*) URL {
if (!self.relativePath)
return self.parent.URL;
// My relativePath is a query string, not a path component
NSString* urlStr = self.parent.URL.absoluteString;
urlStr = [urlStr stringByAppendingString: self.relativePath];
return [NSURL URLWithString: urlStr];
}
- (CouchDocument*) document {
return (CouchDocument*) self.parent;
}
- (NSString*) documentID {
return [(CouchDocument *)self.parent documentID];
}
- (BOOL) isCurrent {
return [self.revisionID isEqualToString: self.document.currentRevisionID];
}
@synthesize isDeleted=_isDeleted;
- (NSString*) revisionID {
return [self.relativePath substringFromIndex: 5];
}
#pragma mark -
#pragma mark CONTENTS / PROPERTIES:
- (BOOL) propertiesAreLoaded {
return _properties != nil;
}
- (NSDictionary*) properties {
if (!_properties && !_gotProperties) {
[[self GET] wait]; // synchronous!
}
return _properties;
}
- (void) setProperties: (NSDictionary*)properties {
if (properties != _properties) {
NSAssert([[properties objectForKey: @"_id"] isEqual: self.documentID],
@"properties have wrong ID %@ for %@", [properties objectForKey: @"_id"], self);
[_properties release];
_properties = [properties copy];
_isDeleted = [$castIf(NSNumber, [properties objectForKey: @"_deleted"]) boolValue];
}
_gotProperties = YES;
}
- (NSDictionary*) userProperties {
NSDictionary* rep = [self properties];
if (!rep)
return nil;
NSMutableDictionary* props = [NSMutableDictionary dictionary];
for (NSString* key in rep) {
if (![key hasPrefix: @"_"])
[props setObject: [rep objectForKey: key] forKey: key];
}
return props;
}
- (id) propertyForKey: (NSString*)key {
return [self.properties objectForKey: key];
}
/** Same as -propertyForKey:. Enables "[]" access in Xcode 4.4+ */
- (id)objectForKeyedSubscript:(NSString*)key {
return [self.properties objectForKey: key];
}
- (RESTOperation*) putProperties: (NSDictionary*)properties {
NSParameterAssert(properties != nil);
NSMutableDictionary* contents = [[properties mutableCopy] autorelease];
[contents setObject: self.documentID forKey: @"_id"];
[contents setObject: self.revisionID forKey: @"_rev"];
RESTOperation* op = [self PUTJSON: contents parameters: nil];
[op onCompletion: ^{
if (op.isSuccessful) {
// Construct a new revision object from the response and assign it to the resultObject:
NSString* rev = $castIf(NSString, [op.responseBody.fromJSON objectForKey: @"rev"]);
if (rev) {
[contents setObject: rev forKey: @"_rev"];
CouchRevision* savedRev = [[CouchRevision alloc] initWithDocument: self.document
properties: contents];
op.resultObject = savedRev;
[savedRev release];
}
}
}];
return op;
}
- (RESTOperation*) sendRequest: (NSURLRequest*)request {
RESTOperation* op = [super sendRequest: request];
if (!op.isReadOnly)
[self.database beginDocumentOperation: self]; // balanced in -operationDidComplete:
return op;
}
- (NSError*) operation: (RESTOperation*)op willCompleteWithError: (NSError*)error {
error = [super operation: op willCompleteWithError: error];
if (op.isSuccessful && !error) {
if (op.isGET) {
// Cache document properties after GET:
self.properties = op.responseBody.fromJSON;
} else if (op.isPUT) {
// Tell the document about the new revision ID:
NSString* rev = $castIf(NSString, [op.responseBody.fromJSON objectForKey: @"rev"]);
if (rev)
[self.document setCurrentRevisionID: rev];
}
} else if (op.isGET && op.httpStatus == 404) {
// Remember that we've checked for properties
_gotProperties = YES;
}
return error;
}
- (void) operationDidComplete: (RESTOperation*)op {
[super operationDidComplete: op];
if (op.resource == self && !op.isReadOnly)
[self.database endDocumentOperation: self];
}
#pragma mark -
#pragma mark ATTACHMENTS:
- (NSDictionary*) attachmentMetadata {
return $castIf(NSDictionary, [self.properties objectForKey: @"_attachments"]);
}
- (NSDictionary*) attachmentMetadataFor: (NSString*)name {
return $castIf(NSDictionary, [self.attachmentMetadata objectForKey: name]);
}
- (NSArray*) attachmentNames {
return [self.attachmentMetadata allKeys];
}
- (CouchAttachment*) attachmentNamed: (NSString*)name {
NSDictionary* metadata = [self attachmentMetadataFor: name];
if (!metadata)
return nil;
return [[[CouchAttachment alloc] initWithParent: self name: name metadata: metadata] autorelease];
}
- (CouchAttachment*) createAttachmentWithName: (NSString*)name type: (NSString*)contentType {
NSDictionary* metadata = [NSDictionary dictionaryWithObject: contentType
forKey: @"content_type"];
return [[[CouchAttachment alloc] initWithParent: self name: name metadata: metadata]
autorelease];
}
@end