| 1 | package Net::Z3950::FOLIO::ResultSet; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::Z3950::FOLIO::Record; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift(); | ||
| 10 | my($session, $setname, $cql) = @_; | ||
| 11 | |||
| 12 | my $barcode = _extractBarcode($cql); | ||
| 13 | |||
| 14 | return bless { | ||
| 15 | session => $session, # back-reference | ||
| 16 | setname => $setname, | ||
| 17 | cql => $cql, | ||
| 18 | barcode => $barcode, | ||
| 19 | totalCount => undef, | ||
| 20 | records => [], | ||
| 21 | id2record => {}, | ||
| 22 | }, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub session { | ||
| 26 | my $this = shift(); | ||
| 27 | return $this->{session}; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub totalCount { | ||
| 31 | my $this = shift(); | ||
| 32 | my($newVal) = @_; | ||
| 33 | |||
| 34 | my $old = $this->{totalCount}; | ||
| 35 | $this->{totalCount} = $newVal if defined $newVal; | ||
| 36 | return $old; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub barcode { | ||
| 40 | my $this = shift(); | ||
| 41 | return $this->{barcode}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub insertRecords { | ||
| 45 | my $this = shift(); | ||
| 46 | my($offset, $records) = @_; | ||
| 47 | |||
| 48 | for (my $i = 0; $i < @$records; $i++) { | ||
| 49 | my $rec = new Net::Z3950::FOLIO::Record($this, $offset + $i, $records->[$i]); | ||
| 50 | $this->{records}->[$offset + $i] = $rec; | ||
| 51 | my $id = $rec->id(); | ||
| 52 | $this->{id2record}->{$id} = $rec; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub record { | ||
| 57 | my $this = shift(); | ||
| 58 | my($index0) = @_; | ||
| 59 | |||
| 60 | return $this->{records}->[$index0]; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub recordById { | ||
| 64 | my $this = shift(); | ||
| 65 | my($id) = @_; | ||
| 66 | |||
| 67 | return $this->{id2record}->{$id}; | ||
| 68 | } | ||
| 69 | |||
| 70 | # If the $cql query is a search for a barcode, return that barcode; | ||
| 71 | # otherwise return undefined. We could do this the sophisticated way, | ||
| 72 | # by parsing the CQL and examing every node, but in practice it | ||
| 73 | # probably suffices to do a simple string check. | ||
| 74 | # | ||
| 75 | sub _extractBarcode { | ||
| 76 | my ($cql) = @_; | ||
| 77 | |||
| 78 | if ($cql =~ /^item.barcode[\t ]*=+[\t ]*(.*)/) { | ||
| 79 | return $1; | ||
| 80 | } | ||
| 81 | |||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |