2 Copyright (C) 2000-2004 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
23 #include <EOControl/EOControl.h>
24 #include <NGExtensions/EODataSource+NGExtensions.h>
25 #include <NGExtensions/NSNull+misc.h>
26 #include "../common.h"
29 EODataSource JavaScript object
32 bool setQualifier(string [,args...]) // Format: EOQualifier Format !
34 bool setEntity(string)
36 bool setSortOrdering(string) // Format: 'name' oder '-name'
37 String getSortOrdering()
38 bool setHint(string, value)
39 Object getHint(string)
41 Array fetchObjects([qual][,orderings][,grouping])
43 Document createObject()
44 bool updateObject(document)
45 bool insertObject(document)
46 bool deleteObject(document)
49 @implementation EODataSource(SkyJSDataSourceBehaviour)
53 static inline void _ensureNull(void) {
55 null = [[NSNull null] retain];
60 - (void)_updateFetchSpecWithEntityName:(NSString *)_ename
61 qualifier:(EOQualifier *)_qual
62 sortOrderings:(NSArray *)_so
64 EOFetchSpecification *fspec;
66 if (_ename == nil && _qual == nil && _so == nil)
67 /* nothing to update .. */
72 if ((fspec = [self fetchSpecification]) == nil) {
73 if (_qual == null) _qual = nil;
74 if (_so == null) _so = nil;
75 if (_ename == null) _ename = nil;
77 fspec = [[EOFetchSpecification alloc]
78 initWithEntityName:_ename
81 usesDistinct:YES isDeep:NO hints:nil];
82 fspec = [fspec autorelease];
84 [self setFetchSpecification:fspec];
87 fspec = [[fspec copy] autorelease];
90 if (_qual == null) _qual = nil;
91 [fspec setQualifier:_qual];
94 if (_ename == null) _ename = nil;
95 [fspec setEntityName:_ename];
98 if (_so == null) _so = nil;
99 [fspec setSortOrderings:_so];
102 [self setFetchSpecification:fspec];
106 - (id)_jsfunc_setEntity:(NSArray *)_args {
108 NSString *entityName;
110 if ((count = [_args count]) == 0) {
112 NSLog(@"%s: missing qualifier argument ..", __PRETTY_FUNCTION__);
117 entityName = [[_args objectAtIndex:0] stringValue];
120 if (entityName == nil) entityName = null;
122 [self _updateFetchSpecWithEntityName:entityName
126 return [NSNumber numberWithBool:YES];
128 - (id)_jsfunc_getEntity:(NSArray *)_args {
129 return [[self fetchSpecification] entityName];
132 - (id)_jsfunc_setSortOrdering:(NSArray *)_args {
135 if ((count = [_args count]) == 0) {
136 [self _updateFetchSpecWithEntityName:nil
138 sortOrderings:[NSArray array]];
140 return [NSNumber numberWithBool:YES];
142 else if (count == 1) {
145 EOSortOrdering *ordering;
147 selector = EOCompareAscending;
149 key = [[_args objectAtIndex:0] stringValue];
150 if ([key hasPrefix:@"-"]) {
151 selector = EOCompareDescending;
152 key = [key substringFromIndex:1];
155 ordering = [EOSortOrdering sortOrderingWithKey:key selector:selector];
157 [self _updateFetchSpecWithEntityName:nil
159 sortOrderings:[NSArray arrayWithObject:ordering]];
161 return [NSNumber numberWithBool:YES];
164 return [NSNumber numberWithBool:NO];
166 - (id)_jsfunc_getSortOrdering:(NSArray *)_args {
169 EOSortOrdering *ordering;
171 orderings = [[self fetchSpecification] sortOrderings];
172 if ((count = [orderings count]) == 0)
175 ordering = [orderings objectAtIndex:0];
177 #if APPLE_RUNTIME || NeXT_RUNTIME
178 if ([ordering selector] == EOCompareDescending)
179 return [@"-" stringByAppendingString:[ordering key]];
180 if ([ordering selector] == EOCompareDescending)
181 return [ordering key];
183 if (sel_eq([ordering selector], EOCompareDescending))
184 return [@"-" stringByAppendingString:[ordering key]];
185 if (sel_eq([ordering selector], EOCompareDescending))
186 return [ordering key];
191 - (id)_jsfunc_setQualifier:(NSArray *)_args {
192 EOFetchSpecification *fspec;
199 fspec = [self fetchSpecification];
201 if ((count = [_args count]) == 0) {
203 NSLog(@"%s: missing qualifier argument ..", __PRETTY_FUNCTION__);
210 qq = [_args objectAtIndex:0];
213 ? [_args subarrayWithRange:NSMakeRange(1, count - 1)]
216 if ([qq isKindOfClass:[EOQualifier class]]) {
219 else if ([[qq stringValue] length] == 0) {
223 q = [EOQualifier qualifierWithQualifierFormat:[qq stringValue]
228 NSLog(@"%s: couldn't parse qualifier '%@' ..",
229 __PRETTY_FUNCTION__, qq);
230 return [NSNumber numberWithBool:NO];
236 if (q == nil) q = null;
238 [self _updateFetchSpecWithEntityName:nil qualifier:q sortOrderings:nil];
240 return [NSNumber numberWithBool:YES];
242 - (id)_jsfunc_getQualifier:(NSArray *)_args {
243 return [(id)[[self fetchSpecification] qualifier] stringValue];
246 - (id)_jsfunc_setHint:(NSArray *)_args {
250 EOFetchSpecification *fspec;
251 NSMutableDictionary *hints;
253 if ((count = [_args count]) == 0)
254 return [NSNumber numberWithBool:NO];
256 return [NSNumber numberWithBool:NO];
258 key = [[_args objectAtIndex:0] stringValue];
259 value = [_args objectAtIndex:1];
262 return [NSNumber numberWithBool:NO];
264 if ((fspec = [[self fetchSpecification] copy]) == nil) {
265 fspec = [[EOFetchSpecification alloc]
266 initWithEntityName:nil
269 usesDistinct:YES isDeep:NO hints:nil];
271 fspec = [fspec autorelease];
273 hints = [[fspec hints] mutableCopy];
275 hints = [[NSMutableDictionary alloc] initWithCapacity:4];
277 if (![value isNotNull]) {
279 [hints removeObjectForKey:key];
282 [hints setObject:value forKey:key];
285 [fspec setHints:hints];
286 [hints release]; hints = nil;
288 [self setFetchSpecification:fspec];
290 return [NSNumber numberWithBool:YES];
292 - (id)_jsfunc_getHint:(NSArray *)_args {
296 if ((count = [_args count]) == 0)
299 hints = [[self fetchSpecification] hints];
300 return [hints objectForKey:[[_args objectAtIndex:0] stringValue]];
303 /* query operation */
305 - (void)logException:(NSException *)_exception {
306 NSLog(@"%s: exception: %@", __PRETTY_FUNCTION__, _exception);
309 - (id)_jsfunc_fetchObjects:(NSArray *)_args {
310 EOFetchSpecification *fspec;
313 id sortOrderings = nil;
317 count = [_args count];
322 if ((fspec = [self fetchSpecification]) == nil) {
323 fspec = [EOFetchSpecification fetchSpecificationWithEntityName:nil
328 fspec = [[fspec copy] autorelease];
334 qualifier = [_args objectAtIndex:0];
335 if (![qualifier isKindOfClass:[EOQualifier class]]) {
336 qs = [qualifier stringValue];
337 qualifier = [EOQualifier qualifierWithQualifierFormat:qs];
339 NSLog(@"%s: made q %@ for string '%@'",
340 __PRETTY_FUNCTION__, qualifier, qs);
344 [fspec setQualifier:qualifier];
348 sortOrderings = [_args objectAtIndex:1];
349 [fspec setSortOrderings:sortOrderings];
353 groupings = [_args objectAtIndex:2];
354 // [fspec setGroupings:..];
358 [self setFetchSpecification:fspec];
360 results = [self fetchObjects];
364 [self logException:localException];
371 /* modification operations */
373 - (id)_jsfunc_createObject:(NSArray *)_args {
377 obj = [self createObject];
380 [self logException:localException];
387 - (id)_jsfunc_insertObject:(NSArray *)_args {
393 if ((count = [_args count]) == 0)
394 return [NSNumber numberWithBool:YES];
396 e = [_args objectEnumerator];
398 while ((obj = [e nextObject]) && ok) {
400 [self insertObject:obj];
403 [self logException:localException];
408 return [NSNumber numberWithBool:ok];
411 - (id)_jsfunc_updateObject:(NSArray *)_args {
417 if ((count = [_args count]) == 0)
418 return [NSNumber numberWithBool:YES];
420 e = [_args objectEnumerator];
422 while ((obj = [e nextObject]) && ok) {
424 [self updateObject:obj];
429 return [NSNumber numberWithBool:ok];
432 - (id)_jsfunc_deleteObject:(NSArray *)_args {
438 if ((count = [_args count]) == 0)
439 return [NSNumber numberWithBool:YES];
441 e = [_args objectEnumerator];
443 while ((obj = [e nextObject]) && ok) {
445 [self deleteObject:obj];
448 [self logException:localException];
452 return [NSNumber numberWithBool:ok];
455 @end /* EOControl(SkyJSDataSourceBehaviour) */