File Coverage

blib/lib/MyLibrary/Resource/Location.pm
Criterion Covered Total %
statement 24 191 12.5
branch 5 88 5.6
condition 0 24 0.0
subroutine 5 16 31.2
pod 11 12 91.6
total 45 331 13.6


line stmt bran cond sub pod time code
1             package MyLibrary::Resource::Location;
2              
3 4     4   71431 use MyLibrary::DB;
  4         10  
  4         109  
4 4     4   2167 use MyLibrary::Resource::Location::Type;
  4         9  
  4         117  
5 4     4   56 use Carp qw(croak);
  4         6  
  4         181  
6 4     4   29 use strict;
  4         7  
  4         8525  
7              
8             =head1 NAME
9              
10             MyLibrary::Resource::Location - A class for representing MyLibrary resource locations
11              
12             =head1 SYNOPSIS
13              
14             # module may be called explicitly
15             use MyLibrary::Resource::Location;
16              
17             # create a new resource location object
18             my $resource_location = MyLibrary::Resource::Location->new();
19              
20             # set attributes of object
21             $resource_location->location('http://mysite.com');
22             $resource_location->location_note('This is mysite');
23             $resource_location->resource_location_type(25);
24             $resource_location->resource_id(45);
25              
26             # remove value for location note
27             $resource_location->delete_location_note();
28              
29             # save to database
30             $resource_location->commit();
31              
32             # retrieve resource location id
33             my $resource_location_id = $resource_location->id();
34              
35             # retrieve object by location id
36             my $resource_location = MyLibrary::Resource::Location->new(id => $resource_location_id);
37              
38             # retrieve object attributes
39             my $resource_location_location = $resource_location->location();
40             my $resource_location_note = $resource_location->location_note();
41             my $resource_location_type_id = $resource_location->resource_location_type();
42              
43             # retrieve all location objects associated with a resource
44             my @resource_location_objects = MyLibrary::Resource::Location->get_locations(id => $resource_id);
45              
46             # retrieve list (array) of all resource location ids
47             my @resource_location_ids = MyLibrary::Resource::Location->id_list();
48              
49             # delete a specific resource location
50             $resource_location->delete();
51              
52             # create a new resource location type
53             MyLibrary::Resource::Location->location_type(action => 'set', name => 'Call Number', description => 'Call number location using the Library of Congress System');
54              
55             # retrieve a location type name based on location type id
56             my $location_type_name = MyLibrary::Resource::Location->location_type(action => 'get_name', id => 25);
57              
58             # retrieve a location type id based on a location type name
59             my $location_type_id = MyLibrary::Resource::Location->location_type(action => 'get_id', name => 'Call Number');
60              
61             # retrieve a location type description based on a locatin type id
62             my $location_type_desc = MyLibrary::Resource::Location->location_type(action => 'get_desc', id => 25);
63              
64             # retrieve an array of all resource location type ids
65             my @resource_location_types = MyLibrary::Resource::Location->location_type(action => 'get_all');
66              
67             # delete a location type based on location type id
68             MyLibrary::Resource::Location->location_type(action => 'delete', id => 25);
69              
70             =head1 DESCRIPTION
71              
72             This is a sub-class of the Resource class which is used to represent individual resource locations and allow manipulation of resource location data. Several locations can be related to a particular resource. Each location has several attributes which describe the location and give the location information itself. Each location has a location type, and class methods are provided in this package which allow manipulation of the resource location types as well.
73              
74             =head1 METHODS
75              
76             =head2 new()
77              
78             This constructor method is used to create a resource location object. The object can then be manipulated using the various accessor methods supplied with this module.
79              
80             # create a new resource location object
81             my $resource_location = MyLibrary::Resource::Location->new();
82              
83             This method can also be called using an id parameter which will then return an object using the persisten data store. If the called location does not exist, this method will return 'undef'.
84              
85             # create an object from persistent data using the location id
86             my $resource_location = MyLibrary::Resource::Location->new(id => $loc_id);
87              
88             The method can also retrieve and build an object or objects using the location text for the location. Please note that this will return 'undef' if an exact match is not found. The type returned is based on context. If the method is called in scalar context, a single value representing either 'undef' for no results or the number of locations matching the criteria will be returned. If called in list context, the result will be an array of location objects matching the criteria.
89              
90             # determine the number of objects matching the location parameter (scalar context)
91             my $resource_location = MyLibrary::Resource::Location->new(location => 'http://mysite.com');
92              
93             # retrieve an array of location objects based on location string criteria (list context)
94             my @resource_locations = MyLibrary::Resource::Location->new(location => 'http://mysite.com');
95              
96             =cut
97              
98             sub new {
99              
100 0     0 1 0 my ($class, %opts) = @_;
101 0         0 my $self = {};
102              
103 0 0       0 if ($opts{id}) {
    0          
104              
105 0         0 my $dbh = MyLibrary::DB->dbh();
106              
107 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM resource_location WHERE resource_location_id = ?', undef, $opts{id});
108              
109 0 0       0 if (ref($rv) eq "HASH") {
110 0         0 $self = $rv;
111             } else {
112 0         0 return;
113             }
114              
115             } elsif ($opts{location}) {
116              
117 0         0 my $dbh = MyLibrary::DB->dbh();
118 0         0 my $rv = $dbh->selectall_hashref('SELECT * FROM resource_location WHERE resource_location = ?', 'resource_location_id', undef, $opts{location});
119 0 0       0 if (ref($rv) eq "HASH") {
120 0         0 my $num_records = keys %{$rv};
  0         0  
121 0 0       0 if (wantarray) {
122 0         0 my @return_records;
123 0         0 foreach my $resource_id (keys %{$rv}) {
  0         0  
124 0         0 my $resource = $rv->{$resource_id};
125 0         0 push(@return_records, bless($resource, $class));
126             }
127 0         0 return @return_records;
128             } else {
129 0         0 return $num_records;
130             }
131             } else {
132 0         0 return;
133             }
134            
135             }
136              
137             # return the object
138 0         0 return bless $self, $class;
139              
140             }
141              
142             =head2 commit()
143              
144             This method will save resource location object data to the database.
145              
146             # commit the resource location
147             $resource_location->commit();
148              
149             =cut
150              
151             sub commit {
152              
153 0     0 1 0 my $self = shift;
154              
155 0         0 my $dbh = MyLibrary::DB->dbh();
156            
157 0 0       0 if ($self->id()) {
158 0         0 my $return = $dbh->do('UPDATE resource_location SET resource_location = ?, resource_location_note = ?, resource_location_type = ?, resource_id = ? WHERE resource_location_id = ?', undef, $self->location(), $self->location_note(), $self->resource_location_type(), $self->resource_id(), $self->id());
159 0 0 0     0 if ($return > 1 || ! $return) { croak "Resource location update in commit() failed. $return records were updated." }
  0         0  
160             } else {
161              
162 0         0 my $id = MyLibrary::DB->nextID();
163              
164 0         0 my $return = $dbh->do('INSERT INTO resource_location (resource_location_id, resource_location, resource_location_note, resource_location_type, resource_id) VALUES (?,?,?,?,?)', undef, $id, $self->location(), $self->location_note(), $self->resource_location_type(), $self->resource_id());
165 0 0       0 if ($return != 1) { croak 'Resource location commit() failed.'; }
  0         0  
166 0         0 $self->{resource_location_id} = $id;
167              
168             }
169              
170 0         0 return 1;
171              
172             }
173              
174             =head2 id()
175              
176             This method can be used simply to retrieve the id (unique key) for the current object. It cannot be used to set the id, which is determined by the database.
177              
178             # retrieve the resource location id
179             my $resource_loc_id = $resource_location->id();
180              
181             =cut
182              
183             sub id {
184              
185 0     0 1 0 my $self = shift;
186 0 0       0 if ($self->{resource_location_id}) {
187 0         0 return $self->{resource_location_id};
188             } else {
189 0         0 return;
190             }
191              
192             }
193              
194             =head2 location()
195              
196             This attribute method can be used to either retrieve or set the resource location attribute. The value entered should match the type of location chosen for the object.
197              
198             # get location name
199             my $location_name = $resource_location->location();
200              
201             # set the location
202             $resource_location->location('http://mysite.com');
203              
204             =cut
205              
206             sub location {
207              
208 0     0 1 0 my ($self, $location) = @_;
209 0 0       0 if ($location) {
210 0         0 $self->{resource_location} = $location;
211             } else {
212 0         0 return $self->{resource_location};
213             }
214              
215             }
216              
217             =head2 location_note()
218              
219             This attribute method is used to either get or set the note for the resource.
220              
221             # get the resource location note
222             my $resource_loc_note = $resource_location->location_note();
223              
224             # set the resource location note
225             $resource_location->location_note('This is the note for my resource');
226              
227             =cut
228              
229             sub location_note {
230              
231 0     0 1 0 my ($self, $note) = @_;
232 0 0       0 if ($note) {
233 0         0 $self->{resource_location_note} = $note;
234             } else {
235 0         0 return $self->{resource_location_note};
236             }
237              
238             }
239              
240             sub delete_location_note {
241              
242 0     0 0 0 my $self = shift;
243 0         0 $self->{resource_location_note} = undef;
244              
245             }
246              
247             =head2 resource_location_type()
248              
249             This method should be used to get or set the resource type id for this location. The input must be an integer that matches a location type id from the database.
250              
251             # get the resource location type id
252             my $resource_loc_type_id = $resource_location->resource_location_type();
253              
254             # set the resource location type id
255             $resource_location->location_type($type_id);
256              
257             =cut
258              
259             sub resource_location_type {
260              
261 0     0 1 0 my ($self, $type) = @_;
262 0 0       0 if ($type) {
263 0         0 my $dbh = MyLibrary::DB->dbh();
264 0         0 my $type_ids = $dbh->selectcol_arrayref('SELECT type_id FROM resource_location_type');
265 0         0 my $found = 0;
266 0         0 foreach my $resource_location_type_id (@{$type_ids}) { # does this type exist?
  0         0  
267 0 0       0 if ($resource_location_type_id == $type) {
268 0         0 $found = 1;
269             }
270             }
271 0 0       0 unless (!$found) {
272 0         0 $self->{resource_location_type} = $type;
273 0         0 return $self->{resource_location_type};
274             } else {
275 0         0 croak ('Type id used as parameter for location_type() method does not exist.');
276             }
277             } else {
278 0         0 return $self->{resource_location_type};
279             }
280              
281             }
282              
283             =head2 resource_id()
284              
285             This method can be used either to retrieve or set the resource id which is related to this location. The resource which corresponds to this id must already exist in the database or this method will fail when used as a 'set' method. For various reasons, this behavior can also be turned off using the 'strict => 'off'' flag. Keep in mind that turning off relational integrity checks could compromise the data.
286              
287             # get the related resource id
288             my $resource_id = $resource_location->resource_id();
289              
290             # set the related resource id
291             $resource_location->resource_id($resource_id);
292              
293             # turn relational integrity checking off
294             $resource_location->resource_id($resource_id, strict => 'off');
295              
296             =cut
297              
298             sub resource_id {
299              
300 0     0 1 0 my $self = shift;
301 0         0 my $resource_id = shift;
302 0         0 my %opts = @_;
303 0 0 0     0 if ($resource_id && $resource_id !~ /^\d+$/) {
304 0         0 croak ('Resource id input for resource_id method must be an integer.');
305             }
306 0 0       0 if ($resource_id) {
307 0 0 0     0 unless ($opts{strict} and $opts{strict} eq 'off') {
308 0         0 my $dbh = MyLibrary::DB->dbh();
309 0         0 my $resource_ids = $dbh->selectcol_arrayref('SELECT resource_id FROM resources');
310 0         0 my $found = 0;
311 0         0 foreach my $database_resource_id (@{$resource_ids}) { # does this resource exist?
  0         0  
312 0 0       0 if ($database_resource_id == $resource_id) {
313 0         0 $found = 1;
314             }
315             }
316 0 0       0 unless (!$found) {
317 0         0 $self->{resource_id} = $resource_id;
318 0         0 return $self->{resource_id};
319             } else {
320 0         0 croak ('Resource id used as parameter for resource_id() method does not exist.');
321             }
322             } else {
323 0         0 $self->{resource_id} = $resource_id;
324 0         0 return $self->{resource_id};
325             }
326             } else {
327 0         0 return $self->{resource_id};
328             }
329             }
330              
331             =head2 get_locations()
332              
333             This class method serves, primarily, the requirements of the parent resource object (defined in Resource.pm). This method will retrieve all of the resource objects associated with a particular resource. The accessor methods in this package are then used to access the attributes of the location objects. This method returns an array of object references.
334              
335             # retrieve the complete set of resource location objects
336             my @resource_locations = MyLibrary::Resource::Location->get_locations();
337              
338             # via a specific resource id
339             my @resource_locations = MyLibrary::Resource::Location->get_locations(id => 27);
340              
341             # the method called from Resource.pm is similar
342             my @resource_locations = $resource->get_locations();
343              
344             The attributes of the objects can be manipulated using either the local package methods or methods which are supplied with the resource module itself. In the latter instance, only locations associated with that particular resource can be accessed.
345              
346             # access attributes using local (Resource::Location.pm) methods
347             my $resource_location = $resource_location->location();
348             my $resource_location_note = $resource_location->note();
349              
350             # manipulate the attributes (Resource.pm methods)
351             my $location_name = $resource->location_name();
352             my $location_note = $resource->location_note();
353              
354             =cut
355              
356             sub get_locations {
357              
358 0     0 1 0 my $self = shift;
359 0         0 my %opts = @_;
360              
361 0         0 my $resource_id = $opts{'id'};
362 0         0 my @location_objs;
363 0         0 my $dbh = MyLibrary::DB->dbh();
364 0 0       0 if ($resource_id) {
365 0         0 my $locations_ids = $dbh->selectcol_arrayref('SELECT resource_location_id FROM resource_location WHERE resource_id = ? ORDER BY resource_location_id', undef, $resource_id);
366 0         0 foreach my $location_id (@{$locations_ids}) {
  0         0  
367 0         0 push(@location_objs, MyLibrary::Resource::Location->new(id => $location_id));
368             }
369             } else {
370 0         0 my $locations_ids = $dbh->selectcol_arrayref('SELECT resource_location_id FROM resource_location ORDER BY resource_location_id');
371 0         0 foreach my $location_id (@{$locations_ids}) {
  0         0  
372 0         0 push(@location_objs, MyLibrary::Resource::Location->new(id => $location_id));
373             }
374             }
375              
376 0         0 return @location_objs;
377             }
378              
379             =head2 id_list()
380              
381             If only the list of resource location ids is required in order to avoid the overhead of dealing with fully fleged objects, that array can be retrieved using this class method. As implied, this method will simply return a list of location ids which can then be subsequently used to process through the list of location ids.
382              
383             # get full list of location ids
384             my @resource_loc_ids = MyLibrary::Resource::Location->id_list();
385              
386             =cut
387              
388             sub id_list {
389              
390 0     0 1 0 my $self = shift;
391 0         0 my @location_ids;
392 0         0 my $dbh = MyLibrary::DB->dbh();
393 0         0 my $locations_ids = $dbh->selectcol_arrayref('SELECT resource_location_id FROM resource_location');
394 0         0 return @{$locations_ids};
  0         0  
395              
396             }
397              
398             =head2 delete()
399              
400             This method can be used to delete a specific resource location.
401              
402             # delete a resource location
403             $resource_location->delete();
404              
405             =cut
406              
407             sub delete {
408              
409 0     0 1 0 my $self = shift;
410 0         0 my $dbh = MyLibrary::DB->dbh();
411 0         0 my $rv = $dbh->do('DELETE FROM resource_location WHERE resource_location_id = ?', undef, $self->id());
412 0 0       0 if ($rv != 1) {
413 0         0 croak ('Deletion of resource location failed in delete() method.');
414             } else {
415 0         0 return $rv;
416             }
417             }
418            
419             =head2 location_type()
420              
421             Location types for resource locations may be retrieved, set or deleted with this method. The required parameter is the type name. A description may also optionally be supplied. This is a class method.
422              
423             Location types can be created by supplying the required parameter and a flag must be set to indicate that this location type should be created. Location types that have identical names with types that already exist in the database will not be created. The key id for each new location type is generated by incrementing the highest key integer in the set of location type keys.
424              
425             A list of all of the current location type ids can be obtained by passing the action parameter 'get_all'. This will return an array of location type ids.
426              
427             NOTE: The MyLibrary::Resource::Location::Type class also allows for direct manipulation of location types. That class was developed after these methods, and thus, these methods are now deprecated. Use these class methods at your own risk.
428              
429             # create a resource location type
430             MyLibrary::Resource::Location->location_type(action => 'set', name => 'Call Number', description => 'Call number location using the Library of Congress System');
431              
432             # retrieve a location type name based on location type id
433             my $location_type_name = MyLibrary::Resource::Location->location_type(action => 'get_name', id => 25);
434              
435             # retrieve a location type id based on a location type name
436             my $location_type_id = MyLibrary::Resource::Location->location_type(action => 'get_id', name => 'Call Number');
437              
438             # retrieve a location type description based on a locatin type id
439             my $location_type_desc = MyLibrary::Resource::Location->location_type(action => 'get_desc', id => 25);
440              
441             # retrieve an array of all location type ids
442             my @resource_location_types = MyLibrary::Resource::Location->location_type(action => 'get_all');
443              
444             # delete a location type based on location type id
445             MyLibrary::Resource::Location->location_type(action => 'delete', id => 25);
446              
447             =cut
448              
449             sub location_type {
450              
451 1     1 1 1013 my $class = shift;
452 1         37 my %opts = @_;
453 1         2 my $action_type;
454              
455 1 50       9 if (!$opts{'action'}) {
    50          
456              
457 0         0 croak ('location_type() method requires an action parameter');
458              
459             } elsif ($opts{'action'}) {
460              
461 1         3 $action_type = $opts{'action'};
462              
463             }
464              
465 1 50       7 if ($action_type eq 'set') {
    0          
    0          
    0          
    0          
    0          
466              
467 1         2 my $name;
468             my $description;
469              
470 1 50       4 if (defined($opts{'name'})) {
471              
472 1         4 $name = $opts{'name'};
473              
474             } else {
475              
476 0         0 croak ('Set action called without required name parameter in location_type() method');
477              
478             }
479              
480 1 50       4 if (defined($opts{'description'})) {
481              
482 1         2 $description = $opts{'description'};
483             } else {
484              
485 0         0 $description = 'No description supplied.';
486             }
487              
488 1         13 my $found_type = MyLibrary::Resource::Location::Type->new(name => $name);
489              
490 0 0         unless ($found_type) {
491              
492             # create a new type
493 0           my $new_type = MyLibrary::Resource::Location::Type->new();
494 0           $new_type->name($name);
495 0           $new_type->description($description);
496 0           $new_type->commit();
497 0           my $id = $new_type->location_type_id();
498 0           return $id;
499              
500             } else {
501              
502 0           croak ('Duplicate resource location type found for location_type() method.');
503              
504             }
505              
506             } elsif ($action_type eq 'get_name') {
507              
508 0           my $id;
509              
510 0 0         if (defined($opts{'id'})) {
511              
512 0           $id = $opts{'id'};
513              
514             } else {
515              
516 0           croak ('get_name action called for location_type() method without required id parameter');
517             }
518              
519 0           my $location_type = MyLibrary::Resource::Location::Type->new(id => $id);
520              
521 0 0 0       if ($location_type && $location_type->isa('MyLibrary::Resource::Location::Type')) {
522              
523 0           my $name = $location_type->name();
524 0           return $name;
525              
526             } else {
527              
528 0           return;
529              
530             }
531              
532             } elsif ($action_type eq 'get_desc') {
533              
534 0           my $id;
535             my $type_name;
536              
537 0 0         if (defined($opts{'id'})) {
    0          
538              
539 0           $id = $opts{'id'};
540              
541             } elsif (defined($opts{'name'})) {
542              
543 0           $type_name = $opts{'name'};
544              
545             } else {
546              
547 0           croak ('A required parameter was not submitted to the location_type() method using the get_desc parameter');
548              
549             }
550              
551 0 0         if ($id) {
    0          
552              
553 0           my $location_type = MyLibrary::Resource::Location::Type->new(id => $id);
554              
555 0 0 0       if ($location_type && $location_type->isa('MyLibrary::Resource::Location::Type')) {
556              
557 0           my $description = $location_type->description();
558 0           return $description;
559              
560             } else {
561              
562 0           return;
563              
564             }
565              
566             } elsif ($type_name) {
567              
568 0           my $location_type = MyLibrary::Resource::Location::Type->new(name => $type_name);
569              
570 0 0 0       if ($location_type && $location_type->isa('MyLibrary::Resource::Location::Type')) {
571              
572 0           my $description = $location_type->description();
573 0           return $description;
574              
575             } else {
576              
577 0           return;
578              
579             }
580             }
581              
582             } elsif ($action_type eq 'get_id') {
583              
584 0           my $name;
585              
586 0 0         if (defined($opts{'name'})) {
587              
588 0           $name = $opts{'name'};
589              
590             } else {
591              
592 0           croak ('get_id action called for location_type() method without required name parameter');
593             }
594              
595 0           my $location_type = MyLibrary::Resource::Location::Type->new(name => $name);
596              
597 0 0 0       if ($location_type && $location_type->isa('MyLibrary::Resource::Location::Type')) {
598              
599 0           my $id = $location_type->location_type_id();
600 0           return $id;
601              
602             } else {
603              
604 0           return;
605              
606             }
607              
608             } elsif ($action_type eq 'get_all') {
609              
610 0           return(MyLibrary::Resource::Location::Type->all_types());
611              
612             } elsif ($action_type eq 'delete') {
613              
614 0           my $id;
615              
616 0 0         if (defined($opts{'id'})) {
617              
618 0           $id = $opts{'id'};
619              
620             } else {
621              
622 0           croak ('delete action called for location_type() method without required id parameter');
623              
624             }
625              
626 0           my $rv;
627              
628 0           my $location_type = MyLibrary::Resource::Location::Type->new(id => $id);
629              
630 0 0 0       unless ($location_type && $location_type->isa('MyLibrary::Resource::Location::Type')) {
631              
632 0           croak ('delete action called for location_type() failed');
633              
634             } else {
635              
636 0           $rv = $location_type->delete();
637              
638             }
639              
640 0           return $rv;
641             }
642             }
643              
644             =head1 SEE ALSO
645              
646             For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
647              
648             =head1 AUTHORS
649              
650             Eric Lease Morgan
651             Robert Fox
652              
653             =cut
654              
655             1;