File Coverage

blib/lib/MyLibrary/Facet.pm
Criterion Covered Total %
statement 23 108 21.3
branch 6 42 14.2
condition 0 12 0.0
subroutine 7 12 58.3
pod 9 9 100.0
total 45 183 24.5


line stmt bran cond sub pod time code
1             package MyLibrary::Facet;
2              
3 4     4   1412 use MyLibrary::DB;
  4         11  
  4         114  
4 4     4   22 use Carp qw(croak);
  4         8  
  4         230  
5 4     4   22 use strict;
  4         6  
  4         5084  
6              
7             =head1 NAME
8              
9             MyLibrary::Facet
10              
11             =head1 SYNOPSIS
12              
13             # require the necessary module
14             use MyLibrary::Facet;
15              
16             # create a new Facet object
17             my $facet = MyLibrary::Facet->new();
18              
19             # set attributes of facet object
20             $facet->facet_name('Facet Name');
21             $facet->facet_note('This is a facet note');
22              
23             # delete a facet note
24             $facet->delete_facet_note();
25              
26             # commit facet to database
27             $facet->commit();
28              
29             # delete facet from database
30             $facet->delete();
31              
32             # get all facets
33             my @facets = MyLibrary::Facet->get_facets();
34             my @facets = MyLibrary::Facet->get_facets(sort => 'name');
35              
36             # get specific facets based on criteria
37             my @facets = MyLibrary::Facet->get_facets(value => 'Discipline', field => 'name');
38              
39             # return related terms
40             my @related_terms = $facet->related_terms();
41              
42             # return a sorted list of related terms
43             my @related_terms = $facet->related_terms(sort => 'name');
44              
45              
46             =head1 DESCRIPTION
47              
48             The purpose of this module is to manipulate MyLibrary Facet objects and perform database I/O against the facets table of a MyLibrary database. You may also retrieve a list of facet objects by using a special class method. A list of term ids with which a particular facet is associated can be retrieved as well and manipulated independently. All changes to either the facet descriptive data can be commited to the database.
49              
50             =head1 METHODS
51              
52             =head2 new()
53              
54             This method creates a new facet object. Called with no input, this constructor will return a new, empty facet object:
55              
56             # create empty facet
57             $facet = MyLibrary::Facet->new();
58              
59             The constructor can also be called using a known facet id or facet name:
60              
61             # create a facet object using a known facet id
62             $facet = MyLibrary::Facet->new(id => $id);
63              
64             # create a facet object using a known facet name
65             $facet = MyLibrary::Facet->new(name => 'Disciplines');
66              
67             =head2 facet_id()
68              
69             This object method is used to retrieve the facet id of the current facet object. This method cannot be used to set the facet id.
70              
71             # get facet id
72             my $facet_id = $facet->facet_id();
73              
74             =head2 facet_name()
75              
76             This is an attribute method which allows you to either get or set the name attribute of a facet. The names for facets will be created by the institutional team tasked with the responsibility of designating the broad categories under which resources will be categorized. To retrieve the name attribute:
77              
78             # get the facet name
79             $facet->facet_name();
80              
81             # set the facet name
82             $facet->facet_name('Format');
83              
84             =head2 facet_note()
85              
86             This method allows one to either retrieve or set the facet descriptive note.
87              
88             To retrieve the note attribute:
89              
90             # get the facet note
91             $facet->facet_note();
92              
93             # set the facet note
94             $facet->facet_note('The subject area under which a resource may be classified.');
95              
96             =head2 delete_facet_note()
97              
98             This object attribute method allows the removal of the facet note
99              
100             # delete the facet note
101             $facet->delete_facet_note()
102              
103             =head2 commit()
104              
105             Use this method to commit the facet in memory to the database. Any updates made to facet attributes will be saved and new facets created will be saved. This method does not take any parameters.
106              
107             # commit the facet
108             $facet->commit();
109              
110             A numeric code will be returned upon successfull completion of the operation. A return code of 1 indicates a successful commit. Otherwise, the method will cease program execution and die with an appropriate error message.
111              
112             =head2 delete()
113              
114             Use this method to remove a facet record from the database. The record will be deleted permanently.
115              
116             # delete the facet
117             $facet->delete();
118              
119             =head2 get_facets()
120              
121             This method can be used to retrieve a list of all of the facets currently in the database. Individual facet objects will be created and can be cycled through. This is a class method, not an object method. If the sort parameter is supplied, the list of facet ids will be sorted. Currently, the list can only be sorted by facet name. A specific list of facets can also be queried for by using the field and value parameters. Searchable fields are name and description. Examples are demonstrated below.
122              
123             # get all facets
124             my @facets = MyLibrary::Facet->get_facets();
125              
126             # sort the returned list
127             my @facets = MyLibrary::Facet->get_facets(sort => 'name');
128              
129             # find all facets based on criteria
130             my @facets = MyLibrary::Facet->get_facets(value => 'Discpline', field => 'name');
131              
132             =head2 related_terms()
133              
134             This method should be used to return an array (a list) of term ids to which this facet is related. It requires no parameter input. If the facet is not related to any terms in the database, the method will return undef. The array of term ids can then be used to process the list of related terms. The returned list of term ids can also be sorted. This is an object method.
135              
136             # return related terms
137             my @related_terms = $facet->related_terms();
138              
139             # return sorted list of related terms
140             my @related_terms = $facet->related_terms(sort => 'name');
141              
142             =head1 AUTHORS
143              
144             Eric Lease Morgan
145             Robert Fox
146              
147             =cut
148              
149              
150             sub new {
151              
152             # declare local variables
153 1     1 1 762 my ($class, %opts) = @_;
154 1         3 my $self = {};
155              
156             # check for an id
157 1 50       8 if ($opts{id}) {
    50          
158            
159 0         0 my $dbh = MyLibrary::DB->dbh();
160 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM facets WHERE facet_id = ?', undef, $opts{id});
161 0 0       0 if (ref($rv) eq "HASH") {
162 0         0 $self = $rv;
163 0         0 $self->{related_terms} = $dbh->selectcol_arrayref('SELECT term_id FROM terms WHERE facet_id = ?', undef, $opts{id});
164             } else {
165 0         0 return;
166             }
167             } elsif ($opts{name}) {
168 0         0 my $dbh = MyLibrary::DB->dbh();
169 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM facets WHERE facet_name = ?', undef, $opts{name});
170 0 0       0 if (ref($rv) eq "HASH") {
171 0         0 $self = $rv;
172 0         0 $self->{related_terms} = $dbh->selectcol_arrayref('SELECT term_id FROM terms WHERE facet_id = ?', undef, $self->{facet_id});
173             } else {
174 0         0 return;
175             }
176             }
177            
178             # return the object
179 1         3 return bless $self, $class;
180            
181             }
182              
183              
184             sub facet_id {
185              
186 0     0 1 0 my $self = shift;
187 0         0 return $self->{facet_id};
188              
189             }
190              
191              
192             sub facet_name {
193              
194             # declare local variables
195 2     2 1 386 my ($self, $facet_name) = @_;
196            
197             # check for the existance of a note
198 2 100       5 if ($facet_name) { $self->{facet_name} = $facet_name }
  1         5  
199            
200             # return the name
201 2         9 return $self->{facet_name};
202            
203             }
204              
205              
206             sub facet_note {
207              
208             # declare local variables
209 2     2 1 5 my ($self, $facet_note) = @_;
210            
211             # check for the existance of a note
212 2 100       8 if ($facet_note) { $self->{facet_note} = $facet_note }
  1         3  
213            
214             # return the note
215 2         7 return $self->{facet_note};
216            
217             }
218              
219             sub delete_facet_note {
220              
221 0     0 1 0 my $self = shift;
222 0         0 $self->{facet_note} = undef;
223             }
224              
225              
226             sub commit {
227              
228             # get myself, :-)
229 1     1 1 17 my $self = shift;
230            
231             # get a database handle
232 1         5 my $dbh = MyLibrary::DB->dbh();
233            
234             # see if the object has an id
235 0 0         if ($self->facet_id()) {
236            
237             # update the record with this id
238 0           my $return = $dbh->do('UPDATE facets SET facet_name = ?, facet_note = ? WHERE facet_id = ?', undef, $self->facet_name(), $self->facet_note(), $self->facet_id());
239 0 0 0       if ($return > 1 || ! $return) { croak "Facet update in commit() failed. $return records were updated." }
  0            
240            
241             } else {
242            
243             # get a new sequence
244 0           my $id = MyLibrary::DB->nextID();
245             # create a new record
246 0           my $return = $dbh->do('INSERT INTO facets (facet_id, facet_name, facet_note) VALUES (?, ?, ?)', undef, $id, $self->facet_name(), $self->facet_note());
247 0 0 0       if ($return > 1 || ! $return) { croak 'Facet commit() failed.'; }
  0            
248 0           $self->{facet_id} = $id;
249            
250             }
251            
252             # done
253 0           return 1;
254            
255             }
256              
257              
258             sub delete {
259              
260 0     0 1   my $self = shift;
261              
262 0 0         if ($self->{facet_id}) {
263              
264 0           my $dbh = MyLibrary::DB->dbh();
265              
266             # delete any related terms first
267 0           my $term_ids = $dbh->selectcol_arrayref('SELECT term_id FROM terms WHERE facet_id = ?', undef, $self->{facet_id});
268 0 0         if (scalar(@{$term_ids}) >=1) {
  0            
269 0           require MyLibrary::Term;
270 0           foreach my $term_id (@{$term_ids}) {
  0            
271 0           my $term = MyLibrary::Term->new(id => $term_id);
272 0           $term->delete();
273             }
274             }
275              
276             # now, delete the primary facet record
277 0           my $rv = $dbh->do('DELETE FROM facets WHERE facet_id = ?', undef, $self->{facet_id});
278 0 0         if ($rv != 1) {croak ("Error deleting facet record. Deleted $rv records.");}
  0            
279            
280 0           return 1;
281              
282             }
283              
284 0           return 0;
285              
286             }
287              
288              
289             sub get_facets {
290              
291 0     0 1   my $self = shift;
292 0           my %opts = @_;
293 0           my @rv = ();
294              
295 0           my ($sort, $field, $value, $sort_clause, $limit_clause, $query);
296 0 0         if (defined($opts{sort})) {
297 0 0         if ($opts{sort} eq 'name') {
298 0           $sort_clause = 'ORDER BY facet_name';
299             }
300             }
301 0 0 0       if (defined($opts{field}) && defined($opts{value})) {
302 0           $field = $opts{'field'};
303 0           $value = $opts{'value'};
304 0 0         if ($field eq 'name') {
    0          
305 0           $limit_clause = "WHERE facet_name LIKE \'%$value%\'";
306             } elsif ($field eq 'description') {
307 0           $limit_clause = "WHERE facet_note LIKE \'%$value%\'";
308             }
309             }
310 0           $query = 'SELECT facet_id FROM facets';
311 0 0         if ($limit_clause) {
312 0           $query .= " $limit_clause";
313             }
314 0 0         if ($sort_clause) {
315 0           $query .= " $sort_clause";
316             }
317              
318             # create and execute a query
319 0           my $dbh = MyLibrary::DB->dbh();
320              
321 0           my $facet_ids = $dbh->selectcol_arrayref("$query");
322            
323 0           foreach my $facet_id (@$facet_ids) {
324 0           push (@rv, MyLibrary::Facet->new(id => $facet_id));
325             }
326            
327 0           return @rv;
328             }
329              
330             sub related_terms {
331              
332 0     0 1   my $self = shift;
333 0           my %opts = @_;
334 0           my $sort;
335 0 0         if (defined($opts{sort})) {
336 0           $sort = $opts{sort};
337             }
338 0           my @related_terms = ();
339 0           my $related_terms;
340             my $related_term_list;
341              
342 0           foreach my $term_id (@{$self->{related_terms}}) {
  0            
343 0           push (@related_terms, $term_id);
344 0           $related_term_list .= "$term_id, ";
345             }
346 0           chop($related_term_list);
347 0           chop($related_term_list);
348              
349 0 0 0       if ($sort && $sort eq 'name') {
350 0           my $dbh = MyLibrary::DB->dbh();
351 0           $related_terms = $dbh->selectcol_arrayref("SELECT term_id FROM terms WHERE term_id IN ($related_term_list) ORDER BY term_name");
352 0           @related_terms = ();
353 0           foreach my $related_term (@$related_terms) {
354 0           push (@related_terms, $related_term);
355             }
356             }
357              
358 0           return @related_terms;
359             }
360            
361             # return true, or else
362             1;