[Perl-workflow-devel] Hack: Adding State Properties to Actions
Brought to you by:
jonasbn
From: Alejandro I. <ai...@ya...> - 2009-09-21 21:57:58
|
After some serious headbanging here is a recipe that may be useful for others... (Any comments or better ways to doing this welcome!) Suppose you want some extra properties to your actions depending on state. For example, you would like to display the actions in a specific order in a particular state and/or you may want to associate an icon and other accessors depending on state. The most maintainable way of doing this is adding the new properties in the XML like so: <!-- INITIAL State --> <state name="INITIAL"> <description> OEM Management Start </description> <action index="0" icon="list_icon" name="OEM Browse" resulting_state="OEM_BROWSE"> <condition name="roleis_oem_mgmt"/> </action> <action index="1" icon="add_icon_med" name="OEM Create" resulting_state="OEM_CREATE"> <condition name="roleis_oem_mgmt"/> </action> <action index="2" icon="back_icon_med" name="Back" resulting_state="CLOSED"/> </state> As can be seen in the example, there are some new xml properties such as index and icon. The XML parser will pick these up and they will be available from the wf's state configuration. So if you make all your Action classes derive from a base class of your own: package your::base::class; use warnings; use strict; use base qw( Workflow::Action ); use Workflow::Exception qw( workflow_error ); # extra action class properties my @EXTRA_FIELDS = qw( index icon type data ); __PACKAGE__->mk_accessors(@EXTRA_FIELDS); #FRAGILE: overriding private method sub new { my $class = shift; my $wf = shift; my $self = $class->SUPER::new($wf, @_); #FRAGILE: overriding private method and properties my $wf_state = $wf->_get_workflow_state; my $action = $wf_state->{_actions}->{$self->name}; $self->index($action->{index}); $self->icon($action->{icon}); $self->type($action->{type}); $self->data($action->{data}); return $self; } 1; Then you can just implement your Action classes as usual but deriving from this hacked base class: package actual::action::class; use warnings; use strict; use base qw( your::base::class ); use Workflow::Exception qw( workflow_error ); sub execute { ... } 1; This hack takes advantage of the fact that the parsing will pick-up these extra xml parameters and available as config in the initialization code. Best, Alejandro Imass |