annotate Lib/IMPL/Web/Application/ControllerUnit.pm @ 111:6c25ea91c985

ControllerUnit concept
author wizard
date Tue, 18 May 2010 01:33:37 +0400
parents c13a215508ca
children 0ed8e2541b1c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
c13a215508ca Refactoring,
wizard
parents:
diff changeset
1 package IMPL::Web::Application::ControllerUnit;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
2
c13a215508ca Refactoring,
wizard
parents:
diff changeset
3 use base qw(IMPL::Object);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
4
c13a215508ca Refactoring,
wizard
parents:
diff changeset
5 use IMPL::Class::Property;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
6
c13a215508ca Refactoring,
wizard
parents:
diff changeset
7 BEGIN {
c13a215508ca Refactoring,
wizard
parents:
diff changeset
8 public property action => prop_get | owner_set;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
9 public property application => prop_get | owner_set;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
10 public property query => prop_get | owner_set;
111
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
11 public property formData => prop_get | owner_set;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
12 public property formSchema => prop_get | owner_set;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
13 public property formErrors => prop_get | owner_set;
110
c13a215508ca Refactoring,
wizard
parents:
diff changeset
14 }
c13a215508ca Refactoring,
wizard
parents:
diff changeset
15
c13a215508ca Refactoring,
wizard
parents:
diff changeset
16 sub CTOR {
c13a215508ca Refactoring,
wizard
parents:
diff changeset
17 my ($this,$action) = @_;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
18
c13a215508ca Refactoring,
wizard
parents:
diff changeset
19 $this->action($action);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
20 $this->application($action->application);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
21 $this->query($action->query);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
22 }
c13a215508ca Refactoring,
wizard
parents:
diff changeset
23
c13a215508ca Refactoring,
wizard
parents:
diff changeset
24 sub InvokeAction {
c13a215508ca Refactoring,
wizard
parents:
diff changeset
25 my ($self,$method,$action) = @_;
c13a215508ca Refactoring,
wizard
parents:
diff changeset
26
c13a215508ca Refactoring,
wizard
parents:
diff changeset
27 if ($self->can($method)) {
c13a215508ca Refactoring,
wizard
parents:
diff changeset
28 my $unit = $self->new($action);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
29 $unit->$method();
c13a215508ca Refactoring,
wizard
parents:
diff changeset
30 } else {
c13a215508ca Refactoring,
wizard
parents:
diff changeset
31 die new IMPL::InvalidOperationException("Invalid method call",$self,$method);
c13a215508ca Refactoring,
wizard
parents:
diff changeset
32 }
111
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
33 }
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
34
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
35 1;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
36
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
37 __END__
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
38
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
39 =pod
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
40
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
41 =head1 NAME
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
42
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
43 C<IMPL::Web::Application::ControllerUnit> - базовый класс для обработчика транзакций в модели контроллера.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
44
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
45 =head1 DESCRIPTION
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
46
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
47 Классы, наследуемые от данного класса используются для выполнения транзакций, которые приходят
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
48 через контроллер запросов. Как правило один класс представляет собой пакет транзакций, каждая
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
49 из которых является независимой от другой.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
50
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
51 Перед выполнением транзакции создается экземпляр объекта, в рамках которого будет выполнена транзакция.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
52 Для этого вызывается метод C<InvokeAction($method,$action)>, который создает/восстанавливает контекст
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
53 транзакции.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
54
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
55 Транзакции на данный момент делятся на простые и формы.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
56
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
57 =head2 Простые транзакции
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
58
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
59 Простые транзакции получаю только запрос, без предварительной обработки, и возвращенный результат напрямую
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
60 передается пользователю.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
61
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
62 =head2 Формы
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
63
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
64 При использовании форм запрос предварительно обрабатывается, для получения DOM документа с данными формы.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
65 Для постороенния DOM документа используется схема. При этом становятся доступны дополнительные свойства
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
66 C<formData>, C<formSchema>, C<formErrors>.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
67
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
68 Результат выполнения транзакции не возвращается напрямую пользователю, а включается в общий ответ, который
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
69 выглядит следующим образом
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
70
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
71 =begin code
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
72
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
73 {
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
74 state => '{ new | correct | invalid }',
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
75 result => $transactionResult,
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
76 formData => $formDOM,
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
77 formSchema => $formSchema,
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
78 formErrors => @errors
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
79 }
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
80
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
81 =end code
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
82
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
83 =over
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
84
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
85 =item C<state>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
86
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
87 Состояние верификации формы.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
88
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
89 =over
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
90
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
91 =item C<new>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
92
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
93 Первоначальное содержимое формы, оно может быть некорректным, но это нормально.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
94 В данном состоянии транзакция обычно не выполняется.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
95
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
96 =item C<correct>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
97
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
98 Данные формы корректны, транзакция выполнена, и ее результат доступен через поле C<result>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
99
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
100 =item C<invalid>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
101
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
102 Содержимое формы не прошло верификацию, ошибки доступны через поле C<formErrors>. Транзакция
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
103 не выполнялась.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
104
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
105 =back
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
106
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
107 =item C<result>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
108
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
109 Результат выполнения транзакции, если конечно таковая выполнялась.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
110
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
111 =item C<formData>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
112
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
113 ДОМ документ с данными формами. Документ существует всегда, не зависимо от его корректности,
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
114 может быть использован для построения формы, уже заполненную параметрами.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
115
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
116 =item C<formSchema>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
117
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
118 Схема данных формы, может использоваться для построения динамических форм.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
119
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
120 =item C<formErrors>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
121
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
122 Ошибки верификации данных, если таковые были
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
123
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
124 =back
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
125
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
126 =head1 MEMBERS
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
127
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
128 =over
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
129
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
130 =item C<[get] application>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
131
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
132 =item C<[get] query>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
133
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
134 =item C<[get] response>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
135
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
136 =item C<[get] formData>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
137
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
138 =item C<[get] formSchema>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
139
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
140 =item C<[get] formErrors>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
141
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
142 =item C<InvokeAction($method,$action)>
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
143
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
144 Конструирует контекст выполнения транзакции, может быть переопределен для конструирования контекста по
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
145 своимправилам.
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
146
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
147 =back
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
148
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
149 =head1 EXAMPLE
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
150
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
151 =begin code
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
152
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
153 package MyBooksUnit;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
154 use strict;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
155 use base qw(IMPL::Web::Application::ControllerUnit);
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
156
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
157 __PACKAGE__->PassThroughArgs;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
158
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
159 __PACKAGE__->transactions(qw(
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
160 find
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
161 info
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
162 ));
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
163 __PACKAGE__->forms(
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
164 create => 'books.create.xml'
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
165 );
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
166
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
167 sub find {
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
168 my ($this) = @_;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
169
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
170 return $this->application->dataSource->resultset('Books')->find({author => $this->query->param('author')});
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
171 }
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
172
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
173 sub info {
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
174 my ($this) = @_;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
175
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
176 return $this->application->dataSource->resultset('Books')->find({id => $this->query->param('id')});
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
177 }
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
178
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
179 sub create {
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
180 my ($this) = @_;
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
181
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
182 my %book = map {
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
183 $_ => $this->formData->selectSingleNode($_)->nodeValue
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
184 } qw(author_id title year ISBN);
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
185
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
186 return $this->application->datasource->resultset('Books')->create(\%book);
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
187 }
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
188
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
189 =end code
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
190
6c25ea91c985 ControllerUnit concept
wizard
parents: 110
diff changeset
191 =cut