]> err.no Git - sope/blob - sope-core/NGExtensions/EOExt.subproj/EOFilterDataSource.m
fixed logging in WOComponent
[sope] / sope-core / NGExtensions / EOExt.subproj / EOFilterDataSource.m
1 /*
2   Copyright (C) 2000-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 "EOFilterDataSource.h"
24 #include "EODataSource+NGExtensions.h"
25 #include "EOGrouping.h"
26 #include <EOControl/EOControl.h>
27 #include "common.h"
28
29 @interface NSDictionary(EOFilterDataSource)
30
31 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
32   andKeys:(NSArray *)_keys;
33
34 @end
35
36 @implementation EOFilterDataSource
37
38 - (id)initWithDataSource:(EODataSource *)_ds {
39   if ((self = [super init])) {
40     [self setSource:_ds];
41   }
42   return self;
43 }
44 - (id)init {
45   return [self initWithDataSource:nil];
46 }
47
48 - (void)dealloc {
49   [[NSNotificationCenter defaultCenter] removeObserver:self];
50   [self->sortOrderings      release];
51   [self->groupings          release];
52   [self->auxiliaryQualifier release];
53   [self->source             release];
54   [super dealloc];
55 }
56
57 /* accessors */
58
59 - (void)setSource:(EODataSource *)_source {
60   NSNotificationCenter *nc;
61   
62   if (self->source == _source)
63     return;
64     
65   nc = [NSNotificationCenter defaultCenter];
66
67   if (self->source) {
68       [nc removeObserver:self
69           name:EODataSourceDidChangeNotification object:self->source];
70   }
71     
72   ASSIGN(self->source, _source);
73     
74   if (self->source) {
75       [nc addObserver:self selector:@selector(_sourceDidChange:)
76           name:EODataSourceDidChangeNotification object:self->source];
77   }
78     
79   [self postDataSourceChangedNotification];
80 }
81 - (EODataSource *)source {
82   return self->source;
83 }
84
85 - (void)setAuxiliaryQualifier:(EOQualifier *)_q {
86   if ([_q isEqual:self->auxiliaryQualifier])
87     return;
88
89   ASSIGN(self->auxiliaryQualifier, _q);
90   [self postDataSourceChangedNotification];
91 }
92 - (EOQualifier *)auxiliaryQualifier {
93   return self->auxiliaryQualifier;
94 }
95
96 - (void)setSortOrderings:(NSArray *)_so {
97   if (self->sortOrderings == _so)
98     return;
99   
100   _so = [_so shallowCopy];
101   [self->sortOrderings release];
102   self->sortOrderings = _so;
103   [self postDataSourceChangedNotification];
104 }
105 - (NSArray *)sortOrderings {
106   return self->sortOrderings;
107 }
108
109 - (void)setGroupings:(NSArray *)_groupings {
110   if (self->groupings == _groupings)
111     return;
112   
113   _groupings = [_groupings shallowCopy];
114   [self->groupings release];
115   self->groupings = _groupings;
116   [self postDataSourceChangedNotification];  
117 }
118 - (NSArray *)groupings {
119   return self->groupings;
120 }
121
122 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
123   [[self source] setFetchSpecification:_fspec];
124 }
125 - (EOFetchSpecification *)fetchSpecification {
126   return [[self source] fetchSpecification];
127 }
128
129 /* notifications */
130
131 - (void)_sourceDidChange:(NSNotification *)_notification {
132   [self postDataSourceChangedNotification];
133 }
134
135 /* operations */
136
137 - (NSArray *)fetchObjects {
138   NSAutoreleasePool *pool;
139   NSArray *objs;
140   NSArray *groups;
141
142   pool = [[NSAutoreleasePool alloc] init];
143   
144   objs = [[self source] fetchObjects];
145   
146   if ([self auxiliaryQualifier])
147     objs = [objs filteredArrayUsingQualifier:[self auxiliaryQualifier]];
148
149   if ((groups = [self groupings]) != nil) {
150     unsigned int cnt;
151     EOGrouping   *grouping;
152     NSArray      *allKeys;
153     NSArray      *sos;
154     NSDictionary *groupDict;
155
156     cnt = [objs count];
157
158     grouping = [groups lastObject];
159     
160     if ((sos = [self sortOrderings]) != nil) {
161       [grouping setSortOrderings:sos];
162     }
163     groupDict = [objs arrayGroupedBy:grouping];
164
165     allKeys = [groupDict allKeys];
166     allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
167     objs    = [groupDict flattenedArrayWithHint:cnt andKeys:allKeys];
168   }
169   else if ([self sortOrderings])
170     objs = [objs sortedArrayUsingKeyOrderArray:[self sortOrderings]];
171   
172   objs = [objs copy];
173   [pool release];
174   
175   return [objs autorelease];
176 }
177
178 - (void)insertObject:(id)_obj {
179   [[self source] insertObject:_obj];
180 }
181
182 - (void)deleteObject:(id)_obj {
183   [[self source] deleteObject:_obj];
184 }
185
186 - (void)updateObject:(id)_obj {
187   [self->source updateObject:_obj];
188 }
189
190 - (id)createObject {
191   return [[self source] createObject];
192 }
193
194 - (EOClassDescription *)classDescriptionForObjects {
195   return [[self source] classDescriptionForObjects];
196 }
197
198 /* description */
199
200 - (NSString *)description {
201   NSMutableString *ms;
202
203   ms = [NSMutableString stringWithCapacity:64];
204   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
205
206   if (self->source) [ms appendFormat:@" source=%@", self->source];
207   if (self->auxiliaryQualifier) 
208     [ms appendFormat:@" qualifier=%@", self->auxiliaryQualifier];
209   if (self->sortOrderings)
210     [ms appendFormat:@" orderings=%@", self->sortOrderings];
211   if (self->groupings)
212     [ms appendFormat:@" groupings=%@", self->sortOrderings];
213   
214   [ms appendString:@">"];
215   return ms;
216 }
217
218 @end /* EOFilterDataSource */
219
220 @implementation NSDictionary(EOFilterDataSource)
221
222 - (NSArray *)flattenedArrayWithHint:(unsigned int)_hint
223   andKeys:(NSArray *)_keys
224 {
225   NSMutableArray *result = nil;
226   unsigned int   i, cnt;
227   
228   result =
229     [[NSMutableArray alloc] initWithCapacity:_hint]; // should be improved
230   
231   for (i = 0, cnt = [_keys count]; i < cnt; i++) {
232     NSString *key;
233     NSArray  *tmp;
234     
235     key = [_keys objectAtIndex:i];
236     tmp = [self objectForKey:key];
237     [result addObjectsFromArray:tmp];
238   }
239   
240   return [result autorelease];
241 }
242
243 @end /* NSDictionary(EOFilterDataSource) */