File Coverage

blib/lib/MyLibrary/Resource/Location/Type.pm
Criterion Covered Total %
statement 27 74 36.4
branch 7 28 25.0
condition 0 3 0.0
subroutine 8 11 72.7
pod 7 7 100.0
total 49 123 39.8


line stmt bran cond sub pod time code
1             package MyLibrary::Resource::Location::Type;
2              
3 4     4   1149 use MyLibrary::DB;
  4         12  
  4         133  
4 4     4   21 use Carp qw(croak);
  4         9  
  4         199  
5 4     4   20 use strict;
  4         8  
  4         2804  
6              
7             =head1 NAME
8              
9             MyLibrary::Resource::Location::Type
10              
11             =head1 SYNOPSIS
12              
13              
14             # require the necessary module
15             use MyLibrary::Resource::Location::Type;
16              
17             # create a new Location Type object
18             my $location_type = MyLibrary::Resource::Location::Type->new();
19              
20             # set the attributes of a Location Type object
21             $location_type->name();
22             $location_type->description();
23              
24             # commit Location Type
25             $location_type->commit();
26              
27             # output the Location Type id
28             my $location_type_id = $location_type->location_type_id();
29              
30             # delete a Location Type from the database
31             $location_type->delete();
32              
33             # return a list of all type ids for processing
34             my @location_type_ids = MyLibrary::Resource::Location::Type->all_types();
35              
36             =head1 DESCRIPTION
37              
38             This is a sub-class of the Location class which is used to represent individual resource location types and allow manipulation of resource location type data. Each location will be assigned one location type. Multiple location types can exist, each representing a method by which a resource can be accessed. Certain resources will be accessed only via physical methods, while others will be purely digital. Each type should have a description assigned which clearly states the mode of access for that type.
39              
40             =head1 METHODS
41              
42             =head2 new()
43              
44             This constructor method is used to create a resource location type object. The object can then be manipulated using the various accessor methods supplied with this module.
45              
46             # create a new resource location type object
47             my $resource_location_type = MyLibrary::Resource::Location::Type->new();
48              
49             This method can also be called using an id parameter which will then return an object using the persistent data store. If the called location does not exist, this method will return 'undef'.
50              
51             # create an object from persistent data using the location type id
52             my $resource_location_type = MyLibrary::Resource::Location::Type->new(id => $loc_id);
53              
54             This method can also be called using the 'name' parameter, so that an object can be created based on the name of the location type. Each location type name must be unique, so only one type will be retrieved using the method in this fashion.
55              
56             # create an object from persistent data using the location type name
57             my $resource_location_type = MyLibrary::Resource::Location::Type->new(name => $loc_name);
58              
59             =cut
60              
61             sub new {
62              
63 2     2 1 794 my ($class, %opts) = @_;
64 2         4 my $self = {};
65              
66 2 50       16 if ($opts{id}) {
    100          
67              
68 0         0 my $dbh = MyLibrary::DB->dbh();
69 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM resource_location_type WHERE type_id = ?', undef, $opts{id});
70              
71 0 0       0 if (ref($rv) eq "HASH") {
72 0         0 $self = $rv;
73             } else {
74 0         0 return;
75             }
76              
77             } elsif ($opts{name}) {
78              
79 1         10 my $dbh = MyLibrary::DB->dbh();
80 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM resource_location_type WHERE type_name = ?', undef, $opts{name});
81              
82 0 0       0 if (ref($rv) eq "HASH") {
83 0         0 $self = $rv;
84             } else {
85 0         0 return;
86             }
87             }
88              
89             # return the object
90 1         10 return bless $self, $class;
91              
92             }
93              
94              
95             =head2 name()
96              
97             This attribute method should be used to either set or retrieve the name for this location type. This name will appear in any context where location type labeling is required.
98              
99             # get location type name
100             my $location_type_name = $location_type->name();
101              
102             # set the location type name
103             $location_type->name('URL WEB SITE');
104              
105             =cut
106              
107             sub name {
108              
109 2     2 1 419 my ($self, $name) = @_;
110 2 100       6 if ($name) {
111 1         8 $self->{type_name} = $name;
112             } else {
113 1         7 return $self->{type_name};
114             }
115             }
116              
117             =head2 description()
118              
119             The description should indicate the mode of access for a location type. This will assist in the usage of a particular type.
120              
121             # retrieve the current location type
122             my $location_type_description = $location_type->description();
123              
124             # set the location type description
125             $location_type->description('This location type applies to any resource which is web accessible.');
126              
127             =cut
128              
129             sub description {
130              
131 2     2 1 4 my ($self, $description) = @_;
132 2 100       6 if ($description) {
133 1         3 $self->{type_description} = $description;
134             } else {
135 1         3 return $self->{type_description};
136             }
137              
138             }
139              
140             =head2 location_type_id()
141              
142             This method can only be used to retrieve the location type id of the current location type. It cannot be used to set the location type id, as this is set internally.
143              
144             # get the current location type id
145             my $location_type_id = location_type_id();
146              
147             =cut
148              
149             sub location_type_id {
150              
151 0     0 1 0 my $self = shift;
152 0 0       0 if ($self->{type_id}) {
153 0         0 return $self->{type_id};
154             } else {
155 0         0 return;
156             }
157              
158             }
159              
160             =head2 commit()
161              
162             This method is used to save a location type to the database.
163              
164             # commit the location type
165             $location_type->commit();
166              
167             =cut
168              
169             sub commit {
170              
171 1     1 1 2 my $self = shift;
172              
173 1         7 my $dbh = MyLibrary::DB->dbh();
174              
175 0 0         if ($self->location_type_id()) {
176              
177 0           my $return = $dbh->do('UPDATE resource_location_type SET type_name = ?, type_description = ? WHERE type_id = ?', undef, $self->name(), $self->description(), $self->location_type_id());
178 0 0 0       if ($return > 1 || $return eq undef) { croak "Location type update in commit() failed. $return records were updated." }
  0            
179              
180             } else {
181            
182 0           my $id = MyLibrary::DB->nextID();
183              
184 0           my $return = $dbh->do('INSERT INTO resource_location_type (type_id, type_name, type_description) VALUES (?, ?, ?)', undef, $id, $self->name(), $self->description());
185 0 0         if ($return != 1) { croak 'Location type commit() failed.'; }
  0            
186 0           $self->{type_id} = $id;
187             }
188              
189 0           return 1;
190              
191             }
192              
193             =head2 delete()
194              
195             Use this method to remove a location type from the database
196              
197             # remove a location type from the database
198             $location_type->delete();
199              
200             =cut
201              
202             sub delete {
203              
204 0     0 1   my $self = shift;
205 0           my $dbh = MyLibrary::DB->dbh();
206 0           my $rv = $dbh->do('DELETE FROM resource_location_type WHERE type_id = ?', undef, $self->location_type_id());
207 0           my $return_code;
208 0 0         if ($rv != 1) {
209 0           croak ('Deletion of resource location failed in delete() method.');
210             } else {
211 0           $return_code = "$rv";
212             }
213              
214             # delete any dangling locations using this location type
215 0           my $location_ids = $dbh->selectcol_arrayref('SELECT resource_location_id FROM resource_location WHERE resource_location_type = ?', undef, $self->location_type_id());
216 0 0         if (scalar(@{$location_ids}) >= 1) {
  0            
217 0           foreach my $location_id (@$location_ids) {
218 4     4   795 use MyLibrary::Resource::Location;
  4         7  
  4         690  
219 0           my $delete_location = MyLibrary::Resource::Location->new(id => $location_id);
220 0           $delete_location->delete();
221             }
222             }
223              
224 0           return $return_code;
225              
226             }
227              
228             =head2 all_types()
229              
230             This is a class method which will return upon invocation the full list of location type ids. If no location types exist in the databse, the method will return undef.
231              
232             # return a list of all location type ids
233             my @location_type_ids = MyLibrary::Resource::Location::Type->all_types();
234              
235             =cut
236              
237             sub all_types {
238              
239 0     0 1   my $class = shift;
240              
241 0 0         unless ($class eq 'MyLibrary::Resource::Location::Type') {
242              
243 0           croak ("This method must be called as a class method. $class was the invocant.");
244              
245             }
246            
247 0           my $dbh = MyLibrary::DB->dbh();
248 0           my $type_ids = $dbh->selectcol_arrayref('SELECT type_id FROM resource_location_type');
249              
250 0 0         if (scalar(@{$type_ids}) >= 1) {
  0            
251              
252 0           return @{$type_ids};
  0            
253              
254             } else {
255              
256 0           return;
257              
258             }
259              
260             }
261              
262             =head1 SEE ALSO
263              
264             For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
265              
266             =head1 AUTHOR
267              
268             Robert Fox
269              
270             =cut
271              
272             return 1;