Source

baseten / BaseTenAppKit / Sources / BXPredicateEditorRowTemplateFactory.mm

  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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
// BXPredicateEditorRowTemplateFactory.mm
// BaseTen
//
// Copyright (C) 2010 Marko Karppinen & Co. LLC.
//
// Before using this software, please review the available licensing options
// by visiting http://www.karppinen.fi/baseten/licensing/ or by contacting
// us at sales@karppinen.fi. Without an additional license, this software
// may be distributed only in compliance with the GNU General Public License.
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// $Id$
//

#import "BXPredicateEditorRowTemplateFactory.h"
#import "BXMultipleChoicePredicateEditorRowTemplate.h"
#import <BaseTen/BaseTen.h>
#import <BaseTen/BXCollectionFunctions.h>
#import <BaseTen/BXArraySize.h>
#import <BaseTen/BXKeyPathParser.h>


using namespace BaseTen;


@interface BXAttributeKeyPathSortKey : NSObject <NSCopying>
{
	NSArray *mOperators;
	NSAttributeType mAttributeType;
	NSComparisonPredicateModifier mModifier;
	NSUInteger mOptions;
}
@property (readonly, copy, nonatomic) NSArray *operators;
@property (readonly, nonatomic) NSAttributeType attributeType;
@property (readonly, nonatomic) NSComparisonPredicateModifier modifier;
@property (readonly, nonatomic) NSUInteger options;
- (id) initWithOperators: (NSArray *) operators 
		   attributeType: (NSAttributeType) attributeType 
				modifier: (NSComparisonPredicateModifier) modifier 
				 options: (NSUInteger) options;
@end



@interface BXRelationshipKeyPathValue : NSObject
{
	NSString *mKeyPath;
	NSComparisonPredicateModifier mModifier;
}
@property (readonly, nonatomic) NSString *keyPath;
@property (readonly, nonatomic) NSComparisonPredicateModifier modifier;
- (id) initWithKeyPath: (NSString *) keyPath
			  modifier: (NSComparisonPredicateModifier) modifier;
@end



@implementation BXAttributeKeyPathSortKey
@synthesize operators = mOperators;
@synthesize attributeType = mAttributeType;
@synthesize modifier = mModifier;
@synthesize options = mOptions;

- (id) initWithOperators: (NSArray *) operators 
		   attributeType: (NSAttributeType) attributeType 
				modifier: (NSComparisonPredicateModifier) modifier 
				 options: (NSUInteger) options
{
	if ((self = [super init]))
	{
		mOperators = [operators copy];
		mAttributeType = attributeType;
		mModifier = modifier;
		mOptions = options;
	}
	return self;
}


- (void) dealloc
{
	[mOperators release];
	[super dealloc];
}


- (id) copyWithZone: (NSZone *) zone
{
	return [self retain];
}


- (NSUInteger) hash
{
	return mAttributeType ^ mModifier ^ mOptions;
}


- (BOOL) isEqual: (id) anObject
{
	BOOL retval = NO;
	if ([anObject isKindOfClass: [self class]])
	{
		// Since we are immutable, no synchronization is necessary.
		BXAttributeKeyPathSortKey *other = anObject;
		if (mAttributeType == other->mAttributeType && mModifier == other->mModifier &&
			mOptions == other->mOptions && [mOperators isEqual: other->mOperators])
		{
			retval = YES;
		}
	}
	return retval;
}
@end



@implementation BXRelationshipKeyPathValue
@synthesize keyPath = mKeyPath;
@synthesize modifier = mModifier;

- (id) initWithKeyPath: (NSString *) keyPath modifier: (NSComparisonPredicateModifier) modifier
{
	if ((self = [super init]))
	{
		mKeyPath = [keyPath copy];
		mModifier = modifier;
	}
	return self;
}


- (void) dealloc
{
	[mKeyPath release];
	[super dealloc];
}
@end



