Source

baseten / Sources / BXEntityDescription.m

Full commit
  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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//
// BXEntityDescription.m
// BaseTen
//
// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
//
// Before using this software, please review the available licensing options
// by visiting http://basetenframework.org/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 "BXEntityDescription.h"
#import "BXEntityDescriptionPrivate.h"
#import "BXDatabaseAdditions.h"
#import "BXDatabaseContext.h"
#import "BXAttributeDescription.h"
#import "BXRelationshipDescription.h"
#import "BXAttributeDescriptionPrivate.h"
#import "BXPropertyDescriptionPrivate.h"
#import "BXDatabaseObject.h"
#import "BXConstantsPrivate.h"
#import "BXLogger.h"
#import "PGTSHOM.h"
#import "BXWeakNotification.h"

#import "MKCCollections.h"


static id gEntities;


/**
 * An entity description contains information about a specific table
 * in a given database.
 * Only one entity description instance is created for a combination of a database
 * URI, a schema and a table.
 *
 * \note This class is not thread-safe, i.e. 
 *       if methods of an BXEntityDescription instance will be called from 
 *       different threads the result is undefined.
 * \ingroup Descriptions
 */
@implementation BXEntityDescription

+ (void) initialize
{
    static BOOL tooLate = NO;
    if (NO == tooLate)
    {
        tooLate = YES;
        gEntities = [MKCDictionary copyDictionaryWithKeyType: kMKCCollectionTypeObject
												   valueType: kMKCCollectionTypeWeakObject];
    }
}

- (id) init
{
	BXLogError (@"This initializer should not have been called.");
    [self release];
    return nil;
}

- (id) initWithName: (NSString *) aName
{
	BXLogError (@"This initializer should not have been called (name: %@).", aName);
    [self release];
    return nil;
}

/** \note Override -dealloc2 in subclasses instead! */
- (void) dealloc
{
	@synchronized (gEntities)
	{
		[gEntities removeObjectForKey: [self entityKey]];
	}
	
	@synchronized (mAttributes)
	{
		TSEnumerate (currentAttribute, e, [mAttributes objectEnumerator])
		{
			[currentAttribute setEntity: nil];
			[[currentAttribute class] unregisterProperty: currentAttribute entity: self];
		}
	}
    
    @synchronized (mRelationships)
    {
        TSEnumerate (currentRel, e, [mRelationships objectEnumerator])
        {
            [currentRel setEntity: nil];
            [[currentRel inverseRelationship] setDestinationEntity: nil];
			[[currentRel class] unregisterProperty: currentRel entity: self];
        }
    }
    
    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
	NSNotification* n = [BXWeakNotification notificationWithName: 
						 kBXEntityDescriptionWillDeallocNotification object: self];
	[nc postNotification: n];
    
	[self dealloc2];
	[super dealloc];
}

/** 
 * Deallocation helper. 
 * Subclasses should override this instead of dealloc and then call 
 * super's implementation of dealloc2. This is because BXEntityDescriptions 
 * will be stored into a non-retaining collection on creation and removed from 
 * it on dealloc.
 */
