File Coverage

blib/lib/App/Dochazka/REST/Model/Schedintvls.pm
Criterion Covered Total %
statement 32 90 35.5
branch 0 8 0.0
condition n/a
subroutine 11 25 44.0
pod 5 5 100.0
total 48 128 37.5


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2017, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32              
33              
34             use 5.012;
35 41     41   1576 use strict;
  41         140  
36 41     41   199 use warnings;
  41         107  
  41         743  
37 41     41   202 use App::CELL qw( $CELL $log $meta $site );
  41         90  
  41         1173  
38 41     41   232 use App::Dochazka::REST::ConnBank qw( $dbix_conn );
  41         126  
  41         4101  
39 41     41   293 use App::Dochazka::REST::Model::Shared;
  41         101  
  41         3098  
40 41     41   274 use Data::Dumper;
  41         118  
  41         1326  
41 41     41   219 use JSON;
  41         102  
  41         1587  
42 41     41   230 use Params::Validate qw( :all );
  41         90  
  41         281  
43 41     41   3993 use Try::Tiny;
  41         96  
  41         5430  
44 41     41   342  
  41         135  
  41         2133  
45             # we get 'spawn', 'reset', and accessors from parent
46             use parent 'App::Dochazka::Common::Model::Schedintvls';
47 41     41   273  
  41         85  
  41         296  
