-
Notifications
You must be signed in to change notification settings - Fork 53
/
zkApexClient.m
206 lines (178 loc) · 7.06 KB
/
zkApexClient.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
// Copyright (c) 2007,2013 Simon Fell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "zkApexClient.h"
#import "ZKEnvelope.h"
#import "zkCompileResult.h"
#import "zkExecuteAnonResult.h"
#import "zkRunTestResult.h"
#import "zkParser.h"
@implementation ZKApexClient
#define APEX_WS_NS @"http://soap.sforce.com/2006/08/apex"
#define SOAP_ENV_NS @"http://schemas.xmlsoap.org/soap/envelope/"
@synthesize debugLog;
static NSArray *logLevelNames;
-(NSString *)stringOfLogCategory:(ZKLogCategory)c {
switch (c) {
case Category_Db: return @"Db";
case Category_Workflow: return @"Workflow";
case Category_Validation: return @"Validation";
case Category_Callout: return @"Callout";
case Category_Apex_code: return @"Apex_code";
case Category_Apex_profiling: return @"Apex_profiling";
case Category_All: return @"All";
}
@throw [NSException exceptionWithName:@"Unexpected log category" reason:[NSString stringWithFormat:@"LogCategory of %d is not valid", c] userInfo:nil];
}
+(NSString *)stringOfLogLevel:(ZKLogCategoryLevel)l {
switch (l) {
case Level_Internal: return @"Internal";
case Level_Finest: return @"Finest";
case Level_Finer: return @"Finer";
case Level_Fine: return @"Fine";
case Level_Debug: return @"Debug";
case Level_Info: return @"Info";
case Level_Warn: return @"Warn";
case Level_Error: return @"Error";
case Level_None: return @"None";
}
@throw [NSException exceptionWithName:@"Unexpected log level" reason:[NSString stringWithFormat:@"LogLevel of %d is not valid", l] userInfo:nil];
}
+(void)initialize {
NSMutableArray *a = [NSMutableArray array];
for (int i =0; i < 9; i++) {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:i], @"Level", [ZKApexClient stringOfLogLevel:i], @"Name", nil];
[a addObject:d];
}
logLevelNames = [a retain];
}
+ (id) fromClient:(ZKSforceClient *)sf {
return [[[ZKApexClient alloc] initFromClient:sf] autorelease];
}
- (id) initFromClient:(ZKSforceClient *)sf {
self = [super init];
sforce = [sf retain];
debugLog = NO;
for (int i = 0; i < ZKLogCategory_Count; i++)
loggingLevels[i] = Level_None;
return self;
}
- (void)dealloc {
[sforce release];
[lastDebugLog release];
[super dealloc];
}
-(ZKLogCategoryLevel)debugLevelForCategory:(ZKLogCategory)c {
return loggingLevels[c];
}
-(void)setDebugLevel:(ZKLogCategoryLevel)lvl forCategory:(ZKLogCategory)c {
loggingLevels[c] = lvl;
}
+(NSArray *)logLevelNames {
return logLevelNames;
}
-(NSString *)lastDebugLog {
return lastDebugLog;
}
-(void)setLastDebugLog:(NSString *)log {
[lastDebugLog autorelease];
lastDebugLog = [log retain];
}
- (void)setEndpointUrl {
[endpointUrl autorelease];
NSString *sUrl = [[sforce serverUrl] absoluteString];
sUrl = [sUrl stringByReplacingOccurrencesOfString:@"/services/Soap/u/" withString:@"/services/Soap/s/"];
endpointUrl = [[NSURL URLWithString:sUrl] retain];
}
- (ZKEnvelope *)startEnvelope {
ZKEnvelope *e = [[[ZKEnvelope alloc] init] autorelease];
[e start:APEX_WS_NS];
[e writeSessionHeader:[sforce sessionId]];
[e writeCallOptionsHeader:[sforce clientId]];
if ([self debugLog]) {
[e startElement:@"DebuggingHeader"];
for (int i =0; i < ZKLogCategory_Count; i++) {
if (loggingLevels[i] != Level_None) {
[e startElement:@"categories"];
[e addElementString:@"category" elemValue:[self stringOfLogCategory:i]];
[e addElementString:@"level" elemValue:[ZKApexClient stringOfLogLevel:loggingLevels[i]]];
[e endElement:@"categories"];
}
}
[e endElement:@"DebuggingHeader"];
}
[e moveToBody];
[self setEndpointUrl];
return e;
}
-(NSString *)getResponseDebugLog:(zkElement *)soapRoot {
zkElement *headers = [soapRoot childElement:@"Header" ns:SOAP_ENV_NS];
zkElement *debugInfo = [headers childElement:@"DebuggingInfo" ns:APEX_WS_NS];
zkElement *debugLogE = [debugInfo childElement:@"debugLog" ns:APEX_WS_NS];
return [debugLogE stringValue];
}
- (NSArray *)sendAndParseResults:(ZKEnvelope *)requestEnv name:(NSString *)callName resultType:(Class)resultClass {
NSString *soapRequest = [requestEnv end];
zkElement *soapRoot = [self sendRequest:soapRequest name:callName returnRoot:YES];
NSString *debugLogStr = [self getResponseDebugLog:soapRoot];
[self setLastDebugLog:debugLogStr];
zkElement *body = [soapRoot childElement:@"Body" ns:SOAP_ENV_NS];
zkElement *resRoot = [[body childElements] objectAtIndex:0];
NSArray * results = [resRoot childElements:@"result"];
NSMutableArray *resArr = [NSMutableArray arrayWithCapacity:[results count]];
for (zkElement *child in results) {
NSObject *o = [[resultClass alloc] initWithXmlElement:child];
[resArr addObject:o];
[o release];
}
return resArr;
}
- (NSArray *)compile:(NSString *)elemName src:(NSArray *)src {
ZKEnvelope * env = [self startEnvelope];
[env startElement:elemName];
[env addElementArray:@"scripts" elemValue:src];
[env endElement:elemName];
return [self sendAndParseResults:env name:[NSString stringWithFormat:@"%@:", elemName] resultType:[ZKCompileResult class]];
}
- (NSArray *)compilePackages:(NSArray *)src {
return [self compile:@"compilePackages" src:src];
}
- (NSArray *)compileTriggers:(NSArray *)src {
return [self compile:@"compileTriggers" src:src];
}
- (ZKExecuteAnonymousResult *)executeAnonymous:(NSString *)src {
ZKEnvelope * env = [self startEnvelope];
[env startElement:@"executeAnonymous"];
[env addElement:@"String" elemValue:src];
[env endElement:@"executeAnonymous"];
return [[self sendAndParseResults:env name:NSStringFromSelector(_cmd) resultType:[ZKExecuteAnonymousResult class]] objectAtIndex:0];
}
- (ZKRunTestResult *)runTests:(BOOL)allTests namespace:(NSString *)ns packages:(NSArray *)pkgs {
ZKEnvelope *env = [self startEnvelope];
[env startElement:@"runTests"];
[env startElement:@"RunTestsRequest"];
[env addElement:@"allTests" elemValue:allTests ? @"true" : @"false"];
[env addElement:@"namespace" elemValue:ns];
[env addElement:@"packages" elemValue:pkgs];
[env endElement:@"RunTestsRequest"];
[env endElement:@"runTests"];
return [[self sendAndParseResults:env name:NSStringFromSelector(_cmd) resultType:[ZKRunTestResult class]] objectAtIndex:0];
}
@end