//
//  ModeledObjectTypes.m
//  drachma
//
//  Created by Niko Matsakis on 7/19/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "ModeledObjectTypes.h"
#import "Queries.h"
#import "OACategories.h"
#import "DrachmaPayeeManagement.h"

@implementation Expense

- (NSDecimalNumber *)amount 
{
    NSDecimalNumber * tmpValue;
    
    [self willAccessValueForKey: @"amount"];
    tmpValue = [self primitiveValueForKey: @"amount"];
    [self didAccessValueForKey: @"amount"];
    
    return tmpValue;
}

- (void)setAmount:(NSDecimalNumber *)value 
{
    [self willChangeValueForKey: @"amount"];
    [self setPrimitiveValue: value forKey: @"amount"];
    [self didChangeValueForKey: @"amount"];
}

- (BOOL)validateAmount: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSDate *)date 
{
    NSDate * tmpValue;
    
    [self willAccessValueForKey: @"date"];
    tmpValue = [self primitiveValueForKey: @"date"];
    [self didAccessValueForKey: @"date"];
    
    return tmpValue;
}

- (void)setDate:(NSDate *)value 
{
    [self willChangeValueForKey: @"date"];
    [self setPrimitiveValue: value forKey: @"date"];
    [self didChangeValueForKey: @"date"];
}

- (BOOL)validateDate: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSString *)memo 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey: @"memo"];
    tmpValue = [self primitiveValueForKey: @"memo"];
    [self didAccessValueForKey: @"memo"];
    
    return tmpValue;
}

- (void)setMemo:(NSString *)value 
{
    [self willChangeValueForKey: @"memo"];
    [self setPrimitiveValue: value forKey: @"memo"];
    [self didChangeValueForKey: @"memo"];
}

- (BOOL)validateMemo: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)checkNumber 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey: @"checkNumber"];
    tmpValue = [self primitiveValueForKey: @"checkNumber"];
    [self didAccessValueForKey: @"checkNumber"];
    
    return tmpValue;
}

- (void)setCheckNumber:(NSNumber *)value 
{
    [self willChangeValueForKey: @"checkNumber"];
    [self setPrimitiveValue: value forKey: @"checkNumber"];
    [self didChangeValueForKey: @"checkNumber"];
}

- (BOOL)validateCheckNumber: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (Category *)category 
{
    id tmpObject;
    
    [self willAccessValueForKey: @"category"];
    tmpObject = [self primitiveValueForKey: @"category"];
    [self didAccessValueForKey: @"category"];
    
    return tmpObject;
}

- (void)setCategory:(Category *)value 
{
    [self willChangeValueForKey: @"category"];
    [self setPrimitiveValue: value
                     forKey: @"category"];
    [self didChangeValueForKey: @"category"];
}


- (BOOL)validateCategory: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

@end

@interface Expense (Internal)
- (Payee *)payee;
- (void)setPayee:(Payee *)value;
- (BOOL)validatePayee: (id *)valueRef error:(NSError **)outError;
@end

@implementation Expense(Internal)

- (Payee *)payee 
{
    id tmpObject;
    
    [self willAccessValueForKey: @"payee"];
    tmpObject = [self primitiveValueForKey: @"payee"];
    [self didAccessValueForKey: @"payee"];
    
    return tmpObject;
}

- (void)setPayee:(Payee *)value 
{
    [self willChangeValueForKey: @"payee"];
    [self setPrimitiveValue: value
                     forKey: @"payee"];
    [self didChangeValueForKey: @"payee"];
}

- (BOOL)validatePayee: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

@end

@implementation Expense (Manual)

+ (void)initialize {
    [self setKeys:[NSArray arrayWithObject:@"payee"] triggerChangeNotificationsForDependentKey:@"payeeName"];
}

- (NSString*) payeeName
{
    Payee *payee = [self payee];
    if (payee != nil)
        return [payee name];
    return @"";
}

- (void) setPayeeName:(NSString*)payeeName
{
    Payee *payee = [self payee];
    if (payee != nil) {
        if ([[payee name] isEqualToStringIgnoringCase:payeeName]) {
            if (![[payee name] isEqualToString:payeeName])
                [payee setName:payeeName];
            return;
        }
    }
    
    NSManagedObjectContext *moc = [self managedObjectContext];
    [self setPayee:[moc payeeNamed:payeeName]];
}

- (void) i_setPayee:(Payee *)payee
{
    [self setPayee:payee];
}

@end

@implementation Category 

- (NSString *)name 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey: @"name"];
    tmpValue = [self primitiveValueForKey: @"name"];
    [self didAccessValueForKey: @"name"];
    
    return tmpValue;
}

- (void)setName:(NSString *)value 
{
    [self willChangeValueForKey: @"name"];
    [self setPrimitiveValue: value forKey: @"name"];
    [self didChangeValueForKey: @"name"];
}

- (BOOL)validateName: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)immutable 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey: @"immutable"];
    tmpValue = [self primitiveValueForKey: @"immutable"];
    [self didAccessValueForKey: @"immutable"];
    
    return tmpValue;
}

