Koha/Contrib/Sudoc/Loader.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 103 CC 6 SCAR 20.4
CC
6
Cov
0%
run
20.0
BUILD
12.0
handle_record
2.0
CRAP 7.7
1 package Koha::Contrib::Sudoc::Loader;
2 # ABSTRACT: Classe de base pour le chargement de notices biblio/autorité
3 $Koha::Contrib::Sudoc::Loader::VERSION = '2.50';
4 use Moose;
5 use Modern::Perl;
6 use utf8;
7 use MARC::Moose::Reader::File::Iso2709;
8 use Koha::Contrib::Sudoc::Converter;
9 use Log::Dispatch;
10 use Log::Dispatch::Screen;
11 use Log::Dispatch::File;
12 use Class::Load ':all';
13 use DateTime;
14  
15  
16 # Moulinette SUDOC
17 has sudoc => ( is => 'rw', isa => 'Koha::Contrib::Sudoc', required => 1 );
18  
19 # Fichier des notices biblios/autorités
20 has file => ( is => 'rw', isa => 'Str', required => 1 );
21  
22 # Chargement effectif ?
23 has doit => ( is => 'rw', isa => 'Bool', default => 0 );
24  
25 # Compteur d'enregistrements traités
26 has count => ( is => 'rw', isa => 'Int', default => 0 );
27  
28 # Compteur d'enregistrements ajoutés
29 has count_added => ( is => 'rw', isa => 'Int', default => 0 );
30  
31 # Compteur d'enregistrements remplacés
32 has count_replaced => ( is => 'rw', isa => 'Int', default => 0 );
33  
34 # Compteur d'enregistrements non traités
35 has count_skipped => ( is => 'rw', isa => 'Int', default => 0 );
36  
37 # Converter
38 has converter => (
39 is => 'rw',
40 isa => 'Koha::Contrib::Sudoc::Converter',
41 );
42  
43 # Le logger
44 has log => (
45 is => 'rw',
46 isa => 'Log::Dispatch',
47 default => sub { Log::Dispatch->new() },
48 );
49  
50  
51 sub BUILD {
52 my $self = shift;
53  
54 my $id = ref($self);
55 ($id) = $id =~ /.*:(.*)$/;
56  
57 $self->log->add( Log::Dispatch::Screen->new(
58 name => 'screen',
59 min_level => 'notice',
60 stderr => 0,
61 ) );
62 binmode(STDOUT, ':encoding(utf8)');
63 $self->log->add( Log::Dispatch::File->new(
64 name => 'file1',
65 min_level => 'debug',
66 filename => $self->sudoc->root . "/var/log/$id.txt",
67 mode => '>>',
68 binmode => ':encoding(utf8)',
69 ) );
70 $self->log->add( Log::Dispatch::File->new(
71 name => 'file2',
72 min_level => $self->sudoc->c->{loading}->{log}->{level},
73 filename => $self->sudoc->root . "/var/log/email.txt",
74 mode => '>>',
75 binmode => ':encoding(utf8)',
76 ) );
77  
78 # Instanciation du converter
79 my $class = 'Koha::Contrib::Sudoc::Converter';
80 if ( my $local_class = $self->sudoc->c->{biblio}->{converter} ) {
81 my ($retcod, $error ) = try_load_class($local_class);
82 if ( $retcod ) {
83 $class = $local_class;
84 }
85 else {
86 die $error ;
87 }
88 }
89 load_class($class);
90 $class = $class->new(sudoc => $self->sudoc, log => $self->log);
91 $self->converter($class);
92 }
93  
94  
95  
96 # C'est cette méthode qui est surchargée par les sous-classes dédiées au
97 # traitement des notices biblio et d'autorités
98 sub handle_record {
99 my ($self, $record) = @_;
100 }
101  
102  
103 sub run {
104 my $self = shift;
105  
106 my $tz = DateTime::TimeZone->new( name => 'local' );
107 my $dt = DateTime->now( time_zone => $tz );;
108 $self->log->debug($dt->dmy . " " . $dt->hms . "\n");
109 $self->log->notice("Chargement du fichier " . $self->file . "\n");
110 $self->log->notice("** Test **\n") unless $self->doit;
111 my $reader = MARC::Moose::Reader::File::Iso2709->new(
112 file => $self->sudoc->spool->file_path( $self->file ) );
113 while ( my $record = $reader->read() ) {
114 $self->count( $self->count + 1 );
115 $self->handle_record($record);
116 }
117 $self->converter->end();
118
119 $self->sudoc->spool->move_done($self->file) if $self->doit;
120 my $format = '%#' . length($self->count) . "d\n";
121 $self->log->notice(
122 "Enregistrements traités : " . sprintf($format, $self->count) .
123 "Enregistrements ajoutés : " . sprintf($format, $self->count_added) .
124 "Enregistrements fusionnés : " . sprintf($format, $self->count_replaced ) .
125 "Enregistrements ignorés : " . sprintf($format, $self->count_skipped)
126 );
127 $self->log->notice("** Test ** Le fichier " . $self->file . " n'a pas été chargé\n")
128 unless $self->doit;
129 $self->log->notice("\n");
130 }
131  
132  
133 1;
134  
135 __END__