]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/MFHD.pm
Redo MFHD, MFHD::Holding, and MFHD::Caption so that they are enhanced MARC::Record...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / MFHD.pm
1 package MFHD;
2 use strict;
3 use integer;
4 use Carp;
5 use Data::Dumper;
6
7 use MARC::Record;
8 use MFHD::Caption;
9 use MFHD::Holding;
10
11 our @ISA;
12
13 @ISA = qw(MARC::Record);
14
15 sub new {
16     my $proto = shift;
17     my $class = ref($proto) || $proto;
18     my $self = shift;
19
20     $self->{_mfhd_CAPTIONS} = {};
21     $self->{_mfhd_COMPRESSIBLE} = (substr($self->leader, 17, 1) =~ /[45]/);
22
23     foreach my $field ('853', '854', '855') {
24         my $captions = {};
25         foreach my $caption ($self->field($field)) {
26             my $cap_id;
27
28             $cap_id = $caption->subfield('8') || '0';
29             print "handling caption '$cap_id'\n";
30
31             if (exists $captions->{$cap_id}) {
32                 carp "Multiple MFHD captions with label '$cap_id'";
33             }
34
35             $captions->{$cap_id} = new MFHD::Caption($caption);
36             if ($self->{_mfhd_COMPRESSIBLE}) {
37                 $self->{_mfhd_COMPRESSIBLE} &&= $captions->{$cap_id}->compressible;
38             }
39         }
40         $self->{_mfhd_CAPTIONS}->{$field} = $captions;
41     }
42
43     foreach my $field ('863', '864', '865') {
44         my $holdings = {};
45         my $cap_field;
46
47         ($cap_field = $field) =~ s/6/5/;
48
49         foreach my $hfield ($self->field($field)) {
50             my ($linkage, $link_id, $seqno);
51             my $holding;
52
53             $linkage = $hfield->subfield('8');
54             ($link_id, $seqno) = split(/\./, $linkage);
55
56             if (!exists $holdings->{$link_id}) {
57                 $holdings->{$link_id} = {};
58             }
59             $holding = new MFHD::Holding($seqno, $hfield,
60                                          $self->{_mfhd_CAPTIONS}->{$cap_field}->{$link_id});
61             $holdings->{$link_id}->{$seqno} = $holding;
62
63             if ($self->{_mfhd_COMPRESSIBLE}) {
64                 $self->{_mfhd_COMPRESSIBLE} &&= $holding->validate;
65             }
66         }
67         $self->{_mfhd_HOLDINGS}->{$field} = $holdings;
68     }
69
70     bless ($self, $class);
71     return $self;
72 }
73
74 sub compressible {
75     my $self = shift;
76
77     return $self->{_mfhd_COMPRESSIBLE};
78 }
79
80 sub captions {
81     my $self = shift;
82     my $field = shift;
83
84     return sort keys %{$self->{_mfhd_CAPTIONS}->{$field}}
85 }
86
87 sub holdings {
88     my $self = shift;
89     my $field = shift;
90     my $capid = shift;
91
92     return sort {$a->seqno <=> $b->seqno} values %{$self->{_mfhd_HOLDINGS}->{$field}->{$capid}};
93 }
94
95 1;