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