-
Notifications
You must be signed in to change notification settings - Fork 53
/
DataSources.m
369 lines (311 loc) · 12.6 KB
/
DataSources.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2006,2012 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 "DataSources.h"
#import "zkDescribeSObject.h"
#import "zkDescribeField.h"
#import "zkChildRelationship.h"
#import "zkSForceClient.h"
#import "DescribeOperation.h"
#import "HighlightTextFieldCell.h"
@interface DescribeListDataSource ()
-(void)updateFilter;
@end
@interface ZKDescribeField (Filtering)
-(BOOL)fieldMatchesFilter:(NSString *)filter;
@end
@implementation ZKDescribeField (Filtering)
-(BOOL)fieldMatchesFilter:(NSString *)filter {
if (filter == nil) return NO;
return [[self name] rangeOfString:filter options:NSCaseInsensitiveSearch].location != NSNotFound;
}
@end
@implementation DescribeListDataSource
- (id)init {
self = [super init];
describeQueue = [[NSOperationQueue alloc] init];
[describeQueue setMaxConcurrentOperationCount:2];
return self;
}
- (void)dealloc {
[types release];
[sforce release];
[operations release];
[describes release];
[describeQueue release];
[filter release];
[filteredTypes release];
[outlineView release];
[descGlobalSobjects release];
[super dealloc];
}
- (void)setTypes:(NSArray *)t view:(NSOutlineView *)ov {
outlineView = [ov retain];
types = [t retain];
describes = [[NSMutableDictionary alloc] init];
operations = [[NSMutableDictionary alloc] init];
NSMutableDictionary *byname = [NSMutableDictionary dictionary];
for (ZKDescribeGlobalSObject *o in t)
[byname setObject:o forKey:[[o name] lowercaseString]];
descGlobalSobjects = [byname retain];
/*
for(NSString *sobject in t) {
sobject = [sobject lowercaseString];
DescribeOperation *op = [[DescribeOperation alloc] initForSObject:sobject cache:self];
[op setQueuePriority:NSOperationQueuePriorityLow];
[operations setObject:op forKey:sobject];
[describeQueue addOperation:op];
}*/
[self updateFilter];
}
- (void)setSforce:(ZKSforceClient *)sf {
sforce = [[sf copy] retain];
}
- (void)prioritizeDescribe:(NSString *)type {
NSOperation *op = [operations objectForKey:[type lowercaseString]];
[op setQueuePriority:NSOperationQueuePriorityHigh];
}
-(void)setFilteredTypes:(NSArray *)t {
NSArray *old = filteredTypes;
[filteredTypes autorelease];
filteredTypes = [t retain];
if (![old isEqualToArray:t])
[outlineView reloadData];
}
-(BOOL)filterIncludesType:(NSString *)type {
if ([type rangeOfString:filter options:NSCaseInsensitiveSearch].location != NSNotFound)
return YES; // easy, type contains the filter clause
if (![self hasDescribe:type])
return NO; // we haven't described it yet
for (ZKDescribeField *f in [[self describe:type] fields]) {
if ([f fieldMatchesFilter:filter])
return YES;
}
return NO;
}
-(void)updateFilter {
if ([filter length] == 0) {
[self setFilteredTypes:types];
return;
}
NSMutableArray *ft = [NSMutableArray array];
for (ZKDescribeGlobalSObject *type in types) {
if ([self filterIncludesType:[type name]])
[ft addObject:type];
}
[self setFilteredTypes:ft];
}
- (NSString *)filter {
return filter;
}
- (void)setFilter:(NSString *)filterValue {
[filter autorelease];
filter = [filterValue copy];
[self updateFilter];
}
- (NSArray *)SObjects {
return types;
}
- (int)numberOfRowsInTableView:(NSTableView *)v {
return [filteredTypes count];
}
- (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)tc row:(int)rowIdx {
return [[filteredTypes objectAtIndex:rowIdx] name];
}
- (BOOL)isTypeDescribable:(NSString *)type {
return nil != [descGlobalSobjects objectForKey:[type lowercaseString]];
}
- (BOOL)hasDescribe:(NSString *)type {
return nil != [describes objectForKey:[type lowercaseString]];
}
- (ZKDescribeSObject *)describe:(NSString *)type {
NSString *t = [type lowercaseString];
ZKDescribeSObject * d = [describes objectForKey:t];
if (d == nil) {
if (![self isTypeDescribable:t])
return nil;
d = [sforce describeSObject:t];
[describes setObject:d forKey:t];
[self performSelectorOnMainThread:@selector(updateFilter) withObject:nil waitUntilDone:NO];
}
return d;
}
// for use in an outline view
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item == nil) return [filteredTypes count];
return [[[self describe:item] fields] count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return item == nil || [item isKindOfClass:[NSString class]];
}
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
if (item == nil) return [[filteredTypes objectAtIndex:index] name];
id f = [[[self describe:item] fields] objectAtIndex:index];
return f;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([[[tableColumn headerCell] stringValue] isEqualToString:@"SObjects"]) {
if ([item isKindOfClass:[NSString class]])
return item;
return [item name];
}
return nil;
}
-(NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
HighlightTextFieldCell *c = [tableColumn dataCell];
[c setTextColor:[NSColor blackColor]];
[c setFont:[NSFont systemFontOfSize:12.0f]];
[c setZkStandout:NO];
if ([item isKindOfClass:[ZKDescribeField class]]) {
if ([item fieldMatchesFilter:filter]) {
[c setFont:[NSFont boldSystemFontOfSize:13.0f]];
[c setTextColor:[[NSColor blueColor] blendedColorWithFraction:0.5 ofColor:[NSColor blackColor]]];
[c setZkStandout:YES];
}
}
return c;
}
-(CGFloat)outlineView:(NSOutlineView *)ov heightOfRowByItem:(id)item {
if ([item isKindOfClass:[ZKDescribeField class]]) {
if ([item fieldMatchesFilter:filter]) {
return [ov rowHeight] + 4;
}
}
return [ov rowHeight];
}
@end
@implementation SObjectDataSource
- (id)initWithDescribe:(ZKDescribeSObject *)s {
self = [super init];
sobject = [s retain];
NSMutableArray *t = [NSMutableArray arrayWithObjects:@"Name", @"Label", @"PluralLabel", @"Key Prefix", @"Custom",
@"Createable", @"Updateable", @"Activateable", @"Deletable", @"Undeletable",
@"Mergeable", @"Queryable", @"Retrieveable", @"Searchable", @"Layoutable",
@"Replicateable", @"Triggerable", @"URL for Edit", @"URL for Detail", @"URL for New", nil];
NSArray *cr = [s childRelationships];
if ([cr count] > 0) {
NSString *sectionTitle = [NSString stringWithFormat:@"Relationships to %@", [sobject name]];
NSAttributedString *boldTitle = [[[NSAttributedString alloc] initWithString:sectionTitle attributes:[NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:11] forKey:NSFontAttributeName]] autorelease];
[t addObject:boldTitle];
for (ZKChildRelationship *r in cr) {
[t addObject:[NSString stringWithFormat:@"%@.%@", [r childSObject], [r field]]];
}
}
titles = [t retain];
return self;
}
- (void)dealloc {
[sobject release];
[titles release];
[super dealloc];
}
- (NSString *)description {
return [NSString stringWithFormat:@"SObject : %@", [sobject name]];
}
// for use in a table view
-(int)numberOfRowsInTableView:(NSTableView *)view {
return [titles count];
}
-(id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)tc row:(int)rowIdx {
if ([[tc identifier] isEqualToString:@"title"])
return [titles objectAtIndex:rowIdx];
SEL selectors[] = { @selector(name), @selector(label), @selector(labelPlural), @selector(keyPrefix), @selector(custom),
@selector(createable), @selector(updateable), @selector(activateable), @selector(deletable), @selector(undeletable),
@selector(mergeable), @selector(queryable), @selector(retrieveable), @selector(searchable), @selector(layoutable),
@selector(replicateable), @selector(triggerable), @selector(urlEdit), @selector(urlDetail), @selector(urlNew) };
int numSelectors = sizeof(selectors)/sizeof(*selectors);
if (rowIdx < numSelectors) {
SEL theSel = selectors[rowIdx];
id f = [sobject performSelector:theSel];
const char *returnType = [[sobject methodSignatureForSelector:theSel] methodReturnType];
if (returnType[0] == 'c') // aka char aka Bool
return f ? @"Yes" : @"";
return [sobject performSelector:theSel];
}
if (rowIdx == numSelectors)
return @""; // this is the value for the Child Relationships title row
ZKChildRelationship *cr = [[sobject childRelationships] objectAtIndex:rowIdx - numSelectors -1];
return [NSString stringWithFormat:@"%@", [cr relationshipName] == nil ? @"" : [cr relationshipName]];
}
@end
@implementation SObjectFieldDataSource
- (id)initWithDescribe:(ZKDescribeField *)f {
self = [super init];
field = [f retain];
titles = [[NSArray arrayWithObjects:@"Name", @"Label", @"Type", @"Custom", @"Help Text",
@"Length", @"Digits", @"Scale", @"Precision", @"Byte Length",
@"Createable", @"Updatable", @"Cascade Delete", @"Restricted Delete",
@"Default On Create", @"Calculated", @"AutoNumber",
@"Unique", @"Case Sensitive", @"Name Pointing", @"Sortable", @"Groupable", @"Permissionable",
@"External Id", @"ID Lookup", @"Filterable", @"HTML Formatted", @"Name Field", @"Nillable",
@"Name Pointing", @"Reference To", @"Relationship Name",
@"Dependent Picklist", @"Controller Name", @"Restricted Picklist",
@"Value Formula", @"Default Formula", @"Relationship Order (CJOs)", @"Write Requires Read on Master (CJOs)", @"Display Location in Decimal", nil] retain];
return self;
}
- (void)dealloc {
[field release];
[titles release];
[super dealloc];
}
- (NSString *)description {
return [NSString stringWithFormat:@"Field : %@.%@", [[field sobject] name], [field name]];
}
// for use in a table view
- (int)numberOfRowsInTableView:(NSTableView *)view {
return [titles count];
}
- (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)tc row:(int)rowIdx {
if ([[tc identifier] isEqualToString:@"title"])
return [titles objectAtIndex:rowIdx];
SEL selectors[] = { @selector(name), @selector(label), @selector(type), @selector(custom), @selector(inlineHelpText),
@selector(length), @selector(digits), @selector(scale), @selector(precision), @selector(byteLength),
@selector(createable), @selector(updateable), @selector(cascadeDelete), @selector(restrictedDelete),
@selector(defaultedOnCreate), @selector(calculated), @selector(autoNumber),
@selector(unique), @selector(caseSensitive), @selector(namePointing), @selector(sortable), @selector(groupable), @selector(permissionable),
@selector(externalId), @selector(idLookup), @selector(filterable), @selector(htmlFormatted), @selector(nameField), @selector(nillable),
@selector(namePointing), @selector(referenceTo), @selector(relationshipName),
@selector(dependentPicklist), @selector(controllerName), @selector(restrictedPicklist),
@selector(calculatedFormula), @selector(defaultValueFormula), @selector(relationshipOrder), @selector(writeRequiresMasterRead), @selector(displayLocationInDecimal) };
if (field == nil) return @"";
id f = [field performSelector:selectors[rowIdx]];
const char *returnType = [[field methodSignatureForSelector:selectors[rowIdx]] methodReturnType];
if (returnType[0] == 'c')
return f ? @"Yes" : @"";
if (returnType[0] == 'i')
return f == 0 ? (id)@"" : (id)[NSNumber numberWithInt:(int)f];
if (returnType[0] == 'q')
return [NSNumber numberWithLongLong:(long long)f];
if (returnType[0] == '@') {
if ([f isKindOfClass:[NSArray class]]) {
if ([f count] == 0) return @"";
return [f componentsJoinedByString:@", "];
}
return f;
}
NSLog(@"Unexpected return type of %c for selector %s", *returnType, sel_getName(selectors[rowIdx]));
return f;
}
@end
@implementation NoSelection
-(BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex {
return NO;
}
@end