Bug 25009: [18.11.x] Add missing biblio_metadata stuffs 18.11.x v18.11.16
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 22 Apr 2020 13:06:16 +0000 (15:06 +0200)
committerHayley Mapley <hayleymapley@catalyst.net.nz>
Wed, 22 Apr 2020 22:08:22 +0000 (10:08 +1200)
It adds methods, DBIC rs and Exceptions class for biblio metadata.

Note that schema is marcflavour in 18.11

Signed-off-by: Hayley Mapley <hayleymapley@catalyst.net.nz>

Koha/Biblio.pm
Koha/Biblio/Metadata.pm
Koha/Exceptions/Metadata.pm [new file with mode: 0644]
Koha/Schema/Result/Biblio.pm

index 1dca4d9..c92881c 100644 (file)
@@ -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();
index f271f6f..bcc2c28 100644 (file)
@@ -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<Koha::Exceptions::Metadata> exception.
+
+=item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> 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 (file)
index 0000000..ca0a466
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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;
index b118011..303ad92 100644 (file)
@@ -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;