]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOKeyValueConditional.m
added DeltaV methods
[sope] / sope-appserver / WOExtensions / WOKeyValueConditional.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 #include <NGObjWeb/WODynamicElement.h>
23
24 @interface WOKeyValueConditional : WODynamicElement
25 {
26 @protected
27   // WODynamicElement: extraAttributes
28   // WODynamicElement: otherTagString
29   
30   WOAssociation *key;
31   WOAssociation *value;
32   WOElement     *template;
33
34   // non-WO
35   WOAssociation *negate;
36 }
37
38 @end /* WOKeyValueConditional */
39
40 #include "common.h"
41
42 @implementation WOKeyValueConditional
43
44 - (id)initWithName:(NSString *)_name
45   associations:(NSDictionary *)_config
46   template:(WOElement *)_c
47 {
48   if ((self = [super initWithName:_name associations:_config template:_c])) {
49     self->key      = WOExtGetProperty(_config, @"key");
50     self->value    = WOExtGetProperty(_config, @"value");
51     self->negate   = WOExtGetProperty(_config, @"negate");
52     self->template = [_c retain];
53   }
54   return self;
55 }
56
57 - (void)dealloc {
58   [self->template release];
59   [self->value    release];
60   [self->key      release];
61   [super dealloc];
62 }
63
64 /* accessors */
65
66 - (id)template {
67   return self->template;
68 }
69
70 /* state */
71
72 static inline BOOL _doShow(WOKeyValueConditional *self, WOContext *_ctx) {
73   WOComponent *c       = [_ctx component];
74   BOOL        doShow   = NO;
75   BOOL        doNegate = [self->negate boolValueInComponent:c];
76   id          v, kv;
77   NSString    *k;
78
79   k  = [self->key   stringValueInComponent:c];
80   v  = [self->value valueInComponent:c];
81   kv = [c valueForKey:k];
82   
83   doShow = [kv isEqual:v];
84   
85   return doNegate ? !doShow : doShow;
86 }
87
88 /* handling requests */
89
90 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
91   if (_doShow(self, _ctx)) {
92     [_ctx appendElementIDComponent:@"1"];
93     [self->template takeValuesFromRequest:_rq inContext:_ctx];
94     [_ctx deleteLastElementIDComponent];
95   }
96 #if 0
97   else {
98     NSLog(@"didn't take value from request: %@\n  doShow=%@\n  doNegate=%@",
99           [self elementID],
100           self->condition, self->negate);
101   }
102 #endif
103 }
104
105 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
106   NSString *state;
107
108   state = [[_ctx currentElementID] stringValue];
109   
110   if (state) {
111     [_ctx consumeElementID]; // consume state-id (on or off)
112
113     if ([state isEqualToString:@"1"]) {
114       id result;
115       
116       [_ctx appendElementIDComponent:state];
117       result = [self->template invokeActionForRequest:_rq inContext:_ctx];
118       [_ctx deleteLastElementIDComponent];
119
120       return result;
121     }
122   }
123   return nil;
124 }
125
126 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
127   if (_doShow(self, _ctx)) {
128     [_ctx appendElementIDComponent:@"1"];
129     [self->template appendToResponse:_response inContext:_ctx];
130     [_ctx deleteLastElementIDComponent];
131   }
132 }
133
134 /* description */
135
136 - (NSString *)associationDescription {
137   NSMutableString *str;
138
139   str = [NSMutableString stringWithCapacity:64];
140   if (self->key)      [str appendFormat:@" key=%@",      self->key];
141   if (self->value)    [str appendFormat:@" value=%@",    self->value];
142   if (self->negate)   [str appendFormat:@" negate=%@",   self->negate];
143   if (self->template) [str appendFormat:@" template=%@", self->template];
144   return str;
145 }
146
147 @end /* WOKeyValueConditional */