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
24 WESwitch { selection | selections };
25 WECase { key | keys };
28 Warning: The DefaultCase must appear at the last position!!!
35 Switch: WESwitch { selection = selection; };
36 FirstCase: WECase { key = "first"; };
37 SecondCase: WECase { keys = ("second", "third"); };
38 DefaultCase: WEDefaultCase {};
42 <#FirstCase>content of first case</#FirstCase>
43 <#SecondCase>content of second case</#SecondCase>
44 <#DefaultCase>content of default case</#SecondCase>
48 #include <NGObjWeb/WODynamicElement.h>
52 @interface WESwitch : WODynamicElement
55 WOAssociation *selection; // string -> single switch
56 WOAssociation *selections; // array -> multi switch
61 @interface WECase : WODynamicElement
63 WOAssociation *key; // string -> unique identifier
64 WOAssociation *keys; // array of unique identifiers
66 WOAssociation *defaultCase; // emulates a WEDefaultCase DEPRECATED!!!
72 @interface WEDefaultCase : WODynamicElement
81 static NSString *WESwitch_DefaultCaseFound = @"WESwitch_DefaultCaseFound";
83 static NSString *WESwitch_CaseDidMatch = @"WESwitch_CaseDidMatch";
84 static NSString *WESwitchSelection = @"WESwitchSelection";
85 static NSString *WESwitchSelections = @"WESwitchSelections";
86 static NSString *WESwitchDict = @"WESwitchDict";
88 @implementation WESwitch
90 - (id)initWithName:(NSString *)_name
91 associations:(NSDictionary *)_config
92 template:(WOElement *)_subs
94 if ((self = [super initWithName:_name associations:_config template:_subs])) {
95 self->selection = WOExtGetProperty(_config, @"selection");
96 self->selections = WOExtGetProperty(_config, @"selections");
98 self->template = [_subs retain];
104 [self->template release];
105 [self->selection release];
106 [self->selections release];
110 /* processing requests */
112 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
113 WOComponent *cmp = nil;
114 NSArray *array = nil;
120 cmp = [_ctx component];
121 array = [self->selections valueInComponent:cmp];
122 k = [self->selection valueInComponent:cmp];
126 [self->template takeValuesFromRequest:_req inContext:_ctx];
129 [_ctx setObject:k forKey:WESwitchSelection];
130 [self->template takeValuesFromRequest:_req inContext:_ctx];
131 [_ctx removeObjectForKey:WESwitchSelection];
134 for (i = 0; i < cnt; i++) {
135 [_ctx setObject:[array objectAtIndex:i] forKey:WESwitchSelection];
136 [self->template takeValuesFromRequest:_req inContext:_ctx];
139 [_ctx setObject:array forKey:WESwitchSelections];
140 [self->template takeValuesFromRequest:_req inContext:_ctx];
141 [_ctx removeObjectForKey:WESwitchSelections];
143 [_ctx removeObjectForKey:WESwitchSelection];
146 NSLog(@"Warning(%s):This case is not implemented!!!", __PRETTY_FUNCTION__);
147 [self->template takeValuesFromRequest:_req inContext:_ctx];
153 @"Warning! WESwitch: Neither 'selection' nor 'selections' set!!!"];
157 [_ctx removeObjectForKey:WESwitch_CaseDidMatch];
160 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
161 return [self->template invokeActionForRequest:_request inContext:_ctx];
164 /* generating response */
166 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
167 WOComponent *cmp = nil;
168 NSArray *array = nil;
174 cmp = [_ctx component];
175 array = [self->selections valueInComponent:cmp];
176 k = [self->selection valueInComponent:cmp];
179 if (_response == nil) {
180 [self->template appendToResponse:_response inContext:_ctx];
183 [_ctx setObject:k forKey:WESwitchSelection];
184 [self->template appendToResponse:_response inContext:_ctx];
185 [_ctx removeObjectForKey:WESwitchSelection];
188 for (i=0; i<cnt; i++) {
189 [_ctx setObject:[array objectAtIndex:i] forKey:WESwitchSelection];
190 [self->template appendToResponse:_response inContext:_ctx];
193 [_ctx setObject:array forKey:WESwitchSelections];
194 [self->template appendToResponse:_response inContext:_ctx];
195 [_ctx removeObjectForKey:WESwitchSelections];
197 [_ctx removeObjectForKey:WESwitchSelection];
200 NSMutableDictionary *dict = nil;
202 // get subcontent of WECases
203 [_ctx setObject:array forKey:WESwitchSelections];
204 [self->template appendToResponse:_response inContext:_ctx];
206 dict = [_ctx objectForKey:WESwitchDict];
210 for (i=0; i<cnt; i++) {
211 NSString *k = [array objectAtIndex:i];
212 NSData *c = [dict objectForKey:k]; // subcontent of WECase
215 [_response appendContentData:c];
219 [_ctx removeObjectForKey:WESwitchDict];
220 [_ctx removeObjectForKey:WESwitchSelections];
226 @"Warning! WESwitch: Neither 'selection' nor 'selections' set!!!"];
230 [_ctx removeObjectForKey:WESwitch_CaseDidMatch];
235 @implementation WECase
237 - (id)initWithName:(NSString *)_name
238 associations:(NSDictionary *)_config
239 template:(WOElement *)_t
241 if ((self = [super initWithName:_name associations:_config template:_t])) {
242 self->key = WOExtGetProperty(_config, @"key");
243 self->keys = WOExtGetProperty(_config, @"keys");
246 self->defaultCase = WOExtGetProperty(_config, @"default");
248 self->template = [_t retain];
254 [self->template release];
256 [self->keys release];
257 [self->defaultCase release];
261 /* processing requests */
263 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
264 NSArray *selections = nil;
265 NSString *selection = nil;
269 k = [self->key stringValueInComponent:[_ctx component]];
270 ks = [self->keys valueInComponent:[_ctx component]];
272 selections = [_ctx objectForKey:WESwitchSelections];
273 selection = [_ctx objectForKey:WESwitchSelection];
275 if ([self->defaultCase boolValueInComponent:[_ctx component]]) {
276 if ([_ctx objectForKey:WESwitch_CaseDidMatch] == nil)
277 [self->template takeValuesFromRequest:_req inContext:_ctx];
281 if ((k == nil) && (ks == nil)) {
283 [[_ctx component] logWithFormat:
284 @"Warning! WECase: Neither 'key' nor 'keys' set!!!"];
288 if ((k != nil) && (ks != nil)) {
290 [[_ctx component] logWithFormat:
291 @"Warning! WECase: Both, 'key' and 'keys' are set!!!"];
297 [self->template takeValuesFromRequest:nil inContext:_ctx];
300 if (k && [k isEqualToString:selection]) {
301 [self->template takeValuesFromRequest:_req inContext:_ctx];
302 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
304 else if (ks && [ks containsObject:selection]) {
305 [self->template takeValuesFromRequest:_req inContext:_ctx];
306 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
309 else if (selections && [selections count] > 0) {
310 NSLog(@"Warning(%s): This case is not implemented!", __PRETTY_FUNCTION__);
311 [self->template takeValuesFromRequest:_req inContext:_ctx];
315 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
316 return [self->template invokeActionForRequest:_req inContext:_ctx];
319 /* generating response */
321 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
322 NSArray *selections = nil;
323 NSString *selection = nil;
328 k = [self->key stringValueInComponent:[_ctx component]];
329 ks = [self->keys valueInComponent:[_ctx component]];
331 selections = [_ctx objectForKey:WESwitchSelections];
332 selection = [_ctx objectForKey:WESwitchSelection];
334 if ([self->defaultCase boolValueInComponent:[_ctx component]]) {
335 if ([_ctx objectForKey:WESwitch_CaseDidMatch] == nil)
336 [self->template appendToResponse:_response inContext:_ctx];
340 if ((k == nil) && (ks == nil)) {
342 [[_ctx component] logWithFormat:
343 @"Warning! WECase: Neither 'key' nor 'keys' set!!!"];
347 if ((k != nil) && (ks != nil)) {
349 [[_ctx component] logWithFormat:
350 @"Warning! WECase: Both, 'key' and 'keys' are set!!!"];
355 if (_response == nil) {
356 [self->template appendToResponse:nil inContext:_ctx];
359 if (k && [k isEqualToString:selection]) {
360 [self->template appendToResponse:_response inContext:_ctx];
361 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
363 else if (ks && [ks containsObject:selection]) {
364 [self->template appendToResponse:_response inContext:_ctx];
365 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
368 else if (selections && [selections count] > 0) {
369 if ([selections containsObject:k]) {
370 static NSData *emptyData = nil;
371 NSMutableDictionary *dict = nil;
372 NSData *oldContent = nil;
374 if (emptyData == nil)
375 emptyData = [[NSData alloc] init];
377 // get subcontent dictionary
378 dict = [_ctx objectForKey:WESwitchDict];
380 dict = [NSMutableDictionary dictionaryWithCapacity:[selections count]];
383 oldContent = [_response content];
385 [_response setContent:emptyData];
387 // append template to new content
388 [self->template appendToResponse:_response inContext:_ctx];
390 // save new content in dict
391 if ([_response content])
392 [dict setObject:[_response content] forKey:k];
393 [_ctx setObject:dict forKey:WESwitchDict];
395 // restore old content
396 [_response setContent:oldContent];
397 [oldContent release]; oldContent = nil;
399 // TODO: use NSNumber here?
400 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
407 @implementation WEDefaultCase
409 - (id)initWithName:(NSString *)_name
410 associations:(NSDictionary *)_config
411 template:(WOElement *)_t
413 if ((self = [super initWithName:_name associations:_config template:_t])) {
414 self->template = [_t retain];
420 [self->template release];
424 /* processing requests */
426 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
427 if (([_ctx objectForKey:WESwitch_CaseDidMatch] == nil))
428 [self->template takeValuesFromRequest:_req inContext:_ctx];
431 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
432 return [self->template invokeActionForRequest:_req inContext:_ctx];
435 /* generating response */
437 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
438 if (([_ctx objectForKey:WESwitch_CaseDidMatch] == nil))
439 [self->template appendToResponse:_response inContext:_ctx];
442 [_ctx setObject:@"Yes" forKey:WESwitch_DefaultCaseFound];
446 @end /* WEDefaultCase */