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