@implementation BXPredicateEditorRowTemplateFactory
- (id) init
{
	if ((self = [super init]))
	{
		mTypeMapping = [[NSDictionary alloc] initWithObjectsAndKeys:
						ObjectValue <NSAttributeType> (NSBooleanAttributeType),		@"bool",
						ObjectValue <NSAttributeType> (NSBinaryDataAttributeType),	@"bytea",
						ObjectValue <NSAttributeType> (NSStringAttributeType),		@"char",
						ObjectValue <NSAttributeType> (NSFloatAttributeType),		@"float4",
						ObjectValue <NSAttributeType> (NSDoubleAttributeType),		@"float8",
						ObjectValue <NSAttributeType> (NSInteger16AttributeType),	@"int2",
						ObjectValue <NSAttributeType> (NSInteger32AttributeType),	@"int4",
						ObjectValue <NSAttributeType> (NSInteger64AttributeType),	@"int8",
						ObjectValue <NSAttributeType> (NSStringAttributeType),		@"name",
						ObjectValue <NSAttributeType> (NSDecimalAttributeType),		@"numeric",
						ObjectValue <NSAttributeType> (NSInteger32AttributeType),	@"oid",
						ObjectValue <NSAttributeType> (NSStringAttributeType),		@"text",
						ObjectValue <NSAttributeType> (NSDateAttributeType),		@"timestamp",
						ObjectValue <NSAttributeType> (NSDateAttributeType),		@"timestamptz",
						ObjectValue <NSAttributeType> (NSStringAttributeType),		@"varchar",
						nil];
	}
	return self;
}


- (NSAttributeType) attributeTypeForAttributeDescription: (BXAttributeDescription *) desc
{
	NSAttributeType retval = NSUndefinedAttributeType;
	NSString *pgType = [desc databaseTypeName];
	NSValue *attrType = [mTypeMapping objectForKey: pgType];
	if (attrType)
	{
		BaseTen::ValueGetter <NSAttributeType> getter;
		getter (attrType, &retval);
	}
	return retval;
}


- (NSArray *) operatorsForAttributeType: (NSAttributeType) attributeType 
				   attributeDescription: (BXAttributeDescription *) desc
{
	NSArray *retval = nil;
	switch (attributeType)
	{
		case NSStringAttributeType:
		{
			id operators [] = {
				ObjectValue <NSPredicateOperatorType> (NSEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSNotEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSMatchesPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSLikePredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSBeginsWithPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSEndsWithPredicateOperatorType),
				//ObjectValue <NSPredicateOperatorType> (NSInPredicateOperatorType), //Not applicable since the lhs should be a substring of rhs but that's not quite usable.
			};
			retval = [NSArray arrayWithObjects: operators count: BXArraySize (operators)];
			break;
		}
			
		case NSInteger16AttributeType:
		case NSInteger32AttributeType:
		case NSInteger64AttributeType:
		case NSDecimalAttributeType:
		case NSDoubleAttributeType:
		case NSFloatAttributeType:
		case NSDateAttributeType:
		{
			id operators [] = {
				ObjectValue <NSPredicateOperatorType> (NSLessThanPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSLessThanOrEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSGreaterThanPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSGreaterThanOrEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSNotEqualToPredicateOperatorType)
			};
			retval = [NSArray arrayWithObjects: operators count: BXArraySize (operators)];
			break;
		}
			
		case NSBooleanAttributeType:
		{
			id operators [] = {
				ObjectValue <NSPredicateOperatorType> (NSEqualToPredicateOperatorType),
				ObjectValue <NSPredicateOperatorType> (NSNotEqualToPredicateOperatorType)
			};
			retval = [NSArray arrayWithObjects: operators count: BXArraySize (operators)];
			break;
		}
			
		default: 
			break;
	}
	return retval;
}


- (NSUInteger) comparisonOptionsForAttributeType: (NSAttributeType) attributeType
							attributeDescription: (BXAttributeDescription *) desc
{
	NSUInteger retval = 0;
	if (NSStringAttributeType == attributeType)
		retval = NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption;
	return retval;
}


