File Coverage

blib/lib/App/Dochazka/REST/Model/Lock.pm
Criterion Covered Total %
statement 31 59 52.5
branch 0 8 0.0
condition 0 3 0.0
subroutine 11 17 64.7
pod 6 6 100.0
total 48 93 51.6


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   650 use strict;
  41         123  
36 41     41   195 use warnings;
  41         118  
  41         681  
37 41     41   164 use App::CELL qw( $CELL $log $meta $site );
  41         71  
  41         1163  
38 41     41   191 use App::Dochazka::REST::Model::Shared qw( cud load load_multiple );
  41         73  
  41         3741  
39 41     41   280 use Data::Dumper;
  41         89  
  41         2426  
40 41     41   259 use Params::Validate qw( :all );
  41         79  
  41         1737  
41 41     41   235  
  41         94  
  41         5360  
42             # we get 'spawn', 'reset', and accessors from parent
43             use parent 'App::Dochazka::Common::Model::Lock';
44 41     41   264  
  41         81  
  41         287  
45              
46              
47              
48             =head1 NAME
49              
50             App::Dochazka::REST::Model::Lock - lock data model
51              
52              
53              
54              
55             =head1 SYNOPSIS
56              
57             use App::Dochazka::REST::Model::Lock;
58              
59             ...
60              
61              
62             =head1 DESCRIPTION
63              
64             A description of the lock data model follows.
65              
66              
67             =head2 Locks in the database
68              
69             CREATE TABLE locks (
70             lid serial PRIMARY KEY,
71             eid integer REFERENCES Employees (EID),
72             intvl tsrange NOT NULL,
73             remark text
74             )
75              
76             There is also a stored procedure, C<fully_locked>, that takes an EID
77             and a tsrange, and returns a boolean value indicating whether or not
78             that period is fully locked for the given employee.
79              
80              
81             =head3 Locks in the Perl API
82              
83             # FIXME: MISSING VERBIAGE
84              
85              
86              
87              
88             =head1 EXPORTS
89              
90             This module provides the following exports:
91              
92             =cut
93              
94             use Exporter qw( import );
95 41     41   41015 our @EXPORT_OK = qw(
  41         90  
  41         15365  
96             count_locks_in_tsrange
97             fetch_locks_by_eid_and_tsrange
98             lid_exists
99             );
100              
101              
102              
103             =head1 METHODS
104              
105              
106             =head2 load_by_lid
107              
108             Instance method. Given an LID, loads a single lock into the object, rewriting
109             whatever was there before. Returns a status object.
110              
111             =cut
112              
113             my $self = shift;
114             my ( $conn, $lid ) = validate_pos( @_,
115 0     0 1   { isa => 'DBIx::Connector' },
116 0           { type => SCALAR },
117             );
118              
119             return load(
120             conn => $conn,
121 0           class => __PACKAGE__,
122             sql => $site->SQL_LOCK_SELECT_BY_LID,
123             keys => [ $lid ],
124             );
125             }
126            
127              
128              
129             =head2 insert
130              
131             Instance method. Attempts to INSERT a record. Field values are taken from the
132             object. Returns a status object.
133              
134             =cut
135              
136             my $self = shift;
137             my ( $context ) = validate_pos( @_, { type => HASHREF } );
138              
139 0     0 1   my $status = cud(
140 0           conn => $context->{'dbix_conn'},
141             eid => $context->{'current'}->{'eid'},
142             object => $self,
143             sql => $site->SQL_LOCK_INSERT,
144 0           attrs => [ 'eid', 'intvl', 'remark' ],
145             );
146              
147             return $status;
148             }
149              
150 0            
151             =head2 update
152              
153             Instance method. Attempts to UPDATE a record. Field values are taken from the
154             object. Returns a status object.
155              
156             =cut
157              
158             my $self = shift;
159             my ( $context ) = validate_pos( @_, { type => HASHREF } );
160              
161             return $CELL->status_err( 'DOCHAZKA_MALFORMED_400' ) unless $self->{'lid'};
162 0     0 1    
163 0           my $status = cud(
164             conn => $context->{'dbix_conn'},
165 0 0         eid => $context->{'current'}->{'eid'},
166             object => $self,
167             sql => $site->SQL_LOCK_UPDATE,
168             attrs => [ 'eid', 'intvl', 'remark', 'lid' ],
169 0           );
170              
171             return $status;
172             }
173              
174              
175 0           =head2 delete
176              
177             Instance method. Attempts to DELETE a record. Field values are taken from the
178             object. Returns a status object.
179              
180             =cut
181              
182             my $self = shift;
183             my ( $context ) = validate_pos( @_, { type => HASHREF } );
184              
185             my $status = cud(
186             conn => $context->{'dbix_conn'},
187 0     0 1   eid => $context->{'current'}->{'eid'},
188 0           object => $self,
189             sql => $site->SQL_LOCK_DELETE,
190             attrs => [ 'lid' ],
191             );
192 0           $self->reset( lid => $self->{lid} ) if $status->ok;
193              
194             #$log->debug( "Returning from " . __PACKAGE__ . "::delete with status code " . $status->code );
195             return $status;
196             }
197 0 0          
198              
199              
200 0           =head1 FUNCTIONS
201              
202              
203             =head2 lid_exists
204              
205             Boolean
206              
207             =cut
208              
209             BEGIN {
210             no strict 'refs';
211             *{'lid_exists'} = App::Dochazka::REST::Model::Shared::make_test_exists( 'lid' );
212             }
213              
214              
215 41     41   302 =head2 fetch_locks_by_eid_and_tsrange
  41         109  
  41         1647  
