]> err.no Git - scalable-opengroupware.org/blob - OGoContentStore/OCSFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@115 d1b88da0-ebda-0310-925b-ed51d...
[scalable-opengroupware.org] / OGoContentStore / OCSFolder.m
1 /*
2   Copyright (C) 2004 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 // $Id$
22
23 #include "OCSFolder.h"
24 #include "OCSFolderManager.h"
25 #include "OCSFolderType.h"
26 #include "OCSChannelManager.h"
27 #include "OCSFieldExtractor.h"
28 #include "NSURL+OCS.h"
29 #include "EOAdaptorChannel+OCS.h"
30 #include "common.h"
31
32 @implementation OCSFolder
33
34 static BOOL debugOn = YES;
35
36 + (void)initialize {
37   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
38   
39   debugOn = [ud boolForKey:@"OCSFolderDebugEnabled"];
40 }
41
42 - (id)initWithPath:(NSString *)_path primaryKey:(id)_folderId
43   folderTypeName:(NSString *)_ftname folderType:(OCSFolderType *)_ftype
44   location:(NSURL *)_loc quickLocation:(NSURL *)_qloc
45   folderManager:(OCSFolderManager *)_fm
46 {
47   if ((self = [super init])) {
48     self->folderManager  = [_fm    retain];
49     self->folderInfo     = [_ftype retain];
50     
51     self->folderId       = [_folderId copy];
52     self->folderName     = [[_path lastPathComponent] copy];
53     self->path           = [_path   copy];
54     self->location       = [_loc    retain];
55     self->quickLocation  = [_qloc   retain];
56     self->folderTypeName = [_ftname copy];
57   }
58   return self;
59 }
60 - (id)init {
61   return [self initWithPath:nil primaryKey:nil
62                folderTypeName:nil folderType:nil 
63                location:nil quickLocation:nil
64                folderManager:nil];
65 }
66
67 - (void)dealloc {
68   [self->folderManager  release];
69   [self->folderInfo     release];
70   [self->folderId       release];
71   [self->folderName     release];
72   [self->path           release];
73   [self->location       release];
74   [self->quickLocation  release];
75   [self->folderTypeName release];
76   [super dealloc];
77 }
78
79 /* accessors */
80
81 - (NSNumber *)folderId {
82   return self->folderId;
83 }
84
85 - (NSString *)folderName {
86   return self->folderName;
87 }
88 - (NSString *)path {
89   return self->path;
90 }
91
92 - (NSURL *)location {
93   return self->location;
94 }
95 - (NSURL *)quickLocation {
96   return self->quickLocation;
97 }
98
99 - (NSString *)folderTypeName {
100   return self->folderTypeName;
101 }
102
103 - (OCSFolderManager *)folderManager {
104   return self->folderManager;
105 }
106 - (OCSChannelManager *)channelManager {
107   return [[self folderManager] channelManager];
108 }
109
110 - (NSString *)storeTableName {
111   return [[self location] ocsTableName];
112 }
113 - (NSString *)quickTableName {
114   return [[self quickLocation] ocsTableName];
115 }
116
117 /* channels */
118
119 - (EOAdaptorChannel *)acquireStoreChannel {
120   return [[self channelManager] acquireOpenChannelForURL:[self location]];
121 }
122 - (EOAdaptorChannel *)acquireQuickChannel {
123   return [[self channelManager] acquireOpenChannelForURL:[self quickLocation]];
124 }
125
126 - (void)releaseChannel:(EOAdaptorChannel *)_channel {
127   [[self channelManager] releaseChannel:_channel];
128   if (debugOn) [self debugWithFormat:@"released channel: %@", _channel];
129 }
130
131 - (BOOL)canConnectStore {
132   return [[self channelManager] canConnect:[self location]];
133 }
134 - (BOOL)canConnectQuick {
135   return [[self channelManager] canConnect:[self quickLocation]];
136 }
137
138 /* operations */
139
140 - (NSArray *)subFolderNames {
141   return [[self folderManager] listSubFoldersAtPath:[self path]
142                                recursive:NO];
143 }
144 - (NSArray *)allSubFolderNames {
145   return [[self folderManager] listSubFoldersAtPath:[self path]
146                                recursive:YES];
147 }
148
149 - (NSString *)fetchContentWithName:(NSString *)_name {
150   EOAdaptorChannel *channel;
151   NSException  *error;
152   NSDictionary *row;
153   NSArray      *attrs;
154   NSString     *result;
155   NSString     *sql;
156   
157   if ((channel = [self acquireStoreChannel]) == nil) {
158     [self logWithFormat:@"ERROR(%s): could not open storage channel!"];
159     return nil;
160   }
161   
162   /* generate SQL */
163   
164   sql = @"SELECT \"c_content\" FROM ";
165   sql = [sql stringByAppendingString:[self storeTableName]];
166   sql = [sql stringByAppendingString:@" WHERE \"c_name\" = '"];
167   sql = [sql stringByAppendingString:_name];
168   sql = [sql stringByAppendingString:@"'"];
169   
170   /* run SQL */
171
172   if ((error = [channel evaluateExpressionX:sql]) != nil) {
173     [self logWithFormat:@"ERROR(%s): cannot execute blob-fetch SQL '%@': %@", 
174             __PRETTY_FUNCTION__, sql, error];
175     [self releaseChannel:channel];
176     return nil;
177   }
178   
179   /* fetch results */
180   
181   result = nil;
182   attrs  = [channel describeResults];
183   if ((row = [channel fetchAttributes:attrs withZone:NULL]) != nil) {
184     result = [[[row objectForKey:@"cContent"] copy] autorelease];
185     if (![result isNotNull]) result = nil;
186     [channel cancelFetch];
187   }
188   
189   /* release and return result */
190   
191   [self releaseChannel:channel];
192   return result;
193 }
194
195 - (NSException *)writeContent:(NSString *)_content toName:(NSString *)_name {
196   EOAdaptorChannel    *storeChannel, *quickChannel;
197   NSMutableDictionary *quickRow;
198   OCSFieldExtractor   *extractor;
199   
200   /* extract info */
201   
202   extractor = [self->folderInfo quickExtractor];
203   quickRow  = [extractor extractQuickFieldsFromContent:_content];
204   
205   /* open channels */
206   
207   if ((storeChannel = [self acquireStoreChannel]) == nil) {
208     [self logWithFormat:@"ERROR(%s): could not open storage channel!"];
209     return nil;
210   }
211   if ((quickChannel = [self acquireQuickChannel]) == nil) {
212     [self logWithFormat:@"ERROR(%s): could not open quick channel!"];
213     [self releaseChannel:storeChannel];
214     return nil;
215   }
216
217   // TODO: gen SQL, execute in transactions
218   
219   [self releaseChannel:storeChannel];
220   [self releaseChannel:quickChannel];
221   
222   return [NSException exceptionWithName:@"NotYetImplemented"
223                       reason:@"no time, no money, ..."
224                       userInfo:nil];
225 }
226
227 /* description */
228
229 - (NSString *)description {
230   NSMutableString *ms;
231   id tmp;
232   
233   ms = [NSMutableString stringWithCapacity:256];
234   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
235
236   if (self->folderId)
237     [ms appendFormat:@" id=%@", self->folderId];
238   else
239     [ms appendString:@" no-id"];
240
241   if ((tmp = [self path]))           [ms appendFormat:@" path=%@", tmp];
242   if ((tmp = [self folderTypeName])) [ms appendFormat:@" type=%@", tmp];
243   if ((tmp = [self location]))
244     [ms appendFormat:@" loc=%@", [tmp absoluteString]];
245   
246   [ms appendString:@">"];
247   return ms;
248 }
249
250 @end /* OCSFolder */