]> err.no Git - scalable-opengroupware.org/commitdiff
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@80 d1b88da0-ebda-0310-925b-ed51d8...
authorhelge <helge@d1b88da0-ebda-0310-925b-ed51d893ca5b>
Tue, 29 Jun 2004 19:13:17 +0000 (19:13 +0000)
committerhelge <helge@d1b88da0-ebda-0310-925b-ed51d893ca5b>
Tue, 29 Jun 2004 19:13:17 +0000 (19:13 +0000)
OGoContentStore/OCSFieldInfo.h
OGoContentStore/OCSFieldInfo.m
OGoContentStore/OCSFolderManager.m
OGoContentStore/OCSFolderType.h
OGoContentStore/OCSFolderType.m
OGoContentStore/appointment.ocs [new file with mode: 0644]
OGoContentStore/sql/appointment-create.psql [new file with mode: 0644]

index da59b882ebd28916c4efd5f9de663808b2ac653e..49c79a18b302144a3adc2979d0429b5d386a9136 100644 (file)
 
 #import <Foundation/NSObject.h>
 
+@class NSString, NSArray;
+
 @interface OCSFieldInfo : NSObject
 {
+  NSString *columnName;
+  NSString *sqlType;
+  BOOL     allowsNull;
+  BOOL     isPrimaryKey;
 }
 
++ (NSArray *)fieldsForPropertyList:(NSArray *)_plist;
+- (id)initWithPropertyList:(id)_plist;
+
+/* accessors */
+
+- (NSString *)columnName;
+- (NSString *)sqlType;
+- (BOOL)doesAllowNull;
+- (BOOL)isPrimaryKey;
+
+/* generating SQL */
+
+- (NSString *)sqlCreateSection;
+
 @end
 
 #endif /* __OGoContentStore_OCSFieldInfo_H__ */
index b7c2bd0cf89d7091c2a141bbbf06e69c34ef18b5..11cd65139d896b2137dcf83e7c5b67db0e7e2374 100644 (file)
 
 @implementation OCSFieldInfo
 
++ (NSArray *)fieldsForPropertyList:(NSArray *)_plist {
+  NSMutableArray *fields;
+  unsigned i, count;
+  
+  if (_plist == nil)
+    return nil;
+  
+  count = [_plist count];
+  fields = [NSMutableArray arrayWithCapacity:count];
+  for (i = 0; i < count; i++) {
+    OCSFieldInfo *field;
+
+    field = [[OCSFieldInfo alloc] initWithPropertyList:
+                                   [_plist objectAtIndex:i]];
+    if (field) [fields addObject:field];
+    [field release];
+  }
+  return fields;
+}
+
+- (id)initWithPropertyList:(id)_plist {
+  if ((self = [super init])) {
+  }
+  return self;
+}
+
 - (void)dealloc {
+  [self->columnName release];
+  [self->sqlType    release];
   [super dealloc];
 }
 
+/* accessors */
+
+- (NSString *)columnName {
+  return self->columnName;
+}
+- (NSString *)sqlType {
+  return self->sqlType;
+}
+
+- (BOOL)doesAllowNull {
+  return self->allowsNull;
+}
+- (BOOL)isPrimaryKey {
+  return self->isPrimaryKey;
+}
+
+/* generating SQL */
+
+- (NSString *)sqlCreateSection {
+  NSMutableString *ms;
+  
+  ms = [NSMutableString stringWithCapacity:32];
+  [ms appendString:[self columnName]];
+  [ms appendString:@" "];
+  [ms appendString:[self sqlType]];
+  
+  [ms appendString:@" "];
+  if (![self doesAllowNull]) [ms appendString:@"NOT "];
+  [ms appendString:@"NULL"];
+  
+  if ([self isPrimaryKey]) [ms appendString:@" PRIMARY KEY"];
+  return ms;
+}
+
+/* description */
+
+- (NSString *)description {
+  NSMutableString *ms;
+  id tmp;
+  
+  ms = [NSMutableString stringWithCapacity:256];
+  [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
+  
+  if ((tmp = [self columnName])) [ms appendFormat:@" column=%@", tmp];
+  if ((tmp = [self sqlType]))    [ms appendFormat:@" sql=%@",    tmp];
+  if ([self doesAllowNull]) [ms appendString:@" allows-null"];
+  if ([self isPrimaryKey])  [ms appendString:@" pkey"];
+  
+  [ms appendString:@">"];
+  return ms;
+}
+
 @end /* OCSFieldInfo */
index bbdef2a02eb5e3b06ed0ab3f63ad66718f453b95..c2f4b8a8c18b8f9c43a11ee572d38cb1ae0e388d 100644 (file)
@@ -98,13 +98,15 @@ static const char *OCSPathColumnPattern     = "c_path%i";
     return nil;
   }
   if ((self = [super init])) {
+    OCSFolderType *cal;
+    
     self->channelManager = [[OCSChannelManager defaultChannelManager] retain];
     self->folderInfoLocation = [_url retain];
     
+    cal = [[OCSFolderType alloc] initWithFolderTypeName:@"appointment"];
     self->nameToType =
-      [[NSDictionary alloc] initWithObjectsAndKeys:
-                             [OCSFolderType defaultCalendarFolderType],
-                             @"appointment", nil];
+      [[NSDictionary alloc] initWithObjectsAndKeys:cal, @"appointment", nil];
+    [cal release]; cal = nil;
   }
   return self;
 }
