Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/ControllerUnit.pm @ 171:59e5fcb59d86
Исправления, изменена концепция веб-форм
| author | sourcer |
|---|---|
| date | Mon, 06 Jun 2011 03:30:36 +0400 |
| parents | b88b7fe60aa3 |
| children | 068acfe903c3 |
| rev | line source |
|---|---|
| 133 | 1 use strict; |
| 110 | 2 package IMPL::Web::Application::ControllerUnit; |
| 166 | 3 use parent qw(IMPL::Object); |
| 110 | 4 |
| 5 use IMPL::Class::Property; | |
| 112 | 6 use IMPL::DOM::Transform::PostToDOM; |
| 7 use IMPL::DOM::Schema; | |
| 113 | 8 use Class::Inspector; |
| 9 use File::Spec; | |
| 133 | 10 use Sub::Name; |
| 112 | 11 |
| 12 use constant { | |
| 13 CONTROLLER_METHODS => 'controller_methods', | |
| 14 STATE_CORRECT => 'correct', | |
| 15 STATE_NEW => 'new', | |
| 16 STATE_INVALID => 'invalid' | |
| 17 }; | |
| 110 | 18 |
| 19 BEGIN { | |
| 20 public property action => prop_get | owner_set; | |
| 21 public property application => prop_get | owner_set; | |
| 22 public property query => prop_get | owner_set; | |
|
144
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
142
diff
changeset
|
23 public property response => prop_get | owner_set; |
| 111 | 24 public property formData => prop_get | owner_set; |
| 25 public property formSchema => prop_get | owner_set; | |
| 26 public property formErrors => prop_get | owner_set; | |
| 110 | 27 } |
| 28 | |
| 170 | 29 my %publicProps = map {$_->Name , 1} __PACKAGE__->get_meta(typeof IMPL::Class::PropertyInfo); |
| 133 | 30 |
| 113 | 31 __PACKAGE__->class_data(CONTROLLER_METHODS,{}); |
| 32 | |
| 170 | 33 our @schemaInc; |
| 34 | |
| 110 | 35 sub CTOR { |
| 112 | 36 my ($this,$action,$args) = @_; |
| 110 | 37 |
| 38 $this->action($action); | |
| 39 $this->application($action->application); | |
| 40 $this->query($action->query); | |
|
144
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
142
diff
changeset
|
41 $this->response($action->response); |
| 112 | 42 |
| 43 $this->$_($args->{$_}) foreach qw(formData formSchema formErrors); | |
| 44 } | |
| 45 | |
| 134 | 46 sub unitNamespace() { |
| 47 "" | |
| 48 } | |
| 49 | |
| 50 sub transactions { | |
| 51 my ($self,%methods) = @_; | |
| 52 | |
| 53 while (my ($method,$info) = each %methods) { | |
| 54 if ($info and ref $info ne 'HASH') { | |
| 55 warn "Bad transaction $method description"; | |
| 56 $info = {}; | |
| 57 } | |
| 58 | |
| 59 $info->{wrapper} = 'TransactionWrapper'; | |
| 60 $self->class_data(CONTROLLER_METHODS)->{$method} = $info; | |
| 61 } | |
| 62 } | |
| 63 | |
| 112 | 64 sub forms { |
| 65 my ($self,%forms) = @_; | |
| 66 | |
| 67 while ( my ($method,$info) = each %forms ) { | |
| 68 die new IMPL::Exception("A method doesn't exists in the controller",$self,$method) unless $self->can($method); | |
| 69 if ( not ref $info ) { | |
| 70 $self->class_data(CONTROLLER_METHODS)->{$method} = { | |
| 71 wrapper => 'FormWrapper', | |
| 72 schema => $info | |
| 73 }; | |
| 74 } elsif (ref $info eq 'HASH') { | |
|
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
146
diff
changeset
|
75 $info->{wrapper} = 'FormWrapper'; |
|
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
146
diff
changeset
|
76 |
|
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
146
diff
changeset
|
77 $self->class_data(CONTROLLER_METHODS)->{$method} = $info; |
| 112 | 78 } else { |
| 79 die new IMPL::Exception("Unsupported method information",$self,$method); | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 | |
| 110 | 84 sub InvokeAction { |
| 85 my ($self,$method,$action) = @_; | |
| 86 | |
| 112 | 87 if (my $methodInfo = $self->class_data(CONTROLLER_METHODS)->{$method}) { |
| 88 if (my $wrapper = $methodInfo->{wrapper}) { | |
| 89 return $self->$wrapper($method,$action,$methodInfo); | |
| 90 } else { | |
| 91 return $self->TransactionWrapper($method,$action,$methodInfo); | |
| 92 } | |
| 110 | 93 } else { |
| 94 die new IMPL::InvalidOperationException("Invalid method call",$self,$method); | |
| 95 } | |
| 111 | 96 } |
| 97 | |
| 133 | 98 sub MakeParams { |
| 99 my ($this,$methodInfo) = @_; | |
| 100 | |
| 101 my $params; | |
| 102 if ($params = $methodInfo->{parameters} and ref $params eq 'ARRAY') { | |
| 146 | 103 return map $this->ResolveParam($_,$methodInfo->{inflate}{$_}), @$params; |
| 133 | 104 } |
| 105 return(); | |
| 106 } | |
| 107 | |
| 108 sub ResolveParam { | |
| 146 | 109 my ($this,$param,$inflate) = @_; |
| 133 | 110 |
| 111 if ( $param =~ /^::(\w+)$/ and $publicProps{$1}) { | |
| 112 return $this->$1(); | |
| 113 } else { | |
| 146 | 114 my $value; |
| 115 if ( my $rx = $inflate->{rx} ) { | |
| 116 $value = $this->action->param($param,$rx); | |
| 117 } else { | |
| 118 $value = $this->query->param($param); | |
| 119 } | |
| 120 | |
| 121 if (my $method = $inflate->{method}) { | |
| 122 $value = $this->$method($value); | |
| 123 } | |
| 124 return $value; | |
| 133 | 125 } |
| 126 } | |
| 127 | |
| 112 | 128 sub TransactionWrapper { |
| 129 my ($self,$method,$action,$methodInfo) = @_; | |
| 130 | |
| 131 my $unit = $self->new($action); | |
| 133 | 132 return $unit->$method($unit->MakeParams($methodInfo)); |
| 112 | 133 } |
| 134 | |
| 135 sub FormWrapper { | |
| 113 | 136 my ($self,$method,$action,$methodInfo) = @_; |
| 137 | |
| 171 | 138 my $schema = $methodInfo->{schema} ? $self->loadSchema($methodInfo->{schema}) : $self->unitSchema; |
| 113 | 139 |
| 140 my $process = $action->query->param('process') || 0; | |
| 141 my $form = $methodInfo->{form} | |
| 142 || $action->query->param('form') | |
| 171 | 143 || $method; |
| 112 | 144 |
| 113 | 145 my %result; |
| 112 | 146 |
| 113 | 147 my $transform = IMPL::DOM::Transform::PostToDOM->new( |
| 148 undef, | |
| 149 $schema, | |
| 150 $form | |
| 151 ); | |
| 152 | |
| 138 | 153 $result{formName} = $form; |
| 113 | 154 $result{formSchema} = $schema; |
| 112 | 155 |
| 113 | 156 if ($process) { |
| 171 | 157 $result{formData} = $transform->Transform($action->query); |
| 113 | 158 $result{formErrors} = $transform->Errors->as_list; |
| 159 if ($transform->Errors->Count) { | |
| 160 $result{state} = STATE_INVALID; | |
| 161 } else { | |
| 162 $result{state} = STATE_CORRECT; | |
| 163 my $unit = $self->new($action,\%result); | |
|
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
164 |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
165 eval { |
| 133 | 166 $result{result} = $unit->$method($unit->MakeParams($methodInfo)); |
|
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
167 }; |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
168 if (my $err = $@) { |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
169 $result{state} = STATE_INVALID; |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
170 if (eval { $err->isa(typeof IMPL::WrongDataException) } ) { |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
171 $result{formErrors} = $err->Args; |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
172 } else { |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
173 die $err; |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
174 } |
|
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
126
diff
changeset
|
175 } |
| 113 | 176 } |
| 177 } else { | |
| 171 | 178 if (my $initMethod = $methodInfo->{init}) { |
| 179 my $unit = $self->new($action,\%result); | |
| 180 $result{formData} = $transform->Transform( $unit->$initMethod($unit->MakeParams($methodInfo)) ); | |
| 181 } else { | |
| 182 $result{formData} = $transform->Transform($action->query); | |
| 183 } | |
| 184 | |
| 185 $result{formErrors} = $transform->Errors->as_list; | |
| 113 | 186 $result{state} = STATE_NEW; |
| 187 } | |
| 112 | 188 |
| 113 | 189 return \%result; |
| 190 } | |
| 191 | |
| 192 sub loadSchema { | |
| 193 my ($self,$name) = @_; | |
| 170 | 194 |
| 195 foreach my $path (map File::Spec->catfile($_,$name) ,@schemaInc) { | |
| 196 return IMPL::DOM::Schema->LoadSchema($path) if -f $path; | |
| 197 } | |
| 113 | 198 |
| 170 | 199 die new IMPL::Exception("A schema isn't found", $name); |
| 200 } | |
| 201 | |
| 202 sub unitSchema { | |
| 203 my ($self) = @_; | |
| 204 | |
| 205 my $class = ref $self || $self; | |
| 206 | |
| 207 my @parts = split(/:+/, $class); | |
| 208 | |
| 209 my $file = pop @parts; | |
| 210 $file = "${file}.schema.xml"; | |
| 211 | |
| 212 foreach my $inc ( @schemaInc ) { | |
| 213 my $path = File::Spec->catfile($inc,@parts,$file); | |
| 126 | 214 |
| 170 | 215 return IMPL::DOM::Schema->LoadSchema($path) if -f $path; |
| 126 | 216 } |
| 170 | 217 |
| 218 return undef; | |
| 112 | 219 } |
| 220 | |
| 134 | 221 sub discover { |
| 222 my ($this) = @_; | |
| 223 | |
| 224 my $methods = $this->class_data(CONTROLLER_METHODS); | |
| 225 | |
| 226 my $namespace = $this->unitNamespace; | |
| 227 (my $module = typeof $this) =~ s/^$namespace//; | |
| 133 | 228 |
| 134 | 229 my %smd = ( |
| 230 module => [grep $_, split /::/, $module ], | |
| 231 ); | |
| 133 | 232 |
| 134 | 233 while (my ($method,$info) = each %$methods) { |
| 234 my %methodInfo = ( | |
| 235 name => $method | |
| 236 ); | |
| 160 | 237 $methodInfo{parameters} = [ grep /^[^\:]/, @{ $info->{parameters} } ] if ref $info->{parameters} eq 'ARRAY'; |
| 134 | 238 push @{$smd{methods}},\%methodInfo; |
| 239 } | |
| 240 return \%smd; | |
| 133 | 241 } |
| 242 | |
| 134 | 243 __PACKAGE__->transactions( |
| 244 discover => undef | |
| 245 ); | |
| 133 | 246 |
| 111 | 247 1; |
| 248 | |
| 249 __END__ | |
| 250 | |
| 251 =pod | |
| 252 | |
| 253 =head1 NAME | |
| 254 | |
| 255 C<IMPL::Web::Application::ControllerUnit> - . | |
| 256 | |
| 257 =head1 DESCRIPTION | |
| 258 | |
| 113 | 259 , . |
| 260 C<transaction>, C<form>. | |
| 111 | 261 |
| 262 , . | |
| 263 C<InvokeAction($method,$action)>, / | |
| 264 . | |
| 265 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
266 . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
267 (C<TransactionWrapper> C<FormWrapper>). |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
268 , . |
| 111 | 269 |
| 270 =head2 | |
| 271 | |
| 272 , , | |
| 273 . | |
| 274 | |
| 275 =head2 | |
| 276 | |
| 277 , DOM . | |
| 278 DOM . | |
| 279 C<formData>, C<formSchema>, C<formErrors>. | |
| 280 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
281 , , |
| 111 | 282 |
| 283 | |
| 284 =begin code | |
| 285 | |
| 286 { | |
| 287 state => '{ new | correct | invalid }', | |
| 288 result => $transactionResult, | |
| 289 formData => $formDOM, | |
| 290 formSchema => $formSchema, | |
| 291 formErrors => @errors | |
| 292 } | |
| 293 | |
| 294 =end code | |
| 295 | |
| 296 =over | |
| 297 | |
| 298 =item C<state> | |
| 299 | |
| 300 . | |
| 301 | |
| 302 =over | |
| 303 | |
| 304 =item C<new> | |
| 305 | |
| 306 , , . | |
| 307 . | |
| 308 | |
| 309 =item C<correct> | |
| 310 | |
| 311 , , C<result> | |
| 312 | |
| 313 =item C<invalid> | |
| 314 | |
| 315 , C<formErrors>. | |
| 316 . | |
| 317 | |
| 318 =back | |
| 319 | |
| 320 =item C<result> | |
| 321 | |
| 322 , . | |
| 323 | |
| 324 =item C<formData> | |
| 325 | |
| 326 . , , | |
| 327 , . | |
| 328 | |
| 329 =item C<formSchema> | |
| 330 | |
| 331 , . | |
| 332 | |
| 333 =item C<formErrors> | |
| 334 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
335 . |
| 111 | 336 |
| 337 =back | |
| 338 | |
| 339 =head1 MEMBERS | |
| 340 | |
| 341 =over | |
| 342 | |
| 343 =item C<[get] application> | |
| 344 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
345 , . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
346 |
| 111 | 347 =item C<[get] query> |
| 348 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
349 . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
350 |
| 111 | 351 =item C<[get] response> |
| 352 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
353 . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
354 |
| 111 | 355 =item C<[get] formData> |
| 356 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
357 C<IMPL::DOM::Document> , . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
358 |
| 111 | 359 =item C<[get] formSchema> |
| 360 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
361 C<IMPL::DOM::Schema> . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
362 |
| 111 | 363 =item C<[get] formErrors> |
| 364 | |
|
128
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
365 , . , |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
366 , . |
|
08753833173d
Fixed a error handling issue in JSON output: errors are correctly transfered
wizard
parents:
127
diff
changeset
|
367 |
| 111 | 368 =item C<InvokeAction($method,$action)> |
| 369 | |
| 370 , | |
| 112 | 371 . |
| 372 | |
| 373 =item C<TransactionWrapper($method,$action,$methodInfo)> | |
| 374 | |
| 375 , | |
| 376 . | |
| 377 | |
| 378 =item C<FormWrapper($method,$action,$methodInfo)> | |
| 379 | |
| 380 , | |
| 381 . | |
| 111 | 382 |
| 134 | 383 =item C<discover()> |
| 384 | |
| 385 , , C<Simple Module Definition>. | |
| 386 | |
| 387 =begin code | |
| 388 | |
| 389 # SMD structure | |
| 390 { | |
| 391 module => ['Foo','Bar'], | |
| 392 methods => [ | |
| 393 { | |
| 394 name => 'search', | |
| 395 parameters => ['text','limit'] #optional | |
| 396 } | |
| 397 ] | |
| 398 } | |
| 399 | |
| 400 =end code | |
| 401 | |
| 111 | 402 =back |
| 403 | |
| 404 =head1 EXAMPLE | |
| 405 | |
| 406 =begin code | |
| 407 | |
| 408 package MyBooksUnit; | |
| 409 use strict; | |
| 166 | 410 use parent qw(IMPL::Web::Application::ControllerUnit); |
| 111 | 411 |
| 412 __PACKAGE__->PassThroughArgs; | |
| 413 | |
| 134 | 414 sub unitDataClass { 'My::Books' } |
| 415 | |
| 416 __PACKAGE__->transactions( | |
| 417 find => { | |
| 418 parameters => [qw(author)] | |
| 419 }, | |
| 420 info => { | |
| 421 parameters => [qw(id)] | |
| 422 } | |
| 423 ); | |
| 111 | 424 __PACKAGE__->forms( |
| 425 create => 'books.create.xml' | |
| 426 ); | |
| 427 | |
| 428 sub find { | |
| 134 | 429 my ($this,$author) = @_; |
| 111 | 430 |
| 134 | 431 return $this->ds->find({author => $author}); |
| 111 | 432 } |
| 433 | |
| 434 sub info { | |
| 134 | 435 my ($this,$id) = @_; |
| 111 | 436 |
| 134 | 437 return $this->ds->find({id => $id}); |
| 111 | 438 } |
| 439 | |
| 440 sub create { | |
| 441 my ($this) = @_; | |
| 442 | |
| 443 my %book = map { | |
| 134 | 444 $_->nodeName, $_->nodeValue |
| 445 } $this->formData->selectNodes([qw(author_id title year ISBN)]); | |
| 111 | 446 |
| 134 | 447 return $this->ds->create(\%book); |
| 111 | 448 } |
| 449 | |
| 450 =end code | |
| 451 | |
| 452 =cut |
