From b0015b1e315c2eceb343308755e767cda844b7ff Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 22 Apr 2020 15:06:16 +0200 Subject: [PATCH] Bug 25009: [18.11.x] Add missing biblio_metadata stuffs It adds methods, DBIC rs and Exceptions class for biblio metadata. Note that schema is marcflavour in 18.11 Signed-off-by: Hayley Mapley --- Koha/Biblio.pm | 16 +++++++++ Koha/Biblio/Metadata.pm | 53 +++++++++++++++++++++++++++ Koha/Exceptions/Metadata.pm | 69 ++++++++++++++++++++++++++++++++++++ Koha/Schema/Result/Biblio.pm | 7 ++++ 4 files changed, 145 insertions(+) create mode 100644 Koha/Exceptions/Metadata.pm diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 1dca4d9cf6..c92881cf45 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -30,6 +30,7 @@ use base qw(Koha::Object); use Koha::Items; use Koha::Biblioitems; +use Koha::Biblio::Metadata; use Koha::ArticleRequests; use Koha::ArticleRequest::Status; use Koha::IssuingRules; @@ -61,6 +62,21 @@ sub store { return $self->SUPER::store; } +=head3 metadata + +my $metadata = $biblio->metadata(); + +Returns a Koha::Biblio::Metadata object + +=cut + +sub metadata { + my ( $self ) = @_; + + my $metadata = $self->_result->metadata; + return Koha::Biblio::Metadata->_new_from_dbic($metadata); +} + =head3 subtitles my @subtitles = $biblio->subtitles(); diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index f271f6f824..bcc2c2810d 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::Exceptions::Metadata; use base qw(Koha::Object); @@ -33,6 +34,58 @@ Koha::Metadata - Koha Metadata Object class =cut +=head3 record + +my $record = $metadata->record; + +Returns an object representing the metadata record. The expected record type +corresponds to this table: + + ------------------------------- + | format | object type | + ------------------------------- + | marcxml | MARC::Record | + ------------------------------- + +=head4 Error handling + +=over + +=item If an unsupported format is found, it throws a I exception. + +=item If it fails to create the record object, it throws a I exception. + +=back + +=cut + +sub record { + + my ($self) = @_; + + my $record; + + if ( $self->format eq 'marcxml' ) { + $record = eval { MARC::Record::new_from_xml( $self->metadata, 'UTF-8', $self->marcflavour ); }; + my $marcxml_error = $@; + chomp $marcxml_error; + unless ($record) { + Koha::Exceptions::Metadata::Invalid->throw( + id => $self->id, + format => $self->format, + marcflavour => $self->marcflavour, + decoding_error => $marcxml_error, + ); + } + } + else { + Koha::Exceptions::Metadata->throw( + 'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format ); + } + + return $record; +} + =head3 type =cut diff --git a/Koha/Exceptions/Metadata.pm b/Koha/Exceptions/Metadata.pm new file mode 100644 index 0000000000..ca0a466fcf --- /dev/null +++ b/Koha/Exceptions/Metadata.pm @@ -0,0 +1,69 @@ +package Koha::Exceptions::Metadata; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Metadata' => { + description => 'Something went wrong!', + }, + 'Koha::Exceptions::Metadata::Invalid' => { + isa => 'Koha::Exceptions::Metadata', + description => 'Invalid data', + fields => ['id','format','marcflavour', 'decoding_error'] + } +); + +sub full_message { + my $self = shift; + + my $msg = $self->message; + + unless ($msg) { + if ( $self->isa('Koha::Exceptions::Metadata::Invalid') ) { + $msg = sprintf( "Invalid data, cannot decode object (id=%s, format=%s, marcflavour=%s, decoding_error='%s')", + $self->id, $self->format, $self->marcflavour, $self->decoding_error ); + } + } + + return $msg; +} + +=head1 NAME + +Koha::Exceptions::Metadata - Base class for metadata exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Metadata + +Generic metadata exception + +=head2 Koha::Exceptions::Metadata::Invalid + +The metadata is invalid. + +=head1 Class methods + +=head2 full_message + +Overloaded method for exception stringifying. + +=cut + +1; diff --git a/Koha/Schema/Result/Biblio.pm b/Koha/Schema/Result/Biblio.pm index b118011f27..303ad92135 100644 --- a/Koha/Schema/Result/Biblio.pm +++ b/Koha/Schema/Result/Biblio.pm @@ -351,4 +351,11 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-02-16 17:54:53 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bUv00JjY09Hj2Zj4klqyxA +__PACKAGE__->has_one( + "metadata", + "Koha::Schema::Result::BiblioMetadata", + { "foreign.biblionumber" => "self.biblionumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + 1; -- 2.39.5