From a4578d1d7deaebeaac38bf5fe4208a1d20293464 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 15 Feb 2006 04:04:20 +0000 Subject: [PATCH] adding dynamic loading of classes --- lib/WebGUI/Workflow.pm | 18 +++++------------- lib/WebGUI/Workflow/Activity.pm | 27 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/WebGUI/Workflow.pm b/lib/WebGUI/Workflow.pm index aef3fcdce..3326ff38c 100644 --- a/lib/WebGUI/Workflow.pm +++ b/lib/WebGUI/Workflow.pm @@ -16,7 +16,7 @@ package WebGUI::Workflow; =cut use strict; - +use WebGUI::Workflow::Activity; =head1 NAME @@ -56,9 +56,7 @@ sub addActivity { my $self = shift; my $class = shift; my $id = shift; - my $cmd = "use ".$class; - eval($cmd); - return $class->create($self->session, $self->getId, $id); + return WebGUI::Workflow::Activity->create($self->session, $self->getId, $id, $class); } @@ -128,9 +126,7 @@ sub deleteActivity { my $self = shift; my $activityId = shift; my ($class) = $self->session->db->quickArray("select className from WorkflowActivity where activityId=?",[$activityId]); - my $cmd = "use ".$class; - eval{$cmd}; - $class->new($self->session, $activityId)->delete; + WebGUI::Workflow::Activity->new($self->session, $activityId, $class)->delete; } #------------------------------------------------------------------- @@ -176,9 +172,7 @@ sub getActivities { my $rs = $self->session->db->prepare("select activityId, className from WorkflowActivity where workflowId=? order by sequenceNumber"); $rs->execute([$self->getId]); while (my ($activityId, $class) = $rs->array) { - my $cmd = "use ".$class; - eval{$cmd}; - push(@activities, $class->new($self->session, $activityId)); + push(@activities, WebGUI::Workflow::Activity->new($self->session, $activityId, $class)); } return \@activities; } @@ -231,9 +225,7 @@ sub getNextActivity { and sequenceNumber>=? order by sequenceNumber", [$self->getId, $sequenceNumber]); my ($id, $class) = $rs->array; $rs->finish; - my $cmd = "use ".$class; - eval{$cmd}; - return $class->new($self->session, $id); + return WebGUI::Workflow::Activity->new($self->session, $id, $class); } diff --git a/lib/WebGUI/Workflow/Activity.pm b/lib/WebGUI/Workflow/Activity.pm index 51f7706b1..14f99f96f 100644 --- a/lib/WebGUI/Workflow/Activity.pm +++ b/lib/WebGUI/Workflow/Activity.pm @@ -39,7 +39,7 @@ These methods are available from this class: #------------------------------------------------------------------- -=head2 create ( session, workflowId [, id ] ) +=head2 create ( session, workflowId [, id, classname ] ) Creates a new instance of this activity in a workflow. @@ -55,22 +55,27 @@ The unique id of the workflow to attach this activity to. Normally an ID will be generated for you, but if you want to override this and provide your own 22 character id, then you can specify it here. +=head3 classname + +The classname of the activity you wish to create. + =cut sub create { my $class = shift; my $session = shift; my $workflowId = shift; + my $classname = shift; my $id = shift; my ($sequenceNumber) = $session->db->quickArray("select count(*) from WorkflowActivity where workflowId=?", [$workflowId]); $sequenceNumber++; my $activityId = $session->db->setRow("WorkflowActivity","activityId", { sequenceNumber=>$sequenceNumber, activityId=>"new", - className=>$class, + className=>$classname || $class, workflowId=>$workflowId }, $id); - return $class->new($session, $activityId); + return $class->new($session, $activityId, $classname); } #------------------------------------------------------------------- @@ -194,7 +199,7 @@ sub getType { #------------------------------------------------------------------- -=head2 new ( session, activityId ) +=head2 new ( session, activityId [, classname] ) Constructor. @@ -206,14 +211,28 @@ A reference to the current session. A unique id refering to an instance of an activity. +=head3 classname + +The classsname of the activity you wish to add. + =cut sub new { my $class = shift; my $session = shift; my $activityId = shift; + my $className = shift; my $main = $session->db->getRow("WorkflowActivity","activityId", $activityId); return undef unless $main->{activityId}; + if ($className) { + my $cmd = "use ".$className; + eval ($cmd); + if ($@) { + $session->errorHandler->error("Couldn't compile workflow activity package: ".$className.". Root cause: ".$@); + return undef; + } + $class = $className; + } my $sub = $session->db->buildHashRef("select name,value from WorkflowActivityData where activityId=?",[$activityId]); my %data = (%{$main}, %{$sub}); bless {_session=>$session, _id=>$activityId, _data=>\%data}, $class;