-
Notifications
You must be signed in to change notification settings - Fork 11
/
Action.pm
463 lines (337 loc) · 12.4 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
package Workflow::Action;
# Note: we may implement a separate event mechanism so that actions
# can trigger other code (to read observations from database?)
use warnings;
use strict;
use base qw( Workflow::Base );
use Log::Log4perl qw( get_logger );
use Workflow::Action::InputField;
use Workflow::Validator::HasRequiredField;
use Workflow::Factory qw( FACTORY );
use Carp qw(croak);
$Workflow::Action::VERSION = '1.50';
my @PROPS = qw( name class description );
my @INTERNAL = qw( _factory );
__PACKAGE__->mk_accessors( @PROPS, @INTERNAL );
####################
# INPUT FIELDS
sub add_fields {
my ( $self, @fields ) = @_;
push @{ $self->{_fields} }, @fields;
}
sub required_fields {
my ($self) = @_;
return grep { $_->requirement() eq 'required' } @{ $self->{_fields} };
}
sub optional_fields {
my ($self) = @_;
return grep { $_->requirement() eq 'optional' } @{ $self->{_fields} };
}
sub fields {
my ($self) = @_;
return @{ $self->{_fields} };
}
####################
# VALIDATION
sub add_validators {
my ( $self, @validator_info ) = @_;
my @validators = ();
foreach my $conf (@validator_info) {
my $validator = $self->_factory()->get_validator( $conf->{name} );
my @args = $self->normalize_array( $conf->{arg} );
push @validators,
{
validator => $validator,
args => \@args
};
}
push @{ $self->{_validators} }, @validators;
}
sub get_validators {
my ($self) = @_;
return () if ( not defined $self->{_validators} );
return @{ $self->{_validators} };
}
sub validate {
my ( $self, $wf ) = @_;
my @validators = $self->get_validators;
return unless ( scalar @validators );
my $context = $wf->context;
foreach my $validator_info (@validators) {
my $validator = $validator_info->{validator};
my $args = $validator_info->{args};
# TODO: Revisit this statement it does not look right
# runtime_args becomes the WF object??
my @runtime_args = ($wf);
foreach my $arg ( @{$args} ) {
if ( $arg =~ /^\$(.*)$/ ) {
push @runtime_args, $context->param($1);
} else {
push @runtime_args, $arg;
}
}
$validator->validate(@runtime_args);
}
}
# Subclasses override...
sub execute {
my ( $self, $wf ) = @_;
croak "Class ", ref($self), " must implement 'execute()'\n";
}
########################################
# PRIVATE
sub init {
my ( $self, $wf, $params ) = @_;
my $log = get_logger();
# So we don't destroy the original...
my %copy_params = %{$params};
$self->_factory( $wf->_factory() );
$self->class( $copy_params{class} );
$self->name( $copy_params{name} );
$self->description( $copy_params{description} );
## init normal fields
my @fields = $self->normalize_array( $copy_params{field} );
foreach my $field_info (@fields) {
if ( my $field_class = $field_info->{class} ) {
$log->debug("Using custom field class $field_class");
$self->add_fields( $field_class->new($field_info) );
} else {
$log->debug("Using standard field class");
$self->add_fields(
Workflow::Action::InputField->new($field_info) );
}
}
## establish validator for fields with is_required="yes"
@fields = $self->required_fields();
my $validator = Workflow::Validator::HasRequiredField->new(
{ name => 'HasRequiredField for is_required fields',
class => 'Workflow::Validator::HasRequiredField'
}
);
my @args = ();
foreach my $field (@fields) {
next if ( not $field ); ## empty @fields array
push @args, $field->name();
}
push @{ $self->{_validators} },
{
validator => $validator,
args => \@args
};
## init normal validators
my @validator_info = $self->normalize_array( $copy_params{validator} );
$self->add_validators(@validator_info);
delete @copy_params{qw( class name description field validator )};
# everything else is just a passthru param
while ( my ( $key, $value ) = each %copy_params ) {
$self->param( $key, $value );
}
}
1;
__END__
=pod
=head1 NAME
Workflow::Action - Base class for Workflow actions
=head1 VERSION
This documentation describes version 1.09 of this package
=head1 SYNOPSIS
# Configure the Action...
<action name="CreateUser"
class="MyApp::Action::CreateUser">
<field name="username" is_required="yes"/>
<field name="email" is_required="yes"/>
<validator name="IsUniqueUser">
<arg>$username</arg>
</validator>
<validator name="IsValidEmail">
<arg>$email</arg>
</validator>
</action>
# Define the action
package MyApp::Action::CreateUser;
use base qw( Workflow::Action );
use Workflow::Exception qw( workflow_error );
sub execute {
my ( $self, $wf ) = @_;
my $context = $wf->context;
# Since 'username' and 'email' have already been validated we
# don't need to check them for uniqueness, well-formedness, etc.
my $user = eval {
User->create({ username => $context->param( 'username' ),
email => $context->param( 'email' ) })
};
# Wrap all errors returned...
if ( $@ ) {
workflow_error
"Cannot create new user with name '", $context->param( 'username' ), "': $EVAL_ERROR";
}
# Set the created user in the context for the application and/or
# other actions (observers) to use
$context->param( user => $user );
# return the username since it might be used elsewhere...
return $user->username;
}
=head1 DESCRIPTION
This is the base class for all Workflow Actions. You do not have to
use it as such but it is strongly recommended.
=head1 CONFIGURATION
You configure your actions and map them to a specific module in your actions
configuration file using the syntax
above and that shown in L<Workflow>. In some cases, you'll have actions
that apply to all workflows. In more elaborate configurations, you may have
one workflow server loading multiple workflows and multiple actions for each.
In these
cases, you'll have multiple workflow types and you may want actions
with the same names to have different behaviors for each type.
For example, you may have a workflow type Ticket and another type Order_Parts.
They both may have a Submit action, but you'll want the Submit to be different
for each.
You can specify a type in your actions configuration to associate that action
with that workflow type. If you don't provide a type, the action is available
to all types. For example:
<actions>
<type>Ticket</type>
<description>Actions for the Ticket workflow only.</description>
<action name="TIX_NEW"
class="TestApp::Action::TicketCreate">
...Addtional configuration...
The type must match an existing workflow type or the action will never
be called.
=head1 ADDITIONAL ATTRIBUTES
You can validate additional attributes in of your action by doing two things:
=over
=item *
Set C<$Workflow::Factory::VALIDATE_ACTION_CONFIG> to 1.
=item *
Provide function validate_config() in your action class.
=back
Then, this function will be called with all the acton attributes when
it is parsed. For exmaple, if your action XML looks like this:
<action name="BEGIN" class="My::Class" when="NOW">
You can validate it like this:
sub My::Class::validate_config {
my $config = shift;
unless ('NOW' eq $config->{when}) {
configuration_error "`$$config{when}' is not a valid value " .
"for `when'";
}
}
=head1 OBJECT METHODS
=head2 Public Methods
=head3 new()
Subclasses may override this method, but it's not very common. It is
called when you invoke a method in your Workflow object that returns
an Action object, for example, methods such as $wf->_get_action will
call this method.
B<Your action classes usually subclass directly from Workflow::Action
and they I<don't> need to override this method at all>. However, under
some circumstances, you may find the need to extend your action
classes.
Suppose you want to define some extra properties to actions but you
also want for some of these properties to depend on a particular
state. For example, the action "icon" will almost allways be the same,
but the action "index" will depend on state, so you can display your
actions in a certain order according to that particular state. Here is
an example on how you easily do this by overriding new():
1) Set the less changing properties in your action definition:
<actions>
<type>foo</type>
<action name="Browse"
type="menu_button" icon="list_icon"
class="actual::action::class">
</action>
2) Set the state dependant properties in the state definition:
<state name="INITIAL">
<description>
Manage Manufaturers
</description>
<action index="0" name="Browse" resulting_state="BROWSE">
<condition name="roleis_oem_mgmt"/>
</action>
<action index="1" name="Create" resulting_state="CREATE">
<condition name="roleis_oem_mgmt"/>
</action>
<action index="2" name="Back" resulting_state="CLOSED"/>
</state>
3) Craft a custom action base class
package your::action::base::class;
use warnings;
use strict;
use base qw( Workflow::Action );
use Workflow::Exception qw( workflow_error );
# extra action class properties
my @EXTRA_PROPS = qw( index icon type data );
__PACKAGE__->mk_accessors(@EXTRA_PROPS);
sub new {
my ($class, $wf, $params) = @_;
my $self = $class->SUPER::new($wf, $params);
# set only our extra properties from action class def
foreach my $prop (@EXTRA_PROPS) {
next if ( $self->$prop );
$self->$prop( $params->{$prop} );
}
# override specific extra action properties according to state
my $wf_state = $wf->_get_workflow_state;
my $action = $wf_state->{_actions}->{$self->name};
$self->index($action->{index});
return $self;
}
1;
B<Note>: this hack takes advantage of the fact that the XML parser
picks up the extra parameters and add them to the action hash of the
current $wf_state. Your milage may vary.
4) Use your custom action base class instead of the default
package actual::action::class;
use warnings;
use strict;
use base qw( your::base::action::class );
use Workflow::Exception qw( workflow_error );
sub execute {
...
}
1;
=head2 Private Methods
=head3 init( $workflow, \%params )
init is called in conjuction with the overall workflow initialization.
It sets up the necessary validators based on the on configured actions, input fields and required fields.
=head3 add_field( @fields )
Add one or more L<Workflow::Action::InputField>s to the action.
=head3 required_fields()
Return a list of L<Workflow::Action::InputField> objects that are required.
=head3 optional_fields()
Return a list of L<Workflow::Action::InputField> objects that are optional.
=head3 fields()
Return a list of all L<Workflow::Action::InputField> objects
associated with this action.
=head3 add_validators( @validator_config )
Given the 'validator' configuration declarations in the action
configuration, ask the L<Workflow::Factory> for the
L<Workflow::Validator> object associated with each name and store that
along with the arguments to be used, runtime and otherwise.
=head3 get_validators()
Get a list of all the validator hashrefs, each with two keys:
'validator' and 'args'. The 'validator' key contains the appropriate
L<Workflow::Validator> object, while 'args' contains an arrayref of
arguments to pass to the validator, some of which may need to be
evaluated at runtime.
=head3 validate( $workflow )
Run through all validators for this action. If any fail they will
throw a L<Workflow::Exception>, the validation subclass.
=head3 execute( $workflow )
Subclasses B<must> implement -- this will perform the actual
work. It's not required that you return anything, but if the action
may be used in a L<Workflow::State> object that has multiple resulting
states you should return a simple scalar for a return value.
=head3 add_fields
Method to add fields to the workflow. The method takes an array of
fields.
=head1 SEE ALSO
L<Workflow>
L<Workflow::Factory>
=head1 COPYRIGHT
Copyright (c) 2003-2004 Chris Winters. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHORS
Chris Winters E<lt>chris@cwinters.comE<gt>
=cut