]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactGCSFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1173 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Contacts / SOGoContactGCSFolder.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 #import <Foundation/NSArray.h>
23 #import <Foundation/NSString.h>
24
25 #import <NGObjWeb/NSException+HTTP.h>
26 #import <NGObjWeb/SoObject+SoDAV.h>
27 #import <NGObjWeb/WOContext.h>
28 #import <NGObjWeb/WORequest.h>
29 #import <NGExtensions/NSObject+Logs.h>
30 #import <EOControl/EOQualifier.h>
31 #import <EOControl/EOSortOrdering.h>
32 #import <GDLContentStore/GCSFolder.h>
33
34 #import <SoObjects/SOGo/NSDictionary+Utilities.h>
35 #import "SOGoContactGCSEntry.h"
36 #import "SOGoContactGCSFolder.h"
37
38 #define folderListingFields [NSArray arrayWithObjects: @"c_name", @"c_cn", \
39                                      @"c_givenname", @"c_sn", @"c_screenname", \
40                                      @"c_o", @"c_mail", @"c_telephonenumber", \
41                                      nil]
42
43 @implementation SOGoContactGCSFolder
44
45 /* name lookup */
46
47 - (id <SOGoContactObject>) lookupContactWithId: (NSString *) recordId
48 {
49   SOGoContactGCSEntry *contact;
50
51   if ([recordId length] > 0)
52     contact = [SOGoContactGCSEntry objectWithName: recordId
53                                    inContainer: self];
54   else
55     contact = nil;
56
57   return contact;
58 }
59
60 - (id) lookupName: (NSString *) _key
61         inContext: (WOContext *) _ctx
62           acquire: (BOOL) _flag
63 {
64   id obj;
65   BOOL isPut;
66
67   isPut = NO;
68   obj = [super lookupName:_key inContext:_ctx acquire:NO];
69   if (!obj)
70     {
71       if ([[[_ctx request] method] isEqualToString: @"PUT"])
72         {
73           if ([_key isEqualToString: @"PUT"])
74             isPut = YES;
75           else
76             obj = [SOGoContactGCSEntry objectWithName: _key
77                                        inContainer: self];
78         }
79       else
80         obj = [self lookupContactWithId: _key];
81     }
82 //   if (!(obj || isPut))
83 //     obj = [NSException exceptionWithHTTPStatus:404 /* Not Found */];
84
85 // #if 0
86 //     if ([[self ocsFolder] versionOfContentWithName:_key])
87 // #endif
88 //       return [self contactWithName:_key inContext:_ctx];
89 //   }
90
91   /* return 404 to stop acquisition */
92   return obj;
93 }
94
95 /* fetching */
96 - (EOQualifier *) _qualifierForFilter: (NSString *) filter
97 {
98   NSString *qs;
99   EOQualifier *qualifier;
100
101   if (filter && [filter length] > 0)
102     {
103       qs = [NSString stringWithFormat:
104                        @"(c_sn isCaseInsensitiveLike: '%@%%') OR "
105                      @"(c_givenname isCaseInsensitiveLike: '%@%%') OR "
106                      @"(c_mail isCaseInsensitiveLike: '%@%%') OR "
107                      @"(c_telephonenumber isCaseInsensitiveLike: '%%%@%%')",
108                      filter, filter, filter, filter];
109       qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
110     }
111   else
112     qualifier = nil;
113
114   return qualifier;
115 }
116
117 - (NSArray *) _flattenedRecords: (NSArray *) records
118 {
119   NSMutableArray *newRecords;
120   NSEnumerator *oldRecords;
121   NSDictionary *oldRecord;
122   NSMutableDictionary *newRecord;
123   NSString *data;
124   
125   newRecords = [NSMutableArray arrayWithCapacity: [records count]];
126
127   oldRecords = [records objectEnumerator];
128   oldRecord = [oldRecords nextObject];
129   while (oldRecord)
130     {
131       newRecord = [NSMutableDictionary new];
132       [newRecord autorelease];
133
134       [newRecord setObject: [oldRecord objectForKey: @"c_name"]
135                  forKey: @"c_uid"];
136       [newRecord setObject: [oldRecord objectForKey: @"c_name"]
137                  forKey: @"c_name"];
138
139       data = [oldRecord objectForKey: @"c_cn"];
140       if (![data length])
141         data = [oldRecord keysWithFormat: @"%{c_givenname} %{c_sn}"];
142       [newRecord setObject: data
143                  forKey: @"displayName"];
144
145       data = [oldRecord objectForKey: @"c_mail"];
146       if (!data)
147         data = @"";
148       [newRecord setObject: data forKey: @"mail"];
149
150       data = [oldRecord objectForKey: @"c_screenname"];
151       if (!data)
152         data = @"";
153       [newRecord setObject: data forKey: @"screenName"];
154
155       data = [oldRecord objectForKey: @"c_o"];
156       if (!data)
157         data = @"";
158       [newRecord setObject: data forKey: @"org"];
159
160       data = [oldRecord objectForKey: @"c_telephonenumber"];
161       if (![data length])
162         data = @"";
163       [newRecord setObject: data forKey: @"phone"];
164
165       [newRecords addObject: newRecord];
166       oldRecord = [oldRecords nextObject];
167     }
168
169   return newRecords;
170 }
171
172 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
173                                 sortBy: (NSString *) sortKey
174                               ordering: (NSComparisonResult) sortOrdering
175 {
176   NSArray *fields, *dbRecords, *records;
177   EOQualifier *qualifier;
178   EOSortOrdering *ordering;
179
180 //   NSLog (@"fetching records matching '%@', sorted by '%@' in order %d",
181 //          filter, sortKey, sortOrdering);
182
183   fields = folderListingFields;
184   qualifier = [self _qualifierForFilter: filter];
185   dbRecords = [[self ocsFolder] fetchFields: fields
186                                 matchingQualifier: qualifier];
187   if ([dbRecords count] > 0)
188     {
189       records = [self _flattenedRecords: dbRecords];
190       ordering
191         = [EOSortOrdering sortOrderingWithKey: sortKey
192                           selector: ((sortOrdering == NSOrderedDescending)
193                                      ? EOCompareCaseInsensitiveDescending
194                                      : EOCompareCaseInsensitiveAscending)];
195       records
196         = [records sortedArrayUsingKeyOrderArray:
197                      [NSArray arrayWithObject: ordering]];
198     }
199   else
200     records = nil;
201 //   else
202 //     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
203
204   //[self debugWithFormat:@"fetched %i records.", [records count]];
205   return records;
206 }
207
208 - (NSArray *) davNamespaces
209 {
210   return [NSArray arrayWithObject: @"urn:ietf:params:xml:ns:carddav"];
211 }
212
213 - (NSArray *) davComplianceClassesInContext: (id)_ctx
214 {
215   NSMutableArray *classes;
216   NSArray *primaryClasses;
217
218   classes = [NSMutableArray new];
219   [classes autorelease];
220
221   primaryClasses = [super davComplianceClassesInContext: _ctx];
222   if (primaryClasses)
223     [classes addObjectsFromArray: primaryClasses];
224   [classes addObject: @"access-control"];
225   [classes addObject: @"addressbook-access"];
226
227   return classes;
228 }
229
230 - (NSString *) groupDavResourceType
231 {
232   return @"vcard-collection";
233 }
234
235 // /* GET */
236
237 // - (id) GETAction: (id)_ctx
238 // {
239 //   // TODO: I guess this should really be done by SOPE (redirect to
240 //   //       default method)
241 //   WOResponse *r;
242 //   NSString *uri;
243
244 //   uri = [[_ctx request] uri];
245 //   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
246 //   uri = [uri stringByAppendingString:@"view"];
247   
248 //   r = [_ctx response];
249 //   [r setStatus:302 /* moved */];
250 //   [r setHeader:uri forKey:@"location"];
251 //   return r;
252 // }
253
254 /* folder type */
255
256 - (NSString *) folderType
257 {
258   return @"Contact";
259 }
260
261 - (NSString *) outlookFolderClass
262 {
263   return @"IPF.Contact";
264 }
265
266 @end /* SOGoContactGCSFolder */