]> err.no Git - sope/blob - sope-gdl1/GDLContentStore/GCSFolderType.m
added OGoContentStore library to sope-gdl1
[sope] / sope-gdl1 / GDLContentStore / GCSFolderType.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "GCSFolderType.h"
23 #include "GCSFolder.h"
24 #include "GCSFieldInfo.h"
25 #include "GCSFieldExtractor.h"
26 #include "common.h"
27 #include <EOControl/EOKeyValueCoding.h>
28 #include <NGExtensions/NGResourceLocator.h>
29
30 @implementation GCSFolderType
31
32 - (id)initWithPropertyList:(id)_plist {
33   if ((self = [super init])) {
34     self->blobTablePattern  = [[_plist objectForKey:@"blobTablePattern"] copy];
35     self->quickTablePattern = [[_plist objectForKey:@"quickTablePattern"]copy];
36     
37     self->extractorClassName =
38       [[_plist objectForKey:@"extractorClassName"] copy];
39     // TODO: qualifier;
40     
41     self->fields = [[GCSFieldInfo fieldsForPropertyList:
42                                     [_plist objectForKey:@"fields"]] retain];
43   }
44   return self;
45 }
46
47 - (id)initWithContentsOfFile:(NSString *)_path {
48   NSDictionary *plist;
49   
50   plist = [NSDictionary dictionaryWithContentsOfFile:_path];
51   if (plist == nil) {
52     NSLog(@"ERROR(%s): could not read dictionary at path %@", 
53           __PRETTY_FUNCTION__, _path);
54     [self release];
55     return nil;
56   }
57   return [self initWithPropertyList:plist];
58 }
59
60 + (NGResourceLocator *)resourceLocator {
61   NGResourceLocator *loc;
62   
63   // TODO: fix me, GCS instead of OCS
64   loc = [NGResourceLocator resourceLocatorForGNUstepPath:
65                              @"Library/OCSTypeModels"
66                            fhsPath:@"share/ocs"];
67   return loc;
68 }
69
70 + (id)folderTypeWithName:(NSString *)_typeName {
71   NSString *filename, *path;
72
73   // TODO: fix me, GCS instead of OCS
74   filename = [_typeName stringByAppendingPathExtension:@"ocs"];
75   path     = [[self resourceLocator] lookupFileWithName:filename];
76   
77   if (path != nil)
78     return [[self alloc] initWithContentsOfFile:path];
79
80   NSLog(@"ERROR(%s): did not find model for type: '%@'", 
81         __PRETTY_FUNCTION__, _typeName);
82   return nil;
83 }
84
85 - (id)initWithFolderTypeName:(NSString *)_typeName {
86   // DEPRECATED
87   [self release];
88   return [[GCSFolderType folderTypeWithName:_typeName] retain];
89 }
90
91 - (void)dealloc {
92   [self->extractor          release];
93   [self->extractorClassName release];
94   [self->blobTablePattern   release];
95   [self->quickTablePattern  release];
96   [self->fields             release];
97   [self->fieldDict          release];
98   [self->folderQualifier    release];
99   [super dealloc];
100 }
101
102 /* operations */
103
104 - (NSString *)blobTableNameForFolder:(GCSFolder *)_folder {
105   return [self->blobTablePattern 
106               stringByReplacingVariablesWithBindings:_folder];
107 }
108 - (NSString *)quickTableNameForFolder:(GCSFolder *)_folder {
109   return [self->quickTablePattern
110               stringByReplacingVariablesWithBindings:_folder];
111 }
112
113 - (EOQualifier *)qualifierForFolder:(GCSFolder *)_folder {
114   NSArray      *keys;
115   NSDictionary *bindings;
116   
117   keys = [[self->folderQualifier allQualifierKeys] allObjects];
118   if ([keys count] == 0)
119     return self->folderQualifier;
120
121   bindings = [_folder valuesForKeys:keys];
122   return [self->folderQualifier qualifierWithBindings:bindings
123                                 requiresAllVariables:NO];
124 }
125
126 /* generating SQL */
127
128 - (NSString *)sqlQuickCreateWithTableName:(NSString *)_tabName {
129   NSMutableString *sql;
130   unsigned i, count;
131   
132   sql = [NSMutableString stringWithCapacity:512];
133   [sql appendString:@"CREATE TABLE "];
134   [sql appendString:_tabName];
135   [sql appendString:@" (\n"];
136
137   count = [self->fields count];
138   for (i = 0; i < count; i++) {
139     if (i > 0) [sql appendString:@",\n"];
140     
141     [sql appendString:@"  "];
142     [sql appendString:[[self->fields objectAtIndex:i] sqlCreateSection]];
143   }
144   
145   [sql appendString:@"\n)"];
146   
147   return sql;
148 }
149
150 /* quick support */
151
152 - (GCSFieldExtractor *)quickExtractor {
153   Class clazz;
154   
155   if (self->extractor)
156     return [self->extractor isNotNull] ? self->extractor : nil;
157
158   clazz = self->extractorClassName
159     ? NSClassFromString(self->extractorClassName)
160     : [GCSFieldExtractor class];
161   if (clazz == Nil) {
162     [self logWithFormat:@"ERROR: did not find field extractor class!"];
163     return nil;
164   }
165   
166   if ((self->extractor = [[clazz alloc] init]) == nil) {
167     [self logWithFormat:@"ERROR: could not create field extractor of class %@",
168             clazz];
169     return nil;
170   }
171   return self->extractor;
172 }
173
174 /* description */
175
176 - (NSString *)description {
177   NSMutableString *ms;
178
179   ms = [NSMutableString stringWithCapacity:256];
180   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
181
182   [ms appendFormat:@" blobtable='%@'",  self->blobTablePattern];
183   [ms appendFormat:@" quicktable='%@'", self->quickTablePattern];
184   [ms appendFormat:@" fields=%@",       self->fields];
185   [ms appendFormat:@" extractor=%@",    self->extractorClassName];
186   
187   if (self->folderQualifier)
188     [ms appendFormat:@" qualifier=%@", self->folderQualifier];
189   
190   [ms appendString:@">"];
191   return ms;
192 }
193
194 @end /* GCSFolderType */