File Coverage

blib/lib/App/Dochazka/REST/Model/Activity.pm
Criterion Covered Total %
statement 36 68 52.9
branch 0 10 0.0
condition n/a
subroutine 12 20 60.0
pod 8 8 100.0
total 56 106 52.8


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   94491 use strict;
  41         135  
36 41     41   200 use warnings;
  41         92  
  41         752  
37 41     41   178 use App::CELL qw( $CELL $log $meta $site );
  41         79  
  41         1057  
38 41     41   190 use App::Dochazka::REST::Model::Shared qw( cud load load_multiple priv_by_eid );
  41         85  
  41         3713  
39 41     41   1975 use DBI;
  41         100  
  41         2203  
40 41     41   1599 use Params::Validate qw{:all};
  41         15373  
  41         1456  
41 41     41   262 use Try::Tiny;
  41         82  
  41         5572  
42 41     41   260  
  41         78  
  41         1976  
43             # we get 'spawn', 'reset', and accessors from parent
44             use parent 'App::Dochazka::Common::Model::Activity';
45 41     41   244  
  41         68  
  41         274  
46              
47              
48              
49             =head1 NAME
50              
51             App::Dochazka::REST::Model::Activity - activity data model
52              
53              
54              
55              
56             =head1 SYNOPSIS
57              
58             use App::Dochazka::REST::Model::Activity;
59              
60             ...
61              
62              
63             =head1 DATA MODEL
64              
65             =head2 Activities in the database
66              
67              
68             CREATE TABLE activities (
69             aid serial PRIMARY KEY,
70             code varchar(32) UNIQUE NOT NULL,
71             long_desc text,
72             remark text
73             )
74              
75             Activity codes will always be in ALL CAPS thanks to a trigger (entitled
76             C<code_to_upper>) that runs the PostgreSQL C<upper> function on the code
77             before every INSERT and UPDATE on this table.
78              
79              
80              
81             =head2 Activities in the Perl API
82              
83             =over
84              
85             =item * constructor (L<spawn>)
86              
87             =item * basic accessors (L<aid>, L<code>, L<long_desc>, L<remark>)
88              
89             =item * L<reset> (recycles an existing object by setting it to desired state)
90              
91             =item * L<TO_JSON> (returns 'unblessed' version of an Activity object)
92              
93             =item * L<compare> (compare two objects)
94              
95             =item * L<clone> (clone an object)
96              
97             =item * L<insert> (inserts object into database)
98              
99             =item * L<update> (updates database to match the object)
100              
101             =item * L<delete> (deletes record from database if nothing references it)
102              
103             =item * L<load_by_aid> (loads a single activity into an object)
104              
105             =item * L<load_by_code> (loads a single activity into an object)
106              
107             =item * L<get_all_activities> (load all activities)
108              
109             =back
110              
111             L<App::Dochazka::REST::Model::Activity> also exports some convenience
112             functions:
113              
114             =over
115              
116             =item * L<aid_exists> (boolean function)
117              
118             =item * L<code_exists> (boolean function)
119              
120             =item * L<aid_by_code> (given a code, returns AID)
121              
122             =item * L<code_by_aid> (given an AID, return a code)
123              
124             =item * L<get_all_activities>
125              
126             =back
127              
128             For basic C<activity> object workflow, see the unit tests in
129             C<t/model/activity.t>.
130              
131              
132              
133             =head1 EXPORTS
134              
135             This module provides the following exports:
136              
137             =over
138              
139             =item C<aid_by_code> - function
140              
141             =back
142              
143             =cut
144              
145             use Exporter qw( import );
146 41     41   83492 our @EXPORT_OK = qw(
  41         97  
  41         19010  
147             aid_by_code
148             aid_exists
149             code_by_aid
150             code_exists
151             get_all_activities
152             );
153              
154              
155              
156              
157             =head1 METHODS
158              
159              
160             =head2 insert
161              
162             Instance method. Takes the object, as it is, and attempts to insert it into
163             the database. On success, overwrites object attributes with field values
164             actually inserted. Returns a status object.
165              
166             =cut
167              
168             my $self = shift;
169             my ( $context ) = validate_pos( @_, { type => HASHREF } );
170 0     0 1    
171 0           my $status = cud(
172             conn => $context->{'dbix_conn'},
173             eid => $context->{'current'}->{'eid'},
174             object => $self,
175 0           sql => $site->SQL_ACTIVITY_INSERT,
176             attrs => [ 'code', 'long_desc', 'remark' ],
177             );
178              
179             return $status;
180             }
181 0            
182              
183             =head2 update
184              
185             Instance method. Assuming that the object has been prepared, i.e. the AID
186             corresponds to the activity to be updated and the attributes have been
187             changed as desired, this function runs the actual UPDATE, hopefully
188             bringing the database into line with the object. Overwrites all the
189             object's attributes with the values actually written to the database.
190             Returns status object.
191              
192             =cut
193              
194             my $self = shift;
195             my ( $context ) = validate_pos( @_, { type => HASHREF } );
196              
197 0     0 1   return $CELL->status_err( 'DOCHAZKA_MALFORMED_400' ) unless $self->{'aid'};
198 0            
199             my $status = cud(
200 0 0         conn => $context->{'dbix_conn'},
201             eid => $context->{'current'}->{'eid'},
202             object => $self,
203             sql => $site->SQL_ACTIVITY_UPDATE,
204 0           attrs => [ 'code', 'long_desc', 'remark', 'disabled', 'aid' ],
205             );
206              
207             return $status;
208             }
209              
210 0            
211             =head2 delete
212              
213             Instance method. Assuming the AID really corresponds to the activity to be
214             deleted, this method will execute the DELETE statement in the database. It
215             won't succeed if the activity has any intervals associated with it. Returns
216             a status object.
217              
218             =cut
219              
220             my $self = shift;
221             my ( $context ) = validate_pos( @_, { type => HASHREF } );
222              
223             my $status = cud(
224 0     0 1   conn => $context->{'dbix_conn'},
225 0           eid => $context->{'current'}->{'eid'},
226             object => $self,
227             sql => $site->SQL_ACTIVITY_DELETE,
228             attrs => [ 'aid' ],
229 0           );
230             $self->reset( aid => $self->{aid} ) if $status->ok;
231              
232             return $status;
233             }
234 0 0          
235              
236 0           =head2 load_by_aid
237              
238             Loads activity from database, by the AID provided in the argument list,
239             into a newly-spawned object. The code must be an exact match. Returns a
240             status object: if the object is loaded, the code will be
241             'DISPATCH_RECORDS_FOUND' and the object will be in the payload; if
242             the AID is not found in the database, the code will be
243             'DISPATCH_NO_RECORDS_FOUND'. A non-OK status indicates a DBI error.
244              
245             =cut
246              
247             my $self = shift;
248             my ( $conn, $aid ) = validate_pos( @_,
249             { isa => 'DBIx::Connector' },
250             { type => SCALAR },
251             );
252 0     0 1    
253 0           return load(
254             conn => $conn,
255             class => __PACKAGE__,
256             sql => $site->SQL_ACTIVITY_SELECT_BY_AID,
257             keys => [ $aid ],
258 0           );
259             }
260              
261              
262             =head2 load_by_code
263              
264             Analogous method to L<"load_by_aid">.
265              
266             =cut
267              
268             my $self = shift;
269             my ( $conn, $code ) = validate_pos( @_,
270             { isa => 'DBIx::Connector' },
271             { type => SCALAR },
272             );
273              
274 0     0 1   return load(
275 0           conn => $conn,
276             class => __PACKAGE__,
277             sql => $site->SQL_ACTIVITY_SELECT_BY_CODE,
278             keys => [ $code ],
279             );
280 0           }
281              
282              
283              
284              
285             =head1 FUNCTIONS
286              
287             The following functions are not object methods.
288              
289              
290             =head2 aid_exists
291              
292             Boolean function
293              
294              
295             =head2 code_exists
296              
297             Boolean function
298              
299             =cut
300              
301             BEGIN {
302             no strict 'refs';
303             *{'aid_exists'} = App::Dochazka::REST::Model::Shared::make_test_exists( 'aid' );
304             *{'code_exists'} = App::Dochazka::REST::Model::Shared::make_test_exists( 'code' );
305             }
306              
307              
308 41     41   289 =head2 aid_by_code
  41         101  
  41         2160  
