-
Notifications
You must be signed in to change notification settings - Fork 11
/
SPOPS.pm
260 lines (194 loc) · 6.97 KB
/
SPOPS.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
package Workflow::Persister::SPOPS;
use warnings;
use strict;
use base qw( Workflow::Persister );
use DateTime;
use Log::Log4perl qw( get_logger );
use Workflow::Exception qw( configuration_error persist_error );
use English qw( -no_match_vars );
$Workflow::Persister::SPOPS::VERSION = '1.50';
my @FIELDS = qw( workflow_class history_class );
__PACKAGE__->mk_accessors(@FIELDS);
sub init {
my ( $self, $params ) = @_;
$self->SUPER::init($params);
unless ( $params->{workflow_class} ) {
configuration_error "SPOPS implementation for persistence must ",
"specify 'workflow_class' parameter ", "in configuration.";
}
$self->workflow_class( $params->{workflow_class} );
unless ( $params->{history_class} ) {
configuration_error "SPOPS implementation for persistence must ",
"specify 'history_class' parameter ", "in configuration.";
}
$self->history_class( $params->{history_class} );
}
sub create_workflow {
my ( $self, $wf ) = @_;
my $wf_persist = $self->workflow_class->new(
{ state => $wf->state,
type => $wf->type,
last_update => DateTime->now( time_zone => $wf->time_zone() ),
}
);
eval { $wf_persist->save };
if ($EVAL_ERROR) {
persist_error "Failed to create new workflow: $EVAL_ERROR";
}
return $wf_persist->id;
}
sub fetch_workflow {
my ( $self, $wf_id ) = @_;
my $wf_persist = eval { $self->workflow_class->fetch($wf_id) };
if ($EVAL_ERROR) {
persist_error "Failed to fetch workflow '$wf_id': $EVAL_ERROR";
}
return $wf_persist;
}
sub update_workflow {
my ( $self, $wf ) = @_;
my $wf_id = $wf->id;
my $wf_persist = eval { $self->workflow_class->fetch($wf_id) };
if ($EVAL_ERROR) {
persist_error
"Cannot fetch record '$wf_id' for updating: $EVAL_ERROR";
}
$wf_persist->state( $wf->state );
$wf_persist->last_update( $wf->last_update );
eval { $wf_persist->save };
if ($EVAL_ERROR) {
persist_error "Failed to update workflow '$wf_id': $EVAL_ERROR";
}
return $wf_persist;
}
sub create_history {
my ( $self, $wf, @history ) = @_;
my $log = get_logger();
$log->debug( "Saving history for workflow ", $wf->id );
foreach my $h (@history) {
next if ( $h->is_saved );
my $hist_persist = eval {
$self->history_class->new(
{ workflow_id => $wf->id,
action => $h->action,
description => $h->description,
state => $h->state,
user => $h->user,
history_date => $h->date
}
)->save();
};
if ($EVAL_ERROR) {
persist_error "Failed to save history record: $EVAL_ERROR";
} else {
$h->id( $hist_persist->id );
$h->set_saved();
$log->info( "Created history record with ID ",
$hist_persist->id );
}
}
return @history;
}
sub fetch_history {
my ( $self, $wf ) = @_;
my $persist_histories = eval {
$self->history_class->fetch_group(
{ where => 'workflow_id = ?',
value => [ $wf->id ],
order => 'history_date DESC'
}
);
};
if ($EVAL_ERROR) {
persist_error "Error fetching workflow history: $EVAL_ERROR";
}
my @histories = ();
for ( @{$persist_histories} ) {
my $hist = Workflow::History->new(
{ id => $_->id,
workflow_id => $_->workflow_id,
action => $_->action,
description => $_->description,
state => $_->state,
user => $_->user,
# NOTE: SPOPS class must return this as a DateTime object...
date => $_->history_date,
time_zone => $_->time_zone,
}
);
$hist->set_saved();
push @histories, $hist;
}
return @histories;
}
1;
__END__
=pod
=head1 NAME
Workflow::Persister::SPOPS - Persist workflows using SPOPS
=head1 VERSION
This documentation describes version 1.07 of this package
=head1 SYNOPSIS
<persister name="SPOPSPersister"
class="Workflow::Persister::SPOPS"
workflow_class="My::Persist::Workflow"
history_class="My::Persist::History"/>
=head1 DESCRIPTION
=head2 Overview
Use a SPOPS classes to persist your workflow and workflow history
information. Configuration is simple: just specify the class names and
everything else is done.
We do not perform any class initialization, so somewhere in your
server/process startup code you should have something like:
my $config = get_workflow_and_history_config();
SPOPS::Initialize->process({ config => $config });
This will generate the classes named in the persister configuration.
=head2 SPOPS Configuration
B<NOTE>: The configuration for your workflow history object B<must>
use the L<SPOPS::Tool::DateConvert> to translate the 'history_date'
field into a L<DateTime> object. We assume when we fetch the history
object that this has already been done.
=head2 METHODS
=head3 init ( \%params)
This method initializes the SPOPS persistance entity.
It requires that a workflow_class and a history_class are specified. If not the
case L<Workflow::Exception>s are thrown.
=head3 create_workflow
Serializes a workflow into the persistance entity configured by our workflow.
Takes a single parameter: a workflow object
Returns a single value, a id for unique identification of out serialized
workflow for possible deserialization.
=head3 fetch_workflow
Deserializes a workflow from the persistance entity configured by our workflow.
Takes a single parameter: the unique id assigned to our workflow upon
serialization (see L</create_workflow>).
Returns a hashref consisting of two keys:
=over
=item * state, the workflows current state
=item * last_update, date indicating last update
=back
=head3 update_workflow
Updates a serialized workflow in the persistance entity configured by our
workflow.
Takes a single parameter: a workflow object
Returns: Nothing
=head3 create_history
Serializes history records associated with a workflow object
Takes two parameters: a workflow object and an array of workflow history objects
Returns: provided array of workflow history objects upon success
=head3 fetch_history
Deserializes history records associated with a workflow object
Takes a single parameter: a workflow object
Returns an array of workflow history objects upon success
=head1 SEE ALSO
L<Workflow::Persister>
L<SPOPS>
L<SPOPS::Tool::DateConvert>
=head1 COPYRIGHT
Copyright (c) 2003-2007 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
Jonas B. Nielsen (jonasbn) E<lt>jonasbn@cpan.orgE<gt> is the current maintainer.
Chris Winters E<lt>chris@cwinters.comE<gt>, original author.
=cut