/*
  Copyright (C) 2004 SKYRIX Software AG

  This file is part of OpenGroupware.org.

  OGo is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  OGo 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 Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with OGo; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
// $Id$

#include "OCSFolderType.h"
#include "OCSFolder.h"
#include "OCSFieldInfo.h"
#include "OCSiCalFieldExtractor.h"
#include "common.h"
#include <EOControl/EOKeyValueCoding.h>

@implementation OCSFolderType

- (id)initWithPropertyList:(id)_plist {
  if ((self = [super init])) {
    self->blobTablePattern  = [[_plist objectForKey:@"blobTablePattern"] copy];
    self->quickTablePattern = [[_plist objectForKey:@"quickTablePattern"]copy];
    // TODO: qualifier;
    
    self->fields = [[OCSFieldInfo fieldsForPropertyList:
				    [_plist objectForKey:@"fields"]] retain];
  }
  return self;
}

- (id)initWithContentsOfFile:(NSString *)_path {
  NSDictionary *plist;
  
  plist = [NSDictionary dictionaryWithContentsOfFile:_path];
  if (plist == nil) {
    NSLog(@"ERROR(%s): could not read dictionary at path %@", 
	  __PRETTY_FUNCTION__, _path);
    [self release];
    return nil;
  }
  return [self initWithPropertyList:plist];
}

- (id)initWithFolderTypeName:(NSString *)_typeName {
  // TODO: use GNUSTEP_PATHPREFIX_LIST
  NSDictionary  *env;
  NSFileManager *fm;
  NSString      *filename, *path;
  
  env      = [[NSProcessInfo processInfo] environment];
  fm       = [NSFileManager defaultManager];
  filename = [_typeName stringByAppendingPathExtension:@"ocs"];
  
  path = [env objectForKey:@"GNUSTEP_USER_ROOT"];
  path = [path stringByAppendingPathComponent:@"Library"];
  path = [path stringByAppendingPathComponent:@"OCSTypeModels"];
  path = [path stringByAppendingPathComponent:filename];
  if ([fm fileExistsAtPath:path])
    return [self initWithContentsOfFile:path];
  
  NSLog(@"ERROR(%s): did not find model for type: '%@'", 
	__PRETTY_FUNCTION__, _typeName);
  [self release];
  return nil;
}

- (void)dealloc {
  [self->blobTablePattern  release];
  [self->quickTablePattern release];
  [self->fields            release];
  [self->fieldDict         release];
  [self->folderQualifier   release];
  [super dealloc];
}

/* operations */

- (NSString *)blobTableNameForFolder:(OCSFolder *)_folder {
  return [self->blobTablePattern 
	      stringByReplacingVariablesWithBindings:_folder];
}
- (NSString *)quickTableNameForFolder:(OCSFolder *)_folder {
  return [self->quickTablePattern
	      stringByReplacingVariablesWithBindings:_folder];
}

- (EOQualifier *)qualifierForFolder:(OCSFolder *)_folder {
  NSArray      *keys;
  NSDictionary *bindings;
  
  keys = [[self->folderQualifier allQualifierKeys] allObjects];
  if ([keys count] == 0)
    return self->folderQualifier;

  bindings = [_folder valuesForKeys:keys];
  return [self->folderQualifier qualifierWithBindings:bindings
	                        requiresAllVariables:NO];
}

/* generating SQL */

- (NSString *)sqlQuickCreateWithTableName:(NSString *)_tabName {
  NSMutableString *sql;
  unsigned i, count;
  
  sql = [NSMutableString stringWithCapacity:512];
  [sql appendString:@"CREATE TABLE "];
  [sql appendString:_tabName];
  [sql appendString:@" ( "];

  count = [self->fields count];
  for (i = 0; i < count; i++) {
    if (i > 0) [sql appendString:@", "];
    
    [sql appendString:[[self->fields objectAtIndex:i] sqlCreateSection]];
  }
  
  [sql appendString:@")"];
  
  return sql;
}

/* quick support */

- (OCSFieldExtractor *)quickExtractor {
  // TODO: hack
  return [OCSiCalFieldExtractor sharedICalFieldExtractor];
}

/* description */

- (NSString *)description {
  NSMutableString *ms;

  ms = [NSMutableString stringWithCapacity:256];
  [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];

  [ms appendFormat:@" blobtable='%@'",  self->blobTablePattern];
  [ms appendFormat:@" quicktable='%@'", self->quickTablePattern];
  [ms appendFormat:@" fields=%@",       self->fields];
  
  if (self->folderQualifier)
    [ms appendFormat:@" qualifier=%@", self->folderQualifier];
  
  [ms appendString:@">"];
  return ms;
}

@end /* OCSFolderType */
