blib/lib/QBit/Application/Model/Multistate/DB.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 14 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 188 CC 25 SCAR 40.7
CC
25
Cov
0%
get_action_log_entries
132.0
do_action
56.0
check_action
30.0
CRAP 58.3
1 package Exception::Multistate::BadAction;
2 $Exception::Multistate::BadAction::VERSION = '0.008';
3 use base qw(Exception::Multistate);
4  
5 package Exception::Multistate::NotFound;
6 $Exception::Multistate::NotFound::VERSION = '0.008';
7 use base qw(Exception::Multistate);
8  
9 package QBit::Application::Model::Multistate::DB;
10 $QBit::Application::Model::Multistate::DB::VERSION = '0.008';
11 use qbit;
12  
13 use base qw(QBit::Application::Model::Multistate);
14  
15 __PACKAGE__->abstract_methods(
16 qw(
17 _multistate_db_table
18 )
19 );
20  
21 sub check_action {
22 my ($self, $object, $action) = @_;
23  
24 $object = $self->_get_object_fields($object, ['multistate']);
25  
26 throw Exception::Multistate::NotFound unless defined($object);
27  
28 return FALSE unless exists($object->{'multistate'});
29 return FALSE unless $self->check_multistate_action($object->{'multistate'}, $action);
30  
31 my $can_action_sub_name = "can_action_$action";
32 return FALSE if $self->can($can_action_sub_name) && !$self->$can_action_sub_name($object);
33  
34 return TRUE;
35 }
36  
37 sub get_actions {
38 my ($self, $object) = @_;
39  
40 $object = $self->_get_object_fields($object, ['multistate']);
41  
42 return {
43 map {$_ => $self->get_action_name($_)}
44 grep {$self->check_action($object, $_)}
45 keys(%{$self->get_multistates()->{$object->{'multistate'}} || {}})
46 };
47 }
48  
49 sub do_action {
50 my ($self, $object, $action, %opts) = @_;
51  
52 my $pk =
53 ref($object) eq 'HASH'
54 ? {map {$_ => $object->{$_}} @{$self->_multistate_db_table->primary_key}}
55 : $object;
56  
57 my $new_multistate;
58  
59 $self->_multistate_db_table->db->transaction(
60 sub {
61 $object = $self->_get_object_fields(
62 $pk,
63 [
64 @{$self->_multistate_db_table->primary_key}, 'multistate',
65 (ref($object) eq 'HASH' ? keys(%$object) : ())
66 ],
67 for_update => TRUE
68 );
69 throw Exception::Multistate::BadAction gettext('Cannot do action "%s".', $action)
70 unless $self->check_action($object, $action);
71  
72 my $old_multistate = $object->{'multistate'};
73 $new_multistate = $self->get_multistates()->{$old_multistate}{$action};
74 $self->_multistate_db_table()->edit($pk, {multistate => $new_multistate});
75 $object->{'multistate'} = $new_multistate;
76  
77 $self->_action_log_db_table()->add(
78 {
79 user_id => $self->get_option('cur_user', {})->{'id'},
80 (map {("elem_$_" => $object->{$_})} @{$self->_multistate_db_table->primary_key}),
81 old_multistate => $old_multistate,
82 action => $action,
83 new_multistate => $new_multistate,
84 dt => curdate(oformat => 'db_time'),
85 ($self->_action_log_db_table()->have_fields('opts') ? (opts => to_json(\%opts)) : ())
86 }
87 ) if $self->_action_log_db_table();
88  
89 my $on_action_name = "on_action_$action";
90 $self->$on_action_name($object, %opts) if $self->can($on_action_name);
91 }
92 );
93  
94 return $new_multistate;
95 }
96  
97 sub get_action_log_entries {
98 my ($self, $id_elem, %opts) = @_;
99  
100 my $fields = [map {"elem_$_"} @{$self->_action_log_db_table->{'elem_table_pk'}}];
101  
102 my $id = {};
103 if (ref($id_elem) ne 'HASH' and @$fields > 1) {
104 throw gettext('Bad argument. Need hash.');
105 } elsif (ref($id_elem) ne 'HASH' and @$fields == 1) {
106 $id->{$fields->[0]} = $id_elem;
107 } elsif (ref($id_elem) eq 'HASH') {
108 $id->{"elem_$_"} = $id_elem->{$_} foreach keys(%$id_elem);
109 throw gettext(
110 'Cannot find fields. Need (%s), got (%s).',
111 join(', ', @{$self->_action_log_db_table->{'elem_table_pk'}}),
112 join(', ', keys(%$id_elem))
113 ) if grep {!exists($id->{$_})} @$fields;
114 }
115  
116 my $filter = $self->_action_log_db_table->db->filter();
117  
118 $filter->and([$_ => '=' => \$id->{$_}]) foreach @$fields;
119  
120 if (grep {$opts{$_}} qw(fd td)) {
121 $filter->and([dt => '>=' => \$opts{'fd'}]) if $opts{'fd'};
122 $filter->and([dt => '<=' => \$opts{'td'}]) if $opts{'td'};
123 }
124  
125 my $res = $self->_action_log_db_table()->get_all(
126 filter => $filter,
127 order_by => [qw(dt id)]
128 );
129  
130 if (grep {$opts{$_}} qw(explain_actions explain_multistates)) {
131 foreach (@$res) {
132 $_->{'action_name'} = $self->get_action_name($_->{'action'}) if $opts{'explain_actions'};
133 if ($opts{'explain_multistates'}) {
134 $_->{'old_multistate_name'} = $self->get_multistate_name($_->{'old_multistate'});
135 $_->{'new_multistate_name'} = $self->get_multistate_name($_->{'new_multistate'});
136 }
137 }
138 }
139  
140 return $res;
141 }
142  
143 sub get_object_fields {
144 my ($self, $object, $fields, %opts) = @_;
145  
146 push(@$fields, keys(%$object)) if ref($object) eq 'HASH';
147  
148 return $self->_get_object_fields($object, $fields, %opts);
149 }
150  
151 sub _get_object_fields {
152 my ($self, $object, $fields, %opts) = @_;
153  
154 if (ref($object) eq 'HASH') {
155 return $object if !$opts{'for_update'} && scalar(grep {exists($object->{$_})} @$fields) == @$fields;
156  
157 throw gettext(
158 'Cannot find PK fields. Need (%s), got (%s).',
159 join(', ', @{$self->_multistate_db_table->primary_key}),
160 join(', ', keys(%$object))
161 ) if grep {!exists($object->{$_})} @{$self->_multistate_db_table->primary_key};
162 }
163  
164 push(@$fields, @{$self->_multistate_db_table->primary_key});
165  
166 return $self->_get(
167 $object,
168 for_update => $opts{'for_update'},
169 fields => array_uniq(@$fields),
170 );
171 }
172  
173 sub _get {
174 my ($self, $object, %opts) = @_;
175  
176 return $self->_multistate_db_table->get($object, %opts);
177 }
178  
179 sub _action_log_db_table { }
180  
181 TRUE;
182  
183 __END__