216 41     41   223  
  41         9879  
217             Given a L<DBIx::Connector> object, an EID, and a tsrange, returns a status
218             object. Upon successfully finding one or more locks, the payload will
219             be an ARRAYREF of lock records.
220              
221             =cut
222              
223             my ( $conn, $eid, $tsrange ) = validate_pos( @_,
224             { isa => 'DBIx::Connector' },
225             { type => SCALAR },
226             { type => SCALAR, optional => 1 },
227             );
228              
229 0     0 1   return load_multiple(
230             conn => $conn,
231             class => __PACKAGE__,
232             sql => $site->SQL_LOCK_SELECT_BY_EID_AND_TSRANGE,
233             keys => [ $eid, $tsrange ],
234             );
235 0           }
236              
237              
238             =head2 count_locks_in_tsrange
239              
240             Given a L<DBIx::Connector> object, an EID, and a tsrange, returns a status
241             object. If the level is OK, the payload can be expected to contain an integer
242             representing the number of locks that overlap (contain points in common) with
243             this tsrange.
244              
245             =cut
246              
247             my ( $conn, $eid, $tsrange ) = validate_pos( @_,
248             { isa => 'DBIx::Connector' },
249             { type => SCALAR },
250             { type => SCALAR, optional => 1 },
251             );
252              
253             my $status = fetch_locks_by_eid_and_tsrange( $conn, $eid, $tsrange );
254 0     0 1   if ( $status->ok ) {
255             my $count = @{ $status->payload };
256             return $CELL->status_ok( "DOCHAZKA_NUMBER_OF_LOCKS", payload => $count );
257             }
258             if ( $status->level eq 'NOTICE' and $status->code eq 'DISPATCH_NO_RECORDS_FOUND' ) {
259             return $CELL->status_ok( "DOCHAZKA_NUMBER_OF_LOCKS", payload => 0 );
260 0           }
261 0 0         return $status;
262 0           }
  0            
263 0            
264              
265 0 0 0       =head1 AUTHOR
266 0            
267             Nathan Cutler, C<< <presnypreklad@gmail.com> >>
268 0            
269             =cut
270              
271             1;
272              
273