annotate Lib/IMPL/Web/Application/Resource.pm @ 376:a54a2faf2f7e

added localizable string maps
author cin
date Mon, 13 Jan 2014 17:52:04 +0400
parents 441e84031c7b
children 2287c72f303a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
1 package IMPL::Web::Application::Resource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
2 use strict;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
3
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
4 use constant {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
5 ResourceClass => __PACKAGE__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
6 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
7 use Scalar::Util qw(blessed);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
8
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
9 use IMPL::lang qw(:hash :base);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
10 use IMPL::Const qw(:prop);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
11 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
12 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
13 Exception => 'IMPL::Exception',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
14 OpException => '-IMPL::InvalidOperationException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
15 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
16 ResourceInterface => '-IMPL::Web::Application',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
17 Loader => 'IMPL::Code::Loader'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
18 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
19 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
20 'IMPL::Object' => undef,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
21 'IMPL::Web::Application::ResourceBase' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
22 ],
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
23 props => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
24 access => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
25 verbs => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
26 children => PROP_RW
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
27 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
28 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
29
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
30 __PACKAGE__->static_accessor(verbNames => [qw(get post put delete options head)]);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
31 __PACKAGE__->static_accessor(httpMethodPrefix => 'Http');
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
32
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
33 sub CTOR {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
34 my ($this, %args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
35
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
36 my %verbs;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
37 my $httpPrefix = $this->httpMethodPrefix;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
38
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
39 foreach my $verb (@{$this->verbNames}) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
40 my $method = exists $args{$verb} ? $args{$verb} : $this->can($httpPrefix . ucfirst($verb));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
41 $verbs{$verb} = $method
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
42 if $method;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
43 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
44
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
45 hashApply(\%verbs,$args{verbs})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
46 if ref($args{verbs}) eq 'HASH' ;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
47
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
48 $this->children($args{children} || $this->GetChildResources());
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
49
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
50 $this->access($args{access})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
51 if $args{access};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
52
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
53 $this->verbs(\%verbs);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
54 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
55
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
56 sub _isInvokable {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
57 my ($this,$method) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
58
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
59 return
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
60 (blessed($method) and $method->can('Invoke')) ||
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
61 ref($method) eq 'CODE'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
62 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
63
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
64 sub _invoke {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
65 my ($this,$method,@args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
66
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
67 if(blessed($method) and $method->can('Invoke')) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
68 return $method->Invoke($this,@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
69 } elsif(ref($method) eq 'CODE' || (not(ref($method)) and $this->can($method))) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
70 return $this->$method(@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
71 } else {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
72 die OpException->new("Can't invoke the specified method: $method");
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
73 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
74 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
75
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
76 sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
77 shift->model;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
78 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
79
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
80 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
81 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
82
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
83 my $children = $this->children
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
84 or die NotFoundException->new( $this->location->url, $childId );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
85
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
86 if (ref($children) eq 'HASH') {
375
cin
parents: 374
diff changeset
87 if(my $child = $children->{$childId}) {
cin
parents: 374
diff changeset
88 return $this->_isInvokable($child) ? $this->_invoke($child, $childId) : $child;
cin
parents: 374
diff changeset
89 } else {
cin
parents: 374
diff changeset
90 die NotFoundException->new( $this->location->url, $childId );
cin
parents: 374
diff changeset
91 }
374
cin
parents: 373
diff changeset
92 } elsif($this->_isInvokable($children)) {
cin
parents: 373
diff changeset
93 return $this->_invoke($children,$childId);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
94 } else {
374
cin
parents: 373
diff changeset
95 die OpException->new("Invalid resource description", $childId, $children);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
96 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
97 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
98
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
99 sub FetchChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
100 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
101
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
102 my $info = $this->Fetch($childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
103
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
104 return $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
105 if (is($info,ResourceInterface));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
106
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
107 return $this->CreateChildResource($info, $childId)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
108 if ref($info) eq 'HASH';
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
109
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
110 die OpException->new("Invalid resource description", $childId, $info);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
111 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
112
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
113 sub CreateChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
114 my ($this,$info, $childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
115
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
116 my $params = hashApply(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
117 {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
118 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
119 id => $childId,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
120 request => $this->request,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
121 class => ResourceClass
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
122 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
123 $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
124 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
125
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
126 $params->{model} = $this->_invoke($params->{model})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
127 if $this->_isInvokable($params->{model});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
128
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
129 my $factory = Loader->default->Require($params->{class});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
130
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
131 return $factory->new(%$params);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
132 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
133
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
134 sub GetChildResources {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
135 return {};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
136 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
137
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
138 1;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
139
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
140 __END__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
141
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
142 =pod
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
143
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
144 =head1 NAME
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
145
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
146 C<IMPL::Web::Application::Resource> - Ресурс C<REST> веб приложения
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
147
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
148 =head1 SYNOPSIS
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
149
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
150 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
151
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
152 use IMPL::require {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
153 Resource => 'IMPL::Web::Application::Resource',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
154 Security => 'IMPL::Security',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
155 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
156 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
157 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
158
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
159 my $model = Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
160 get => sub { },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
161 verbs => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
162 # non-standart verbs placed here
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
163 myverb => sub { }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
164 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
165 #child resources can be a hash
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
166 chidren => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
167 user => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
168 # a resource class may be specified optionally
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
169 # class => Resource,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
170 model => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
171 return Security->principal
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
172 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
173 # the default get implementation is implied
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
174 # get => sub { shift->model },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
175 access => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
176 my ($this,$verb) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
177 die ForbiddenException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
178 if Security->principal->isNobody
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
179 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
180 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
181 catalog => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
182 get => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
183 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
184
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
185 return $ctx->products->find_rs({ in_stock => 1 });
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
186 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
187 # chid resource may be created dynamically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
188 children => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
189 # binds model against the parent reource and id
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
190 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
191
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
192 ($id) = ($id =~ /^(\w+)$/)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
193 or die NotFoundException->new($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
194
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
195 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
196
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
197 my $item = $ctx->products->fetch($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
198
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
199 die NotFoundException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
200 unless $item;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
201
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
202 # return parameters for the new resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
203 return {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
204 model => $item,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
205 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
206 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
207 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
208 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
209 # dynamically binds whole child resource. The result of binding is
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
210 # the new resource or a hash with arguments to create one
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
211 posts => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
212 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
213
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
214 # this approach can be used to create a dynamic resource relaying
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
215 # on the type of the model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
216
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
217 return Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
218 id => $id,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
219 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
220 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
221 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
222
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
223 # ditto
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
224 # parent and id will be mixed in automagically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
225 # return { get => sub { shift->model} }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
226 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
227 post_only => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
228 get => undef, # remove GET verb implicitly
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
229 post => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
230 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
231 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
232 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
233 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
234 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
235
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
236 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
237
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
238 Альтернативный вариант для создания класса ресурса.
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
239
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
240 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
241
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
242 package MyResource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
243
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
244 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
245 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
246 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
247 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
248 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
249 'IMPL::Web::Application::Resource' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
250 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
251 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
252
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
253 sub ds {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
254 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
255
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
256 $this->context->{ds} ||= $this->application->ConnectDb();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
257 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
258
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
259 sub InvokeHttpVerb {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
260 my $this = shift;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
261
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
262 $this->ds->Begin();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
263
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
264 my $result = $this->next::method(@_);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
265
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
266 # in case of error the data context will be disposed and the transaction
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
267 # will be reverted
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
268 $this->ds->Commit();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
269
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
270 return $result;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
271 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
272
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
273 # this method is inherited by default
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
274 # sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
275 # shift->model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
276 #
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
277 # }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
278
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
279 sub HttpPost {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
280 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
281
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
282 my %data = map {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
283 $_,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
284 $this->request->param($_)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
285 } qw(name description value);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
286
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
287 die ForbiddenException->new("The item with the scpecified name can't be created'")
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
288 if(not $data{name} or $this->ds->items->find({ name => $data{name}))
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
289
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
290 $this->ds->items->insert(\%data);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
291
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
292 return $this->NoContent();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
293 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
294
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
295 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
296 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
297
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
298 my $item = $this->ds->items->find({name => $childId})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
299 or die NotFoundException->new();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
300
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
301 # return parameters for the child resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
302 return { model => $item, role => "item food" };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
303 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
304
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
305 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
306
374
cin
parents: 373
diff changeset
307 =head1 MEMBERS
cin
parents: 373
diff changeset
308
375
cin
parents: 374
diff changeset
309 =head2 C<[get,set]verbs>
cin
parents: 374
diff changeset
310
cin
parents: 374
diff changeset
311 Хеш с C<HTTP> методами. При попытке вызова C<HTTP> метода, которого нет в этом
cin
parents: 374
diff changeset
312 хеше приводит к исключению C<IMPL::Web::NotAllowedException>.
cin
parents: 374
diff changeset
313
cin
parents: 374
diff changeset
314 =head2 C<[get,set]access>
cin
parents: 374
diff changeset
315
cin
parents: 374
diff changeset
316 Метод для проверки прав доступа. Если не задан, то доспуп возможен для всех.
cin
parents: 374
diff changeset
317
cin
parents: 374
diff changeset
318 =head2 C<[get,set]children>
cin
parents: 374
diff changeset
319
cin
parents: 374
diff changeset
320 Дочерние ресурсы. Дочерние ресурсы могут быть описаны либо в виде хеша, либо
cin
parents: 374
diff changeset
321 в виде метода.
cin
parents: 374
diff changeset
322
cin
parents: 374
diff changeset
323 =head3 C<HASH>
cin
parents: 374
diff changeset
324
cin
parents: 374
diff changeset
325 Данный хещ содержит в себе таблицу идентификаторов дочерних ресурсов и их
cin
parents: 374
diff changeset
326 описаний.
cin
parents: 374
diff changeset
327
cin
parents: 374
diff changeset
328 Описание каждого ресурса представляет собой либо функцию, либо параметры для
cin
parents: 374
diff changeset
329 создания ресурса C<CraeteChildResource>. Если описание в виде функции, то она
cin
parents: 374
diff changeset
330 должна возвращать либо объект типа ресурс либо параметры для его создания.
cin
parents: 374
diff changeset
331
cin
parents: 374
diff changeset
332 =head3 C<CODE>
cin
parents: 374
diff changeset
333
cin
parents: 374
diff changeset
334 Если дочерние ресурсы описаны в виде функции (возможно использовать имя метода
cin
parents: 374
diff changeset
335 класса текущего ресурса), то для получения дочернего ресурса будет вызвана
cin
parents: 374
diff changeset
336 функция с параметрами C<($this,$childId)>, где C<$this> - текущий ресурс,
cin
parents: 374
diff changeset
337 C<$childId> - идентификатор дочернего ресурса, который нужно вернуть.
cin
parents: 374
diff changeset
338
cin
parents: 374
diff changeset
339 Данная функция должна возвратить либо объект типа ресурс, либо ссылку на хеш с
cin
parents: 374
diff changeset
340 параметрами для создания оного при помощи метода
cin
parents: 374
diff changeset
341 C<CreateChildResource($params,$childId)>.
cin
parents: 374
diff changeset
342
374
cin
parents: 373
diff changeset
343 =head2 C<[virtual]Fetch($childId)>
cin
parents: 373
diff changeset
344
cin
parents: 373
diff changeset
345 Метод для получения дочернего ресурса.
cin
parents: 373
diff changeset
346
cin
parents: 373
diff changeset
347 Возвращает параметры для создания дочернего ресурса, либо уже созданный ресурс.
cin
parents: 373
diff changeset
348 Создание дочернего ресурса происходит при помощи метода C<CreateChildResource()>
cin
parents: 373
diff changeset
349 который добавляет недостающие параметры к возвращенным в данным методом и
cin
parents: 373
diff changeset
350 создает новый ресурс
cin
parents: 373
diff changeset
351
375
cin
parents: 374
diff changeset
352 =head2 C<CreateChildResource($params,$id)>
cin
parents: 374
diff changeset
353
cin
parents: 374
diff changeset
354 Создает новый дочерний ресурс с указанным идентификатором и параметрами.
cin
parents: 374
diff changeset
355 Автоматически заполняет параметры
cin
parents: 374
diff changeset
356
cin
parents: 374
diff changeset
357 =over
cin
parents: 374
diff changeset
358
cin
parents: 374
diff changeset
359 =item * C<parent>
cin
parents: 374
diff changeset
360
cin
parents: 374
diff changeset
361 =item * C<id>
cin
parents: 374
diff changeset
362
cin
parents: 374
diff changeset
363 =item * C<request>
cin
parents: 374
diff changeset
364
cin
parents: 374
diff changeset
365 =back
cin
parents: 374
diff changeset
366
cin
parents: 374
diff changeset
367 Тип создаваемого ресурса C<IMPL::Web::Application::Resource>, либо указывается
cin
parents: 374
diff changeset
368 в параметре C<class>.
cin
parents: 374
diff changeset
369
cin
parents: 374
diff changeset
370 =head2 C<[virtual]HttpGet()>
cin
parents: 374
diff changeset
371
cin
parents: 374
diff changeset
372 Реализует C<HTTP> метод C<GET>. По-умолчанию возвращает модель.
cin
parents: 374
diff changeset
373
cin
parents: 374
diff changeset
374 Данный метод нужен для того, чтобы ресурс по-умолчанию поддерживал метод C<GET>,
cin
parents: 374
diff changeset
375 что является самым частым случаем, если нужно изменить данное поведение, нужно:
cin
parents: 374
diff changeset
376
cin
parents: 374
diff changeset
377 =over
cin
parents: 374
diff changeset
378
cin
parents: 374
diff changeset
379 =item * Передать в параметр конструктора C<get> значение undef
cin
parents: 374
diff changeset
380
cin
parents: 374
diff changeset
381 =item * Переопределить метод C<HttpGet>
cin
parents: 374
diff changeset
382
cin
parents: 374
diff changeset
383 =item * При проверке прав доступа выдать исключение
cin
parents: 374
diff changeset
384
cin
parents: 374
diff changeset
385 =back
374
cin
parents: 373
diff changeset
386
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
387 =cut
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
388