/*
        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 "FlatFileDescription.h"
#import "FlatFileAdaptor.h"
#import "FlatFileContext.h"

@interface FlatFileChannel (ModelDescriptionPrivate)

- (EOAttribute *)newAttributeWithColumnInfo:(NSDictionary *)info;

@end

@implementation FlatFileChannel (ModelDescription)

- (NSArray *)describeTableNames
{
    NSArray *filenames;
    NSMutableArray *tablenames;
    NSString *filename, *path, *fullPath;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    int i, fileCount;

    filenames = [[[[self adaptorContext] adaptor] connectionDictionary] objectForKey:FlatFileFilesKey];
    fileCount = [filenames count];

    if (!fileCount)
        return nil;

    tablenames = [NSMutableArray arrayWithCapacity:fileCount];
    path = [(FlatFileContext *)[self adaptorContext] path];

    for (i = 0; i < fileCount; i++) {
        filename = [filenames objectAtIndex:i];
        fullPath = [path stringByAppendingPathComponent:filename];
        if ([fileManager isReadableFileAtPath:fullPath])
            [tablenames addObject:filename];
    }

    return tablenames;
}

- (EOModel *)describeModelWithTableNames:(NSArray *)tableNames
{
    EOAdaptor *adaptor;
    EOModel *model = [[EOModel new] autorelease];
    EOEntity *entity;
    NSString *name;
    int i, tableCount = [tableNames count];

    adaptor = [[self adaptorContext] adaptor];
    [model setAdaptorName:[adaptor name]];
    [model setConnectionDictionary:[adaptor connectionDictionary]];

    for (i = 0; i < tableCount; i++) {
        name = [tableNames objectAtIndex:i];
        entity = [self describeEntityWithTableName:name];
        if (entity) {
            [model addEntity:entity];
        }
    }
    return model;
}

- (void)describeStoredProceduresForModel:(EOModel *)model
{
    return;
}

- (EOEntity *)describeEntityWithTableName:(NSString *)tableName
{
    EOEntity *entity = nil;
    EOAttribute *attribute;
    NSArray *columnInfos = [(FlatFileContext *)_context columnInfosForTable:tableName];
    NSString *columnName, *path, *fullPath;
    NSDictionary *columnInfo;
    NSEnumerator *enumerator = [columnInfos objectEnumerator];
    
    while (columnInfo = [enumerator nextObject]) {
        columnName = [columnInfo objectForKey:ColumnNameKey];
        attribute = [self newAttributeWithColumnInfo:columnInfo];
        if (attribute) {
            if (!entity) {
                entity = [EOEntity new];
                [entity setName:tableName];
                [entity setExternalName:tableName];

                path = [(FlatFileContext *)_context path];
                fullPath = [path stringByAppendingPathComponent:tableName];
                
                if (![[NSFileManager defaultManager] isWritableFileAtPath:fullPath])
                    [entity setReadOnly:YES];
            }

            [entity addAttribute:attribute];
        }
    }
    return entity;
}

- (EOAttribute *)newAttributeWithColumnInfo:(NSDictionary *)info
{
    EOAttribute *attribute = [[EOAttribute new] autorelease];
    EOAdaptor *adaptor = [[self adaptorContext] adaptor];
    NSString *name, *type;

    name = [info objectForKey:ColumnNameKey];
    type = [info objectForKey:ColumnTypeKey];
    
    [attribute setName:name];
    [attribute setColumnName:name];
    [attribute setExternalType:type];
    [attribute setValueClassName:[[[adaptor class] externalToInternalTypeMap] objectForKey:type]];

    return attribute;
}

@end
