-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBSoap+Creating.m
319 lines (270 loc) · 10.1 KB
/
DBSoap+Creating.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
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
316
317
318
319
/* -*- mode: objc -*-
Project: DataBasinKit
Copyright (C) 2009-2019 Free Software Foundation
Author: Riccardo Mottola
Created: 2017-11-10 11:17:31 +0100 by Riccardo Mottola
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import "DBSoap.h"
#import "DBSObject.h"
#import "DBProgressProtocol.h"
#import "DBLoggerProtocol.h"
#import "DBSFTypeWrappers.h"
@implementation DBSoap (Creating)
- (NSMutableArray *)_create :(NSString *)objectName fromArray:(NSMutableArray *)objects progressMonitor:(id<DBProgressProtocol>)p
{
GWSService *service;
NSMutableDictionary *headerDict;
NSMutableDictionary *assignmentRuleHeaderDict;
NSMutableDictionary *sessionHeaderDict;
NSDictionary *resultDict;
NSEnumerator *enumerator;
NSDictionary *queryResult;
NSArray *fieldNames;
int fieldCount;
NSMutableArray *queryObjectsArray;
DBSObject *sObject;
unsigned batchCounter;
NSUInteger totalCounter;
NSMutableArray *resultArray;
if ([objects count] == 0)
return nil;
[p setMaximumValue: [objects count]];
/* prepare the header */
headerDict = [[NSMutableDictionary dictionaryWithCapacity: 2] retain];
[headerDict setObject: GWSSOAPUseLiteral forKey: GWSSOAPUseKey];
/* session header */
sessionHeaderDict = [[NSMutableDictionary dictionaryWithCapacity: 2] retain];
[sessionHeaderDict setObject: sessionId forKey: @"sessionId"];
[sessionHeaderDict setObject: @"urn:partner.soap.sforce.com" forKey: GWSSOAPNamespaceURIKey];
[headerDict setObject: sessionHeaderDict forKey: @"SessionHeader"];
/* assignmentRuleHeader - set Default assignment rule */
if (runAssignmentRules)
{
assignmentRuleHeaderDict = [[NSMutableDictionary dictionaryWithCapacity: 2] retain];
[assignmentRuleHeaderDict setObject: @"true" forKey: @"useDefaultRule"];
[assignmentRuleHeaderDict setObject: @"urn:partner.soap.sforce.com" forKey: GWSSOAPNamespaceURIKey];
[headerDict setObject: assignmentRuleHeaderDict forKey: @"AssignmentRuleHeader"];
[assignmentRuleHeaderDict release];
}
/* init our service */
service = [[DBSoap gwserviceForDBSoap] retain];
[service setURL:serverURL];
[p setCurrentDescription:@"Creating"];
enumerator = [objects objectEnumerator];
batchCounter = 0;
totalCounter = 0;
queryObjectsArray = [[NSMutableArray arrayWithCapacity: upBatchSize] retain];
resultArray = [[NSMutableArray arrayWithCapacity:1] retain];
while ((sObject = [enumerator nextObject]) && ![p shouldStop])
{
unsigned i;
NSMutableDictionary *sObj;
NSMutableDictionary *sObjType;
NSMutableArray *sObjKeyOrder;
NSDictionary *queryObjectsDict;
NSMutableDictionary *parmsDict;
NSMutableDictionary *queryParmDict;
id result;
NSDictionary *queryFault;
NSDictionary *queryError;
NSMutableArray *fieldsToNullArr;
//NSLog(@"inner cycle: %d", batchCounter);
sObj = [NSMutableDictionary dictionaryWithCapacity: 2];
[sObj setObject: @"urn:partner.soap.sforce.com" forKey: GWSSOAPNamespaceURIKey];
sObjKeyOrder = [NSMutableArray arrayWithCapacity: 2];
/* each objects needs its type specifier which has its own namespace */
sObjType = [NSMutableDictionary dictionaryWithCapacity: 2];
[sObjType setObject: @"urn:sobject.partner.soap.sforce.com" forKey: GWSSOAPNamespaceURIKey];
[sObjType setObject: objectName forKey:GWSSOAPValueKey];
[sObj setObject: sObjType forKey:@"type"];
[sObjKeyOrder addObject:@"type"];
/* now we separate fields into two categories:
- fields with empty strings are considered NULL
- fields with a value are considered to update */
fieldNames = [sObject fieldNames];
fieldCount = [fieldNames count];
fieldsToNullArr = [[NSMutableArray alloc] init];
for (i = 0; i < fieldCount; i++)
{
NSString *keyName;
NSString *fieldValue;
keyName = [fieldNames objectAtIndex:i];
fieldValue = [sObject valueForField:keyName];
if ([fieldValue length])
{
[sObj setObject:fieldValue forKey:keyName];
[sObjKeyOrder addObject:keyName];
}
else
{
[fieldsToNullArr addObject:keyName];
}
}
[sObj setObject: sObjKeyOrder forKey: GWSOrderKey];
[queryObjectsArray addObject: sObj];
if ([fieldsToNullArr count])
{
NSDictionary *sObjNillablesList;
sObjNillablesList = [NSDictionary dictionaryWithObjectsAndKeys: fieldsToNullArr, GWSSOAPValueKey, nil];
[sObj setObject:sObjNillablesList forKey: @"fieldsToNull"];
[sObjKeyOrder addObject:@"fieldsToNull"];
}
[fieldsToNullArr release];
if (batchCounter == upBatchSize-1 || totalCounter == [objects count]-1)
{
/* prepare the parameters */
queryParmDict = [NSMutableDictionary dictionaryWithCapacity: 2];
[queryParmDict setObject: @"urn:partner.soap.sforce.com" forKey: GWSSOAPNamespaceURIKey];
[logger log: LogDebug: @"[DBSoap create] create objects array: %@\n", objects];
queryObjectsDict = [NSDictionary dictionaryWithObjectsAndKeys: queryObjectsArray, GWSSOAPValueKey, nil];
[queryParmDict setObject: queryObjectsDict forKey: @"sObjects"];
parmsDict = [NSMutableDictionary dictionaryWithCapacity: 1];
[parmsDict setObject: queryParmDict forKey: @"create"];
[parmsDict setObject: headerDict forKey:GWSSOAPMessageHeadersKey];
/* make the query */
resultDict = [service invokeMethod: @"create"
parameters : parmsDict
order : nil
timeout : standardTimeoutSec];
queryError = [resultDict objectForKey:GWSErrorKey];
if (queryError != nil)
{
[logger log: LogStandard: @"[DBSoap create] Error:%@\n", queryError];
[[NSException exceptionWithName:@"DBException" reason:@"Coder Error, check log" userInfo:nil] raise];
[queryObjectsArray release];
[sessionHeaderDict release];
[headerDict release];
[resultArray release];
[service release];
return nil;
}
queryFault = [resultDict objectForKey:GWSFaultKey];
if (queryFault != nil)
{
NSString *faultCode;
NSString *faultString;
faultCode = [queryFault objectForKey:@"faultcode"];
faultString = [queryFault objectForKey:@"faultstring"];
[logger log: LogStandard: @"[DBSoap create] fault code: %@\n", faultCode];
[logger log: LogStandard: @"[DBSoap create] fault String: %@\n", faultString];
[[NSException exceptionWithName:@"DBException" reason:faultString userInfo:nil] raise];
[queryObjectsArray release];
[sessionHeaderDict release];
[headerDict release];
[resultArray release];
[service release];
return nil;
}
queryResult = [resultDict objectForKey:GWSParametersKey];
result = [queryResult objectForKey:@"result"];
[logger log: LogDebug: @"[DBSoap create] result: %@\n", result];
if (result != nil)
{
NSArray *results;
NSUInteger j;
if (![result isKindOfClass:[NSArray class]])
{
results = [NSArray arrayWithObject:result];
}
else
{
results = (NSArray *)result;
}
//NSLog(@"results : %@", results);
for (j = 0; j < [results count]; j++)
{
NSString *objId;
NSString *successStr;
BOOL success;
NSString *message;
NSString *code;
NSDictionary *r;
NSDictionary *rowDict;
r = [results objectAtIndex:j];
objId = [r objectForKey:@"id"];
successStr = [r objectForKey:@"success"];
success = YES;
if (![successStr isEqualToString:@"true"])
success = NO;
if (success)
{
if (returnSuccessResults)
{
rowDict = [NSDictionary dictionaryWithObjectsAndKeys:
successStr, @"success",
objId, @"id",
@"", @"message",
@"", @"statusCode",
nil];
[resultArray addObject:rowDict];
}
}
else
{
id errorsObj;
NSArray *errors;
errorsObj = [r objectForKey:@"errors"];
if (errorsObj != nil)
{
NSUInteger ec;
NSUInteger howManyErrors;
howManyErrors = 1;
// NSLog(@"errors: %@", errorsObj);
if (![errorsObj isKindOfClass:[NSArray class]])
{
errors = [NSArray arrayWithObject:errorsObj];
}
else
{
errors = (NSArray *)errorsObj;
if (returnMultipleErrors)
howManyErrors = [errors count];
}
for (ec = 0; ec < howManyErrors; ec++)
{
NSDictionary *error = [errors objectAtIndex:ec];
message = [error objectForKey:@"message"];
code = [error objectForKey:@"statusCode"];
rowDict = [NSDictionary dictionaryWithObjectsAndKeys:
successStr, @"success",
objId, @"id",
message, @"message",
code, @"statusCode",
nil];
[resultArray addObject:rowDict];
}
}
}
}
}
[logger log: LogDebug: @"[DBSoap create] reiniting cycle...\n"];
[p incrementCurrentValue: batchCounter+1];
[queryObjectsArray removeAllObjects];
batchCounter = 0;
}
else /* of batch */
{
batchCounter++;
}
totalCounter++;
}
[logger log: LogDebug: @"[DBSoap create] Outer cycle ended\n"];
[queryObjectsArray release];
[sessionHeaderDict release];
[headerDict release];
[service release];
return [resultArray autorelease];
}
@end