309 41     41   290  
  41         197  
310 41         172 Given a code, attempt to retrieve the corresponding AID.
  41         11387  
311             Returns AID or undef on failure.
312              
313             =cut
314              
315             my ( $conn, $code ) = validate_pos( @_,
316             { isa => 'DBIx::Connector' },
317             { type => SCALAR },
318             );
319              
320             my $status = __PACKAGE__->load_by_code( $conn, $code );
321             return $status->payload->{'aid'} if $status->code eq 'DISPATCH_RECORDS_FOUND';
322 0     0 1   return;
323             }
324              
325              
326             =head2 code_by_aid
327 0            
328 0 0         Given an AID, attempt to retrieve the corresponding code.
329 0           Returns code or undef on failure.
330              
331             =cut
332              
333             my ( $conn, $aid ) = validate_pos( @_,
334             { isa => 'DBIx::Connector' },
335             { type => SCALAR },
336             );
337              
338             my $status = __PACKAGE__->load_by_aid( $conn, $aid );
339             return $status->payload->{'code'} if $status->code eq 'DISPATCH_RECORDS_FOUND';
340             return;
341 0     0 1   }
342              
343              
344             =head2 get_all_activities
345              
346 0           Optionally takes a PARAMHASH that can contain a 'disabled' key which can be
347 0 0         either true or false (defaults to false).
348 0            
349             Returns a reference to a hash of hashes, where each hash is one activity object.
350             If 'disabled' is true, all activities including disabled ones will be included,
351             otherwise only the non-disabled activities will be retrieved.
352              
353             =cut
354              
355             my $conn = shift;
356             my %PH = validate( @_, {
357             disabled => { type => SCALAR, default => 0 }
358             } );
359            
360             my $sql = $PH{disabled}
361             ? $site->SQL_ACTIVITY_SELECT_ALL_INCLUDING_DISABLED
362             : $site->SQL_ACTIVITY_SELECT_ALL_EXCEPT_DISABLED;
363              
364 0     0 1   return load_multiple(
365 0           conn => $conn,
366             class => __PACKAGE__,
367             sql => $sql,
368             keys => [],
369             );
370 0 0         }
371              
372              
373 0            
374              
375             =head1 AUTHOR
376              
377             Nathan Cutler, C<< <presnypreklad@gmail.com> >>
378              
379             =cut
380              
381             1;
382