@@ -538,6 +540,16 @@ static const char *OCSPathColumnPattern     = "c_path%i";
 
 - (NSException *)createFolderOfType:(NSString *)_type atPath:(NSString *)_path{
   // TODO: implement folder create
+  OCSFolderType *ftype;
+  
+  if ((ftype = [self folderTypeWithName:_type]) == nil) {
+    return [NSException exceptionWithName:@"OCSMissingFolderType"
+                       reason:@"missing folder type"
+                       userInfo:nil];
+  }
+  
+  [self logWithFormat:@"create folder of type: %@", _type];
+  
   return [NSException exceptionWithName:@"NotYetImplemented"
                      reason:@"no money, no time, ..."
                      userInfo:nil];
index 097f280c9c31aa6abc650fafc302ba6b3d7b9daf..57eb7dc9f416c3ced9fcc4f7abfffb824b5120e4 100644 (file)
@@ -39,7 +39,7 @@
 
 #import <Foundation/NSObject.h>
 
-@class NSString, NSDictionary;
+@class NSString, NSArray, NSDictionary;
 @class EOQualifier;
 @class OCSFolder;
 
 {
   NSString     *blobTablePattern;  // eg 'SOGo_$folderId$_blob
   NSString     *quickTablePattern; // eg 'SOGo_$folderId$_quick
-  NSDictionary *fields;            // maps a name to OCSFieldInfo
+  NSArray      *fields;            // OCSFieldInfo objects
+  NSDictionary *fieldDict;         // maps a name to OCSFieldInfo
   EOQualifier  *folderQualifier;   // to further limit the table set
 }
 
-+ (id)defaultCalendarFolderType;
+- (id)initWithPropertyList:(id)_plist;
+- (id)initWithContentsOfFile:(NSString *)_path;
+- (id)initWithFolderTypeName:(NSString *)_typeName;
 
 /* operations */
 
 - (NSString *)blobTableNameForFolder:(OCSFolder *)_folder;
 - (NSString *)quickTableNameForFolder:(OCSFolder *)_folder;
 
+/* generating SQL */
+
+- (NSString *)sqlQuickCreateWithTableName:(NSString *)_tabName;
+
 @end
 
 #endif /* __OGoContentStore_OCSFolderType_H__ */
index 358f920daee4c76d434fbad4836695e8ccef0c13..5f15e9dca9067bd7e206adbc936ddd82c6d4c7f2 100644 (file)
 
 #include "OCSFolderType.h"
 #include "OCSFolder.h"
+#include "OCSFieldInfo.h"
 #include "common.h"
 #include <EOControl/EOKeyValueCoding.h>
 
 @implementation OCSFolderType
 
-static OCSFolderType *calendarType = nil;
-
-- (id)initAsCalendarFolderType {
-  // TODO: should be moved to a plist file
+- (id)initWithPropertyList:(id)_plist {
   if ((self = [super init])) {
-    self->blobTablePattern  = @"SOGo_$folderId$_blob";
-    self->quickTablePattern = @"SOGo_$folderId$_quick";
-    self->folderQualifier   = nil; // a table per folder
+    self->blobTablePattern  = [[_plist objectForKey:@"blobTablePattern"] copy];
+    self->quickTablePattern = [[_plist objectForKey:@"quickTablePattern"]copy];
+    // TODO: qualifier;
     
-    // TODO: which fields?
+    self->fields = [[OCSFieldInfo fieldsForPropertyList:
+                                   [_plist objectForKey:@"fields"]] retain];
   }
   return self;
 }
 
-+ (id)defaultCalendarFolderType {
-  if (calendarType == nil)
-    calendarType = [[self alloc] init];
-  return calendarType;
+- (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];
 }
@@ -79,6 +109,29 @@ static OCSFolderType *calendarType = nil;
                                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;
+}
+
 /* description */
 
 - (NSString *)description {
diff --git a/OGoContentStore/appointment.ocs b/OGoContentStore/appointment.ocs
new file mode 100644 (file)
index 0000000..081a4cb
--- /dev/null
@@ -0,0 +1,33 @@
+{
+  blobTablePattern  = "SOGo_$folderId$_blob";
+  quickTablePattern = "SOGo_$folderId$_quick";
+
+  fields = (
+    {
+      columnName   = pkey;
+      sqlType      = "INTEGER";
+      allowsNull   = NO;
+      isPrimaryKey = YES;
+    },
+    {
+      columnName = startdate;
+      sqlType    = "INT";
+      allowsNull = NO;
+    },
+    {
+      columnName = enddate;
+      sqlType    = "INT";
+      allowsNull = NO;
+    },
+    {
+      columnName = title;
+      sqlType    = "VARCHAR(1000)";
+      allowsNull = NO;
+    },
+    {
+      columnName = participants;
+      sqlType    = "VARCHAR(1000000)";
+      allowsNull = NO;
+    },
+  );
+}
diff --git a/OGoContentStore/sql/appointment-create.psql b/OGoContentStore/sql/appointment-create.psql
new file mode 100644 (file)
index 0000000..0a0459d
--- /dev/null
@@ -0,0 +1,9 @@
+CREATE TABLE %s (
+  pkey         INTEGER 
+    NOT NULL
+    PRIMARY KEY,
+  startdate    INT NOT NULL,
+  enddate      INT NOT NULL,
+  title        VARCHAR(1000) NOT NULL,
+  participants VARCHAR(100000) NOT NULL
+);