]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/MFHD/Caption.pm
Refactor publication pattern parsing to make processing simpler later
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / MFHD / Caption.pm
1 package MFHD::Caption;
2 use strict;
3 use integer;
4 use Carp;
5
6 use Data::Dumper;
7
8 use DateTime;
9
10 use base 'MARC::Field';
11
12 sub new
13 {
14     my $proto = shift;
15     my $class = ref($proto) || $proto;
16     my $self = shift;
17     my $last_enum = undef;
18
19     $self->{_mfhdc_ENUMS} = {};
20     $self->{_mfhdc_CHRONS} = {};
21     $self->{_mfhdc_PATTERN} = {};
22     $self->{_mfhdc_COPY} = undef;
23     $self->{_mfhdc_UNIT} = undef;
24     $self->{_mfhdc_COMPRESSIBLE} = 1;   # until proven otherwise
25
26     foreach my $subfield ($self->subfields) {
27         my ($key, $val) = @$subfield;
28         if ($key eq '8') {
29             $self->{LINK} = $val;
30         } elsif ($key =~ /[a-h]/) {
31             # Enumeration Captions
32             $self->{_mfhdc_ENUMS}->{$key} = {CAPTION => $val,
33                                              COUNT => undef,
34                                              RESTART => undef};
35             if ($key =~ /[ag]/) {
36                 $last_enum = undef;
37             } else {
38                 $last_enum = $key;
39             }
40         } elsif ($key =~ /[i-m]/) {
41             # Chronology captions
42             $self->{_mfhdc_CHRONS}->{$key} = $val;
43         } elsif ($key eq 'u') {
44             # Bib units per next higher enumeration level
45             carp('$u specified for top-level enumeration')
46               unless defined($last_enum);
47             $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
48         } elsif ($key eq 'v') {
49             carp '$v specified for top-level enumeration'
50               unless defined($last_enum);
51             $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
52         } elsif ($key =~ /[npwz]/) {
53             # Publication Pattern info ('o' == type of unit, 'q'..'t' undefined)
54             $self->{_mfhdc_PATTERN}->{$key} = $val;
55         } elsif ($key =~ /x/) {
56             # Calendar change can have multiple comma-separated values
57             $self->{_mfhdc_PATTERN}->{x} = [split /,/, $val];
58         } elsif ($key eq 'y') {
59             $self->{_mfhdc_PATTERN}->{y} = {}
60               unless exists $self->{_mfhdc_PATTERN}->{y};
61             update_pattern($self, $val);
62         } elsif ($key eq 'o') {
63             # Type of unit
64             $self->{_mfhdc_UNIT} = $val;
65         } elsif ($key eq 't') {
66             $self->{_mfhdc_COPY} = $val;
67         } else {
68             carp "Unknown caption subfield '$key'";
69         }
70     }
71
72     # subsequent levels of enumeration (primary and alternate)
73     # If an enumeration level doesn't document the number
74     # of "issues" per "volume", or whether numbering of issues
75     # restarts, then we can't compress.
76     foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
77         if (exists $self->{_mfhdc_ENUMS}->{$key}) {
78             my $pattern = $self->{_mfhdc_ENUMS}->{$key};
79             if (!$pattern->{RESTART} || !$pattern->{COUNT}
80                 || ($pattern->{COUNT} eq 'var')
81                 || ($pattern->{COUNT} eq 'und')) {
82                 $self->{_mfhdc_COMPRESSIBLE} = 0;
83                 last;
84             }
85         }
86     }
87
88     my $pat = $self->{_mfhdc_PATTERN};
89
90     # Sanity check publication frequency vs publication pattern:
91     # if the frequency is a number, then the pattern better
92     # have that number of values associated with it.
93     if (exists($pat->{w}) && ($pat->{w} =~ /^\d+$/)
94         && ($pat->{w} != scalar(@{$pat->{y}->{p}}))) {
95         carp("Caption::new: publication frequency '$pat->{w}' != publication pattern @{$pat->{y}->{p}}");
96     }
97
98
99     # If there's a $x subfield and a $j, then it's compressible
100     if (exists $pat->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
101         $self->{_mfhdc_COMPRESSIBLE} = 1;
102     }
103
104     bless ($self, $class);
105
106     return $self;
107 }
108
109 sub update_pattern {
110     my $self = shift;
111     my $val = shift;
112     my $pathash = $self->{_mfhdc_PATTERN}->{y};
113     my ($pubcode, $pat) = unpack("a1a*", $val);
114
115     $pathash->{$pubcode} = [] unless exists $pathash->{$pubcode};
116     push @{$pathash->{$pubcode}}, $pat;
117 }
118
119 sub decode_pattern {
120     my $self = shift;
121     my $pattern = $self->{_mfhdc_PATTERN}->{y};
122
123     # XXX WRITE ME (?)
124 }
125
126 sub compressible {
127     my $self = shift;
128
129     return $self->{_mfhdc_COMPRESSIBLE};
130 }
131
132 sub chrons {
133     my $self = shift;
134     my $key = shift;
135
136     if (exists $self->{_mfhdc_CHRONS}->{$key}) {
137         return $self->{_mfhdc_CHRONS}->{$key};
138     } else {
139         return undef;
140     }
141 }
142
143 sub capfield {
144     my $self = shift;
145     my $key = shift;
146
147     if (exists $self->{_mfhdc_ENUMS}->{$key}) {
148         return $self->{_mfhdc_ENUMS}->{$key};
149     } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
150         return $self->{_mfhdc_CHRONS}->{$key};
151     } else {
152         return undef;
153     }
154 }
155
156 sub capstr {
157     my $self = shift;
158     my $key = shift;
159     my $val = $self->capfield($key);
160
161     if (ref $val) {
162         return $val->{CAPTION};
163     } else {
164         return $val;
165     }
166 }
167
168 sub calendar_change {
169     my $self = shift;
170
171     return $self->{_mfhdc_PATTERN}->{x};
172 }
173
174 # If items are identified by chronology only, with no separate
175 # enumeration (eg, a newspaper issue), then the chronology is
176 # recorded in the enumeration subfields $a - $f.  We can tell
177 # that this is the case if there are $a - $f subfields and no
178 # chronology subfields ($i-$k), and none of the $a-$f subfields
179 # have associated $u or $v subfields, but there's a $w and no $x
180
181 sub enumeration_is_chronology {
182     my $self = shift;
183
184     # There is always a '$a' subfield in well-formed fields.
185     return 0 if exists $self->{_mfhdc_CHRONS}->{i}
186       || exists $self->{_mfhdc_PATTERN}->{x};
187
188     foreach my $key ('a' .. 'f') {
189         my $enum;
190
191         last if !exists $self->{_mfhdc_ENUMS}->{$key};
192
193         $enum = $self->{_mfhdc_ENUMS}->{$key};
194         return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
195     }
196
197     return (exists $self->{_mfhdc_PATTERN}->{w});
198 }
199
200 my %daynames = (
201                 'mo' => 1,
202                 'tu' => 2,
203                 'we' => 3,
204                 'th' => 4,
205                 'fr' => 5,
206                 'sa' => 6,
207                 'su' => 7,
208                );
209
210 my $daypat = '(mo|tu|we|th|fr|sa|su)';
211 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
212 my $weeknopat;
213 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
214 my $seasonpat = '(21|22|23|24)';
215
216 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
217 $weeknopat = '(';
218 foreach my $weekno (1..52) {
219     $weeknopat .= sprintf('%02d|', $weekno);
220 }
221 $weeknopat .= '53)';
222
223 sub match_day {
224     my $pat = shift;
225     my @date = @_;
226     # Translate daynames into day of week for DateTime
227     # also used to check if dayname is valid.
228
229     if (exists $daynames{$pat}) {
230         # dd
231         # figure out day of week for date and compare
232         my $dt = DateTime->new(year  => $date[0],
233                                month => $date[1],
234                                day   => $date[2]);
235         return ($dt->day_of_week == $daynames{$pat});
236     } elsif (length($pat) == 2) {
237         # MM
238         return $pat == $date[3];
239     } elsif (length($pat) == 4) {
240         # MMDD
241         my ($mon, $day);
242         $mon = substr($pat, 0, 2);
243         $day = substr($pat, 2, 2);
244
245         return (($mon == $date[1]) && ($day == $date[2]));
246     } else {
247         carp "Invalid day pattern '$pat'";
248         return 0;
249     }
250 }
251
252 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
253 sub last_week_of_month {
254     my $dt = shift;
255     my $week = shift;
256     my $day = shift;
257     my $end_dt = DateTime->last_day_of_month(year  => $dt->year,
258                                              month => $dt->month);
259
260     $day = $daynames{$day};
261     while ($end_dt->day_of_week != $day) {
262         $end_dt->subtract(days => 1);
263     }
264
265     # 99: last week of month, 98: second last, etc.
266     for (my $i = 99 - $week; $i > 0; $i--) {
267         $end_dt->subtract(weeks => 1);
268     }
269
270     return $end_dt;
271 }
272
273 sub check_date {
274     my $dt = shift;
275     my $month = shift;
276     my $weekno = shift;
277     my $day = shift;
278
279     if (!defined $day) {
280         # MMWW
281         return (($dt->month == $month)
282                 && (($dt->week_of_month == $weekno)
283                     || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
284     }
285
286     # simple cases first
287     if ($daynames{$day} != $dt->day_of_week) {
288         # if it's the wrong day of the week, rest doesn't matter
289         return 0;
290     }
291
292     if (!defined $month) {
293         # WWdd
294         return (($dt->weekday_of_month == $weekno)
295                 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
296     }
297
298     # MMWWdd
299     if ($month != $dt->month) {
300         # If it's the wrong month, then we're done
301         return 0;
302     }
303
304     # It's the right day of the week
305     # It's the right month
306
307     if ($weekno == $dt->weekday_of_month) {
308         # If this matches, then we're counting from the beginning
309         # of the month and it matches and we're done.
310         return 1;
311     }
312
313     # only case left is that the week number is counting from
314     # the end of the month: eg, second last wednesday
315     return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
316 }
317
318 sub match_week {
319     my $pat = shift;
320     my @date = @_;
321     my $dt = DateTime->new(year  => $date[0],
322                            month => $date[1],
323                            day   => $date[2]);
324
325     if ($pat =~ m/^$weekpat$daypat$/) {
326         # WWdd: 03we = Third Wednesday
327         return check_date($dt, undef, $1, $2);
328     } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
329         # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
330         return check_date($dt, $1, $2, $3);
331     } elsif ($pat =~ m/^$monthpat$weekpat$/) {
332         # MMWW: 1204: Fourth week in December XXX WRITE ME
333         return check_date($dt, $1, $2, undef);
334     } else {
335         carp "invalid week pattern '$pat'";
336         return 0;
337     }
338 }
339
340 sub match_month {
341     my $pat = shift;
342     my @date = @_;
343
344     return ($pat eq $date[1]);
345 }
346
347 sub match_season {
348     my $pat = shift;
349     my @date = @_;
350
351     return ($pat eq $date[1]);
352 }
353
354 sub match_year {
355     my $pat = shift;
356     my @date = @_;
357
358     # XXX WRITE ME
359     return 0;
360 }
361
362 sub match_issue {
363     my $pat = shift;
364     my @date = @_;
365
366     # We handle enumeration patterns separately. This just
367     # ensures that when we're processing chronological patterns
368     # we don't match an enumeration pattern.
369     return 0;
370 }
371
372 my %dispatch = (
373                 'd' => \&match_day,
374                 'e' => \&match_issue, # not really a "chron" code
375                 'w' => \&match_week,
376                 'm' => \&match_month,
377                 's' => \&match_season,
378                 'y' => \&match_year,
379 );
380
381 sub regularity_match {
382     my $self = shift;
383     my $pubcode = shift;
384     my @date = @_;
385
386     # we can't match something that doesn't exist.
387     return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{$pubcode};
388
389     foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{$pubcode}}) {
390         my $chroncode= substr($regularity, 0, 1);
391         my @pats = split(/,/, substr($regularity, 1));
392
393         if (!exists $dispatch{$chroncode}) {
394             carp "Unrecognized chroncode '$chroncode'";
395             return 0;
396         }
397
398         # XXX WRITE ME
399         foreach my $pat (@pats) {
400             $pat =~ s|/.+||;    # If it's a combined date, match the start
401             if ($dispatch{$chroncode}->($pat, @date)) {
402                 return 1;
403             }
404         }
405     }
406
407     return 0;
408 }
409
410 sub is_omitted {
411     my $self = shift;
412     my @date = @_;
413
414     return $self->regularity_match('o', @date);
415 }
416
417 sub is_published {
418     my $self = shift;
419     my @date = @_;
420
421     return $self->regularity_match('p', @date);
422 }
423
424 sub is_combined {
425     my $self = shift;
426     my @date = @_;
427
428     return $self->regularity_match('c', @date);
429 }
430
431 sub enum_is_combined {
432     my $self = shift;
433     my $subfield = shift;
434     my $iss = shift;
435     my $level = ord($subfield) - ord('a') + 1;
436
437     return 0 if !exists $self->{_mfhdc_PATTERN}->{y}->{c};
438
439     foreach my $regularity (@{$self->{_mfhdc_PATTERN}->{y}->{c}}) {
440         next unless $regularity =~ m/^e$level/o;
441
442         my @pats = split(/,/, substr($regularity, 2));
443
444         foreach my $pat (@pats) {
445             $pat =~ s|/.+||;    # if it's a combined issue, match the start
446             return 1 if ($iss eq $pat);
447         }
448     }
449
450     return 0;
451 }
452
453
454 my %increments = (
455                   a => {years => 1}, # annual
456                   b => {months => 2}, # bimonthly
457                   c => {days => 3}, # semiweekly
458                   d => {days => 1}, # daily
459                   e => {weeks => 2}, # biweekly
460                   f => {months => 6}, # semiannual
461                   g => {years => 2},  # biennial
462                   h => {years => 3},  # triennial
463                   i => {days => 2}, # three times / week
464                   j => {days => 10}, # three times /month
465                   # k => continuous
466                   m => {months => 1}, # monthly
467                   q => {months => 3}, # quarterly
468                   s => {days => 15},  # semimonthly
469                   t => {months => 4}, # three times / year
470                   w => {weeks => 1},  # weekly
471                   # x => completely irregular
472 );
473
474 sub incr_date {
475     my $incr = shift;
476     my @new = @_;
477
478     if (scalar(@new) == 1) {
479         # only a year is specified. Next date is easy
480         $new[0] += $incr->{years} || 1;
481     } elsif (scalar(@new) == 2) {
482         # Year and month or season
483         if ($new[1] > 20) {
484             # season
485             $new[1] += ($incr->{months}/3) || 1;
486             if ($new[1] > 24) {
487                 # carry
488                 $new[0] += 1;
489                 $new[1] -= 4;   # 25 - 4 == 21 == Spring after Winter
490             }
491         } else {
492             # month
493             $new[1] += $incr->{months} || 1;
494             if ($new[1] > 12) {
495                 # carry
496                 $new[0] += 1;
497                 $new[1] -= 12;
498             }
499             $new[1] = '0' . $new[1] if ($new[1] < 10);
500         }
501     } elsif (scalar(@new) == 3) {
502         # Year, Month, Day: now it gets complicated.
503
504         if ($new[2] =~ /^[0-9]+$/) {
505             # A single number for the day of month, relatively simple
506             my $dt = DateTime->new(year => $new[0],
507                                    month=> $new[1],
508                                    day  => $new[2]);
509             $dt->add(%{$incr});
510             $new[0] = $dt->year;
511             $new[1] = $dt->month;
512             $new[2] = $dt->day;
513         }
514         $new[1] = '0' . $new[1] if ($new[1] < 10);
515         $new[2] = '0' . $new[2] if ($new[2] < 10);
516     } else {
517         warn("Don't know how to cope with @new");
518     }
519
520     return @new;
521 }
522
523 # Test to see if $m1/$d1 is on or after $m2/$d2
524 # if $d2 is undefined, test is based on just months
525 sub on_or_after {
526     my ($m1, $d1, $m2, $d2) = @_;
527
528     return (($m1 > $m2)
529             || ($m1 == $m2 && ((!defined $d2) || ($d1 >= $d2))));
530 }
531
532 sub calendar_increment {
533     my $self = shift;
534     my $cur = shift;
535     my @new = @_;
536     my $cal_change = $self->calendar_change;
537     my $month;
538     my $day;
539     my $cur_before;
540     my $new_on_or_after;
541
542     # A calendar change is defined, need to check if it applies
543     if ((scalar(@new) == 2 && $new[1] > 20) || (scalar(@new) == 1)) {
544         carp "Can't calculate date change for ", $self->as_string;
545         return;
546     }
547
548     foreach my $change (@{$cal_change}) {
549         my $incr;
550
551         if (length($change) == 2) {
552             $month = $change;
553         } elsif (length($change) == 4) {
554             ($month, $day) = unpack("a2a2", $change);
555         }
556
557         if ($cur->[0] == $new[0]) {
558             # Same year, so a 'simple' month/day comparison will be fine
559             $incr = (!on_or_after($cur->[1], $cur->[2], $month, $day)
560                      && on_or_after($new[1], $new[2], $month, $day));
561         } else {
562             # @cur is in the year before @new. There are
563             # two possible cases for the calendar change date that
564             # indicate that it's time to change the volume:
565             # (1) the change date is AFTER @cur in the year, or
566             # (2) the change date is BEFORE @new in the year.
567             # 
568             #  -------|------|------X------|------|
569             #       @cur    (1)   Jan 1   (2)   @new
570
571             $incr = (on_or_after($new[1], $new[2], $month, $day)
572                      || !on_or_after($cur->[1], $cur->[2], $month, $day));
573         }
574         return $incr if $incr;
575     }
576 }
577
578 sub next_date {
579     my $self = shift;
580     my $next = shift;
581     my $carry = shift;
582     my @keys = @_;
583     my @cur;
584     my @new;
585     my $incr;
586
587     my $reg = $self->{_mfhdc_REGULARITY};
588     my $pattern = $self->{_mfhdc_PATTERN};
589     my $freq = $pattern->{w};
590
591     foreach my $i (0..$#keys) {
592         $new[$i] = $cur[$i] = $next->{$keys[$i]} if exists $next->{$keys[$i]};
593     }
594
595     # If the current issue has a combined date (eg, May/June)
596     # get rid of the first date and base the calculation
597     # on the final date in the combined issue.
598     $new[-1] =~ s|^[^/]+/||;
599
600     # If $frequency is not one of the standard codes defined in %increments
601     # then there has to be a $yp publication regularity pattern that
602     # lists the dates of publication. Use that that list to find the next
603     # date following the current one.
604     # XXX: the code doesn't handle this case yet.
605     if (!defined($freq)) {
606         carp "Undefined frequency in next_date!";
607     } elsif (!exists $increments{$freq}) {
608         carp "Don't know how to deal with frequency '$freq'!";
609     } else {
610         #
611         # One of the standard defined issue frequencies
612         #
613         @new = incr_date($increments{$freq}, @new);
614
615         while ($self->is_omitted(@new)) {
616             @new = incr_date($increments{$freq}, @new);
617         }
618
619         if ($self->is_combined(@new)) {
620             my @second_date = incr_date($increments{$freq}, @new);
621
622             # I am cheating: This code assumes that only the smallest
623             # time increment is combined. So, no "Apr 15/May 1" allowed.
624             $new[-1] = $new[-1] . '/' . $second_date[-1];
625         }
626     }
627
628     for my $i (0..$#new) {
629         $next->{$keys[$i]} = $new[$i];
630     }
631
632     # Figure out if we need to adust volume number
633     # right now just use the $carry that was passed in.
634     # in long run, need to base this on ($carry or date_change)
635     if ($carry) {
636         # if $carry is set, the date doesn't matter: we're not
637         # going to increment the v. number twice at year-change.
638         $next->{a} += $carry;
639     } elsif (defined $self->{_mfhdc_PATTERN}->{x}) {
640         $next->{a} += $self->calendar_increment(\@cur, @new);
641     }
642 }
643
644 sub next_alt_enum {
645     my $self = shift;
646     my $next = shift;
647
648     # First handle any "alternative enumeration", since they're
649     # a lot simpler, and don't depend on the the calendar
650     foreach my $key ('h', 'g') {
651         next if !exists $next->{$key};
652         if (!$self->capstr($key)) {
653             warn "Holding data exists for $key, but no caption specified";
654             $next->{$key} += 1;
655             last;
656         }
657
658         my $cap = $self->capfield($key);
659         if ($cap->{RESTART} && $cap->{COUNT}
660             && ($next->{$key} == $cap->{COUNT})) {
661             $next->{$key} = 1;
662         } else {
663             $next->{$key} += 1;
664             last;
665         }
666     }
667 }
668
669 sub next_enum {
670     my $self = shift;
671     my $next = shift;
672     my $carry;
673
674     # $carry keeps track of whether we need to carry into the next
675     # higher level of enumeration. It's not actually necessary except
676     # for when the loop ends: if we need to carry from $b into $a
677     # then $carry will be set when the loop ends.
678     #
679     # We need to keep track of this because there are two different
680     # reasons why we might increment the highest level of enumeration ($a)
681     # 1) we hit the correct number of items in $b (ie, 5th iss of quarterly)
682     # 2) it's the right time of the year.
683     #
684     $carry = 0;
685     foreach my $key (reverse('b'..'f')) {
686         next if !exists $next->{$key};
687
688         if (!$self->capstr($key)) {
689             # Just assume that it increments continuously and give up
690             warn "Holding data exists for $key, but no caption specified";
691             $next->{$key} += 1;
692             $carry = 0;
693             last;
694         }
695
696         # If the current issue has a combined issue number (eg, 2/3)
697         # get rid of the first issue number and base the calculation
698         # on the final issue number in the combined issue.
699         if ($next->{$key} =~ m|/|) {
700             $next->{$key} =~ s|^[^/]+/||;
701         }
702
703         my $cap = $self->capfield($key);
704         if ($cap->{RESTART} && $cap->{COUNT}
705             && ($next->{$key} eq $cap->{COUNT})) {
706             $next->{$key} = 1;
707             $carry = 1;
708         } else {
709             # If I don't need to "carry" beyond here, then I just increment
710             # this level of the enumeration and stop looping, since the
711             # "next" hash has been initialized with the current values
712
713             $next->{$key} += 1;
714             $carry = 0;
715         }
716
717         # You can't have a combined issue that spans two volumes: no.12/1
718         # is forbidden
719         if ($self->enum_is_combined($key, $next->{$key})) {
720             $next->{$key} .= '/' . ($next->{$key} + 1);
721         }
722
723         last if !$carry;
724     }
725
726     # The easy part is done. There are two things left to do:
727     # 1) Calculate the date of the next issue, if necessary
728     # 2) Increment the highest level of enumeration (either by date
729     #    or because $carry is set because of the above loop
730
731     if (!$self->subfield('i')) {
732         # The simple case: if there is no chronology specified
733         # then just check $carry and return
734         $next->{'a'} += $carry;
735     } else {
736         # Figure out date of next issue, then decide if we need
737         # to adjust top level enumeration based on that
738         $self->next_date($next, $carry, ('i'..'m'));
739     }
740 }
741
742 sub next {
743     my $self = shift;
744     my $holding = shift;
745     my $next = {};
746
747     # Initialize $next with current enumeration & chronology, then
748     # we can just operate on $next, based on the contents of the caption
749
750     if ($self->enumeration_is_chronology) {
751         foreach my $key ('a' .. 'h') {
752             $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
753               if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
754         }
755         $self->next_date($next, 0, ('a' .. 'h'));
756
757         return $next;
758     }
759
760     foreach my $key ('a' .. 'h') {
761         $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}->{HOLDINGS}
762           if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
763     }
764
765     foreach my $key ('i'..'m') {
766         $next->{$key} = $holding->{_mfhdh_SUBFIELDS}->{$key}
767           if defined $holding->{_mfhdh_SUBFIELDS}->{$key};
768     }
769
770     if (exists $next->{'h'}) {
771         $self->next_alt_enum($next);
772     }
773
774     $self->next_enum($next);
775
776     return($next);
777 }
778
779 1;