// OSPArrayFaultSuppressorRecord.m
// Created by Patrick Middleton on 06-Nov-2006.
// Copyright (c) 2006-7 OneStep Solutions Plc. All rights reserved.

#import <EOAccess/EOAccess.h>
#import "OSPArrayFaultSuppressorRecord.h"

@implementation OSPArrayFaultSuppressorRecord

- (id)init {
    [super init];

    return self;
}

- (void)dealloc {
    [editingContext release];
    [masterObject release];
    [masterDetailName release];
    [masterEntityName release];
    [detailObject release];
    [detailToMasterName release];
    [detailEntityName release];

    [super dealloc];
}

- (EOEditingContext *)editingContext { return editingContext; }
- (void)setEditingContext:(EOEditingContext *)aEditingContext {
    [aEditingContext retain];
    [editingContext release];
    editingContext = aEditingContext;
}

- (id)masterObject { return masterObject; }
- (void)setMasterObject:(id)aMasterObject {
    [aMasterObject retain];
    [masterObject release];
    masterObject = aMasterObject;
}

- (NSString *)masterDetailName { return masterDetailName; }
- (void)setMasterDetailName:(NSString *)aMasterDetailName {
    [aMasterDetailName retain];
    [masterDetailName release];
    masterDetailName = aMasterDetailName;
}

- (NSString *)masterEntityName { return masterEntityName; }
- (void)setMasterEntityName:(NSString *)aMasterEntityName {
    [aMasterEntityName retain];
    [masterEntityName release];
    masterEntityName = aMasterEntityName;
}

- (id)detailObject { return detailObject; }
- (void)setDetailObject:(id)aDetailObject {
    [aDetailObject retain];
    [detailObject release];
    detailObject = aDetailObject;
}

- (NSString *)detailToMasterName { return detailToMasterName; }
- (void)setDetailToMasterName:(NSString *)aDetailToMasterName {
    [aDetailToMasterName retain];
    [detailToMasterName release];
    detailToMasterName = aDetailToMasterName;
}

- (NSString *)detailEntityName { return detailEntityName; }
- (void)setDetailEntityName:(NSString *)aDetailEntityName {
    [aDetailEntityName retain];
    [detailEntityName release];
    detailEntityName = aDetailEntityName;
}

- (BOOL)tripped { return tripped; }
- (void)setTripped:(BOOL)aTripped { tripped = aTripped; }

- (void)configure {
    if (!editingContext) [self setEditingContext:[masterObject editingContext]];
    if (!editingContext) [self setEditingContext:[detailObject editingContext]];
    [self setMasterEntityName:[masterObject entityName]];
    [self setDetailEntityName:[detailObject entityName]];

    if (!(masterDetailName && detailToMasterName)) {
        EOEntity *masterEntity = [(EOEntityClassDescription *)[masterObject classDescription] entity];
        EOEntity *detailEntity = [(EOEntityClassDescription *)[detailObject classDescription] entity];
        int i;

        if (!masterDetailName) {
            NSArray *relationships = [masterEntity relationships];
            for (i=[relationships count]-1; masterDetailName==nil && i>=0 ; i--) {
                EORelationship *relationship = (EORelationship *)[relationships objectAtIndex:i];
                if ([relationship isToMany] && [[[relationship destinationEntity] name] isEqualToString:[detailEntity name]]) {
                    [self setMasterDetailName:[relationship name]];
                }
            }
        }

        if (!detailToMasterName) {
            NSArray *relationships = [detailEntity relationships];
            for (i=[relationships count]-1; detailToMasterName==nil && i>=0 ; i--) {
                EORelationship *relationship = (EORelationship *)[relationships objectAtIndex:i];
                if (![relationship isToMany] && [[[relationship destinationEntity] name] isEqualToString:[masterEntity name]]) {
                    [self setDetailToMasterName:[relationship name]];
                }
            }
        }

    }
}

@end
