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)];
49 $self->{_mfhdh_FIELDS}->{$key}{HOLDINGS} = [$val];
51 if ($key =~ /[a-h]/) {
52 # Enumeration specific details of holdings
53 $self->{_mfhdh_FIELDS}->{$key}{UNIT} = undef;
56 } elsif ($key eq 'o') {
57 warn '$o specified prior to first enumeration'
58 unless defined($last_enum);
59 $self->{_mfhdh_FIELDS}->{$last_enum}->{UNIT} = $val;
61 } elsif ($key =~ /[npq]/) {
62 $self->{_mfhdh_DESCR}->{$key} = $val;
63 } elsif ($key eq 's') {
64 push @{$self->{_mfhdh_COPYRIGHT}}, $val;
65 } elsif ($key eq 't') {
66 $self->{_mfhdh_COPY} = $val;
67 } elsif ($key eq 'w') {
68 carp "Unrecognized break indicator '$val'"
69 unless $val =~ /^[gn]$/;
70 $self->{_mfhdh_BREAK} = $val;
71 } elsif ($key eq 'x') {
72 push @{$self->{_mfhdh_NOTES}{private}}, $val;
73 } elsif ($key eq 'z') {
74 push @{$self->{_mfhdh_NOTES}{public}}, $val;
78 if ( $self->{_mfhdh_COMPRESSED}
79 && $self->{_mfhdh_FIELDS}{'a'}{HOLDINGS}[1] eq '') {
80 $self->{_mfhdh_OPEN_ENDED} = 1;
87 # accessor to the object's field hash
89 # We are avoiding calling these elements 'subfields' because they are more
90 # than simply the MARC subfields, although in the current implementation they
91 # are indexed on the subfield key
93 # TODO: this accessor should probably be replaced with methods which hide the
94 # underlying structure of {_mfhdh_FIELDS} (see field_values for a start)
99 return $self->{_mfhdh_FIELDS};
103 # Given a field key, returns an array ref of one (for single statements)
104 # or two (for compressed statements) values
106 # TODO: add setter functionality to replace direct {HOLDINGS} access in other
107 # methods. It also makes sense to override some of the MARC::Field setter
108 # methods (such as update()) to accomplish this level of encapsulation.
111 my ($self, $key) = @_;
113 if (exists $self->fields->{$key}) {
114 my @values = @{$self->fields->{$key}{HOLDINGS}};
125 $self->{_mfhdh_SEQNO} = $_[0];
126 $self->update(8 => $self->caption->link_id . '.' . $_[0]);
129 return $self->{_mfhdh_SEQNO};
133 # Optionally accepts a true/false value to set the 'compressed' attribute
134 # Returns 'compressed' attribute
138 my $is_compressed = shift;
140 if (defined($is_compressed)) {
141 if ($is_compressed) {
142 $self->{_mfhdh_COMPRESSED} = 1;
143 $self->update(ind2 => '0');
145 $self->{_mfhdh_COMPRESSED} = 0;
146 $self->update(ind2 => '1');
150 return $self->{_mfhdh_COMPRESSED};
156 return $self->{_mfhdh_OPEN_ENDED};
162 return $self->{_mfhdh_CAPTION};
166 # notes: If called with no arguments, returns the public notes array ref.
167 # If called with a single argument, it returns either 'public' or
168 # 'private' notes based on the passed string.
170 # If called with more than one argument, it sets the proper note field, with
171 # type being the first argument and the note value(s) as the remaining
174 # It is also optional to pass in an array ref of note values as the third
175 # argument rather than a list.
184 } elsif ($type ne 'public' && $type ne 'private') {
185 carp("Notes being applied without specifying type");
186 unshift(@notes, $type);
190 if (ref($notes[0])) {
191 $self->{_mfhdh_NOTES}{$type} = $notes[0];
192 $self->_replace_note_subfields($type, @{$notes[0]});
195 $self->{_mfhdh_NOTES}{$type} = \@notes;
197 $self->{_mfhdh_NOTES}{$type} = [];
199 $self->_replace_note_subfields($type, @notes);
202 return $self->{_mfhdh_NOTES}{$type};
206 # utility function for 'notes' method
208 sub _replace_note_subfields {
212 my %note_subfield_ids = ('public' => 'z', 'private' => 'x');
214 $self->delete_subfield(code => $note_subfield_ids{$type});
216 foreach my $note (@notes) {
217 $self->add_subfields($note_subfield_ids{$type} => $note);
222 # return a simple subfields list (for easier revivification from database)
228 foreach my $subfield ($self->subfields) {
229 push(@subfields, $subfield->[0], $subfield->[1]);
235 # Called by method 'format_part' for formatting the chronology portion of
236 # the holding statement
240 my $holdings = shift;
241 my $caption = $self->caption;
263 foreach my $i (0..@keys) {
269 last if !defined $caption->capstr($key);
271 $capstr = $caption->capstr($key);
272 if (substr($capstr, 0, 1) eq '(') {
273 # a caption enclosed in parentheses is not displayed
277 # If this is the second level of chronology, then it's
278 # likely to be a month or season, so we should use the
279 # string name rather than the number given.
281 # account for possible combined issue chronology
282 my @chron_parts = split('/', $holdings->{$key});
283 for (my $i = 0; $i < @chron_parts; $i++) {
284 $chron_parts[$i] = $month{$chron_parts[$i]} if exists $month{$chron_parts[$i]};
286 $chron = join('/', @chron_parts);
288 $chron = $holdings->{$key};
291 $str .= (($i == 0 || $str =~ /[. ]$/) ? '' : ':') . $capstr . $chron;
298 # Called by method 'format' for each member of a possibly compressed holding
302 my $holding_values = shift;
303 my $caption = $self->caption;
306 if ($caption->type_of_unit) {
307 $str = $caption->type_of_unit . ' ';
310 if ($caption->enumeration_is_chronology) {
311 # if issues are identified by chronology only, then the
312 # chronology data is stored in the enumeration subfields,
313 # so format those fields as if they were chronological.
314 $str = $self->format_chron($holding_values, 'a'..'f');
316 # OK, there is enumeration data and maybe chronology
317 # data as well, format both parts appropriately
320 foreach my $key ('a'..'f') {
325 last if !defined $caption->capstr($key);
327 $capstr = $caption->capstr($key);
328 if (substr($capstr, 0, 1) eq '(') {
329 # a caption enclosed in parentheses is not displayed
333 ($key eq 'a' ? '' : ':') . $capstr . $holding_values->{$key};
337 if (defined $caption->capstr('i')) {
339 $str .= $self->format_chron($holding_values, 'i'..'l');
343 if ($caption->capstr('g')) {
344 # There's at least one level of alternative enumeration
346 foreach my $key ('g', 'h') {
348 ($key eq 'g' ? '' : ':')
349 . $caption->capstr($key)
350 . $holding_values->{$key};
353 # This assumes that alternative chronology is only ever
354 # provided if there is an alternative enumeration.
355 if ($caption->capstr('m')) {
356 # Alternative Chronology
358 $str .= $caption->capstr('m') . $holding_values->{'m'};
364 # Breaks in the sequence
365 # XXX: this is non-standard and also not the right place for this, since gaps
366 # only make sense in the context of multiple holding segments, not a single
368 # if (defined($self->{_mfhdh_BREAK})) {
369 # if ($self->{_mfhdh_BREAK} eq 'n') {
370 # $str .= ' non-gap break';
371 # } elsif ($self->{_mfhdh_BREAK} eq 'g') {
374 # warn "unrecognized break indicator '$self->{_mfhdh_BREAK}'";
382 # Create and return a string which conforms to display standard Z39.71
386 my $subfields = $self->fields;
391 foreach my $key (keys %$subfields) {
392 ($holding_start{$key}, $holding_end{$key}) =
393 @{$self->field_values($key)};
396 if ($self->is_compressed) {
397 # deal with open-ended statements
399 if ($self->is_open_ended) {
402 $formatted_end = $self->format_part(\%holding_end);
405 $self->format_part(\%holding_start) . ' - ' . $formatted_end;
407 $formatted = $self->format_part(\%holding_start);
411 if (@{$self->notes}) {
412 $formatted .= ' -- ' . join(', ', @{$self->notes});
418 # next: Given a holding statement, return a hash containing the
419 # enumeration values for the next issues, whether we hold it or not
420 # Just pass through to Caption::next
424 my $caption = $self->caption;
426 return $caption->next($self);
430 # matches($pat): check to see if $self matches the enumeration hashref passed
431 # in as $pat, as returned by the 'next' method. e.g.:
432 # $holding2->matches($holding1->next) # true if $holding2 directly follows
435 # Always returns false if $self is compressed
441 return 0 if $self->is_compressed;
443 foreach my $key ('a'..'f') {
444 # If a subfield exists in $self but not in $pat, or vice versa
445 # or if the field has different values, then fail
447 defined($self->field_values($key)) != exists($pat->{$key})
448 || (exists $pat->{$key}
449 && ($self->field_values($key)->[0] ne $pat->{$key}))
458 # Check that all the fields in a holdings statement are
459 # included in the corresponding caption.
464 foreach my $key (keys %{$self->fields}) {
465 if (!$self->caption || !$self->caption->capfield($key)) {
473 # Replace a single holding with it's next prediction
479 if ($self->is_open_ended) {
480 carp "Holding is open-ended, cannot increment";
482 } elsif ($self->is_compressed) {
483 carp "Incrementing a compressed holding is deprecated, use extend instead";
484 return $self->extend;
487 my $next = $self->next();
489 foreach my $key (keys %{$next}) {
490 $self->fields->{$key}{HOLDINGS}[0] = $next->{$key};
493 $self->seqno($self->seqno + 1);
494 $self->update(%{$next}); # update underlying subfields
499 # Extends a holding (compressing if needed) to include the next
500 # prediction and returns itself
505 if ($self->is_open_ended) {
506 carp "Holding is open-ended, cannot extend";
510 my $next = $self->next();
512 if (!$self->is_compressed) {
513 $self->is_compressed(1); # add compressed state
516 foreach my $key (keys %{$next}) {
517 my @values = @{$self->field_values($key)};
518 $values[1] = $next->{$key};
519 $self->fields->{$key}{HOLDINGS} = \@values;
520 $next->{$key} = join('-', @values);
523 $self->update(%{$next}); # update underlying subfields
528 # Turns a compressed holding into the singular form of the first member
531 sub compressed_to_first {
534 if (!$self->is_compressed) {
535 carp "Holding not compressed, cannot convert to first member";
540 foreach my $key (keys %{$self->fields}) {
541 my @values = @{$self->field_values($key)};
542 $self->fields->{$key}{HOLDINGS} = [$values[0]];
543 $changes{$key} = $values[0];
546 $self->update(%changes); # update underlying subfields
547 $self->is_compressed(0); # remove compressed state
553 # Turns a compressed holding into the singular form of the last member
556 sub compressed_to_last {
559 if (!$self->is_compressed) {
560 carp "Holding not compressed, cannot convert to last member";
562 } elsif ($self->is_open_ended) {
563 carp "Holding is open-ended, cannot convert to last member";
568 foreach my $key (keys %{$self->fields}) {
569 my @values = @{$self->field_values($key)};
570 $self->fields->{$key}{HOLDINGS} = [$values[1]];
571 $changes{$key} = $values[1];
574 $self->update(%changes); # update underlying subfields
575 $self->is_compressed(0); # remove compressed state
581 # Basic, working, unoptimized clone operation
586 my $clone_field = $self->SUPER::clone();
587 return new MFHD::Holding($self->seqno, $clone_field, $self->caption);
591 # Turn a chronology instance into date(s) in YYYY-MM-DD format
593 # In list context it returns a list of start and (possibly undefined)
596 # In scalar context, it returns a YYYY-MM-DD date string of either the
597 # single date or the (possibly undefined) end date of a compressed holding
601 my $caption = $self->caption;
604 if ($caption->enumeration_is_chronology) {
610 # @chron_start and @chron_end will hold the (year, month, day) values
611 # represented by the start and optional end of the chronology instance.
612 # Default to January 1 with a year of 0 as initial values.
613 my @chron_start = (0, 1, 1);
614 my @chron_end = (0, 1, 1);
615 my @chrons = (\@chron_start, \@chron_end);
616 foreach my $key (@keys) {
617 my $capstr = $caption->capstr($key);
618 last if !defined($capstr);
619 if ($capstr =~ /year/) {
620 ($chron_start[0], $chron_end[0]) = @{$self->field_values($key)};
621 } elsif ($capstr =~ /month/) {
622 ($chron_start[1], $chron_end[1]) = @{$self->field_values($key)};
623 } elsif ($capstr =~ /day/) {
624 ($chron_start[2], $chron_end[2]) = @{$self->field_values($key)};
625 } elsif ($capstr =~ /season/) {
626 # chrons defined as season-only will use the astronomical season
627 # dates as a basic estimate.
628 my @seasons = @{$self->field_values($key)};
629 for (my $i = 0; $i < @seasons; $i++) {
630 $seasons[$i] = &_uncombine($seasons[$i], 0);
631 if ($seasons[$i] == 21) {
632 $chrons[$i]->[1] = 3;
633 $chrons[$i]->[2] = 20;
634 } elsif ($seasons[$i] == 22) {
635 $chrons[$i]->[1] = 6;
636 $chrons[$i]->[2] = 21;
637 } elsif ($seasons[$i] == 23) {
638 $chrons[$i]->[1] = 9;
639 $chrons[$i]->[2] = 22;
640 } elsif ($seasons[$i] == 24) {
641 # "winter" can come at the beginning or end of a year,
642 if ($self->caption->winter_starts_year()) {
643 $chrons[$i]->[1] = 1;
644 $chrons[$i]->[2] = 1;
645 } else { # default to astronomical
646 $chrons[$i]->[1] = 12;
647 $chrons[$i]->[2] = 21;
654 # if we have an an annual, set the month to ypm## if available
655 if (exists($self->caption->{_mfhdc_PATTERN}->{y}->{p}) and $self->caption->{_mfhdc_PATTERN}->{w} eq 'a') {
656 my $reg = $self->caption->{_mfhdc_PATTERN}->{y}->{p}->[0];
657 if ($reg =~ /^m(\d+)/) {
658 $chrons[0]->[1] = $1;
659 $chrons[1]->[1] = $1;
664 foreach my $chron (@chrons) {
666 if ($chron->[0] != 0) {
668 &_uncombine($chron->[0], 0) . '-'
669 . sprintf('%02d', $chron->[1]) . '-'
670 . sprintf('%02d', $chron->[2]);
677 } elsif ($self->is_compressed) {
685 # utility function for uncombining instance parts
688 my ($combo, $pos) = @_;
691 carp("Function '_uncombine' is not an instance method");
695 my @parts = split('/', $combo);
700 # Overload string comparison operators
702 # We are not overloading '<=>' because '==' is used liberally in MARC::Record
703 # to compare field identity (i.e. is this the same exact Field object?), not value
705 # Other string operators are auto-generated from 'cmp'
707 # Please note that this comparison is based on what the holding represents,
708 # not whether it is strictly identical (e.g. the seqno and link may vary)
710 use overload ('cmp' => \&_compare,
713 my ($holding_1, $holding_2) = @_;
715 # TODO: this needs some more consideration
716 # fall back to 'built-in' comparison
717 if (!UNIVERSAL::isa($holding_2, ref $holding_1)) {
718 if (defined $holding_2) {
719 carp("Use of non-holding in holding comparison operation");
720 return ( "$holding_1" cmp "$holding_2" );
722 carp("Use of undefined value in holding comparison operation");
723 return 1; # similar to built-in, something is "greater than" nothing
727 # special cases for compressed holdings
728 my ($holding_1_first, $holding_1_last, $holding_2_first, $holding_2_last, $found_compressed);
729 # 0 for no compressed, 1 for first compressed, 2 for second compressed, 3 for both compressed
730 $found_compressed = 0;
731 if ($holding_1->is_compressed) {
732 $holding_1_last = $holding_1->clone->compressed_to_last;
733 $found_compressed += 1;
735 $holding_1_first = $holding_1;
736 $holding_1_last = $holding_1;
738 if ($holding_2->is_compressed) {
739 $holding_2_first = $holding_2->clone->compressed_to_first;
740 $found_compressed += 2;
742 $holding_2_first = $holding_2;
743 $holding_2_last = $holding_2;
746 if ($found_compressed) {
747 my $cmp = ($holding_1_last cmp $holding_2_first); # 1 ends before 2 starts
749 return -1; # 1 is fully lt
750 } elsif ($cmp == 0) {
751 carp("Overlapping holdings in comparison, lt and gt based on start value only");
753 } else { # check the opposite, 2 ends before 1 starts
754 # clone is expensive, wait until we need it (here)
755 if (!defined($holding_2_last)) {
756 $holding_2_last = $holding_2->clone->compressed_to_last;
758 if (!defined($holding_1_first)) {
759 $holding_1_first = $holding_1->clone->compressed_to_first;
761 $cmp = ($holding_2_last cmp $holding_1_first);
763 return 1; # 1 is fully gt
764 } elsif ($cmp == 0) {
765 carp("Overlapping holdings in comparison, lt and gt based on start value only");
768 $cmp = ($holding_1_first cmp $holding_2_first);
769 if (!$cmp) { # they are not equal
770 carp("Overlapping holdings in comparison, lt and gt based on start value only");
772 } elsif ($found_compressed == 1) {
773 carp("Compressed holding found with start equal to non-compressed holding");
774 return 1; # compressed (first holding) is 'greater than' non-compressed
775 } elsif ($found_compressed == 2) {
776 carp("Compressed holding found with start equal to non-compressed holding");
777 return -1; # compressed (second holding) is 'greater than' non-compressed
778 } else { # both holdings compressed, check for full equality
779 $cmp = ($holding_1_last cmp $holding_2_last);
780 if (!$cmp) { # they are not equal
781 carp("Compressed holdings in comparison have equal starts, lt and gt based on end value only");
784 return 0; # both are compressed, both ends are equal
791 # start doing the actual comparison
793 foreach my $key ('a'..'f') {
794 if (defined($holding_1->field_values($key))) {
795 if (!defined($holding_2->field_values($key))) {
796 return 1; # more details equals 'greater' (?)
798 $result = $holding_1->field_values($key)->[0] <=> $holding_2->field_values($key)->[0];
800 } elsif (defined($holding_2->field_values($key))) {
801 return -1; # more details equals 'greater' (?)
804 return $result if $result;
807 # got through, return 0 for equal