2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include "SOGoGroupAppointmentFolder.h"
25 @implementation SOGoGroupAppointmentFolder
28 return [super version] + 0 /* v1 */;
31 NSAssert2([super version] == 1,
32 @"invalid superclass (%@) version %i !",
33 NSStringFromClass([self superclass]), [super version]);
37 [self->uidToFolder release];
41 /* looking up shared objects */
43 - (SOGoGroupsFolder *)lookupGroupsFolder {
44 return [[self container] lookupGroupsFolder];
49 - (NSArray *)calendarUIDs {
50 return [[self container] valueForKey:@"uids"];
55 - (void)resetFolderCaches {
56 [self->uidToFolder release]; self->uidToFolder = nil;
59 - (SOGoAppointmentFolder *)folderForUID:(NSString *)_uid {
60 if (self->uidToFolder == nil) {
61 // TODO: can we trigger a fetch?
62 [self errorWithFormat:
63 @"called -folderForUID method before fetchCoreInfos .."];
67 return [self->uidToFolder objectForKey:_uid];
72 - (BOOL)doesRecord:(NSDictionary *)_rec conflictWith:(NSDictionary *)_other {
75 if ([_rec isEqual:_other])
81 - (NSDictionary *)_registerConflictingRecord:(NSDictionary *)_other
82 inRecord:(NSDictionary *)_record
84 NSMutableArray *conflicts;
86 if (_record == nil) return _other;
87 if (_other == nil) return _record;
89 if ((conflicts = [_record objectForKey:@"conflicts"]) == nil) {
90 NSMutableDictionary *md;
92 md = [[_record mutableCopy] autorelease];
93 conflicts = [NSMutableArray arrayWithCapacity:4];
94 [md setObject:conflicts forKey:@"conflicts"];
97 [conflicts addObject:_other];
103 - (SOGoAppointmentFolder *)calendarFolderForMemberFolder:(id)_folder {
104 SOGoAppointmentFolder *aptFolder;
106 if (![_folder isNotNull])
109 aptFolder = [_folder lookupName:@"Calendar" inContext:nil acquire:NO];
110 if (![aptFolder isNotNull])
113 if (![aptFolder respondsToSelector:@selector(fetchCoreInfosFrom:to:)]) {
114 [self errorWithFormat:@"folder does not implemented required API: %@",
122 - (NSArray *)fetchFields:(NSArray *)_fields
123 from:(NSCalendarDate *)_startDate
124 to:(NSCalendarDate *)_endDate
127 NSMutableArray *result;
128 NSMutableDictionary *uidToRecord;
131 if ((folders = [[self container] valueForKey:@"memberFolders"]) == nil) {
132 [self errorWithFormat:@"calendar container has no 'memberFolders'?!"];
136 [self resetFolderCaches];
138 if ((count = [folders count]) == 0)
139 return [NSArray array];
141 if (self->uidToFolder == nil)
142 self->uidToFolder = [[NSMutableDictionary alloc] initWithCapacity:7*count];
144 [self->uidToFolder removeAllObjects];
146 uidToRecord = [NSMutableDictionary dictionaryWithCapacity:(7 * count)];
147 result = [NSMutableArray arrayWithCapacity:(7 * count)];
148 for (i = 0; i < count; i++) {
149 SOGoAppointmentFolder *aptFolder;
151 NSDictionary *record;
153 aptFolder = [self calendarFolderForMemberFolder:
154 [folders objectAtIndex:i]];
155 if (![aptFolder isNotNull]) {
156 [self debugWithFormat:@"did not find a Calendar folder in folder: %@",
157 [folders objectAtIndex:i]];
161 results = [aptFolder fetchFields:_fields
164 if (![results isNotNull]) continue;
166 results = [results objectEnumerator];
167 while ((record = [results nextObject])) {
169 NSDictionary *existingRecord;
171 uid = [record objectForKey:@"uid"];
172 if (![uid isNotNull]) {
173 [self warnWithFormat:@"record without uid: %@", result];
174 [result addObject:record];
178 if ((existingRecord = [uidToRecord objectForKey:uid]) == nil) {
179 /* record not yet in result set */
180 [uidToRecord setObject:record forKey:uid];
181 [result addObject:record];
183 [self->uidToFolder setObject:aptFolder forKey:uid];
185 else if ([self doesRecord:existingRecord conflictWith:record]) {
186 /* record already registered and it conflicts (diff values) */
187 NSDictionary *newRecord;
190 newRecord = [self _registerConflictingRecord:record
191 inRecord:existingRecord];
192 [uidToRecord setObject:newRecord forKey:uid];
194 if ((idx = [result indexOfObject:existingRecord]) != NSNotFound)
195 [result replaceObjectAtIndex:idx withObject:newRecord];
198 /* record already registered, but values in sync, nothing to do */
208 - (NSString *)baseURLForAptWithUID:(NSString *)_uid inContext:(id)_ctx {
209 /* Note: fetchCore must have been called before this works */
210 SOGoAppointmentFolder *folder;
212 if ([_uid length] == 0) {
213 [self errorWithFormat:@"got invalid UID."];
217 if ((folder = [self folderForUID:_uid]) == nil) {
218 [self errorWithFormat:@"did not find a folder containing UID: '%@'",
222 if (![folder respondsToSelector:_cmd]) {
223 [self errorWithFormat:@"found folder cannot construct UID URLs: %@",
228 [self debugWithFormat:@"found ID %@ in folder: %@", _uid, folder];
230 return [folder baseURLForAptWithUID:_uid inContext:_ctx];
233 @end /* SOGoGroupAppointmentFolder */