Facet.pm

filter: Statementstmt 22.0% 24 / 109 Branchbran 14.2% 6 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 58.3% 7 / 12 Podpod 100% 9 / 9 totaltotal 23.9% 46 / 192 CC 30 SCAR 37.8
CC
30
Cov
18%
get_facets
110.0
related_terms
42.0
commit
33.7
CRAP 43.8
« Auth/Basic.pm ↑ Summary Auth.pm »
1 package MyLibrary::Facet;
2  
3 4 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 <emorgan@nd.edu>
145 Robert Fox <rfox2@nd.edu>
146  
147 =cut
148  
149  
150 1 ▶sub new {
Subroutine new: called CC=5 SCAR=27.4
Pod documented
151  
152 # declare local variables
153 1 my ($class, %opts) = @_;
154 1 my $self = {};
155  
156 # check for an id
157 1 ▶ if ($opts{id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'id'}) { } 0 1 1
elsif ($opts{'name'}) { } 0 1 1
158
159 0 my $dbh = MyLibrary::DB->dbh();
160 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM facets WHERE facet_id = ?', undef, $opts{id});
161 0 ▶ if (ref($rv) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
162 0 $self = $rv;
163 0 $self->{related_terms} = $dbh->selectcol_arrayref('SELECT term_id FROM terms WHERE facet_id = ?', undef, $opts{id});
164 } else {
165 0 return;
166 }
167 } elsif ($opts{name}) {
168 0 my $dbh = MyLibrary::DB->dbh();
169 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM facets WHERE facet_name = ?', undef, $opts{name});
170 0 ▶ if (ref($rv) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
171 0 $self = $rv;
172 0 $self->{related_terms} = $dbh->selectcol_arrayref('SELECT term_id FROM terms WHERE facet_id = ?', undef, $self->{facet_id});
173 } else {
174 0 return;
175 }
176 }
177
178 # return the object
179 1 return bless $self, $class;
180
181 }
182  
183  
184 0 ▶sub facet_id {
Subroutine facet_id: not called CC=1 SCAR=6.9
Pod documented
185  
186 0 my $self = shift;
187 0 return $self->{facet_id};
188  
189 }
190  
191  
192 2 ▶sub facet_name {
Subroutine facet_name: called CC=2 SCAR=6.9
Pod documented
193  
194 # declare local variables
195 2 my ($self, $facet_name) = @_;
196
197 # check for the existance of a note
198 2 ▶ if ($facet_name) { $self->{facet_name} = $facet_name }
Branch: 1 branch
BranchTrueFalseTotal
if ($facet_name) 1 1 2
199
200 # return the name
201 2 return $self->{facet_name};
202
203 }
204  
205  
206 2 ▶sub facet_note {
Subroutine facet_note: called CC=2 SCAR=6.9
Pod documented
207  
208 # declare local variables
209 2 my ($self, $facet_note) = @_;
210
211 # check for the existance of a note
212 2 ▶ if ($facet_note) { $self->{facet_note} = $facet_note }
Branch: 1 branch
BranchTrueFalseTotal
if ($facet_note) 1 1 2
213
214 # return the note
215 2 return $self->{facet_note};
216
217 }
218  
219 0 ▶sub delete_facet_note {
Subroutine delete_facet_note: not called CC=1 SCAR=6.9
Pod documented
220  
221 0 my $self = shift;
222 0 $self->{facet_note} = undef;
223 }
224  
225  
226 1 ▶sub commit {
Subroutine commit: called CC=6 SCAR=35.2
Pod documented
227  
228 # get myself, :-)
229 1 my $self = shift;
230
231 # get a database handle
232 1 my $dbh = MyLibrary::DB->dbh();
233
234 # see if the object has an id
235 0 ▶ if ($self->facet_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->facet_id) { } 0 0 0
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 ▶ if ($return > 1 || ! $return) { croak "Facet update in commit() failed. $return records were updated." }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
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 ▶ if ($return > 1 || ! $return) { croak 'Facet commit() failed.'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
248 0 $self->{facet_id} = $id;
249
250 }
251
252 # done
253 0 return 1;
254
255 }
256  
257  
258 0 ▶sub delete {
Subroutine delete: not called CC=5 SCAR=34.0
Pod documented
259  
260 0 my $self = shift;
261  
262 0 ▶ if ($self->{facet_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"facet_id"}) 0 0 0
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 ▶ if (scalar(@{$term_ids}) >=1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$term_ids;} >= 1) 0 0 0
269 0 require MyLibrary::Term;
270 0 foreach my $term_id (@{$term_ids}) {
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 ▶ if ($rv != 1) {croak ("Error deleting facet record. Deleted $rv records.");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 1) 0 0 0
279
280 0 return 1;
281  
282 }
283  
284 0 return 0;
285  
286 }
287  
288  
289 0 ▶sub get_facets {
Subroutine get_facets: not called CC=10 SCAR=47.0
Pod documented
290  
291 0 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 ▶ if (defined($opts{sort})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"sort"}) 0 0 0
297 0 ▶ if ($opts{sort} eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"sort"} eq "name") 0 0 0
298 0 $sort_clause = 'ORDER BY facet_name';
299 }
300 }
301 0 ▶ if (defined($opts{field}) && defined($opts{value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"field"} and defined $opts{"value"}) 0 0 0
Condition0%0/3
defined $opts{"field"} and defined $opts{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"field"} and defined $opts{"value"}
defined $opts{"field"} defined $opts{"value"}
Truth table0%0/3
A and B
A= defined $opts{"field"}B= defined $opts{"value"}
ABresult
0X0
100
111
302 0 $field = $opts{'field'};
303 0 $value = $opts{'value'};
304 0 ▶ if ($field eq 'name') {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'name') { } 0 0 0
elsif ($field eq 'description') { } 0 0 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 ▶ if ($limit_clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit_clause) 0 0 0
312 0 $query .= " $limit_clause";
313 }
314 0 ▶ if ($sort_clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_clause) 0 0 0
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 0 ▶sub related_terms {
Subroutine related_terms: not called CC=6 SCAR=37.4
Pod documented
331  
332 0 my $self = shift;
333 0 my %opts = @_;
334 0 my $sort;
335 0 ▶ if (defined($opts{sort})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"sort"}) 0 0 0
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}}) {
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 ▶ if ($sort && $sort eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort and $sort eq "name") 0 0 0
Condition0%0/3
$sort and $sort eq "name"
!ll&&!rl&&r
000
MC/DC0%0/2
$sort and $sort eq "name"
$sort $sort eq "name"
Truth table0%0/3
A and B
A= $sortB= $sort eq "name"
ABresult
0X0
100
111
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 4 1;
« Auth/Basic.pm ↑ Summary Auth.pm »