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

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 14 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 183 CC 24 SCAR 41.2
CC
24
Cov
0%
get_action_log_entries
132.0
do_action
56.0
check_action
30.0
CRAP 61.4
1 package Exception::Multistate::BadAction;
2 $Exception::Multistate::BadAction::VERSION = '0.007';
3 use base qw(Exception::Multistate);
4  
5 package Exception::Multistate::NotFound;
6 $Exception::Multistate::NotFound::VERSION = '0.007';
7 use base qw(Exception::Multistate);
8  
9 package QBit::Application::Model::Multistate::DB;
10 $QBit::Application::Model::Multistate::DB::VERSION = '0.007';
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 my $on_action_name = "on_action_$action";
78 $self->$on_action_name($object, %opts) if $self->can($on_action_name);
79  
80 $self->_action_log_db_table()->add(
81 {
82 user_id => $self->get_option('cur_user', {})->{'id'},
83 (map {("elem_$_" => $object->{$_})} @{$self->_multistate_db_table->primary_key}),
84 old_multistate => $old_multistate,
85 action => $action,
86 new_multistate => $new_multistate,
87 dt => curdate(oformat => 'db_time'),
88 ($self->_action_log_db_table()->have_fields('opts') ? (opts => to_json(\%opts)) : ())
89 }
90 ) if $self->_action_log_db_table();
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 if (ref($object) eq 'HASH') {
147 return $object if !$opts{'for_update'} && @{arrays_intersection([keys(%$object)], $fields)} == @$fields;
148  
149 throw gettext(
150 'Cannot find PK fields. Need (%s), got (%s).',
151 join(', ', @{$self->_multistate_db_table->primary_key}),
152 join(', ', keys(%$object))
153 ) if grep {!exists($object->{$_})} @{$self->_multistate_db_table->primary_key};
154 }
155  
156 push(@$fields, @{$self->_multistate_db_table->primary_key});
157  
158 return $self->_get(
159 $object,
160 for_update => $opts{'for_update'},
161 fields => array_uniq(@$fields)
162 );
163 }
164  
165 sub _get {
166 my ($self, $object, %opts) = @_;
167  
168 return $self->_multistate_db_table->get($object, %opts);
169 }
170  
171 sub _action_log_db_table { }
172  
173 TRUE;
174  
175 __END__