From 13356ae74ee50ecb55694678bf2fa78b6679e65e Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 20 Jul 2023 14:44:01 +0000 Subject: [PATCH] Bug 34587: COUNTER file upload Adds the ability to manually upload a counter file and harvest data to the provider Signed-off-by: Jessica Zairo Signed-off-by: Michaela Sieber Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/CounterFile.pm | 29 ++++++----- Koha/ERM/UsageDataProvider.pm | 1 - Koha/Exceptions/ERM/CounterFile.pm | 50 +++++++++++++++++++ Koha/REST/V1/ERM/CounterFiles.pm | 1 + ...UsageStatisticsDataProvidersFileImport.vue | 16 +++++- 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 Koha/Exceptions/ERM/CounterFile.pm diff --git a/Koha/ERM/CounterFile.pm b/Koha/ERM/CounterFile.pm index c6775fe227..046d62f4e5 100644 --- a/Koha/ERM/CounterFile.pm +++ b/Koha/ERM/CounterFile.pm @@ -29,6 +29,7 @@ use Koha::ERM::UsageItems; use Koha::ERM::UsageTitle; use Koha::ERM::UsageTitles; use Koha::ERM::UsageDataProvider; +use Koha::Exceptions::ERM::CounterFile; use base qw(Koha::Object); @@ -78,6 +79,9 @@ Receive background_job_callbacks to be able to update job progress sub store { my ( $self, $background_job_callbacks ) = @_; + $self->_validate; + $self->_set_report_type_from_file; + my $result = $self->SUPER::store; # Set class wide background_job callbacks @@ -134,6 +138,9 @@ sub _add_usage_objects { $usage_object = $previous_object; } else { + # Update background job step + $self->{job_callbacks}->{step_callback}->() if $self->{job_callbacks}; + # Check if usage object already exists in this data provider, e.g. from a previous harvest $usage_object = $self->_search_for_usage_object($row); @@ -266,7 +273,7 @@ sub _add_yearly_usage_entries { } } -=head3 validate +=head3 _validate Verifies if the given file_content is a valid COUNTER file or not @@ -275,25 +282,23 @@ A I exception is thrown =cut -sub validate { +sub _validate { my ($self) = @_; open my $fh, "<", \$self->file_content or die; - my $csv = Text::CSV_XS->new( - { binary => 1, always_quote => 1, eol => $/, decode_utf8 => 1 } ); + my $csv = Text::CSV_XS->new( { binary => 1, always_quote => 1, eol => $/, decode_utf8 => 1 } ); $csv->column_names(qw( header_key header_value )); my @header_rows = $csv->getline_hr_all( $fh, 0, 12 ); - my @header = $header_rows[0]; + my @header = $header_rows[0]; - my @release_row = - map( $_->{header_key} eq 'Release' ? $_ : (), @{ $header[0] } ); + my @release_row = map( $_->{header_key} eq 'Release' ? $_ : (), @{ $header[0] } ); my $release = $release_row[0]; # TODO: Validate that there is an empty row between header and body Koha::Exceptions::ERM::CounterFile::UnsupportedRelease->throw - if $release && $release->{header_value} != 5; + if $release && $release->{header_value} != 5; } @@ -307,16 +312,14 @@ sub _set_report_type_from_file { my ($self) = @_; open my $fh, "<", \$self->file_content or die; - my $csv = Text::CSV_XS->new( - { binary => 1, always_quote => 1, eol => $/, decode_utf8 => 1 } ); + my $csv = Text::CSV_XS->new( { binary => 1, always_quote => 1, eol => $/, decode_utf8 => 1 } ); $csv->column_names(qw( header_key header_value )); my @header_rows = $csv->getline_hr_all( $fh, 0, 12 ); my @header = $header_rows[0]; - my @report_id_row = - map( $_->{header_key} eq 'Report_ID' ? $_ : (), @{ $header[0] } ); - my $report = $report_id_row[0]; + my @report_id_row = map( $_->{header_key} eq 'Report_ID' ? $_ : (), @{ $header[0] } ); + my $report = $report_id_row[0]; $self->type( $report->{header_value} ); } diff --git a/Koha/ERM/UsageDataProvider.pm b/Koha/ERM/UsageDataProvider.pm index 36844fe548..761cd2449f 100644 --- a/Koha/ERM/UsageDataProvider.pm +++ b/Koha/ERM/UsageDataProvider.pm @@ -299,7 +299,6 @@ sub _build_COUNTER_report_file { #TODO: add ".csv" to end of filename here filename => $self->name . "_" . $self->{report_type}, - type => $self->{report_type} } ] ); diff --git a/Koha/Exceptions/ERM/CounterFile.pm b/Koha/Exceptions/ERM/CounterFile.pm new file mode 100644 index 0000000000..8da4acc06e --- /dev/null +++ b/Koha/Exceptions/ERM/CounterFile.pm @@ -0,0 +1,50 @@ +package Koha::Exceptions::ERM::CounterFile; + +# 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 Koha::Exception; + +use Exception::Class ( + + 'Koha::Exceptions::ERM::CounterFile' => { + isa => 'Koha::Exception', + }, + 'Koha::Exceptions::ERM::CounterFile::UnsupportedRelease' => { + isa => 'Koha::Exceptions::ERM::CounterFile', + description => 'This COUNTER release is not supported' + } +); + +=head1 NAME + +Koha::Exceptions::ERM::CounterFile - Base class for CounterFile exceptions + +=head1 Exceptions + + +=head2 Koha::Exceptions::ERM::CounterFile + +Generic CounterFile exception + +=head2 Koha::Exceptions::ERM::CounterFile::UnsupportedRelease + +Exception to be used when a report is submit with an unsupported COUNTER release + +=cut + +1; diff --git a/Koha/REST/V1/ERM/CounterFiles.pm b/Koha/REST/V1/ERM/CounterFiles.pm index b19afd497e..57e845a0e4 100644 --- a/Koha/REST/V1/ERM/CounterFiles.pm +++ b/Koha/REST/V1/ERM/CounterFiles.pm @@ -17,6 +17,7 @@ package Koha::REST::V1::ERM::CounterFiles; use Modern::Perl; +use MIME::Base64 qw( decode_base64 ); use Mojo::Base 'Mojolicious::Controller'; use Koha::ERM::CounterFiles; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsDataProvidersFileImport.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsDataProvidersFileImport.vue index 2e9ac594ad..3c48ee06ec 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsDataProvidersFileImport.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsDataProvidersFileImport.vue @@ -37,6 +37,7 @@