48              
49              
50              
51             =head1 NAME
52              
53             App::Dochazka::REST::Model::Schedintvls - object class for "scratch schedules"
54              
55              
56              
57              
58             =head1 SYNOPSIS
59              
60             use App::Dochazka::REST::Model::Schedintvls;
61              
62             ...
63              
64              
65              
66              
67             =head1 METHODS
68              
69              
70             =head2 populate
71              
72             Populate the schedintvls object (called automatically by 'reset' method
73             which is, in turn, called automatically by 'spawn')
74              
75             =cut
76              
77             my $self = shift;
78              
79 0     0 1   my $ss = _next_scratch_sid();
80             $log->debug( "Got next scratch SID: $ss" );
81 0           $self->{'ssid'} = $ss;
82 0           return;
83 0           }
84 0            
85              
86             =head2 load
87              
88             Instance method. Once the scratch intervals are inserted, we have a fully
89             populated object. This method runs each scratch interval through the stored
90             procedure 'translate_schedintvl' -- upon success, it creates a new attribute,
91             C<< $self->{schedule} >>, containing the translated intervals.
92              
93             =cut
94              
95             my $self = shift;
96             my ( $conn ) = validate_pos( @_,
97             { isa => 'DBIx::Connector' }
98 0     0 1   );
99 0            
100             my $status;
101             my @results;
102             try {
103 0           $conn->run( fixup => sub {
104             # prepare and execute statement
105             my $sth = $_->prepare( $site->SQL_SCHEDINTVLS_SELECT );
106             $sth->execute( $self->{'ssid'} );
107              
108 0           # since the statement returns n rows, we use a loop to fetch them
109 0           while( defined( my $tmpres = $sth->fetchrow_hashref() ) ) {
110             push( @results, $tmpres );
111             }
112 0           } );
113 0           } catch {
114             $status = $CELL->status_err( 'DOCHAZKA_DBI_ERR', args => [ $_ ] );
115 0     0     };
116             return $status if $status;
117 0     0      
118 0           # success: add a new attribute with the translated intervals
119 0 0         $self->{schedule} = \@results;
120              
121             return $CELL->status_ok( "Schedule has " . scalar( @results ) . " rows" );
122 0           }
123            
124 0            
125              
126             =head2 insert
127              
128             Instance method. Attempts to INSERT one or more records (one for each
129             interval in the 'intvls' attribute) into the 'schedintvls' table.
130             Field values are taken from the object. Returns a status object.
131              
132             =cut
133              
134             my $self = shift;
135             my ( $conn ) = validate_pos( @_,
136             { isa => 'DBIx::Connector' }
137             );
138 0     0 1    
139 0           # the insert operation needs to take place within a transaction,
140             # because all the intervals are inserted in one go
141             my $status;
142             try {
143             $conn->txn( fixup => sub {
144             my $sth = $_->prepare( $site->SQL_SCHEDINTVLS_INSERT );
145 0           my $intvls;
146              
147             # the next sequence value is already in $self->{ssid}
148 0           $sth->bind_param( 1, $self->{ssid} );
149 0            
150             # execute SQL_SCHEDINTVLS_INSERT for each element of $self->{intvls}
151             map {
152 0           $sth->bind_param( 2, $_ );
153             $sth->execute;
154             push @$intvls, $_;
155             } @{ $self->{intvls} };
156 0           $status = $CELL->status_ok(
157 0           'DOCHAZKA_SCHEDINTVLS_INSERT_OK',
158 0           payload => {
159 0           intervals => $intvls,
  0            
160             ssid => $self->{ssid},
161             }
162             );
163             } );
164             } catch {
165             $status = $CELL->status_err( 'DOCHAZKA_DBI_ERR', args => [ $_ ] );
166 0           };
167 0     0     return $status;
168             }
169 0     0      
170 0            
171 0           =head2 update
172              
173             There is no update method for schedintvls. Instead, delete and re-create.
174              
175              
176             =head2 delete
177              
178             Instance method. Once we are done with the scratch intervals, they can be deleted.
179             Returns a status object.
180              
181             =cut
182              
183             my $self = shift;
184             my ( $conn ) = validate_pos( @_,
185             { isa => 'DBIx::Connector' }
186             );
187              
188 0     0 1   my $status;
189 0           try {
190             $conn->run( fixup => sub {
191             my $sth = $_->prepare( $site->SQL_SCHEDINTVLS_DELETE );
192             $sth->bind_param( 1, $self->ssid );
193 0           $sth->execute;
194             my $rows = $sth->rows;
195             if ( $rows > 0 ) {
196 0           $status = $CELL->status_ok( 'DOCHAZKA_RECORDS_DELETED', args => [ $rows ] );
197 0           } elsif ( $rows == 0 ) {
198 0           $status = $CELL->status_warn( 'DOCHAZKA_RECORDS_DELETED', args => [ $rows ] );
199 0           } else {
200 0 0         die( "\$sth->rows returned a weird value $rows" );
    0          
201 0           }
202             } );
203 0           } catch {
204             $status = $CELL->status_err( 'DOCHAZKA_DBI_ERR', args => [ $_ ] );
205 0           };
206             return $status;
207 0     0     }
208              
209 0     0      
210 0           =head2 json
211 0            
212             Instance method. Returns a JSON string representation of the schedule.
213              
214             =cut
215              
216             my ( $self ) = @_;
217              
218             return JSON->new->utf8->canonical(1)->encode( $self->{schedule} );
219             }
220              
221              
222 0     0 1    
223              
224 0           =head1 FUNCTIONS
225              
226             =head2 Exported functions
227              
228              
229              
230             =head2 Non-exported functions
231              
232             =head3 _next_scratch_sid
233              
234             Get next value from the scratch_sid_seq sequence
235              
236             =cut
237              
238             my $val;
239             my $status;
240             try {
241             $dbix_conn->run( fixup => sub {
242             ( $val ) = $_->selectrow_array( $site->SQL_SCRATCH_SID );
243             } );
244             } catch {
245 0     0     $status = $CELL->status_err( 'DOCHAZKA_DBI_ERR', args => [ $_ ] );
246             };
247             return if $status;
248             return $val;
249 0           }
250 0     0      
251              
252 0     0      
253 0           =head1 AUTHOR
254 0 0          
255 0           Nathan Cutler, C<< <presnypreklad@gmail.com> >>
256              
257             =cut
258              
259             1;
260