- (void)setImmutable:(NSNumber *)value 
{
    [self willChangeValueForKey: @"immutable"];
    [self setPrimitiveValue: value forKey: @"immutable"];
    [self didChangeValueForKey: @"immutable"];
}

- (BOOL)validateImmutable: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)salary 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey: @"salary"];
    tmpValue = [self primitiveValueForKey: @"salary"];
    [self didAccessValueForKey: @"salary"];
    
    return tmpValue;
}

- (void)setSalary:(NSNumber *)value 
{
    [self willChangeValueForKey: @"salary"];
    [self setPrimitiveValue: value forKey: @"salary"];
    [self didChangeValueForKey: @"salary"];
}

- (BOOL)validateSalary: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (void)addExpensesObject:(Expense *)value 
{    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"expenses" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"expenses"] addObject: value];
    
    [self didChangeValueForKey:@"expenses" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

- (void)removeExpensesObject:(Expense *)value 
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"expenses" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"expenses"] removeObject: value];
    
    [self didChangeValueForKey:@"expenses" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}


- (void)addLineItemsObject:(NSManagedObject *)value 
{    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"lineItems"] addObject: value];
    
    [self didChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

- (void)removeLineItemsObject:(NSManagedObject *)value 
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"lineItems"] removeObject: value];
    
    [self didChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

@end

@implementation Budget

- (NSDate *)startDate 
{
    NSDate * tmpValue;
    
    [self willAccessValueForKey: @"startDate"];
    tmpValue = [self primitiveValueForKey: @"startDate"];
    [self didAccessValueForKey: @"startDate"];
    
    return tmpValue;
}

- (void)setStartDate:(NSDate *)value 
{
    [self willChangeValueForKey: @"startDate"];
    [self setPrimitiveValue: value forKey: @"startDate"];
    [self didChangeValueForKey: @"startDate"];
}

- (BOOL)validateStartDate: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}


- (void)addLineItemsObject:(NSManagedObject *)value 
{    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"lineItems"] addObject: value];
    
    [self didChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

- (void)removeLineItemsObject:(NSManagedObject *)value 
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"lineItems"] removeObject: value];
    
    [self didChangeValueForKey:@"lineItems" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

@end

@implementation Budget (Manual) // methods that are not auto generated
- (NSMutableSet*)getTheLineItems
{
    return [self mutableSetValueForKey:@"lineItems"];
}
@end

@implementation LineItem

- (NSDecimalNumber *)amount 
{
    NSDecimalNumber * tmpValue;
    
    [self willAccessValueForKey: @"amount"];
    tmpValue = [self primitiveValueForKey: @"amount"];
    [self didAccessValueForKey: @"amount"];
    
    return tmpValue;
}

- (void)setAmount:(NSDecimalNumber *)value 
{
    [self willChangeValueForKey: @"amount"];
    [self setPrimitiveValue: value forKey: @"amount"];
    [self didChangeValueForKey: @"amount"];
}

- (BOOL)validateAmount: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}


- (Budget *)budget 
{
    id tmpObject;
    
    [self willAccessValueForKey: @"budget"];
    tmpObject = [self primitiveValueForKey: @"budget"];
    [self didAccessValueForKey: @"budget"];
    
    return tmpObject;
}

- (void)setBudget:(Budget *)value 
{
    [self willChangeValueForKey: @"budget"];
    [self setPrimitiveValue: value
                     forKey: @"budget"];
    [self didChangeValueForKey: @"budget"];
}


- (BOOL)validateBudget: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}


- (Category *)category 
{
    id tmpObject;
    
    [self willAccessValueForKey: @"category"];
    tmpObject = [self primitiveValueForKey: @"category"];
    [self didAccessValueForKey: @"category"];
    
    return tmpObject;
}

- (void)setCategory:(Category *)value 
{
    [self willChangeValueForKey: @"category"];
    [self setPrimitiveValue: value
                     forKey: @"category"];
    [self didChangeValueForKey: @"category"];
}


- (BOOL)validateCategory: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

@end

@implementation Payee

- (NSString *)name 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey: @"name"];
    tmpValue = [self primitiveValueForKey: @"name"];
    [self didAccessValueForKey: @"name"];
    
    return tmpValue;
}

- (void)setName:(NSString *)value 
{
    [self willChangeValueForKey: @"name"];
    [self setPrimitiveValue: value forKey: @"name"];
    [self didChangeValueForKey: @"name"];
}

- (BOOL)validateName: (id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}


- (void)addExpensesObject:(Expense *)value 
{    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"expenses" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"expenses"] addObject: value];
    
    [self didChangeValueForKey:@"expenses" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

- (void)removeExpensesObject:(Expense *)value 
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    
    [self willChangeValueForKey:@"expenses" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [[self primitiveValueForKey: @"expenses"] removeObject: value];
    
    [self didChangeValueForKey:@"expenses" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    
    [changedObjects release];
}

@end