/*
        Copyright (c) 1996, NeXT Software, Inc.
        All rights reserved.

        You may freely copy, distribute and reuse the code in this example.
        NeXT disclaims any warranty of any kind, expressed or implied,
        as to its fitness for any particular use.
*/
#import <BusinessLogic/Guest.h>
#import <BusinessLogic/Member.h>

@implementation Guest

- (void)awakeFromInsertionInEditingContext:(EOEditingContext *)ctx
{
    // set default values
    if (!costRestriction)
        costRestriction = [[NSDecimalNumber alloc] initWithString:@"50"]; // Limit is $50 by default

    [super awakeFromInsertionInEditingContext:ctx];
}

- (NSDecimalNumber *)costRestriction
{
    return costRestriction;
}

- (void)setCostRestriction:(NSDecimalNumber *)restriction
{
    [self willChange];
    [costRestriction autorelease];
    costRestriction = [restriction copy];
}

- (NSException *)validateForSave
{
    NSException *error;
    
    if([[self costRestriction] compare:[member costRestriction]] == NSOrderedDescending) {
        return [NSException validationExceptionWithFormat:@"The cost restriction for the guest %@ is greater than his/her sponsor %@.", [self fullName], [member fullName]];
    }
    
    // Our sponsoring member must validate some of our changes
    error = [super validateForSave];
    return (error) ? error : [member validateForSave];
}

- (Member *)member
{
    return member;
}

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

@end
