]> err.no Git - sope/blob - sope-appserver/WOExtensions/WODictionaryRepetition.m
updated framework version
[sope] / sope-appserver / WOExtensions / WODictionaryRepetition.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/NGObjWeb.h>
23
24 @interface WODictionaryRepetition : WODynamicElement
25 {
26 @protected
27   WOAssociation *dictionary;
28   WOAssociation *key;
29   WOAssociation *item;
30
31   WOElement *template;
32 }
33 @end
34
35 #include "common.h"
36
37 /* TODO: the implementation does not work with keys that contain a dot! */
38
39 @implementation WODictionaryRepetition
40
41 - (id)initWithName:(NSString *)_name
42   associations:(NSDictionary *)_config
43   template:(WOElement *)_temp
44 {
45   if ((self = [super initWithName:_name associations:_config template:_temp])) {
46     self->dictionary = WOExtGetProperty(_config, @"dictionary");
47     self->key        = WOExtGetProperty(_config, @"key");
48     self->item       = WOExtGetProperty(_config, @"item");
49
50     self->template = [_temp retain];
51   }
52   return self;
53 }
54
55 - (void)dealloc {
56   [self->dictionary release];
57   [self->key        release];
58   [self->item       release];
59   [self->template   release];
60   [super dealloc];
61 }
62
63 - (NSString *)unescapeKey:(NSString *)_key {
64   return _key;
65 }
66 - (NSString *)escapeKey:(NSString *)_key {
67   if ([_key rangeOfString:@"."].length == 0)
68     return _key;
69   
70   NSLog(@"WARNING(%s): key '%@' can't be processed by "
71         @"WODictionaryRepetition !!",
72         __PRETTY_FUNCTION__, _key);
73   return _key;
74 }
75
76 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
77   WOComponent  *comp;
78   NSDictionary *dict;
79   NSEnumerator *keyEnum;
80   NSString     *k;
81   BOOL         isMutable;
82   id           obj;
83   
84   comp    = [_ctx component];
85   dict    = [self->dictionary valueInComponent:comp];
86   keyEnum = [dict keyEnumerator];
87
88   isMutable = [dict isKindOfClass:[NSMutableDictionary class]];
89
90 #if 0
91   if (!isMutable) {
92     NSLog(@"WARNING: WODictionaryRepetition: 'dictionary' is immutable."
93           @" Cannot change values.");
94   }
95 #endif
96   
97   while ((k = [keyEnum nextObject])) {
98     
99     if ([self->key isValueSettable])
100       [self->key setValue:k inComponent:comp];
101     
102     [_ctx appendElementIDComponent:[self escapeKey:k]];
103     [self->template takeValuesFromRequest:_req inContext:_ctx];
104     [_ctx deleteLastElementIDComponent];
105
106     if (isMutable) {
107       obj = [self->item valueInComponent:comp];
108       if (obj) {
109         [(NSMutableDictionary *)dict setObject:obj forKey:k];
110       }
111       else
112         NSLog(@"WARNING: WODictionaryRepetition: nil object forKey: '%@'", k);
113     }
114   }
115 }
116
117 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
118   WOComponent  *comp;
119   NSDictionary *dict;
120   NSString     *k;
121   id           obj;
122   id           result = nil;
123
124   comp = [_ctx component];
125   dict = [self->dictionary valueInComponent:comp];
126   k    = [self unescapeKey:[_ctx currentElementID]];
127   
128   if (k) {
129     if ((obj = [dict objectForKey:k])) {
130       if ([self->item isValueSettable])
131         [self->item setValue:obj inComponent:comp];
132       if ([self->key isValueSettable])
133         [self->key setStringValue:k inComponent:comp];
134
135       [_ctx consumeElementID]; // consume k
136
137       [_ctx appendElementIDComponent:k];
138       result = [self->template invokeActionForRequest:_req inContext:_ctx];
139       [_ctx deleteLastElementIDComponent];
140     }
141     else 
142       NSLog(@"WARNING: WODictionaryRepetition nil object for key:'%@'", k);
143   }
144   return result;
145 }
146
147 - (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
148   WOComponent  *comp;
149   NSDictionary *dict;
150   NSEnumerator *keyEnum;
151   NSString     *k;
152
153   comp = [_ctx component];
154   dict = [self->dictionary valueInComponent:comp];
155   
156   keyEnum = [dict keyEnumerator];
157   
158   while ((k = [keyEnum nextObject])) {
159     if ([self->item isValueSettable])
160       [self->item setValue:[dict objectForKey:k] inComponent:comp];
161     if ([self->key isValueSettable])
162       [self->key setStringValue:k inComponent:comp];
163     
164     [_ctx appendElementIDComponent:[self escapeKey:k]];
165     [self->template appendToResponse:_resp inContext:_ctx];
166     [_ctx deleteLastElementIDComponent];
167   }
168 }
169   
170 @end /* WODictionaryRepetition */