-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAVObject.cpp
More file actions
315 lines (262 loc) · 8.95 KB
/
Copy pathAVObject.cpp
File metadata and controls
315 lines (262 loc) · 8.95 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* @file AVObject.cpp
* @author yang chaozhong <cyang@avoscloud.com>
* @date Tue Jul 29 11:20:25 2014
*
* @brief
*
* Copyright 2014 AVOS Cloud Inc. All rights reserved.
*/
#include "AVObject/AVObject.h"
#include <string>
#include <vector>
#include <algorithm>
#include "AVObject/AVRelation.h"
#include "Request/AVPaasClient.h"
NS_AV_BEGIN
using boost::any_cast;
AVObject::AVObject():objectId(),
addedRelationData(),
removedRelationData() {
}
AVObject* AVObject::objectWithClassName(std::string const & className) {
AVObject *object = new AVObject();
object->className = className;
return object;
}
AVObject* AVObject::objectWithoutDataWithClassName(std::string const & className,
std::string const & objectId) {
AVObject *object = new AVObject();
object->className = className;
object->objectId = objectId;
return object;
}
void AVObject::release() {
AV_SAFE_DELETE(this);
}
void AVObject::setACL(AVACL* acl) {
this->acl = acl;
this->localData["ACL"] = acl->permissionById;
}
AVACL* AVObject::getACL() {
return this->acl;
}
std::vector<std::string> AVObject::allKeys() {
std::vector<std::string> keys;
for (auto it = this->localData.begin(); it != this->localData.end(); ++it) {
keys.push_back(it.key().asString());
}
return keys;
}
Json::Value AVObject::objectForKey(std::string const & key) {
return this->localData[key];
}
void AVObject::setObjectForKey(Json::Value const & object,
std::string const & key) {
this->localData[key] = object;
}
void AVObject::setObjectForKey(AVObject* const & object,
std::string const & key) {
if (object->hasValidObjectId()) {
Json::Value value(Json::objectValue);
value["__type"] = "Pointer";
value["className"] = object->className;
value["objectId"] = object->objectId;
this->localData[key] = value;
}
}
void AVObject::setObjectForKey(AVGeoPoint* const & geoPoint,
std::string const & key) {
Json::Value value = AVGeoPoint::dictionaryFromGeoPoint(geoPoint);
this->localData[key] = value;
}
void AVObject::removeObjectForKey(std::string const & key) {
this->localData.removeMember(key);
}
AVRelation* AVObject::relationForKey(std::string key) {
std::vector<AVObject*> array;
if (this->addedRelationData.find(key) != this->addedRelationData.end()) {
array = this->addedRelationData[key];
} else {
this->addedRelationData[key] = array;
}
AVObject* target = nullptr;
if (array.size() > 0) {
target = array[0];
}
AVRelation* relation = new AVRelation();
relation->parent = this;
relation->key = key;
if (target != nullptr) {
relation->targetClass = target->className;
}
return relation;
}
void AVObject::addRelationForKey(AVObject* object, std::string key) {
this->addObjectIntoRelationDataForKey(&(this->addedRelationData), object, key);
}
void AVObject::removeRelationForKey(AVObject* object, std::string key) {
this->addObjectIntoRelationDataForKey(&(this->removedRelationData), object, key);
}
void AVObject::addObjectIntoRelationDataForKey(std::unordered_map<std::string, std::vector<AVObject*>> *relationData,
AVObject* object,
std::string key) {
if (object->hasValidObjectId()) {
auto got = relationData->find(key);
if (got == relationData->end()) {
std::vector<AVObject*> array;
array.push_back(object);
(*relationData)[key] = array;
} else {
got->second.push_back(object);
}
}
}
Json::Value AVObject::generateRelationObjectsByArray(std::vector<AVObject*> objects,
bool isAdded) {
Json::Value root;
if (isAdded) {
root["__op"] = "AddRelation";
} else {
root["__op"] = "RemoveRelation";
}
root["objects"] = Json::arrayValue;
for (auto& object : objects) {
Json::Value obj(Json::objectValue);
obj["__type"] = "Pointer";
obj["className"] = object->className;
obj["objectId"] = object->objectId;
root["objects"].append(obj);
}
return root;
}
void AVObject::saveInBackground() {
this->saveInBackgroundWithCallback([&](bool const& succeeded, AVError const& error){
// do nothing
});
}
void AVObject::saveInBackgroundWithCallback(AVBooleanResultCallback callback) {
for (auto& kv : this->addedRelationData) {
std::string key = kv.first;
std::vector<AVObject*> objects = kv.second;
this->localData[key] = this->generateRelationObjectsByArray(objects, true);
}
for (auto& kv : this->removedRelationData) {
std::string key = kv.first;
std::vector<AVObject*> objects = kv.second;
this->localData[key] = this->generateRelationObjectsByArray(objects, false);
}
this->addedRelationData.clear();
this->removedRelationData.clear();
if (this->hasValidObjectId()) {
std::string sessionToken;
AVPaasClient::sharedInstance()->
putObject(this->myObjectPath(),
this->localData,
sessionToken, [&](Json::Value const & root,
AVError const & error){
if (error.domain.length() == 0) {
callback(true, error);
} else {
callback(false, error);
}
});
} else {
AVPaasClient::sharedInstance()->
postObject(this->myObjectPath(),
this->localData,
[&](Json::Value const & root,
AVError const & error){
if (error.domain.length() == 0) {
this->objectId = root["objectId"].asString();
this->createdAt = root["createdAt"].asString();
callback(true, error);
} else {
callback(false, error);
}
});
}
}
void AVObject::saveAllInBackground(std::vector<AVObject*> objects) {
for (auto & object : objects) {
object->saveInBackground();
}
}
void AVObject::saveAllInBackgroundWithCallback(std::vector<AVObject*> objects,
AVBooleanResultCallback callback) {
for (auto & object : objects) {
object->saveInBackgroundWithCallback(callback);
}
}
void AVObject::fetch() {
std::vector<std::string> keys;
this->fetchWithKeys(keys);
}
void AVObject::fetchWithKeys(std::vector<std::string> keys) {
if (this->hasValidObjectId()) {
Json::Value parameters;
if (keys.size() > 0) {
std::string includeKeys;
for (auto& key:keys) {
includeKeys.append(key);
includeKeys.append(",");
}
parameters["include"] = includeKeys;
}
AVPaasClient::sharedInstance()->
getObject(this->myObjectPath(),
parameters,
[&](Json::Value const & root,
AVError const & error){
if (error.domain.length() == 0) {
for (auto it = root.begin(); it != root.end(); ++it) {
std::string key = it.key().asString();
if (key == "createdAt") {
std::string value = (*it).asString();
this->createdAt = value;
} else if (key == "updateAt") {
std::string value = (*it).asString();
this->updatedAt = value;
}
}
this->localData = root;
this->localData.removeMember("objectId");
this->localData.removeMember("createdAt");
this->localData.removeMember("updatedAt");
}
});
}
}
void AVObject::deleteInBackground() {
this->deleteInBackgroundWithCallback([&](bool const& succeeded, AVError const& error){
// do nothing
});
}
void AVObject::deleteInBackgroundWithCallback(AVBooleanResultCallback callback) {
if (this->hasValidObjectId()) {
Json::Value parameters;
AVPaasClient::sharedInstance()->
deleteObject(this->myObjectPath(),
parameters,
[&](Json::Value const & root,
AVError const & error){
if (error.domain.length() == 0) {
callback(true, error);
} else {
callback(false, error);
}
});
}
}
bool AVObject::hasValidObjectId() {
return this->objectId.length() > 0;
}
///////////////////////////////private methods /////////////////////////////
std::string AVObject::myObjectPath() {
if (this->hasValidObjectId()) {
return StringUtils::string_format("classes/%s/%s", this->className, this->objectId);
} else {
return StringUtils::string_format("classes/%s", this->className);
}
}
NS_AV_END