]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WODirectAction.m
fixed duplicate -awake
[sope] / sope-appserver / NGObjWeb / WODirectAction.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/WODirectAction.h>
23 #include "NSObject+WO.h"
24 #include <NGObjWeb/WOSession.h>
25 #include <NGObjWeb/WOApplication.h>
26 #include <NGObjWeb/WOContext.h>
27 #include <NGObjWeb/WORequest.h>
28 #include <NGObjWeb/WOSession.h>
29 #include <NGObjWeb/WOSessionStore.h>
30 #include "common.h"
31
32 #if !LIB_FOUNDATION_LIBRARY
33 #  define NG_USE_KVC_FALLBACK 1
34 #endif
35
36 @implementation WODirectAction
37
38 + (int)version {
39   return 4;
40 }
41
42 - (id)initWithRequest:(WORequest *)_request {
43   if ((self = [super init])) {
44   }
45   return self;
46 }
47 - (id)initWithContext:(WOContext *)_ctx {
48   if ((self = [self initWithRequest:[_ctx request]])) {
49     self->context = [_ctx retain];
50   }
51   return self;
52 }
53 - (id)init {
54   return [self initWithRequest:nil];
55 }
56
57 - (void)dealloc {
58   [self->context release];
59   [super dealloc];
60 }
61
62 /* accessors */
63
64 - (WOContext *)context {
65   if (self->context == nil)
66     self->context = [[[WOApplication application] context] retain];
67   return self->context;
68 }
69
70 - (WORequest *)request {
71   return [[self context] request];
72 }
73
74 - (WOSession *)session {
75   return [[self context] session];
76 }
77
78 - (WOSession *)existingSession {
79   WOContext *ctx = [self context];
80   
81   /* check whether the context has a session */
82   
83   return [ctx hasSession] ? [ctx session] : nil;
84 }
85
86 /* perform actions */
87
88 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName {
89   SEL actionSel;
90   NSRange rng;
91
92   /* discard everything after a point in the URL */
93   rng = [_actionName rangeOfString:@"."];
94   if (rng.length > 0)
95     _actionName = [_actionName substringToIndex:rng.location];
96
97   _actionName = [_actionName stringByAppendingString:@"Action"];
98   actionSel   = NSSelectorFromString(_actionName);
99   
100   if ([self respondsToSelector:actionSel]) 
101     return [self performSelector:actionSel];
102   else {
103     [self logWithFormat:@"DirectAction class %@ cannot handle action %@",
104             NSStringFromClass([self class]), _actionName];
105     return nil;
106   }
107 }
108
109 /* applying form values */
110
111 - (void)takeFormValuesForKeyArray:(NSArray *)_keys {
112   NSEnumerator *keys;
113   NSString     *key;
114   WORequest    *rq;
115
116   rq   = [self request];
117   keys = [_keys objectEnumerator];
118
119   while ((key = [keys nextObject])) {
120     NSString *value;
121
122     value = [rq formValueForKey:key];
123     
124     [self takeValue:value forKey:key];
125   }
126 }
127 - (void)takeFormValuesForKeys:(NSString *)_key1,... {
128   va_list   va;
129   NSString  *key;
130   WORequest *rq;
131
132   rq = [self request];
133
134   va_start(va, _key1);
135   {  
136     for (key = _key1; key; key = va_arg(va, NSString *)) {
137       NSString *value;
138
139       value = [rq formValueForKey:key];
140       [self takeValue:value forKey:key];
141     }
142   }
143   va_end(va);
144 }
145
146 - (void)takeFormValueArraysForKeyArray:(NSArray *)_keys {
147   NSEnumerator *keys;
148   NSString     *key;
149   WORequest    *rq;
150
151   rq   = [self request];
152   keys = [_keys objectEnumerator];
153
154   while ((key = [keys nextObject])) {
155     NSArray *value;
156
157     value = [rq formValuesForKey:key];
158     [self takeValue:value forKey:key];
159   }
160 }
161 - (void)takeFormValueArraysForKeys:(NSString *)_key1,... {
162   va_list   va;
163   NSString  *key;
164   WORequest *rq;
165
166   rq = [self request];
167   
168   va_start(va, _key1);
169   {  
170     for (key = _key1; key; key = va_arg(va, NSString *)) {
171       NSArray *value;
172
173       value = [rq formValuesForKey:key];
174       [self takeValue:value forKey:key];
175     }
176   }
177   va_end(va);
178 }
179
180 /* pages */
181
182 - (WOComponent *)pageWithName:(NSString *)_name {
183   return [[WOApplication application]
184                          pageWithName:_name
185                          inContext:[self context]];
186 }
187
188 /* logging */
189
190 - (NSString *)loggingPrefix {
191   return [NSString stringWithFormat:@">%@>", NSStringFromClass([self class])];
192 }
193 - (BOOL)isDebuggingEnabled {
194   static char showDebug = 2;
195   
196   if (showDebug == 2)
197     showDebug = [WOApplication isDebuggingEnabled] ? 1 : 0;
198   return showDebug ? YES : NO;
199 }
200
201 /* Key-Value coding */
202
203 - (void)takeValue:(id)_value forKey:(NSString *)_key {
204   if (!WOSetKVCValueUsingMethod(self, _key, _value))
205     [self handleTakeValue:_value forUnboundKey:_key];
206 }
207 - (id)valueForKey:(NSString *)_key {
208   return WOGetKVCValueUsingMethod(self, _key);
209 }
210
211 #if !LIB_FOUNDATION_LIBRARY
212
213 - (void)setValue:(id)_value forUndefinedKey:(NSString *)_key {
214   [self warnWithFormat:
215           @"tried to set value for undefined KVC key: '%@'", _key];
216 }
217 - (id)valueForUndefinedKey:(NSString *)_key {
218   [self warnWithFormat:
219              @"tried to access undefined KVC key: '%@'", _key];
220   return nil;
221 }
222
223 #endif
224
225 @end /* WODirectAction */