- (void) dealloc2
{
	[mDatabaseURI release];
	mDatabaseURI = nil;
	
	[mSchemaName release];
	mSchemaName = nil;

	[mAttributes release];
	mAttributes = nil;

	[mValidationLock release];
	mValidationLock = nil;

	[mObjectIDs release];
	mObjectIDs = nil;
	
	[mRelationships release];
	mRelationships = nil;
	    
    [mSuperEntities release];
    mSuperEntities = nil;
    
    [mSubEntities release];
    mSubEntities = nil;
	
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

/** The schema name. */
- (NSString *) schemaName
{
    return [[mSchemaName retain] autorelease];
}

/** The database URI. */
- (NSURL *) databaseURI
{
    return mDatabaseURI;
}

- (id) initWithCoder: (NSCoder *) decoder
{
    NSURL* databaseURI = [decoder decodeObjectForKey: @"databaseURI"];
    NSString* schemaName = [decoder decodeObjectForKey: @"schemaName"];
    NSString* name = [decoder decodeObjectForKey: @"name"];
    id rval = [[[self class] entityWithDatabaseURI: databaseURI table: name inSchema: schemaName] retain];
    
    Class cls = NSClassFromString ([decoder decodeObjectForKey: @"databaseObjectClassName"]);
    if (Nil != cls)
        [rval setDatabaseObjectClass: cls];
		
	[self setAttributes: [decoder decodeObjectForKey: @"attributes"]];
	//FIXME: relationships as well?
 	        
    return rval;
}

- (void) encodeWithCoder: (NSCoder *) encoder
{
    [encoder encodeObject: mName forKey: @"name"];
    [encoder encodeObject: mSchemaName forKey: @"schemaName"];
    [encoder encodeObject: mDatabaseURI forKey: @"databaseURI"];
    [encoder encodeObject: NSStringFromClass (mDatabaseObjectClass) forKey: @"databaseObjectClassName"];
	[encoder encodeObject: mAttributes forKey: @"attributes"];
	//FIXME: relationships as well?
}

/** Retain on copy. */
- (id) copyWithZone: (NSZone *) zone
{
    return [self retain];
}

- (BOOL) isEqual: (id) anObject
{
    BOOL retval = NO;
    
    if (self == anObject)
        retval = YES;
    else if ([anObject isKindOfClass: [self class]] && [super isEqual: anObject])
	{
		
		BXEntityDescription* aDesc = (BXEntityDescription *) anObject;
        
		BXAssertValueReturn (nil != mName && nil != mSchemaName && nil != mDatabaseURI, NO, 
							   @"Properties should not be nil in -isEqual:.");
		BXAssertValueReturn (nil != aDesc->mName && nil != aDesc->mSchemaName && nil != aDesc->mDatabaseURI, NO, 
							   @"Properties should not be nil in -isEqual:.");
		
		
		if (![mSchemaName isEqualToString: aDesc->mSchemaName])
			goto bail;
		
		if (![mDatabaseURI isEqual: aDesc->mDatabaseURI])
			goto bail;
			
		retval = YES;
	}
bail:
    return retval;
}

- (unsigned int) hash
{
    if (0 == mHash)
    {
        //We use a real hash function with the URI.
        mHash = ([super hash] ^ [mSchemaName hash] ^ [mDatabaseURI BXHash]);
    }
    return mHash;
}

- (NSString *) description
{
    return [NSString stringWithFormat: @"<%@ %@ (%p)>", mDatabaseURI, [self name], self];
}

/**
 * Set the class for this entity.
 * Objects fetched using this entity will be instances of
 * the given class, which needs to be a subclass of BXDatabaseObject.
 * \param       cls         The object class.
 */
- (void) setDatabaseObjectClass: (Class) cls
{
    if (YES == [cls isSubclassOfClass: [BXDatabaseObject class]])
	{
        mDatabaseObjectClass = cls;
	}
    else
    {
        NSString* reason = [NSString stringWithFormat: @"Expected %@ to be a subclass of BXDatabaseObject.", cls];
        [NSException exceptionWithName: NSInternalInconsistencyException
                                reason: reason userInfo: nil];
    }
}

/**
 * The class for this entity
 * \return          The default class is BXDatabaseObject.
 */
- (Class) databaseObjectClass
{
    return mDatabaseObjectClass;
}

/**
 * Set the primary key fields for this entity.
 * Normally the database context determines the primary key, when
 * an entity is used in a database query. However, when an entity is a view, the fields
 * may need to be set manually before using the entity in a query.
 * \param   anArray     An NSArray of NSStrings.
 * \internal
 * \note BXAttributeDescriptions should only be created here and in -[BXInterface validateEntity:]
 */
- (void) setPrimaryKeyFields: (NSArray *) anArray
{
	if (nil != anArray)
	{
		NSMutableDictionary* attributes = [[mAttributes mutableCopy] autorelease];
		TSEnumerate (currentField, e, [anArray objectEnumerator])
		{
			BXAttributeDescription* attribute = nil;
			if ([currentField isKindOfClass: [BXAttributeDescription class]])
			{
				BXAssertVoidReturn ([currentField entity] == self, 
									  @"Expected to receive only attributes in which entity is self (self: %@ currentField: %@).",
									  self, currentField);
				attribute = currentField;
			}
			else if ([currentField isKindOfClass: [NSString class]])
			{
                attribute = [BXAttributeDescription attributeWithName: currentField entity: self];
			}
			[attribute setPrimaryKey: YES];
			[attribute setOptional: NO];
			[attributes setObject: attribute forKey: [attribute name]];
		}
		[self setAttributes: attributes];
	}
}

/**
 * Registered object IDs for this entity.
 */
- (NSArray *) objectIDs
{
	return [mObjectIDs allObjects];
}

static int
FilterPkeyAttributes (id attribute, void* arg)
{
	int retval = 0;
	long shouldBePkey = (long) arg;
	if ([attribute isPrimaryKey] == shouldBePkey)
		retval = 1;
	return retval;
}

/**
 * Primary key fields for this entity.
 * The fields get determined automatically after database connection has been made.
 * \return          An array of BXAttributeDescriptions
 * \see #isValidated
 */
- (NSArray *) primaryKeyFields
{
	return [mAttributes PGTSValueSelectFunction: &FilterPkeyAttributes argument: (void *) 1L] ?: nil;
}
	
+ (NSSet *) keyPathsForValuesAffectingFields
{
	return [NSSet setWithObject: @"primaryKeyFields"];
}

/** 
 * Non-primary key fields for this entity.
 * \return          An array of BXAttributeDescriptions
 * \see #isValidated
 */
- (NSArray *) fields
{
	return [mAttributes PGTSValueSelectFunction: &FilterPkeyAttributes argument: (void *) 0L] ?: nil;
}

/** Whether this entity is marked as a view or not. */
- (BOOL) isView
{
    return (mFlags & kBXEntityIsView) ? YES : NO;
}

- (NSComparisonResult) caseInsensitiveCompare: (BXEntityDescription *) anotherEntity
{
    BXAssertValueReturn ([anotherEntity isKindOfClass: [BXEntityDescription class]], NSOrderedSame, 
					 @"Entity descriptions can only be compared with other similar objects for now.");
    NSComparisonResult rval = NSOrderedSame;
    if (self != anotherEntity)
    {
        rval = [mSchemaName caseInsensitiveCompare: [anotherEntity schemaName]];
        if (NSOrderedSame == rval)
        {
            rval = [mName caseInsensitiveCompare: [anotherEntity name]];
        }
    }
    return rval;
}

/** 
 * Attributes for this entity.
 * Primary key fields and other fields for this entity.
 * \return          An NSDictionary with NSStrings as keys and BXAttributeDescriptions as objects.
 * \see #isValidated
 */
- (NSDictionary *) attributesByName
{
	return mAttributes;
}

/**
 * Entity validation.
 * The entity will be validated after a database connection has been made. Afterwards, 
 * #fields, #primaryKeyFields, #attributesByName and #relationshipsByName return meaningful values.
 */
- (BOOL) isValidated
{
	return (mFlags & kBXEntityIsValidated) ? YES : NO;
}

/**
 * Relationships for this entity.
 * \return An NSDictionary with NSStrings as keys and BXRelationshipDescriptions as objects.
 */
- (NSDictionary *) relationshipsByName
{
	if (! [self hasCapability: kBXEntityCapabilityRelationships])
		[NSException raise: NSInvalidArgumentException format: @"Entity %@ can't access its relationships.", self];
	return [[mRelationships copy] autorelease];
}

- (BOOL) hasCapability: (enum BXEntityCapability) aCapability
{
	return (mCapabilities & aCapability ? YES : NO);
}

- (BOOL) isEnabled
{
	return (mFlags & kBXEntityIsEnabled) ? YES : NO;
}
@end


@implementation BXEntityDescription (PrivateMethods)

- (NSURL *) entityKey
{
	return [[self class] entityKeyForDatabaseURI: mDatabaseURI schema: mSchemaName table: mName];
}

+ (NSURL *) entityKeyForDatabaseURI: (NSURL *) databaseURI schema: (NSString *) schemaName table: (NSString *) tableName
{
    databaseURI = [databaseURI BXURIForHost: nil database: nil username: @"" password: @""];
	return [NSURL URLWithString: [NSString stringWithFormat: @"%@/%@", schemaName, tableName] relativeToURL: databaseURI];
}

/**
 * \internal
 * \name Retrieving an entity description
 */
//@{
/**
 * \internal
 * Create the entity.
 * \param       anURI   The database URI
 * \param       tName   Table name
 * \param       sName   Schema name
 */
+ (id) entityWithDatabaseURI: (NSURL *) anURI table: (NSString *) tName inSchema: (NSString *) sName
{
	id retval = nil;
	@synchronized (gEntities)
	{
		if (nil == sName)
			sName = @"public";
		
		NSURL* uri = [self entityKeyForDatabaseURI: anURI schema: sName table: tName];
		
		retval = [gEntities objectForKey: uri];
		if (nil == retval)
		{
			retval = [[[self alloc] initWithDatabaseURI: anURI table: tName inSchema: sName] autorelease];
			[gEntities setObject: retval forKey: uri];
		}		
	}
	
	return retval;
}

/**
 * \internal
 * The designated initializer.
 * Create the entity.
 * \param       anURI   The database URI
 * \param       tName   Table name
 * \param       sName   Schema name
 */
- (id) initWithDatabaseURI: (NSURL *) anURI table: (NSString *) tName inSchema: (NSString *) sName
{
	BXAssertValueReturn (nil != sName, nil, @"Expected sName not to be nil.");
	BXAssertValueReturn (nil != anURI, nil, @"Expected anURI to be set.");
	
    if ((self = [super initWithName: tName]))
    {
        mDatabaseObjectClass = [BXDatabaseObject class];
        mDatabaseURI = [anURI copy];
        mSchemaName = [sName copy];
		mRelationships = [[NSMutableDictionary alloc] init];
		
		mObjectIDs = [[MKCHashTable alloc] init];
        mSuperEntities = [[MKCHashTable alloc] init];
        mSubEntities = [[MKCHashTable alloc] init];
		
		mValidationLock = [[NSLock alloc] init];
    }
    return self;
}
//@}

- (void) registerObjectID: (BXDatabaseObjectID *) anID
{
	@synchronized (mObjectIDs)
	{
		BXAssertVoidReturn ([anID entity] == self, 
							  @"Attempted to register an object ID the entity of which is other than self.\n"
							  "\tanID:\t%@ \n\tself:\t%@", anID, self);
		if (self == [anID entity])
			[mObjectIDs addObject: anID];
	}
}

- (void) unregisterObjectID: (BXDatabaseObjectID *) anID
{
	@synchronized (mObjectIDs)
	{
		[mObjectIDs removeObject: anID];
	}
}

- (void) setAttributes: (NSDictionary *) attributes
{
	if (attributes != mAttributes)
	{
		TSEnumerate (currentAttribute, e, [mAttributes objectEnumerator])
			[[currentAttribute class] unregisterProperty: currentAttribute entity: self];
		
		[mAttributes release];
		mAttributes = [attributes copy];
	}
}

- (void) setDatabaseURI: (NSURL *) anURI
{
	//In case we really modify the URI, remove self from collections and have the hash calculated again.
	if (anURI != mDatabaseURI && NO == [anURI isEqual: mDatabaseURI])
	{
		@synchronized (gEntities)
		{
			[gEntities removeObjectForKey: [self entityKey]];
			mHash = 0;
			
			[mDatabaseURI release];
			mDatabaseURI = [anURI retain];
			
			[gEntities setObject: self forKey: [self entityKey]];
		}
	}
}

- (void) resetAttributeExclusion
{
	TSEnumerate (currentProp, e, [mAttributes objectEnumerator])
		[currentProp setExcluded: NO];
}

- (NSArray *) attributes: (NSArray *) strings
{
	NSMutableArray* rval = nil;
	if (0 < [strings count])
	{
		rval = [NSMutableArray arrayWithCapacity: [strings count]];
		TSEnumerate (currentField, e, [strings objectEnumerator])
		{
			if ([currentField isKindOfClass: [NSString class]])
				currentField = [mAttributes objectForKey: currentField];
			BXAssertValueReturn ([currentField isKindOfClass: [BXAttributeDescription class]], nil, 
								   @"Expected to receive NSStrings or BXAttributeDescriptions (%@ was a %@).",
								   currentField, [currentField class]);
			
			[rval addObject: currentField];
		}
	}
	return rval;
}

- (void) setValidated: (BOOL) flag
{
	if (flag)
		mFlags |= kBXEntityIsValidated;
	else
		mFlags &= ~kBXEntityIsValidated;
}

- (void) setIsView: (BOOL) flag
{
	if (flag)
		mFlags |= kBXEntityIsView;
	else
		mFlags &= ~kBXEntityIsView;
}

- (void) setRelationships: (NSDictionary *) aDict
{
    @synchronized (mRelationships)
    {
		TSEnumerate (currentRel, e, [mRelationships objectEnumerator])
			[[currentRel class] unregisterProperty: currentRel entity: self];

		[mRelationships removeAllObjects];
		[mRelationships addEntriesFromDictionary: aDict];
    }
}

- (NSLock *) validationLock
{
	return mValidationLock;
}

- (void) removeRelationship: (BXRelationshipDescription *) aRelationship;
{
    @synchronized (mRelationships)
    {
        //FIXME: this is a bit bad but there seems to be an over-retain for BXEntityDescription somewhere.
        [self setValidated: NO];
        
		[[aRelationship class] unregisterProperty: aRelationship entity: self];
        [mRelationships removeObjectForKey: [aRelationship name]];
    }
}

- (void) viewGetsUpdatedWith: (NSArray *) entities
{
	BXAssertVoidReturn ([self isView], @"Expected entity %@ to be a view.", self);
	[self inherits: entities];
}

- (id) viewsUpdated
{
	BXAssertValueReturn ([self isView], nil, @"Expected entity %@ to be a view.", self);
	return [self inheritedEntities];
}

- (void) inherits: (NSArray *) entities
{
    @synchronized (mSuperEntities)
    {
        //FIXME: We only implement cascading notifications from "root tables" to 
        //inheriting tables and not vice-versa.
        //FIXME: only single entity supported for now.
        BXAssertVoidReturn (0 == [mSuperEntities count], @"Expected inheritance/dependant relations not to have been set.");
        BXAssertVoidReturn (1 == [entities count], @"Multiple inheritance/dependant relations is not supported.");
        TSEnumerate (currentEntity, e, [entities objectEnumerator])
        {
            [mSuperEntities addObject: currentEntity];
            [currentEntity addSubEntity: self];
            [[NSNotificationCenter defaultCenter] addObserver: self
                                                     selector: @selector (superEntityWillDealloc:)
                                                         name: kBXEntityDescriptionWillDeallocNotification
                                                       object: currentEntity];
        }
    }
}

- (void) addSubEntity: (BXEntityDescription *) entity
{
    @synchronized (mSubEntities)
    {
        //FIXME: We only implement cascading notifications from "root tables" to 
        //inheriting tables and not vice-versa.
        [mSubEntities addObject: entity];
        [[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector (subEntityWillDealloc:)
                                                     name: kBXEntityDescriptionWillDeallocNotification
                                                   object: entity];        
    }
}

- (id) inheritedEntities
{
    id retval = nil;
    @synchronized (mSuperEntities)
    {
        retval = [mSuperEntities allObjects];
    }
    return retval;
}

- (id) subEntities
{
    id retval = nil;
    @synchronized (mSubEntities)
    {
        retval = [mSubEntities allObjects];
    }
    return retval;
}

- (void) superEntityWillDealloc: (NSNotification *) n
{
    @synchronized (mSuperEntities)
    {
        [mSuperEntities removeObject: [n object]];
    }
}

- (void) subEntityWillDealloc: (NSNotification *) n
{
    @synchronized (mSubEntities)
    {
        [mSubEntities removeObject: [n object]];
    }
}

/**
 * \internal
 * Whether this entity gets changed by triggers, rules etc.
 * If the entity gets changed only directly, some queries may possibly be optimized.
 */
- (BOOL) getsChangedByTriggers
{
	return mFlags & kBXEntityGetsChangedByTriggers ? YES : NO;
}

- (void) setGetsChangedByTriggers: (BOOL) flag
{
	if (flag)
		mFlags |= kBXEntityGetsChangedByTriggers;
	else
		mFlags &= ~kBXEntityGetsChangedByTriggers;
}

- (void) setHasCapability: (enum BXEntityCapability) aCapability to: (BOOL) flag
{
	if (flag)
		mCapabilities |= aCapability;
	else
		mCapabilities &= ~aCapability;
}

- (void) setEnabled: (BOOL) flag
{
	if (flag)
		mFlags |= kBXEntityIsEnabled;
	else
		mFlags &= ~kBXEntityIsEnabled;
}
@end