]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEContextKey.m
fix for SoProductResourceManager.m
[sope] / sope-appserver / WEExtensions / WEContextKey.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 <NGObjWeb/WODynamicElement.h>
24
25 @interface WEContextKey : WODynamicElement
26 {
27   WOAssociation *key;
28   WOAssociation *value;
29   WOElement     *template;
30 }
31 @end
32
33 #include "common.h"
34
35 @implementation WEContextKey
36
37 - (id)initWithName:(NSString *)_name
38   associations:(NSDictionary *)_config
39   template:(WOElement *)_tmp
40 {
41   if ((self = [super initWithName:_name associations:_config template:_tmp])) {
42     self->key   = WOExtGetProperty(_config, @"key");
43     self->value = WOExtGetProperty(_config, @"value");
44
45     if (self->key == nil)
46       NSLog(@"Warning! WEContextKey no key set");
47     
48     if (self->value == nil) {
49       self->value = [WOAssociation associationWithValue:@"YES"];
50       RETAIN(self->value);
51     }
52     
53     ASSIGN(self->template, _tmp);
54   }
55   return self;
56 }
57
58 #if !LIB_FOUNDATION_BOEHM_GC
59 - (void)dealloc {
60   RELEASE(self->key);
61   RELEASE(self->value);
62   RELEASE(self->template);
63   
64   [super dealloc];
65 }
66 #endif
67
68 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
69   NSString *k  = nil;
70   id       v   = nil;
71   id       tmp = nil;
72
73   k = [self->key   stringValueInComponent:[_ctx component]];
74   v = [self->value valueInComponent:[_ctx component]];
75
76   if (k && v) {
77     tmp = [_ctx objectForKey:k]; // save old context value
78     [_ctx setObject:v forKey:k];
79   }
80   
81   [self->template takeValuesFromRequest:_req inContext:_ctx];
82
83   if (k && v)   [_ctx removeObjectForKey:k];
84   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
85 }
86
87 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
88   NSString *k     = nil;
89   id       v      = nil;
90   id       tmp    = nil;
91   id       result = nil;
92
93   k = [self->key   stringValueInComponent:[_ctx component]];
94   v = [self->value valueInComponent:[_ctx component]];
95
96   if (k && v) {
97     tmp = [_ctx objectForKey:k]; // save old context value
98     [_ctx setObject:v forKey:k];
99   }
100   
101   result = [self->template invokeActionForRequest:_req inContext:_ctx];
102   
103   if (k && v)   [_ctx removeObjectForKey:k];
104   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
105   
106   return result;
107 }
108
109 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
110   NSString *k  = nil;
111   id       v   = nil;
112   id       tmp = nil;
113   
114   k = [self->key   stringValueInComponent:[_ctx component]];
115   v = [self->value valueInComponent:[_ctx component]];
116
117   if (k && v) {
118     tmp = [_ctx objectForKey:k]; // save old context value
119     [_ctx setObject:v forKey:k];
120   }
121
122   [self->template appendToResponse:_response inContext:_ctx];
123   
124   if (k && v)   [_ctx removeObjectForKey:k];
125   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
126 }
127
128 @end /* WEContextKey */