-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTRestModelManager.m
186 lines (153 loc) · 5.52 KB
/
OTRestModelManager.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
//
// OTRestModelManager.m
// OTRestFramework
//
// Created by Jeremy Ellison on 8/14/09.
// Copyright 2009 Two Toasters. All rights reserved.
//
#import "OTRestModelManager.h"
#import "OTRestModelLoader.h"
extern NSString* const kOTRestDidEnterOfflineMode = @"kOTRestDidEnterOfflineMode";
extern NSString* const kOTRestDidEnterOnlineMode = @"kOTRestDidEnterOnlineMode";
//////////////////////////////////
// Global Instance
static OTRestModelManager* sharedManager = nil;
///////////////////////////////////
@implementation OTRestModelManager
@synthesize mapper = _mapper;
@synthesize client = _client;
@synthesize objectStore = _objectStore;
@synthesize format = _format;
- (id)initWithBaseURL:(NSString*)baseURL {
if (self = [super init]) {
_mapper = [[OTRestModelMapper alloc] init];
_client = [[OTRestClient clientWithBaseURL:baseURL] retain];
self.format = OTRestMappingFormatXML;
_isOnline = YES;
}
return self;
}
+ (OTRestModelManager*)manager {
return sharedManager;
}
+ (void)setManager:(OTRestModelManager*)manager {
[sharedManager release];
sharedManager = [manager retain];
}
+ (OTRestModelManager*)managerWithBaseURL:(NSString*)baseURL {
OTRestModelManager* manager = [[[OTRestModelManager alloc] initWithBaseURL:baseURL] autorelease];
if (sharedManager == nil) {
[OTRestModelManager setManager:manager];
}
return manager;
}
- (void)dealloc {
[_mapper release];
[_client release];
[super dealloc];
}
- (void)goOffline {
_isOnline = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:kOTRestDidEnterOfflineMode object:[OTRestModelManager manager]];
}
- (void)goOnline {
_isOnline = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kOTRestDidEnterOnlineMode object:[OTRestModelManager manager]];
}
- (BOOL)isOnline {
return _isOnline;
}
- (BOOL)isOffline {
return ![self isOnline];
}
- (void)setFormat:(OTRestMappingFormat)format {
_format = format;
if (OTRestMappingFormatXML == _format) {
[_client setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
} else if (OTRestMappingFormatJSON == _format) {
[_client setValue:@"application/json" forHTTPHeaderField:@"Accept"];
}
}
#pragma mark Model Methods
- (void)registerModel:(Class<OTRestModelMappable>)class forElementNamed:(NSString*)elementName {
[_mapper registerModel:class forElementNamed:elementName];
}
/**
* Load a model from a restful resource and invoke the callback
*/
- (OTRestRequest*)loadModel:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
return [_client get:resourcePath delegate:loader callback:loader.memberCallback];
}
/**
* Load a collection of models from a restful resource and invoke the callback
*/
- (OTRestRequest*)loadModels:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
return [_client get:resourcePath delegate:loader callback:loader.collectionCallback];
}
- (OTRestRequest*)loadModels:(NSString*)resourcePath params:(NSDictionary*)params delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
return [_client get:resourcePath params:params delegate:loader callback:loader.collectionCallback];
}
- (OTRestRequest*)getModel:(id<OTRestModelMappable>)model delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
OTRestRequest* request = [_client get:[model memberPath] delegate:loader callback:loader.memberCallback];
request.userData = model;
return request;
}
- (OTRestRequest*)postModel:(id<OTRestModelMappable>)model delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
OTRestParams* params = [OTRestParams paramsWithDictionary:[model resourceParams]];
OTRestRequest* request = [_client post:[model collectionPath] params:params delegate:loader callback:loader.memberCallback];
request.userData = model;
return request;
}
- (OTRestRequest*)putModel:(id<OTRestModelMappable>)model delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
OTRestParams* params = [OTRestParams paramsWithDictionary:[model resourceParams]];
OTRestRequest* request = [_client put:[model memberPath] params:params delegate:loader callback:loader.memberCallback];
request.userData = model;
return request;
}
- (OTRestRequest*)deleteModel:(id<OTRestModelMappable>)model delegate:(id)delegate callback:(SEL)callback {
if ([self isOffline]) {
return nil;
}
// TODO: are we responsible for deleting the object too,
// or are we to assume that the caller has/will delete it?
OTRestRequest* request = [_client delete:[model memberPath] delegate:delegate callback:callback];
request.userData = model;
return request;
}
@end