]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOSwitchComponent.m
started TAL element builder
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOSwitchComponent.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 "WOHTMLDynamicElement.h"
23
24 @class NSDictionary;
25 @class WOAssociation;
26
27 @interface WOSwitchComponent : WOHTMLDynamicElement
28 {
29   // WODynamicElement: extraAttributes
30   // WODynamicElement: otherTagString
31 @protected
32   WOAssociation *componentName; // WOComponentName attribute
33   NSDictionary  *bindings;
34   WOElement     *template;
35 }
36
37 @end
38
39 #include <NGObjWeb/WOComponent.h>
40 #include <NGObjWeb/WOAssociation.h>
41 #include <NGObjWeb/WOContext.h>
42 #include "WOElement+private.h"
43 #include "WOContext+private.h"
44 #include "WOComponent+private.h"
45 #include "decommon.h"
46
47 @implementation WOSwitchComponent
48
49 - (id)initWithName:(NSString *)_name
50   associations:(NSDictionary *)_config
51   template:(WOElement *)_c
52 {
53   if ((self = [super initWithName:_name associations:_config template:_c])) {
54     self->containsForm  = YES;
55     self->componentName = OWGetProperty(_config, @"WOComponentName");
56     self->bindings      = [_config copy];
57     [(NSMutableDictionary *)_config removeAllObjects];
58
59     self->template = [_c retain];
60   }
61   return self;
62 }
63
64 - (void)dealloc {
65   [self->template      release];
66   [self->componentName release];
67   [self->bindings      release];
68   [super dealloc];
69 }
70
71 /* component lookup */
72
73 - (WOComponent *)lookupComponent:(NSString *)cname
74   inContext:(WOContext *)_ctx
75 {
76   WOComponent *component;
77   
78   if (cname == nil)
79     return nil;
80
81   if ((component = [[_ctx component] pageWithName:cname]) == nil) {
82     [[_ctx component] debugWithFormat:@"couldn't find component '%@'", cname];
83     return nil;
84   }
85   
86   [component setParent:[_ctx component]];
87   [component setBindings:self->bindings];
88   
89   return component;
90 }
91
92 /* handling requests */
93
94 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
95   WOComponent *c;
96   NSString    *cname;
97   
98   cname = [self->componentName stringValueInComponent:[_ctx component]];
99   
100   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
101     return;
102   
103   [_ctx appendElementIDComponent:cname];
104   [_ctx enterComponent:c content:self->template];
105   [c takeValuesFromRequest:_req inContext:_ctx];
106   [_ctx leaveComponent:c];
107   [_ctx deleteLastElementIDComponent];
108 }
109
110 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
111   WOComponent *c;
112   id       result;
113   NSString *cname, *reqname;
114
115   if ((reqname = [_ctx currentElementID]) == nil)
116     /* missing id in request */
117     return nil;
118   
119   cname = [self->componentName stringValueInComponent:[_ctx component]];
120   
121   if (![cname isEqualToString:reqname]) {
122     /* component mismatch */
123     [[_ctx component] logWithFormat:
124                         @"WOSwitchComponent: component name mismatch"
125                         @" (%@ vs %@), ignoring action.",
126                         cname, reqname];
127     return nil;
128   }
129   
130   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
131     return nil;
132
133   [_ctx appendElementIDComponent:cname];
134   [_ctx enterComponent:c content:self->template];
135   result = [c invokeActionForRequest:_req inContext:_ctx];
136   [_ctx leaveComponent:c];
137   [_ctx deleteLastElementIDComponent];
138   
139   return result;
140 }
141
142 /* generate response */
143
144 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
145   WOComponent *c;
146   NSString    *cname;
147   
148   cname = [self->componentName stringValueInComponent:[_ctx component]];
149   
150   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
151     return;
152   
153   [_ctx appendElementIDComponent:cname];
154   [_ctx enterComponent:c content:self->template];
155   [c appendToResponse:_response inContext:_ctx];
156   [_ctx leaveComponent:c];
157   [_ctx deleteLastElementIDComponent];
158 }
159
160 @end /* WOSwitchComponent */