]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/MFHDParser.pm
More polish for MFHD record display
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / MFHDParser.pm
1 package OpenILS::Utils::MFHDParser;
2 use strict; use warnings;
3
4 use OpenSRF::EX qw/:try/;
5 use Time::HiRes qw(time);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenSRF::Utils::Logger qw/$logger/;
9
10 use OpenILS::Utils::MFHD;
11 use MARC::File::XML;
12 use Data::Dumper;
13
14 sub new { return bless( {}, shift() ); }
15
16 =head1 Subroutines
17
18 =over
19
20 =item * format_textual_holdings($field)
21
22 =back
23
24 Returns concatenated subfields $a with $z for textual holdings (866-868)
25
26 =cut
27
28 sub format_textual_holdings {
29         my ($self, $field) = @_;
30         my $holdings;
31         my $public_note;
32
33         $holdings = $field->subfield('a');
34         if (!$holdings) {
35                 return undef;
36         }
37
38         $public_note = $field->subfield('z');
39         if ($public_note) {
40                 return "$holdings - $public_note";
41         }
42         return $holdings;
43 }
44
45 =over
46
47 =item * mfhd_to_hash($mfhd_xml)
48
49 =back
50
51 Returns a Perl hash containing fields of interest from the MFHD record
52
53 =cut
54 sub mfhd_to_hash {
55         my ($self, $mfhd_xml) = @_;
56
57         my $holdings = [];
58         my $supplements = [];
59         my $indexes = [];
60         my $current_holdings = [];
61         my $current_supplements = [];
62         my $current_indexes = [];
63         my $online = []; # Laurentian extension to MFHD standard
64         my $missing = []; # Laurentian extension to MFHD standard
65         my $incomplete = []; # Laurentian extension to MFHD standard
66
67         my $marc = MARC::Record->new_from_xml($mfhd_xml);
68         my $mfhd = MFHD->new($marc);
69
70         foreach my $field ($marc->field('866')) {
71                 my $textual_holdings = $self->format_textual_holdings($field);
72                 if ($textual_holdings) {
73                         push @$holdings, $textual_holdings;
74                 }
75         }
76         foreach my $field ($marc->field('867')) {
77                 my $textual_holdings = $self->format_textual_holdings($field);
78                 if ($textual_holdings) {
79                         push @$supplements, $textual_holdings;
80                 }
81         }
82         foreach my $field ($marc->field('868')) {
83                 my $textual_holdings = $self->format_textual_holdings($field);
84                 if ($textual_holdings) {
85                         push @$indexes, $textual_holdings;
86                 }
87         }
88
89         foreach my $cap_id ($mfhd->captions('853')) {
90                 my @curr_holdings = $mfhd->holdings('863', $cap_id);
91                 next unless scalar @curr_holdings;
92                 foreach (@curr_holdings) {
93                         push @$current_holdings, $_->format();
94                 }
95         }
96
97         foreach my $cap_id ($mfhd->captions('854')) {
98                 my @curr_supplements = $mfhd->holdings('864', $cap_id);
99                 next unless scalar @curr_supplements;
100                 foreach (@curr_supplements) {
101                         push @$current_supplements, $_->format();
102                 }
103         }
104
105         foreach my $cap_id ($mfhd->captions('855')) {
106                 my @curr_indexes = $mfhd->holdings('865', $cap_id);
107                 next unless scalar @curr_indexes;
108                 foreach (@curr_indexes) {
109                         push @$current_indexes, $_->format();
110                 }
111         }
112
113         # Laurentian extensions
114         foreach my $field ($marc->field('530')) {
115                 my $online_stmt = $self->format_textual_holdings($field);
116                 if ($online_stmt) {
117                         push @$online, $online_stmt;
118                 }
119         }
120
121         foreach my $field ($marc->field('590')) {
122                 my $missing_stmt = $self->format_textual_holdings($field);
123                 if ($missing_stmt) {
124                         push @$missing, $missing_stmt;
125                 }
126         }
127
128         foreach my $field ($marc->field('591')) {
129                 my $incomplete_stmt = $self->format_textual_holdings($field);
130                 if ($incomplete_stmt) {
131                         push @$incomplete, $incomplete_stmt;
132                 }
133         }
134
135         return { holdings => $holdings, current_holdings => $current_holdings,
136                         supplements => $supplements, current_supplements => $current_supplements,
137                         indexes => $indexes, current_indexes => $current_indexes,
138                         missing => $missing, incomplete => $incomplete, };
139 }
140
141 =over
142
143 =item * init_holdings_virtual_record()
144
145 =back
146
147 Initialize the serial virtual record (svr) instance
148
149 =cut
150 sub init_holdings_virtual_record {
151         my $record = Fieldmapper::serial::virtual_record->new;
152         $record->id();
153         $record->owning_lib();
154         $record->holdings([]);
155         $record->current_holdings([]);
156         $record->supplements([]);
157         $record->current_supplements([]);
158         $record->indexes([]);
159         $record->current_indexes([]);
160         $record->online([]);
161         $record->missing([]);
162         $record->incomplete([]);
163         return $record;
164 }
165
166 =over
167
168 =item * init_holdings_virtual_record($mfhd)
169
170 =back
171
172 Given an MFHD record, return a populated svr instance
173
174 =cut
175 sub generate_svr {
176         my ($self, $id, $mfhd, $owning_lib) = @_;
177
178         if (!$mfhd) {
179                 return undef;
180         }
181
182         my $record = init_holdings_virtual_record();
183         my $holdings = $self->mfhd_to_hash($mfhd);
184
185         $record->id($id);
186         $record->owning_lib($owning_lib);
187         $record->holdings($holdings->{holdings});
188         $record->current_holdings($holdings->{current_holdings});
189         $record->supplements($holdings->{supplements});
190         $record->current_supplements($holdings->{current_supplements});
191         $record->indexes($holdings->{indexes});
192         $record->current_indexes($holdings->{current_indexes});
193         $record->online($holdings->{online});
194         $record->missing($holdings->{missing});
195         $record->incomplete($holdings->{incomplete});
196
197         return $record;
198 }
199
200 1;