This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.pm
785 lines (535 loc) · 18.3 KB
/
Action.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package Action;
=head1 NAME
Action (act)
=head1 DESCRIPTION
This class is a version of the Facade design pattern. It provides a
common interface to and supports an execution environment for
instances of the Operation class and the PIFiller class.
The Operation class implements mainly database operations.
The PIFiller class implements the PI handlers for a given template.
Together, these components provide the complete functionality of an
Action.
=head1 VERSION
$Id: Action.pm,v 1.25 2010/02/03 20:31:16 pfarber Exp $
=head1 SYNOPSIS
$act->execute_action($C);
=head1 METHODS
=over 8
=cut
BEGIN
{
if ( $ENV{'HT_DEV'} )
{
require "strict.pm";
strict::import();
}
}
use Debug::DUtils;
use Utils;
use ObjFactory;
use Operation::Status;
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize(@_);
return $self;
}
# ---------------------------------------------------------------------
=item _initialize
Initialize Action
=cut
# ---------------------------------------------------------------------
sub _initialize
{
my $self = shift;
my $C = shift;
# Propagate data if a prior action exists
my $act = $C->get_object('Action', 1);
if ($act)
{
my $error_ref = $act->get_error_record();
$self->set_error_record($C, $error_ref);
}
my $ab = $C->get_object('Bind');
$self->set_name($ab->get_action_name($C));
$self->set_type($ab->get_action_type($C));
$self->{'of'} = new ObjFactory;
my $pifiller_name = $ab->get_action_pifiller_name($C);
if ($pifiller_name)
{
my %of_pif_attrs = (
'class_name' => $pifiller_name,
'parameters' => {'C' => $C},
);
my $pif = $self->instantiate_object($C, \%of_pif_attrs);
$self->set_PI_filler($pif);
}
my @operations;
my $operation_name_arr_ref = $ab->get_action_operation_names($C);
foreach my $op_name (@$operation_name_arr_ref)
{
my %of_op_attrs = (
'class_name' => $op_name,
'parameters' => {
'C' => $C,
'act' => $self,
},
);
my $op = $self->instantiate_object($C, \%of_op_attrs);
push(@operations, $op);
}
$self->set_operations(\@operations);
# Child initialization
$self->after_initialize($C);
}
# ---------------------------------------------------------------------
=item after_initialize
Pure virtual method must be overridden in derived classes to effect
initialization in children but called from parent. Uses the Template
Method Design Pattern
=cut
# ---------------------------------------------------------------------
sub after_initialize
{
ASSERT(0, qq{Pure virtual method after_initialize() not implemented in child});
}
# ---------------------------------------------------------------------
=item execute_action
Run each of this Action's Operation objects. Database actions create
Action facade member data that UI actions read. Since the Actions can
separated by redirects, the facade member data is saved on and retrieved
from the Session.
=cut
# ---------------------------------------------------------------------
sub execute_action
{
my $self = shift;
my $C = shift;
my $type = $self->get_type($C);
my $op_arr_ref = $self->get_operations();
my $status = $Operation::Status::ST_OK;
if ( $type eq 'database')
{
$status = $self->execute_early_ops($C)
if ($status == $Operation::Status::ST_OK);
foreach my $op (@$op_arr_ref)
{
$status = $op->execute_operation($C, $self)
if ($status == $Operation::Status::ST_OK);
}
$status = $self->execute_late_ops($C, $self)
if ($status == $Operation::Status::ST_OK);
$self->WRITE_facade_data($C);
}
elsif ($type eq 'UI')
{
$self->READ_facade_data($C);
$status = $self->execute_early_ops($C, $self)
if ($status == $Operation::Status::ST_OK);
# View can have Operations as well as Builders. Just do the
# Operations here. View is responsible for running Builders
foreach my $op (@$op_arr_ref)
{
$status = $op->execute_operation($C, $self)
if ($status == $Operation::Status::ST_OK);
}
$self->execute_late_ops($C, $self)
if ($status == $Operation::Status::ST_OK);
}
else
{
ASSERT(0, qq{Invalid action type="$type"});
}
return $status;
}
# ---------------------------------------------------------------------
=item WRITE_facade_data
WRITE: Database Action facade member data for use by UI Action.
Must be smart to allow multiple Actions in a row to save their
contribution to facade member data persistently: Merge current facade
member data with any pre-existing facade member data set by previous
'database' Actions.
Each collection of facade member data
corresponding to the execution of an Action is segregated with its own
read flag. Only segregated facade member data collections with the
flag set to zero are read back from the session to the Action's member
data.
=cut
# ---------------------------------------------------------------------
sub WRITE_facade_data
{
my $self = shift;
my $C = shift;
my $type = $self->get_type($C);
ASSERT(($type eq 'database'),
qq{Illegal write of Facade data by Action type="$type"});
# Mark this collection of facade member data as not read yet by a
# UI Action.
$self->set_persistent_facade_member_data($C, 'facade_data_read', 0);
my $current_facade_dataref = $self->get_all_facade_member_data($C);
# Save this collection of facade member data segregated with its
# flag on the session keyed by the name of the Action
my $ses = $C->get_object('Session');
$ses->set_persistent_subkey('facade', $self->get_name(), $current_facade_dataref);
}
# ---------------------------------------------------------------------
=item READ_facade_data
READ: Database Action facade member data for use by UI Action. Handle
logic of debug=xml,xsltwrite to persist the data but not reuse it, if
not debugging.
=cut
# ---------------------------------------------------------------------
sub READ_facade_data
{
my $self = shift;
my $C = shift;
my $type = $self->get_type($C);
ASSERT(($type eq 'UI'),
qq{Illegal read of Facade data by Action type="$type"});
my $ses = $C->get_object('Session');
if (DEBUG('xml,xsltwrite'))
{
my %all_facade_data;
my $all_segregated_facade_data = $ses->get_persistent('facade');
foreach my $key (keys %$all_segregated_facade_data)
{
%all_facade_data =
(%all_facade_data, %{$$all_segregated_facade_data{$key}});
}
$self->set_all_facade_member_data($C, \%all_facade_data);
}
else
{
# Flip idempotent 'accessed' switch so we don't reuse this
# data for each segregation that hasn't been read yet.
# Already read segregations are left on the session for
# debugging persistence but not communicated to the UI Action
# Facade.
my %unread_facade_data;
my $all_segregated_facade_data = $ses->get_persistent('facade');
foreach my $key (keys %$all_segregated_facade_data)
{
if (! $$all_segregated_facade_data{$key}{'facade_data_read'})
{
# Not read. Merge this segregation of Facade Action
# data from Session into the Action Facade member date
# and set the read switch
$$all_segregated_facade_data{$key}{'facade_data_read'} = 1;
%unread_facade_data =
(%unread_facade_data, %{$$all_segregated_facade_data{$key}});
# record this segregation persistently on the Session
# to preserve the new 'facade_data_read' settings
$ses->set_persistent_subkey('facade',
$key,
$$all_segregated_facade_data{$key});
}
}
$self->set_all_facade_member_data($C, \%unread_facade_data);
}
}
# ---------------------------------------------------------------------
=item execute_early_ops
Run global early Operations
=cut
# ---------------------------------------------------------------------
sub execute_early_ops
{
my $self = shift;
my $C = shift;
my $status = $Operation::Status::ST_OK;
my $ab = $C->get_object('Bind');
foreach my $op_name (@{ $ab->get_early_op_names($C) })
{
my %of_op_attrs = (
'class_name' => $op_name,
'parameters' => {
'C' => $C,
'act' => $self,
},
);
my $op = $self->instantiate_object($C, \%of_op_attrs);
$status = $op->execute_operation($C)
if ($status == $Operation::Status::ST_OK);
}
return $status;
}
# ---------------------------------------------------------------------
=item execute_late_ops
Run global late Operations
=cut
# ---------------------------------------------------------------------
sub execute_late_ops
{
my $self = shift;
my $C = shift;
my $status = $Operation::Status::ST_OK;
my $ab = $C->get_object('Bind');
foreach my $op_name (@{ $ab->get_late_op_names($C) })
{
my %of_op_attrs = (
'class_name' => $op_name,
'parameters' => {
'C' => $C,
'act' => $self,
},
);
my $op = $self->instantiate_object($C, \%of_op_attrs);
$status = $op->execute_operation($C)
if ($status == $Operation::Status::ST_OK);
}
return $status;
}
# ---------------------------------------------------------------------
=item instantiate_object
Helper routine
=cut
# ---------------------------------------------------------------------
sub instantiate_object
{
my $self = shift;
my $C = shift;
my $of_attrs_ref = shift;
my $of = $self->get_object_factory();
return $of->create_instance($C, $of_attrs_ref);
}
# ---------------------------------------------------------------------
=item get_PI_handler_mapping
Facade method to PIFiller::get_PI_handler_mapping, which see
=cut
# ---------------------------------------------------------------------
sub get_PI_handler_mapping
{
my $self = shift;
my $C = shift;
my $pif = $self->get_PI_filler();
ASSERT($pif, qq{PIFiller not set in Action=} . ref($self));
return $pif->get_PI_handler_mapping($C);
}
# ---------------------------------------------------------------------
=item get_object_factory
Accessor for the class ObjFactory
=cut
# ---------------------------------------------------------------------
sub get_object_factory
{
my $self = shift;
return $self->{'of'};
}
# ---------------------------------------------------------------------
=item get_PI_filler
Return the PI_filler set by a subclass of this Action handler
=cut
# ---------------------------------------------------------------------
sub get_PI_filler
{
my $self = shift;
my $pif = $self->{'pif'};
silent_ASSERT($pif, qq{PIFiller not set by subclass});
return $pif;
}
# ---------------------------------------------------------------------
=item set_PI_filler
Set the PI_filler of a subclass of this Action handler
=cut
# ---------------------------------------------------------------------
sub set_PI_filler
{
my $self = shift;
my $pif = shift;
$self->{'pif'} = $pif;
}
# ---------------------------------------------------------------------
=item get_operations
Return the operations set by a subclass of this Action handler
=cut
# ---------------------------------------------------------------------
sub get_operations
{
my $self = shift;
my $ops_arr_ref = $self->{'operations'};
ASSERT($ops_arr_ref, qq{Operations not set by subclass});
return $ops_arr_ref;
}
# ---------------------------------------------------------------------
=item set_operations
Set the operations of a subclass of this Action handler
=cut
# ---------------------------------------------------------------------
sub set_operations
{
my $self = shift;
my $op_arr_ref = shift;
$self->{'operations'} = $op_arr_ref;
}
# ---------------------------------------------------------------------
=item set_persistent_facade_member_data
Allows Operations and PIFillers contained in this Action to share data
across redirects
=cut
# ---------------------------------------------------------------------
sub set_persistent_facade_member_data
{
my $self = shift;
my ($C, $key, $val) = @_;
$self->{'facade'}{'persistent'}{$key} = $val;
}
# ---------------------------------------------------------------------
=item set_transient_facade_member_data
Allows Operations and PIFillers contained in this Action to share data
over one pass of the script
=cut
# ---------------------------------------------------------------------
sub set_transient_facade_member_data
{
my $self = shift;
my ($C, $key, $val) = @_;
$self->{'facade'}{'transient'}{$key} = $val;
}
# ---------------------------------------------------------------------
=item get_persistent_facade_member_data
Allows Operations and PIFillers contained in this Action to share data
across redirects
=cut
# ---------------------------------------------------------------------
sub get_persistent_facade_member_data
{
my $self = shift;
my ($C, $key) = @_;
return $self->{'facade'}{'persistent'}{$key};
}
# ---------------------------------------------------------------------
=item get_transient_facade_member_data
Allows Operations and PIFillers contained in this Action to share data
within one pass of the script
=cut
# ---------------------------------------------------------------------
sub get_transient_facade_member_data
{
my $self = shift;
my ($C, $key) = @_;
return $self->{'facade'}{'transient'}{$key};
}
# ---------------------------------------------------------------------
=item get_all_facade_member_data
Allows storage of this data en masse
=cut
# ---------------------------------------------------------------------
sub get_all_facade_member_data
{
my $self = shift;
my $C = shift;
return $self->{'facade'}{'persistent'};
}
# ---------------------------------------------------------------------
=item set_all_facade_member_data
Allows retrieval of this data en masse
=cut
# ---------------------------------------------------------------------
sub set_all_facade_member_data
{
my $self = shift;
my ($C, $data_ref) = @_;
$self->{'facade'}{'persistent'} = $data_ref;
}
# ---------------------------------------------------------------------
=item get_error_record
=cut
# ---------------------------------------------------------------------
sub get_error_record
{
my $self = shift;
my $C = shift;
return $self->{'facade'}{'error_record'};
}
# ---------------------------------------------------------------------
=item set_error_record
=cut
# ---------------------------------------------------------------------
sub set_error_record
{
my $self = shift;
my ($C, $ref) = @_;
$self->{'facade'}{'error_record'} = $ref;
}
# ---------------------------------------------------------------------
=item make_error_record
=cut
# ---------------------------------------------------------------------
sub make_error_record
{
my $self = shift;
my ($C, $msg) = @_;
my $cgi = new CGI($C->get_object('CGI'));
my $error_ref = {
'CGI' => $cgi,
'msg' => $msg,
};
return $error_ref;
}
# ---------------------------------------------------------------------
=item get_name
Obvious
=cut
# ---------------------------------------------------------------------
sub get_name
{
my $self = shift;
return $self->{'name'};
}
# ---------------------------------------------------------------------
=item get_type
Obvious
=cut
# ---------------------------------------------------------------------
sub get_type
{
my $self = shift;
return $self->{'type'};
}
# ---------------------------------------------------------------------
=item set_name
Obvious
=cut
# ---------------------------------------------------------------------
sub set_name
{
my $self = shift;
my $name = shift;
$self->{'name'} = $name;
}
# ---------------------------------------------------------------------
=item set_type
Obvious
=cut
# ---------------------------------------------------------------------
sub set_type
{
my $self = shift;
my $type = shift;
$self->{'type'} = $type;
}
1;
__END__
=head1 AUTHOR
Phillip Farber, University of Michigan, pfarber@umich.edu
=head1 COPYRIGHT
Copyright 2007 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut