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

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