- (NSArray *) templatesWithDisplayNames: (NSArray *) displayNames
				   forAttributeKeyPaths: (NSArray *) keyPaths
					inEntityDescription: (BXEntityDescription *) originalEntity;
{
	// Sort key paths by attribute type, modifier and the operators.
	// (The user might want to override -operatorsForAttributeType:attributeDescription: for individual attributes.)
	// Create the templates and use the display names for display.
	
	NSDictionary *displayNamesByKeyPath = [NSDictionary dictionaryWithObjects: displayNames forKeys: keyPaths];
	NSMutableDictionary *sortedKeyPaths = [NSMutableDictionary dictionary];
	
	for (NSString *keyPath in keyPaths)
	{
		BXEntityDescription *currentEntity = originalEntity;
		NSComparisonPredicateModifier modifier = NSDirectPredicateModifier;
		BOOL inAttribute = NO;
		NSArray *components = BXKeyPathComponents (keyPath);
		BXAttributeDescription *attr = nil;
		
		for (NSString *component in components)
		{
			if (inAttribute)
			{
				[NSException raise: NSInvalidArgumentException format:
				 @"The component '%@' (key path '%@') points to an attribute, even though the end wasn't reached yet.",
				 component, keyPath];
			}
			
			BXRelationshipDescription *rel = nil;
			if ((attr = [[currentEntity attributesByName] objectForKey: component]))
				inAttribute = YES;
			else if ((rel = [[currentEntity relationshipsByName] objectForKey: component]))
			{
				currentEntity = [rel destinationEntity];
				if ([rel isToMany])
					modifier = NSAnyPredicateModifier;
			}
			else
			{
				[NSException raise: NSInvalidArgumentException format: 
				 @"Didn't find property '%@' (key path '%@') in entity %@.%@.", 
				 component, keyPath, [currentEntity schemaName], [currentEntity name]];
			}
		}
		
		if (! inAttribute)
		{
			[NSException raise: NSInvalidArgumentException format:
			 @"The key path '%@' didn't end in an attribute.", keyPath];
		}
		
		NSAttributeType attributeType = [self attributeTypeForAttributeDescription: attr];
		NSArray *operators = [self operatorsForAttributeType: attributeType attributeDescription: attr];
		NSUInteger options = [self comparisonOptionsForAttributeType: attributeType attributeDescription: attr];
		if (operators)
		{
			BXAttributeKeyPathSortKey *sortKey = [[BXAttributeKeyPathSortKey alloc] initWithOperators: operators
																						attributeType: attributeType
																							 modifier: modifier
																							  options: options];
			NSMutableArray *keyPaths = [sortedKeyPaths objectForKey: sortKey];
			if (! keyPaths)
			{
				keyPaths = [[NSMutableArray alloc] init];
				[sortedKeyPaths setObject: keyPaths forKey: sortKey];
				[keyPaths release];
			}
			[keyPaths addObject: keyPath];
			[sortKey release];
		}		
	}
	
	NSMutableArray *templates = [NSMutableArray arrayWithCapacity: [sortedKeyPaths count]];
	for (BXAttributeKeyPathSortKey *sortKey in sortedKeyPaths)
	{
		NSArray *keyPaths = [sortedKeyPaths objectForKey: sortKey];
		NSMutableArray *expressions = [NSMutableArray arrayWithCapacity: [keyPaths count]];
		for (NSString *keyPath in keyPaths)
			[expressions addObject: [NSExpression expressionForKeyPath: keyPath]];
		
		NSPredicateEditorRowTemplate *rowTemplate = nil;
		rowTemplate = [[[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions: expressions
														rightExpressionAttributeType: [sortKey attributeType]
																			modifier: [sortKey modifier]
																		   operators: [sortKey operators]
																			 options: [sortKey options]] autorelease];

		for (NSMenuItem *item in [[[rowTemplate templateViews] objectAtIndex: 0] itemArray])
		{
			NSString *keyPath = [[item representedObject] keyPath];
			[item setTitle: [displayNamesByKeyPath objectForKey: keyPath]];
		}
		
		[templates addObject: rowTemplate];
	}
	
	return [[templates copy] autorelease];
}


- (NSArray *) multipleChoiceTemplatesWithDisplayNames: (NSArray *) displayNames
						 andOptionDisplayNameKeyPaths: (NSArray *) displayNameKeyPaths
							  forRelationshipKeyPaths: (NSArray *) keyPaths
								  inEntityDescription: (BXEntityDescription *) originalEntity
									  databaseContext: (BXDatabaseContext *) ctx
												error: (NSError **) error
{
	// Fetch all objects from the entity at the relationship key path end.
	// For each object just the corresponding display name key path for the display name.
	// Instead of display names etc., compare the primary key and the foreign key.
	
	NSArray *retval = nil;
	NSDictionary *displayNamesByRelKeyPath = [NSDictionary dictionaryWithObjects: displayNames forKeys: keyPaths];
	NSDictionary *optionDisplayNameKeyPathsByRelKeyPath = [NSDictionary dictionaryWithObjects: displayNameKeyPaths forKeys: keyPaths];
	NSMutableDictionary *sortedKeyPaths = [NSMutableDictionary dictionary];
	
	for (NSString *keyPath in keyPaths)
	{
		BXEntityDescription *currentEntity = originalEntity;
		NSComparisonPredicateModifier modifier = NSDirectPredicateModifier;
		NSArray *components = BXKeyPathComponents (keyPath);
		BXRelationshipDescription *rel = nil;
		
		for (NSString *component in components)
		{			
			if ((rel = [[currentEntity relationshipsByName] objectForKey: component]))
			{
				currentEntity = [rel destinationEntity];
				if ([rel isToMany])
					modifier = NSAnyPredicateModifier;
			}
			else if ([[currentEntity attributesByName] objectForKey: component])
			{
				[NSException raise: NSInvalidArgumentException format:
				 @"The component '%@' (key path '%@') points to an attribute, but a relationship was expected.",
				 component, keyPath];				
			}
			else
			{
				[NSException raise: NSInvalidArgumentException format: 
				 @"Didn't find property '%@' (key path '%@') in entity %@.%@.", 
				 component, keyPath, [currentEntity schemaName], [currentEntity name]];
			}
		}
		
		NSMutableArray *keyPaths = [sortedKeyPaths objectForKey: currentEntity];
		if (! keyPaths)
		{
			keyPaths = [NSMutableArray array];
			[sortedKeyPaths setObject: keyPaths forKey: currentEntity];
		}
		
		BXRelationshipKeyPathValue *val = [[BXRelationshipKeyPathValue alloc] initWithKeyPath: keyPath modifier: modifier];
		[keyPaths addObject: val];
		[val release];
	}
	
	NSMutableArray *templates = [NSMutableArray array];
	for (BXEntityDescription *entity in sortedKeyPaths)
	{
		id res = [ctx executeFetchForEntity: entity 
							  withPredicate: nil 
							returningFaults: NO 
						updateAutomatically: YES 
									  error: error];
		
		if (! res)
			goto bail;
		
		for (BXRelationshipKeyPathValue *val in [sortedKeyPaths objectForKey: entity])
		{
			NSString *keyPath = [val keyPath];
			NSComparisonPredicateModifier modifier = [val modifier];
			NSExpression *rightExpression = [NSExpression expressionForKeyPath: keyPath];
			NSString *optionDisplayNameKeyPath = [optionDisplayNameKeyPathsByRelKeyPath objectForKey: keyPath];
			NSString *displayName = [displayNamesByRelKeyPath objectForKey: keyPath];		
			
			BXMultipleChoicePredicateEditorRowTemplate *rowTemplate = nil;
			rowTemplate = [[BXMultipleChoicePredicateEditorRowTemplate alloc] initWithLeftExpressionOptions: res
																							rightExpression: rightExpression
																				   optionDisplayNameKeyPath: optionDisplayNameKeyPath
																				 rightExpressionDisplayName: displayName
																								   modifier: modifier];
			
			[templates addObject: rowTemplate];
			[rowTemplate release];			
		}
	}
	
	retval = [[templates copy] autorelease];
	
bail:
	return retval;
}
@end