1 # MFHD::Holding provides some additional holdings logic to a MARC::Field
2 # object. In its current state it is primarily read-only, as direct changes
3 # to the underlying MARC::Field are not reflected in the MFHD logic layer, and
4 # only the 'increment', 'notes', and 'seqno' methods do updates to the
15 use base 'MARC::Field';
19 my $class = ref($proto) || $proto;
23 my $last_enum = undef;
25 $self->{_mfhdh_SEQNO} = $seqno;
26 $self->{_mfhdh_CAPTION} = $caption;
27 $self->{_mfhdh_DESCR} = {};
28 $self->{_mfhdh_COPY} = undef;
29 $self->{_mfhdh_BREAK} = undef;
30 $self->{_mfhdh_NOTES} = {};
31 $self->{_mfhdh_NOTES}{public} = [];
32 $self->{_mfhdh_NOTES}{private} = [];
33 $self->{_mfhdh_COPYRIGHT} = [];
34 $self->{_mfhdh_COMPRESSED} = ($self->indicator(2) eq '0' || $self->indicator(2) eq '2') ? 1 : 0;
35 # TODO: full support for second indicators 2, 3, and 4
36 $self->{_mfhdh_OPEN_ENDED} = 0;
38 foreach my $subfield ($self->subfields) {
39 my ($key, $val) = @$subfield;
41 if ($key =~ /[a-m]/) {
42 if (exists($self->{_mfhdh_FIELDS}->{$key})) {
43 carp("Duplicate, non-repeatable subfield '$key' found, ignoring");
46 if ($self->{_mfhdh_COMPRESSED}) {
47 $self->{_mfhdh_FIELDS}->{$key}{HOLDINGS} = [split(/\-/, $val, -1)];
48 if (!defined($self->{_mfhdh_FIELDS}->{$key}{HOLDINGS}[1])) {
49 $self->{_mfhdh_FIELDS}->{$key}{HOLDINGS}[1] = $self->{_mfhdh_FIELDS}->{$key}{HOLDINGS}[0];
52 $self->{_mfhdh_FIELDS}->{$key}{HOLDINGS} = [$val];
54 if ($key =~ /[a-h]/) {
55 # Enumeration specific details of holdings
56 $self->{_mfhdh_FIELDS}->{$key}{UNIT} = undef;
59 } elsif ($key eq 'o') {
60 warn '$o specified prior to first enumeration'
61 unless defined($last_enum);
62 $self->{_mfhdh_FIELDS}->{$last_enum}->{UNIT} = $val;
64 } elsif ($key =~ /[npq]/) {
65 $self->{_mfhdh_DESCR}->{$key} = $val;
66 } elsif ($key eq 's') {
67 push @{$self->{_mfhdh_COPYRIGHT}}, $val;
68 } elsif ($key eq 't') {
69 $self->{_mfhdh_COPY} = $val;
70 } elsif ($key eq 'w') {
71 carp "Unrecognized break indicator '$val'"
72 unless $val =~ /^[gn]$/;
73 $self->{_mfhdh_BREAK} = $val;
74 } elsif ($key eq 'x') {
75 push @{$self->{_mfhdh_NOTES}{private}}, $val;
76 } elsif ($key eq 'z') {
77 push @{$self->{_mfhdh_NOTES}{public}}, $val;
81 if ( $self->{_mfhdh_COMPRESSED}
82 && $self->{_mfhdh_FIELDS}{'a'}{HOLDINGS}[1] eq '') {
83 $self->{_mfhdh_OPEN_ENDED} = 1;
90 # accessor to the object's field hash
92 # We are avoiding calling these elements 'subfields' because they are more
93 # than simply the MARC subfields, although in the current implementation they
94 # are indexed on the subfield key
96 # TODO: this accessor should probably be replaced with methods which hide the
97 # underlying structure of {_mfhdh_FIELDS} (see field_values for a start)
102 return $self->{_mfhdh_FIELDS};
106 # Given a field key, returns an array ref of one (for single statements)
107 # or two (for compressed statements) values
109 # TODO: add setter functionality to replace direct {HOLDINGS} access in other
110 # methods. It also makes sense to override some of the MARC::Field setter
111 # methods (such as update()) to accomplish this level of encapsulation.
114 my ($self, $key) = @_;
116 if (exists $self->fields->{$key}) {
117 my @values = @{$self->fields->{$key}{HOLDINGS}};
128 $self->{_mfhdh_SEQNO} = $_[0];
129 $self->update(8 => $self->caption->link_id . '.' . $_[0]);
132 return $self->{_mfhdh_SEQNO};
136 # Optionally accepts a true/false value to set the 'compressed' attribute
137 # Returns 'compressed' attribute
141 my $is_compressed = shift;
143 if (defined($is_compressed)) {
144 if ($is_compressed) {
145 $self->{_mfhdh_COMPRESSED} = 1;
146 $self->update(ind2 => '0');
148 $self->{_mfhdh_COMPRESSED} = 0;
149 $self->update(ind2 => '1');
153 return $self->{_mfhdh_COMPRESSED};
159 return $self->{_mfhdh_OPEN_ENDED};
165 return $self->{_mfhdh_CAPTION};
169 # notes: If called with no arguments, returns the public notes array ref.
170 # If called with a single argument, it returns either 'public' or
171 # 'private' notes based on the passed string.
173 # If called with more than one argument, it sets the proper note field, with
174 # type being the first argument and the note value(s) as the remaining
177 # It is also optional to pass in an array ref of note values as the third
178 # argument rather than a list.
187 } elsif ($type ne 'public' && $type ne 'private') {
188 carp("Notes being applied without specifying type");
189 unshift(@notes, $type);
193 if (ref($notes[0])) {
194 $self->{_mfhdh_NOTES}{$type} = $notes[0];
195 $self->_replace_note_subfields($type, @{$notes[0]});
198 $self->{_mfhdh_NOTES}{$type} = \@notes;
200 $self->{_mfhdh_NOTES}{$type} = [];
202 $self->_replace_note_subfields($type, @notes);
205 return $self->{_mfhdh_NOTES}{$type};
209 # utility function for 'notes' method
211 sub _replace_note_subfields {
215 my %note_subfield_ids = ('public' => 'z', 'private' => 'x');
217 $self->delete_subfield(code => $note_subfield_ids{$type});
219 foreach my $note (@notes) {
220 $self->add_subfields($note_subfield_ids{$type} => $note);
225 # return a simple subfields list (for easier revivification from database)
231 foreach my $subfield ($self->subfields) {
232 push(@subfields, $subfield->[0], $subfield->[1]);
238 # Called by method 'format_part' for formatting the chronology portion of
239 # the holding statement
243 my $holdings = shift;
244 my $caption = $self->caption;
266 foreach my $i (0..@keys) {
272 last if !defined $caption->capstr($key);
274 $capstr = $caption->capstr($key);
275 if (substr($capstr, 0, 1) eq '(') {
276 # a caption enclosed in parentheses is not displayed
280 # If this is the second level of chronology, then it's
281 # likely to be a month or season, so we should use the
282 # string name rather than the number given.
284 # account for possible combined issue chronology
285 my @chron_parts = split('/', $holdings->{$key});
286 for (my $i = 0; $i < @chron_parts; $i++) {
287 $chron_parts[$i] = $month{$chron_parts[$i]} if exists $month{$chron_parts[$i]};
289 $chron = join('/', @chron_parts);
291 $chron = $holdings->{$key};
294 $str .= (($i == 0 || $str =~ /[. ]$/) ? '' : ':') . $capstr . $chron;
301 # Called by method 'format' for each member of a possibly compressed holding
305 my $holding_values = shift;
306 my $caption = $self->caption;
309 if ($caption->type_of_unit) {
310 $str = $caption->type_of_unit . ' ';
313 if ($caption->enumeration_is_chronology) {
314 # if issues are identified by chronology only, then the
315 # chronology data is stored in the enumeration subfields,
316 # so format those fields as if they were chronological.
317 $str = $self->format_chron($holding_values, 'a'..'f');
319 # OK, there is enumeration data and maybe chronology
320 # data as well, format both parts appropriately
323 foreach my $key ('a'..'f') {
328 last if !defined $caption->capstr($key);
330 $capstr = $caption->capstr($key);
331 if (substr($capstr, 0, 1) eq '(') {
332 # a caption enclosed in parentheses is not displayed
336 ($key eq 'a' ? '' : ':') . $capstr . $holding_values->{$key};
340 if (defined $caption->capstr('i')) {
342 $str .= $self->format_chron($holding_values, 'i'..'l');
346 if ($caption->capstr('g')) {
347 # There's at least one level of alternative enumeration
349 foreach my $key ('g', 'h') {
351 ($key eq 'g' ? '' : ':')
352 . $caption->capstr($key)
353 . $holding_values->{$key};
356 # This assumes that alternative chronology is only ever
357 # provided if there is an alternative enumeration.
358 if ($caption->capstr('m')) {
359 # Alternative Chronology
361 $str .= $caption->capstr('m') . $holding_values->{'m'};
367 # Breaks in the sequence
368 # XXX: this is non-standard and also not the right place for this, since gaps
369 # only make sense in the context of multiple holding segments, not a single
371 # if (defined($self->{_mfhdh_BREAK})) {
372 # if ($self->{_mfhdh_BREAK} eq 'n') {
373 # $str .= ' non-gap break';
374 # } elsif ($self->{_mfhdh_BREAK} eq 'g') {
377 # warn "unrecognized break indicator '$self->{_mfhdh_BREAK}'";
385 # Create and return a string which conforms to display standard Z39.71
389 my $subfields = $self->fields;
394 foreach my $key (keys %$subfields) {
395 ($holding_start{$key}, $holding_end{$key}) =
396 @{$self->field_values($key)};
399 if ($self->is_compressed) {
400 # deal with open-ended statements
402 if ($self->is_open_ended) {
405 $formatted_end = $self->format_part(\%holding_end);
408 $self->format_part(\%holding_start) . ' - ' . $formatted_end;
410 $formatted = $self->format_part(\%holding_start);
414 if (@{$self->notes}) {
415 $formatted .= ' -- ' . join(', ', @{$self->notes});
421 # next: Given a holding statement, return a hash containing the
422 # enumeration values for the next issues, whether we hold it or not
423 # Just pass through to Caption::next
427 my $caption = $self->caption;
429 return $caption->next($self);
433 # matches($pat): check to see if $self matches the enumeration hashref passed
434 # in as $pat, as returned by the 'next' method. e.g.:
435 # $holding2->matches($holding1->next) # true if $holding2 directly follows
438 # Always returns false if $self is compressed
444 return 0 if $self->is_compressed;
446 foreach my $key ('a'..'f') {
447 # If a subfield exists in $self but not in $pat, or vice versa
448 # or if the field has different values, then fail
450 defined($self->field_values($key)) != exists($pat->{$key})
451 || (exists $pat->{$key}
452 && ($self->field_values($key)->[0] ne $pat->{$key}))
461 # Check that all the fields in a holdings statement are
462 # included in the corresponding caption.
467 foreach my $key (keys %{$self->fields}) {
468 if (!$self->caption || !$self->caption->capfield($key)) {
476 # Replace a single holding with it's next prediction
482 if ($self->is_open_ended) {
483 carp "Holding is open-ended, cannot increment";
485 } elsif ($self->is_compressed) {
486 carp "Incrementing a compressed holding is deprecated, use extend instead";
487 return $self->extend;
490 my $next = $self->next();
492 foreach my $key (keys %{$next}) {
493 $self->fields->{$key}{HOLDINGS}[0] = $next->{$key};
496 $self->seqno($self->seqno + 1);
497 $self->update(%{$next}); # update underlying subfields
502 # Extends a holding (compressing if needed) to include the next
503 # prediction and returns itself
508 if ($self->is_open_ended) {
509 carp "Holding is open-ended, cannot extend";
513 my $next = $self->next();
515 if (!$self->is_compressed) {
516 $self->is_compressed(1); # add compressed state
519 foreach my $key (keys %{$next}) {
520 my @values = @{$self->field_values($key)};
521 $values[1] = $next->{$key};
522 $self->fields->{$key}{HOLDINGS} = \@values;
523 $next->{$key} = join('-', @values);
526 $self->update(%{$next}); # update underlying subfields
531 # Turns a compressed holding into the singular form of the first member
534 sub compressed_to_first {
537 if (!$self->is_compressed) {
538 carp "Holding not compressed, cannot convert to first member";
543 foreach my $key (keys %{$self->fields}) {
544 my @values = @{$self->field_values($key)};
545 $self->fields->{$key}{HOLDINGS} = [$values[0]];
546 $changes{$key} = $values[0];
549 $self->update(%changes); # update underlying subfields
550 $self->is_compressed(0); # remove compressed state
556 # Turns a compressed holding into the singular form of the last member
559 sub compressed_to_last {
562 if (!$self->is_compressed) {
563 carp "Holding not compressed, cannot convert to last member";
565 } elsif ($self->is_open_ended) {
566 carp "Holding is open-ended, cannot convert to last member";
571 foreach my $key (keys %{$self->fields}) {
572 my @values = @{$self->field_values($key)};
573 $self->fields->{$key}{HOLDINGS} = [$values[1]];
574 $changes{$key} = $values[1];
577 $self->update(%changes); # update underlying subfields
578 $self->is_compressed(0); # remove compressed state
584 # Creates or replaces an end of a compressed holding
588 my $end_holding = shift;
592 foreach my $key (keys %{$self->fields}) {
593 my @values = @{$self->field_values($key)};
594 my @end_values = @{$end_holding->field_values($key)};
595 $values[1] = $end_values[0];
596 $self->fields->{$key}{HOLDINGS} = \@values;
597 $changes{$key} = join('-', @values);
599 } elsif (!$self->is_open_ended) { # make open-ended if no $end_holding
600 foreach my $key (keys %{$self->fields}) {
601 my @values = @{$self->field_values($key)};
602 $self->fields->{$key}{HOLDINGS} = [$values[0]];
603 $changes{$key} = $values[0] . '-';
605 $self->{_mfhdh_OPEN_ENDED} = 1; #TODO: setter for this value
608 $self->update(%changes); # update underlying subfields
610 if (!$self->is_compressed) {
611 $self->is_compressed(1); # add compressed state
618 # Basic, working, unoptimized clone operation
623 my $clone_field = $self->SUPER::clone();
624 return new MFHD::Holding($self->seqno, $clone_field, $self->caption);
628 # Turn a chronology instance into date(s) in YYYY-MM-DD format
630 # In list context it returns a list of start and (possibly undefined)
633 # In scalar context, it returns a YYYY-MM-DD date string of either the
634 # single date or the (possibly undefined) end date of a compressed holding
638 my $caption = $self->caption;
641 if ($caption->enumeration_is_chronology) {
647 # @chron_start and @chron_end will hold the (year, month, day) values
648 # represented by the start and optional end of the chronology instance.
649 # Default to January 1 with a year of 0 as initial values.
650 my @chron_start = (0, 1, 1);
651 my @chron_end = (0, 1, 1);
652 my @chrons = (\@chron_start, \@chron_end);
653 foreach my $key (@keys) {
654 my $capstr = $caption->capstr($key);
655 last if !defined($capstr);
656 if ($capstr =~ /year/) {
657 ($chron_start[0], $chron_end[0]) = @{$self->field_values($key)};
658 } elsif ($capstr =~ /month/) {
659 ($chron_start[1], $chron_end[1]) = @{$self->field_values($key)};
660 } elsif ($capstr =~ /day/) {
661 ($chron_start[2], $chron_end[2]) = @{$self->field_values($key)};
662 } elsif ($capstr =~ /season/) {
663 # chrons defined as season-only will use the astronomical season
664 # dates as a basic estimate.
665 my @seasons = @{$self->field_values($key)};
666 for (my $i = 0; $i < @seasons; $i++) {
667 $seasons[$i] = &_uncombine($seasons[$i], 0);
668 if ($seasons[$i] == 21) {
669 $chrons[$i]->[1] = 3;
670 $chrons[$i]->[2] = 20;
671 } elsif ($seasons[$i] == 22) {
672 $chrons[$i]->[1] = 6;
673 $chrons[$i]->[2] = 21;
674 } elsif ($seasons[$i] == 23) {
675 $chrons[$i]->[1] = 9;
676 $chrons[$i]->[2] = 22;
677 } elsif ($seasons[$i] == 24) {
678 # "winter" can come at the beginning or end of a year,
679 if ($self->caption->winter_starts_year()) {
680 $chrons[$i]->[1] = 1;
681 $chrons[$i]->[2] = 1;
682 } else { # default to astronomical
683 $chrons[$i]->[1] = 12;
684 $chrons[$i]->[2] = 21;
691 # if we have an an annual, set the month to ypm## if available
692 if (exists($self->caption->{_mfhdc_PATTERN}->{y}->{p}) and $self->caption->{_mfhdc_PATTERN}->{w} eq 'a') {
693 my $reg = $self->caption->{_mfhdc_PATTERN}->{y}->{p}->[0];
694 if ($reg =~ /^m(\d+)/) {
695 $chrons[0]->[1] = $1;
696 $chrons[1]->[1] = $1;
701 foreach my $chron (@chrons) {
703 if ($chron->[0] != 0) {
705 &_uncombine($chron->[0], 0) . '-'
706 . sprintf('%02d', $chron->[1]) . '-'
707 . sprintf('%02d', $chron->[2]);
714 } elsif ($self->is_compressed) {
722 # utility function for uncombining instance parts
725 my ($combo, $pos) = @_;
728 carp("Function '_uncombine' is not an instance method");
732 my @parts = split('/', $combo);
737 # Overload string comparison operators
739 # We are not overloading '<=>' because '==' is used liberally in MARC::Record
740 # to compare field identity (i.e. is this the same exact Field object?), not value
742 # Other string operators are auto-generated from 'cmp'
744 # Please note that this comparison is based on what the holding represents,
745 # not whether it is strictly identical (e.g. the seqno and link may vary)
747 # XXX: sorting using this operator is currently not deterministic for
748 # nonsensical holdings (e.g. V.10-V.5), and may require further consideration
749 use overload ('cmp' => \&_compare,
752 my ($holding_1, $holding_2, $swap) = @_;
754 # TODO: this needs some more consideration
755 # fall back to 'built-in' comparison
756 if (!UNIVERSAL::isa($holding_2, ref $holding_1)) {
757 if (defined $holding_2) {
758 carp("Use of non-holding in holding comparison operation") if $holding_2 ne '~~~';
760 return ( "$holding_2" cmp "$holding_1" );
762 return ( "$holding_1" cmp "$holding_2" );
765 carp("Use of undefined value in holding comparison operation");
766 return 1; # similar to built-in, something is "greater than" nothing
770 # special cases for compressed holdings
771 my ($holding_1_first, $holding_1_last, $holding_2_first, $holding_2_last, $found_compressed);
772 # 0 for no compressed, 1 for first compressed, 2 for second compressed, 3 for both compressed
773 $found_compressed = 0;
774 if ($holding_1->is_compressed) {
775 if (!$holding_1->is_open_ended) {
776 $holding_1_last = $holding_1->clone->compressed_to_last;
778 $holding_1_last = '~~~'; # take advantage of string sort fallback
780 $found_compressed += 1;
782 $holding_1_first = $holding_1;
783 $holding_1_last = $holding_1;
785 if ($holding_2->is_compressed) {
786 $holding_2_first = $holding_2->clone->compressed_to_first;
787 $found_compressed += 2;
789 $holding_2_first = $holding_2;
790 $holding_2_last = $holding_2;
793 if ($found_compressed) {
794 my $cmp = ($holding_1_last cmp $holding_2_first); # 1 ends before 2 starts
796 return -1; # 1 is fully lt
797 } elsif ($cmp == 0) {
798 carp("Overlapping holdings in comparison, lt and gt based on start value only");
800 } else { # check the opposite, 2 ends before 1 starts
801 # clone is expensive, wait until we need it (here)
802 if (!defined($holding_2_last)) {
803 if (!$holding_2->is_open_ended) {
804 $holding_2_last = $holding_2->clone->compressed_to_last;
806 $holding_2_last = '~~~'; # take advantage of string sort fallback
809 if (!defined($holding_1_first)) {
810 $holding_1_first = $holding_1->clone->compressed_to_first;
812 $cmp = ($holding_2_last cmp $holding_1_first);
814 return 1; # 1 is fully gt
815 } elsif ($cmp == 0) {
816 carp("Overlapping holdings in comparison, lt and gt based on start value only");
819 $cmp = ($holding_1_first cmp $holding_2_first);
820 if ($cmp) { # they are not equal
821 carp("Overlapping holdings in comparison, lt and gt based on start value only");
823 } elsif ($found_compressed == 1) {
824 carp("Compressed holding found with start equal to non-compressed holding");
825 return 1; # compressed (first holding) is 'greater than' non-compressed
826 } elsif ($found_compressed == 2) {
827 carp("Compressed holding found with start equal to non-compressed holding");
828 return -1; # compressed (second holding) is 'greater than' non-compressed
829 } else { # both holdings compressed, check for full equality
830 $cmp = ($holding_1_last cmp $holding_2_last);
831 if ($cmp) { # they are not equal
832 carp("Compressed holdings in comparison have equal starts, lt and gt based on end value only");
835 return 0; # both are compressed, both ends are equal
842 # start doing the actual comparison
844 foreach my $key ('a'..'f') {
845 if (defined($holding_1->field_values($key))) {
846 if (!defined($holding_2->field_values($key))) {
847 return 1; # more details equals 'greater' (?)
849 $result = $holding_1->field_values($key)->[0] <=> $holding_2->field_values($key)->[0];
851 } elsif (defined($holding_2->field_values($key))) {
852 return -1; # more details equals 'greater' (?)
855 return $result if $result;
858 # got through, return 0 for equal