~ubuntu-branches/ubuntu/saucy/sope/saucy

« back to all changes in this revision

Viewing changes to sope-appserver/NGObjWeb/DynamicElements/WOSwitchComponent.m

  • Committer: Package Import Robot
  • Author(s): Jeroen Dekkers
  • Date: 2012-05-09 15:39:17 UTC
  • Revision ID: package-import@ubuntu.com-20120509153917-nr4jlm8mktma1yv3
Tags: upstream-1.3.14
ImportĀ upstreamĀ versionĀ 1.3.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/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
  [_ctx consumeElementID];
 
133
  
 
134
  [_ctx appendElementIDComponent:cname];
 
135
  [_ctx enterComponent:c content:self->template];
 
136
  result = [c invokeActionForRequest:_req inContext:_ctx];
 
137
  [_ctx leaveComponent:c];
 
138
  [_ctx deleteLastElementIDComponent];
 
139
  
 
140
  return result;
 
141
}
 
142
 
 
143
/* generate response */
 
144
 
 
145
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
 
146
  WOComponent *c;
 
147
  NSString    *cname;
 
148
  
 
149
  cname = [self->componentName stringValueInComponent:[_ctx component]];
 
150
  
 
151
  if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
 
152
    return;
 
153
  
 
154
  [_ctx appendElementIDComponent:cname];
 
155
  [_ctx enterComponent:c content:self->template];
 
156
  [c appendToResponse:_response inContext:_ctx];
 
157
  [_ctx leaveComponent:c];
 
158
  [_ctx deleteLastElementIDComponent];
 
159
}
 
160
 
 
161
@end /* WOSwitchComponent */