310 lines
6.8 KiB
Perl
310 lines
6.8 KiB
Perl
package WebGUI::Workflow;
|
|
|
|
|
|
=head1 LEGAL
|
|
|
|
-------------------------------------------------------------------
|
|
WebGUI is Copyright 2001-2006 Plain Black Corporation.
|
|
-------------------------------------------------------------------
|
|
Please read the legal notices (docs/legal.txt) and the license
|
|
(docs/license.txt) that came with this distribution before using
|
|
this software.
|
|
-------------------------------------------------------------------
|
|
http://www.plainblack.com info@plainblack.com
|
|
-------------------------------------------------------------------
|
|
|
|
=cut
|
|
|
|
use strict;
|
|
|
|
|
|
=head1 NAME
|
|
|
|
Package WebGUI::Workflow
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This package provides the API for manipulating workflows.
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
use WebGUI::Workflow;
|
|
|
|
=head1 METHODS
|
|
|
|
These methods are available from this class:
|
|
|
|
=cut
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 addActivity ( class [, id ] )
|
|
|
|
Adds an activity to this workflow. Returns a reference to the new activity object.
|
|
|
|
=head3 class
|
|
|
|
The classname of the activity to add.
|
|
|
|
=head3 id
|
|
|
|
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.
|
|
|
|
=cut
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 create ( session, properties, [, id ] )
|
|
|
|
Creates a new instance of a workflow.
|
|
|
|
=head3 session
|
|
|
|
A reference to the current session.
|
|
|
|
=head3 properties
|
|
|
|
A hash reference of properties to set for this workflow. See set() for details.
|
|
|
|
=head3 id
|
|
|
|
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.
|
|
|
|
=cut
|
|
|
|
sub create {
|
|
my $class = shift;
|
|
my $session = shift;
|
|
my $properties = shift;
|
|
my $id = shift;
|
|
my $workflowId = $session->db->setRow("Workflow","workflowId",{workflowId=>"new",enabled=>0},$id);
|
|
my $self = $class->new($session, $workflowId);
|
|
$self->set($properties);
|
|
return $self;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 delete ( )
|
|
|
|
Removes this workflow and everything associated with it..
|
|
|
|
=cut
|
|
|
|
sub delete {
|
|
my $self = shift;
|
|
# delete crons
|
|
foreach my $activity (@{$self->getActivities}) {
|
|
$activity->delete;
|
|
}
|
|
# delete instances
|
|
$self->session->db->deleteRow("Workflow","workflowId",$self->getId);
|
|
$self->undef;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 deleteActivity ( activityId )
|
|
|
|
Removes an activity from this workflow.
|
|
|
|
=head3 activityId
|
|
|
|
The unique id of the activity to remove.
|
|
|
|
=cut
|
|
|
|
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;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 DESTROY ( )
|
|
|
|
Deconstructor.
|
|
|
|
=cut
|
|
|
|
sub DESTROY {
|
|
my $self = shift;
|
|
undef $self;
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 get ( name )
|
|
|
|
Returns the value for a given property.
|
|
|
|
=cut
|
|
|
|
sub get {
|
|
my $self = shift;
|
|
my $name = shift;
|
|
return $self->{_data}{$name};
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 getActivities ( )
|
|
|
|
Returns an array reference of the activity objects associated with this workflow.
|
|
|
|
=cut
|
|
|
|
sub getActivities {
|
|
my $self = shift;
|
|
my @activities = ();
|
|
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));
|
|
}
|
|
return \@activities;
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 getNextActivity ( [ activityId ] )
|
|
|
|
Returns the next activity in the workflow after the activity specified. If no activity id is specified, then the first workflow will be returned.
|
|
|
|
=head3 activityId
|
|
|
|
The unique id of an activity in this workflow.
|
|
|
|
=cut
|
|
|
|
sub getNextActivity {
|
|
my $self = shift;
|
|
my $activityId = shift;
|
|
my ($sequenceNumber) = $self->session->db->quickArray("select sequenceNumber from WorkflowActivity where activityId=?", [$activityId]);
|
|
$sequenceNumber++;
|
|
my $rs = $self->session->db->read("select activityId, className from WorkflowActivity where workflowId=?
|
|
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);
|
|
}
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 getId ( )
|
|
|
|
Returns the ID of this workflow.
|
|
|
|
=cut
|
|
|
|
sub getId {
|
|
my $self = shift;
|
|
return $self->{_id};
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 new ( session, workflowId )
|
|
|
|
Constructor.
|
|
|
|
=head3 session
|
|
|
|
A reference to the current session.
|
|
|
|
=head3 workflowId
|
|
|
|
The unique id of this workflow.
|
|
|
|
=cut
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $session = shift;
|
|
my $workflowId = shift;
|
|
my $data = $session->db->getRow("Workflow","workflowId", $workflowId);
|
|
return undef unless $data->{workflowId};
|
|
bless {_session=>$session, _id=>$workflowId, _data=>$data}, $class;
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 session ( )
|
|
|
|
Returns a reference to the current session.
|
|
|
|
=cut
|
|
|
|
sub session {
|
|
my $self = shift;
|
|
return $self->{_session};
|
|
}
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
=head2 set ( properties )
|
|
|
|
Sets a variable for this workflow.
|
|
|
|
=head3 properties
|
|
|
|
A hash reference containing the properties to set.
|
|
|
|
=head4 title
|
|
|
|
A title indicating what this workflow does. It should be short and descriptive as it will appear in dropdown forms.
|
|
|
|
=head4 description
|
|
|
|
A longer description of the workflow.
|
|
|
|
=head4 enabled
|
|
|
|
A boolean indicating whether this workflow may be executed right now.
|
|
|
|
=head4 type
|
|
|
|
A string indicating the type of object this workflow will be operating on. Valid values are "none", "versiontag" and "user".
|
|
|
|
=cut
|
|
|
|
sub set {
|
|
my $self = shift;
|
|
my $properties = shift;
|
|
if ($properties->{enabled} == 1) {
|
|
$self->{_data}{enabled} = 1;
|
|
} elsif ($properties->{enabled} == 0) {
|
|
$self->{_data}{enabled} = 0;
|
|
}
|
|
$self->{_data}{title} = $properties->{title} || $self->{_data}{title} || "Untitled";
|
|
$self->{_data}{description} = (exists $properties->{description}) ? $properties->{description} : $self->{_data}{description};
|
|
$self->{_data}{type} = $properties->{type} || $self->{_data}{type} || "none";
|
|
$self->session->db->setRow("Workflow","workflowId",$self->{_data});
|
|
}
|
|
|
|
|
|
1;
|
|
|
|
|