]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Appointments/SOGoAppointmentFolder.m
6019eee564897f1d2a2f174259f546a7c437d477
[scalable-opengroupware.org] / SOGo / SoObjects / Appointments / SOGoAppointmentFolder.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 "SOGoAppointmentFolder.h"
24 #include <OGoContentStore/OCSFolder.h>
25 #include "common.h"
26 #include <unistd.h>
27 #include <stdlib.h>
28
29
30 @implementation SOGoAppointmentFolder
31
32 static BOOL debugOn = NO;
33
34 + (NSString *)globallyUniqueObjectId {
35 /*
36 4C08AE1A-A808-11D8-AC5A-000393BBAFF6
37  SOGo-Web-28273-18283-288182
38 printf( "%x", *(int *) &f);
39  */
40     static int pid = 0;
41     static int sequence = 0;
42     static float rndm = 0;
43     float f;
44
45     if(pid == 0) {
46       pid = getpid();
47       rndm = random();
48     }
49     sequence++;
50     f = [[NSDate date] timeIntervalSince1970];
51     return [NSString stringWithFormat:@"%0X-%0X-%0X-%0X",
52         pid,
53         *(int *)&f,
54         sequence++,
55         random];
56 }
57
58 - (void)dealloc {
59   [super dealloc];
60 }
61
62 /* name lookup */
63
64 - (BOOL)isValidAppointmentName:(NSString *)_key {
65   if ([_key length] == 0)
66     return NO;
67   
68   return YES;
69 }
70
71 - (id)appointmentWithName:(NSString *)_key inContext:(id)_ctx {
72   static Class aptClass = Nil;
73   id apt;
74   
75   if (aptClass == Nil)
76     aptClass = NSClassFromString(@"SOGoAppointmentObject");
77   if (aptClass == Nil) {
78     [self logWithFormat:@"ERROR: missing SOGoAppointmentObject class!"];
79     return nil;
80   }
81   
82   apt = [[aptClass alloc] initWithName:_key inContainer:self];
83   return [apt autorelease];
84 }
85
86 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
87   id obj;
88   
89   /* first check attributes directly bound to the application */
90   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
91     return obj;
92   
93   if ([self isValidAppointmentName:_key])
94     return [self appointmentWithName:_key inContext:_ctx];
95   
96   /* return 404 to stop acquisition */
97   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
98 }
99
100 /* fetching */
101
102 - (NSMutableDictionary *)fixupRecord:(NSDictionary *)_record {
103   NSMutableDictionary *md;
104   id tmp;
105   
106   md = [[_record mutableCopy] autorelease];
107   
108   if ((tmp = [_record objectForKey:@"startdate"])) {
109     tmp = [[NSCalendarDate alloc] initWithTimeIntervalSince1970:
110                                     (NSTimeInterval)[tmp unsignedIntValue]];
111     if (tmp) [md setObject:tmp forKey:@"startDate"];
112     [tmp release];
113   }
114   else
115     [self logWithFormat:@"missing 'startdate' in record?"];
116   
117   if ((tmp = [_record objectForKey:@"enddate"])) {
118     tmp = [[NSCalendarDate alloc] initWithTimeIntervalSince1970:
119                                     (NSTimeInterval)[tmp unsignedIntValue]];
120     if (tmp) [md setObject:tmp forKey:@"endDate"];
121     [tmp release];
122   }
123   else
124     [self logWithFormat:@"missing 'enddate' in record?"];
125   
126   return md;
127 }
128
129 - (NSArray *)fixupRecords:(NSArray *)_records {
130   NSMutableArray *ma;
131   unsigned i, count;
132
133   if (_records == nil) return nil;
134   if ((count = [_records count]) == 0)
135     return _records;
136   
137   ma = [NSMutableArray arrayWithCapacity:count];
138   for (i = 0; i < count; i++) {
139     id row;
140     
141     row = [self fixupRecord:[_records objectAtIndex:i]];
142     if (row) [ma addObject:row];
143   }
144   return ma;
145 }
146
147 - (NSArray *)fetchCoreInfosFrom:(NSCalendarDate *)_startDate
148   to:(NSCalendarDate *)_endDate 
149 {
150   OCSFolder   *folder;
151   EOQualifier *qualifier;
152   NSArray     *fields, *records;
153   NSString    *sql;
154   
155   if ((folder = [self ocsFolder]) == nil) {
156     [self logWithFormat:@"ERROR(%s): missing folder for fetch!",
157             __PRETTY_FUNCTION__];
158     return nil;
159   }
160   
161   [self logWithFormat:@"should fetch (%@ => %@) ...", _startDate, _endDate];
162   
163   sql = [NSString stringWithFormat:@"(startdate < %d) AND (enddate > %d)",
164                   (unsigned int)[_endDate   timeIntervalSince1970],
165                   (unsigned int)[_startDate timeIntervalSince1970]];
166   qualifier = [EOQualifier qualifierWithQualifierFormat:sql];
167   
168   fields = [NSArray arrayWithObjects:
169                       @"uid", @"startdate", @"enddate",
170                       @"title", @"participants", nil];
171   
172   records = [folder fetchFields:fields matchingQualifier:qualifier];
173   if (records == nil) {
174     [self logWithFormat:@"ERROR(%s): fetch failed!", __PRETTY_FUNCTION__];
175     return nil;
176   }
177   
178   records = [self fixupRecords:records];
179   if (debugOn)
180     [self logWithFormat:@"fetched %i records: %@", [records count], records];
181   return records;
182 }
183
184 /* GET */
185
186 - (id)GETAction:(WOContext *)_ctx {
187   // TODO: I guess this should really be done by SOPE (redirect to
188   //       default method)
189   WOResponse *r;
190   NSString *uri;
191
192   uri = [[_ctx request] uri];
193   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
194   uri = [uri stringByAppendingString:@"weekoverview"];
195   
196   r = [_ctx response];
197   [r setStatus:302 /* moved */];
198   [r setHeader:uri forKey:@"location"];
199   return r;
200 }
201
202 @end /* SOGoAppointmentFolder */