annotate Lib/IMPL/Web/Application/OperationContract.pm @ 245:7c517134c42f

Added Unsupported media type Web exception corrected resourceLocation setting in the resource Implemented localizable resources for text messages fixed TT view scopings, INIT block in controls now sets globals correctly.
author sergey
date Mon, 29 Oct 2012 03:15:22 +0400
parents 6d8092d8ce1b
children 4abda21186cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
1 package IMPL::Web::Application::OperationContract;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
2 use strict;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
3
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
4 use IMPL::lang qw(:declare);
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
5 use IMPL::declare {
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
6 require => {
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
7 Exception => 'IMPL::Exception',
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
8 ArgumentException => '-IMPL::InvalidArgumentException',
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
9 ResourceInterface => 'IMPL::Web::Application::ResourceInterface'
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
10 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
11 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
12 'IMPL::Object' => undef,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
13 'IMPL::Object::Autofill' => '@_'
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
14 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
15 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
16 binding => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
17 success => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
18 error => PROP_ALL
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
19 ]
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
20 };
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
21
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
22 sub Invoke {
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
23 my ( $this, $resource, $request ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
24
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
25 die ArgumentException->new( resource => 'A valid resource is required' )
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
26 unless eval { $resource->isa(ResourceInterface) };
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
27
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
28 my $result = eval {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
29 _InvokeDelegate($this->binding, $resource, $request)
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
30 };
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
31
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
32 if (my $e = $@) {
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
33 if ($this->error) {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
34 $result = _InvokeDelegate($this->error, $resource, $request, $e) ;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
35 } else {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
36 die $e;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
37 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
38
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
39 } else {
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
40 $result = _InvokeDelegate($this->success, $resource, $request, $result)
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
41 if ($this->success);
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
42 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
43
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
44 return $result;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
45 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
46
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
47 sub _InvokeDelegate {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
48 my $delegate = shift;
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
49
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
50 return $delegate->(@_) if ref $delegate eq 'CODE';
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
51 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')};
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
52 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
53
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
54 1;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
55
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
56 __END__
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
57
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
58 =pod
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
59
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
60 =head1 NAME
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
61
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
62 C<IMPL::Web::Application::OperationContract> - Описание операции над
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
63 веб-ресурсом.
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
64
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
65 =head1 SYNOPSIS
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
66
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
67 =begin code
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
68
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
69 use IMPL::require {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
70 'OperationContract' => 'IMPL::Web::Application::OperationContract',
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
71 'RedirectResponse' => 'IMPL::Web::Application::RedirectResponse'
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
72 };
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
73
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
74 my $operation = OperationContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
75 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
76 my ($resource,$request) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
77
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
78 my $itemName = $request->param('itemName', qr/^(\w+)$/);
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
79
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
80 return $model->FindItem($itemName);
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
81 },
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
82 success => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
83 my ($resource,$request,$result) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
84 return HttpReponse->Redirect(location => $resource->location->Child($result->id));
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
85 }
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
86 );
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
87
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
88 my $response = $operation->InvokeOperation($resource);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
89
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
90 =end code
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
91
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
92 =head1 DESCRIPTION
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
93
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
94 Для орисания контракта операции используется понятие делегата, тоесть объекта,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
95 представляющего собой функцию, либо объект, имеющий метод C<Invoke>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
96
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
97 Поскольку предметная область должна быть отделена от
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
98 контроллеров веб-сервиса, она ничего не знает про существование ресурсов и их
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
99 организацию и тем более о протоколе C<HTTP>, поэтому все вещи, связанные с
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
100 формированием ответов сервера, представлениями данных и т.п. должны выполняться
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
101 самими контроллерами. Поведение контроллеров описывается контрактами, в которых
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
102 указываются делегаты для реализации необходимого функционала, для корректного
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
103 отображения ресурсов в объекты предметной области и обратно.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
104
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
105 Контракт операции состоит из нескольких свойств, осуществляющих привязку к
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
106 предметной области:
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
107
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
108 =over
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
109
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
110 =item * C<binding>
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
111
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
112 делегат для привязки операции над ресурсом к предметной области.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
113
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
114 =item * C<success>
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
115
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
116 делегат для обработки результат операции, например для формирования ответа с
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
117 перенаправлением.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
118
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
119 =item * C<error>
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
120
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
121 делегат для обработки исключительной ситуации, может быть использован для
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
122 формирования представления для повторного ввода данных на форме.
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
123
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
124 =back
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
125
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
126 =head1 MEMBERS
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
127
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
128 =head2 C<[get,set] binding>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
129
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
130 Привязка операции к ресурсу, например
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
131
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
132 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
133
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
134 $operationContract->binding(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
135 my ($resource,$action) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
136 $resource->model
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
137 })
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
138
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
139 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
140
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
141 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
142 C<Invoke>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
143
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
144 =head2 C<[get,set] success>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
145
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
146 Обрабатывает результат привязки к предметной области.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
147
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
148 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
149
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
150 # redirect (for example after POST)
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
151 $operationContract->success(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
152 my ($resource,$action,$result) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
153
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
154 return IMPL::Web::HttpResponse
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
155 ->Redirect($resource->location->Child($result->id));
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
156 })
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
157
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
158 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
159
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
160 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
161 C<Invoke>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
162
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
163 =head2 C<[get,set] error>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
164
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
165 Обрабатывает ошибку возникшую при выполнении привязки к предметной области.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
166
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
167 =begin
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
168
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
169 $operationContract->error(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
170 my ($resource,$action,$error) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
171
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
172 $action->form->errors->{''} = $error;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
173
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
174 return $resource->model;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
175 });
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
176
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
177 =end
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
178
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
179 =cut