annotate Lib/IMPL/Web/Application/OperationContract.pm @ 229:47f77e6409f7

heavily reworked the resource model of the web application: *some ResourcesContraact functionality moved to Resource +Added CustomResource *Corrected action handlers
author sergey
date Sat, 29 Sep 2012 02:34:47 +0400
parents 431db7034a88
children 6d8092d8ce1b
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 => {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
7 'Exception' => 'IMPL::Exception',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
8 'ArgumentException' => '-IMPL::ArgumentException',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
9 'ResourceBaseClass' => 'IMPL::Web::Application::ResourceBase'
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
25 die ArgumentException( resource => 'A valid resource is required' )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
26 unless eval { $resource->isa(ResourceBaseClass) };
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
27
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 = $@) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
33 $result = _InvokeDelegate($this->error, $resource, $request, $e);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
34 } else {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
35 $result = _InvokeDelegate($this->success, $resource, $request, $result);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
36 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
37
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
38 return $result;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
39 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
40
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
41 sub _InvokeDelegate {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
42 my $delegate = shift;
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
43
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
44 return $delegete->(@_) if ref $delegate eq 'CODE';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
45 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')};
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
46 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
47
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
48 1;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
49
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
50 __END__
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
51
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
52 =pod
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
53
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
54 =head1 NAME
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
55
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
56 C<IMPL::Web::Application::OperationContract> - Описание операции над
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
57 веб-ресурсом.
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
58
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
59 =head1 SYNOPSIS
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
60
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
61 =begin code
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
62
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
63 use IMPL::require {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
64 'OperationContract' => 'IMPL::Web::Application::OperationContract',
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
65 'RedirectResponse' => 'IMPL::Web::Application::RedirectResponse'
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
66 };
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
67
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
68 my $operation = OperationContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
69 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
70 my ($resource,$request) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
71
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
72 my $itemName = $request->param('itemName', qr/^(\w+)$/);
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
73
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
74 return $model->FindItem($itemName);
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
75 },
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
76 success => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
77 my ($resource,$request,$result) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
78 return HttpReponse->Redirect(location => $resource->location->Child($result->id));
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
79 }
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
80 );
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
81
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
82 my $response = $operation->InvokeOperation($resource);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
83
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
84 =end code
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
85
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
86 =head1 DESCRIPTION
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
87
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
88 Для орисания контракта операции используется понятие делегата, тоесть объекта,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
89 представляющего собой функцию, либо объект, имеющий метод C<Invoke>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
90
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
91 Поскольку предметная область должна быть отделена от
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
92 контроллеров веб-сервиса, она ничего не знает про существование ресурсов и их
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
93 организацию и тем более о протоколе C<HTTP>, поэтому все вещи, связанные с
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 самими контроллерами. Поведение контроллеров описывается контрактами, в которых
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 Контракт операции состоит из нескольких свойств, осуществляющих привязку к
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
100 предметной области:
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
101
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
102 =over
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
103
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
104 =item * C<binding>
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
105
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
106 делегат для привязки операции над ресурсом к предметной области.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
107
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
108 =item * C<success>
228
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 делегат для обработки результат операции, например для формирования ответа с
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
111 перенаправлением.
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 =item * C<error>
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
114
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
115 делегат для обработки исключительной ситуации, может быть использован для
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
116 формирования представления для повторного ввода данных на форме.
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
117
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
118 =back
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
119
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
120 =head1 MEMBERS
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents:
diff changeset
121
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
122 =head2 C<[get,set] binding>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
123
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
124 Привязка операции к ресурсу, например
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
125
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
126 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
127
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
128 $operationContract->binding(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
129 my ($resource,$action) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
130 $resource->model
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
133 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
134
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
135 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
136 C<Invoke>.
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 =head2 C<[get,set] success>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
139
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 =begin code
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 # redirect (for example after POST)
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
145 $operationContract->success(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
146 my ($resource,$action,$result) = @_;
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 return IMPL::Web::HttpResponse
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
149 ->Redirect($resource->location->Child($result->id));
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
150 })
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
151
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
152 =end code
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 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
155 C<Invoke>.
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 =head2 C<[get,set] error>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
158
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 =begin
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 $operationContract->error(sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
164 my ($resource,$action,$error) = @_;
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 $action->form->errors->{''} = $error;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
167
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
168 return $resource->model;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
169 });
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
170
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
171 =end
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
172
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
173 =cut