Skip to content
Snippets Groups Projects
Commit 62654a0d authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, extracted output related code to module SLUB::LZA::TA::Output

parent 4e6ac280
No related branches found
No related tags found
No related merge requests found
package SLUB::LZA::TA::Command::search; package SLUB::LZA::TA::Command::search;
use SLUB::LZA::TA -command; use SLUB::LZA::TA -command;
use strict; use v5.36;
use warnings;
use feature qw(say);
use SLUB::LZA::TA::Archivematica::Elasticsearch; use SLUB::LZA::TA::Archivematica::Elasticsearch;
use SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery; use SLUB::LZA::TA::Archivematica::Elasticsearch::PrepareQuery;
use Data::Printer; use SLUB::LZA::TA::Output;
use constant VALUE_TERMINATOR => 0xff;
use constant NULL => 0xfe;
use constant ROW_TERMINATOR => 0xfd;
use constant CHR_VALUE_TERMINATOR => chr(VALUE_TERMINATOR);
use constant CHR_NULL => chr(NULL);
use constant CHR_ROW_TERMINATOR => chr(ROW_TERMINATOR);
use List::Util qw(any none);
use Date::Calc qw(Date_to_Time); use Date::Calc qw(Date_to_Time);
use namespace::autoclean -except => qr{SLUB::LZA::TA::.*}; use namespace::autoclean -except => qr{SLUB::LZA::TA::.*};
...@@ -143,53 +135,6 @@ sub validate_args { ...@@ -143,53 +135,6 @@ sub validate_args {
1; 1;
} }
sub line_result_mapper {
my ($partial_result, $opt) = @_;
my %line;
$line{aipid}= $partial_result->{_source}->{uuid};
my @tmd = qw(
External-Description
External-Identifier
LDP-collection
LDP-funder
LDP-lender
LDP-project
SLUBArchiv-archivalValueDescription
SLUBArchiv-exportToArchiveDate
SLUBArchiv-externalId
SLUBArchiv-externalIsilId
SLUBArchiv-externalWorkflow
SLUBArchiv-hasConservationReason
SLUBArchiv-lzaId
SLUBArchiv-migrated-AIP
SLUBArchiv-origin-AIS
SLUBArchiv-previous-AIP
SLUBArchiv-rightsVersion
SLUBArchiv-sipVersion
Title
);
foreach my $t (@tmd) {
my $linekey = lc($t); $linekey=~s/-/_/g;
my $optionkey = lc("with_".$t); $optionkey=~s/-/_/g;
if (exists $opt->{$optionkey}) {
# FIXME: HashRef error in next line!
#p($partial_result);
if (exists($partial_result->{_source}->{transferMetadata}->[0]->{'bim:bag-info_dict'}->{ "bim:$t" }) ) {
$line{$linekey} = $partial_result->{_source}->{transferMetadata}->[0]->{'bim:bag-info_dict'}->{ "bim:$t" };
} #else {/
# say "$t -->";
#say np($partial_result);
#}
}
}
if (defined $opt->{with_creationdate}) { $line{creation_date} = $partial_result->{_source}->{created} };
if (defined $opt->{with_filecount}) { $line{filecount} = $partial_result->{_source}->{file_count} };
if (defined $opt->{with_path}) { $line{path}= $partial_result->{_source}->{filePath} };
if (defined $opt->{with_score}) { $line{score} = $partial_result->{_score}}
if (defined $opt->{with_size}) { $line{size}= $partial_result->{_source}->{size} };
\%line;
}
sub find_aips_by_file_pronom_id { sub find_aips_by_file_pronom_id {
my ($self, $opt, $args) = @_; my ($self, $opt, $args) = @_;
...@@ -248,65 +193,8 @@ sub execute { ...@@ -248,65 +193,8 @@ sub execute {
$aips_query, # query_hash ref $aips_query, # query_hash ref
); );
#p($aips_response); #p($aips_response);
SLUB::LZA::TA::Output::print_results($aips_response, $opt);
# print pagination to STDERR
my $all = $aips_response->{hits}->{total};
my $from = $opt->{startrecord} || 1;
my $to = $all > $opt->{maxrecords} ? ($opt->{maxrecords} + $from -1) : ($all + $from -1);
my $idx = $from;
my @result = map {
my $tmp_new = line_result_mapper($_, $opt);
if (defined $opt->{with_result_index}) {
$tmp_new->{_idx} = $idx++;
}
$tmp_new;
} @{$aips_response->{hits}->{hits}};
my @headers = sort keys %{$result[0]};
my $aip_only = List::Util::none {$_ =~ m/^with/} keys %{$opt};
if (!exists $opt->{output_format}) {
if ($aip_only) {
$opt->{output_format} = 'output_as_csv'
}
else {
$opt->{output_format} = 'output_as_rsv';
}
}
say STDERR "found $all result" . ($all != 1 ? 's' : '');
if ($all > 0) {
my $output_format_human = $opt->{output_format}; $output_format_human =~ s/_/ /g;
say STDERR "reporting results". ($from != $to ? " $from$to " : " ")."$output_format_human";
}
if ($opt->{output_format} eq 'output_as_csv') {
binmode(STDOUT, ':encoding(UTF-8)');
say join(",", @headers);
say join("\n", map {
my $line = $_;
my $res_line = join(
",",
map {$line->{$_}} @headers
);
$res_line;
} @result
);
}
elsif ($opt->{output_format} eq 'output_as_rsv') {
binmode(STDOUT, ':bytes');
print join(CHR_VALUE_TERMINATOR, @headers) . CHR_VALUE_TERMINATOR;
print CHR_ROW_TERMINATOR;
print join("", map {
my $line = $_;
my $res_line = join("", map {$line->{$_} . CHR_VALUE_TERMINATOR} @headers
);
$res_line . CHR_ROW_TERMINATOR;
} @result
);
binmode(STDOUT, ':encoding(UTF-8)');
}
elsif ($opt->{output_format} eq 'output_as_raw') {
say np($aips_response);
}
return 1;
} }
1; 1;
package SLUB::LZA::TA::Output;
use v5.36;
use constant VALUE_TERMINATOR => 0xff;
use constant NULL => 0xfe;
use constant ROW_TERMINATOR => 0xfd;
use constant CHR_VALUE_TERMINATOR => chr(VALUE_TERMINATOR);
use constant CHR_NULL => chr(NULL);
use constant CHR_ROW_TERMINATOR => chr(ROW_TERMINATOR);
use List::Util qw(any none);
use Data::Printer;
use namespace::autoclean -except => qr{SLUB::LZA::TA::.*};
# VERSION
# ABSTRACT: search IEs module for ta-tool
sub line_result_mapper {
my ($partial_result, $opt) = @_;
my %line;
$line{aipid}= $partial_result->{_source}->{uuid};
my @tmd = qw(
External-Description
External-Identifier
LDP-collection
LDP-funder
LDP-lender
LDP-project
SLUBArchiv-archivalValueDescription
SLUBArchiv-exportToArchiveDate
SLUBArchiv-externalId
SLUBArchiv-externalIsilId
SLUBArchiv-externalWorkflow
SLUBArchiv-hasConservationReason
SLUBArchiv-lzaId
SLUBArchiv-migrated-AIP
SLUBArchiv-origin-AIS
SLUBArchiv-previous-AIP
SLUBArchiv-rightsVersion
SLUBArchiv-sipVersion
Title
);
foreach my $t (@tmd) {
my $linekey = lc($t); $linekey=~s/-/_/g;
my $optionkey = lc("with_".$t); $optionkey=~s/-/_/g;
if (exists $opt->{$optionkey}) {
# FIXME: HashRef error in next line!
#p($partial_result);
if (exists($partial_result->{_source}->{transferMetadata}->[0]->{'bim:bag-info_dict'}->{ "bim:$t" }) ) {
$line{$linekey} = $partial_result->{_source}->{transferMetadata}->[0]->{'bim:bag-info_dict'}->{ "bim:$t" };
} #else {/
# say "$t -->";
#say np($partial_result);
#}
}
}
if (defined $opt->{with_creationdate}) { $line{creation_date} = $partial_result->{_source}->{created} };
if (defined $opt->{with_filecount}) { $line{filecount} = $partial_result->{_source}->{file_count} };
if (defined $opt->{with_path}) { $line{path}= $partial_result->{_source}->{filePath} };
if (defined $opt->{with_score}) { $line{score} = $partial_result->{_score}}
if (defined $opt->{with_size}) { $line{size}= $partial_result->{_source}->{size} };
\%line;
}
sub print_results ($aips_response, $opt) {
# print pagination to STDERR
my $all = $aips_response->{hits}->{total};
my $from = $opt->{startrecord} || 1;
my $to = $all > $opt->{maxrecords} ? ($opt->{maxrecords} + $from -1) : ($all + $from -1);
my $idx = $from;
my @result = map {
my $tmp_new = line_result_mapper($_, $opt);
if (defined $opt->{with_result_index}) {
$tmp_new->{_idx} = $idx++;
}
$tmp_new;
} @{$aips_response->{hits}->{hits}};
my @headers = sort keys %{$result[0]};
my $aip_only = List::Util::none {$_ =~ m/^with/} keys %{$opt};
if (!exists $opt->{output_format}) {
if ($aip_only) {
$opt->{output_format} = 'output_as_csv'
}
else {
$opt->{output_format} = 'output_as_rsv';
}
}
say STDERR "found $all result" . ($all != 1 ? 's' : '');
if ($all > 0) {
my $output_format_human = $opt->{output_format}; $output_format_human =~ s/_/ /g;
say STDERR "reporting results". ($from != $to ? " $from$to " : " ")."$output_format_human";
}
if ($opt->{output_format} eq 'output_as_csv') {
binmode(STDOUT, ':encoding(UTF-8)');
say join(",", @headers);
say join("\n", map {
my $line = $_;
my $res_line = join(
",",
map {$line->{$_}} @headers
);
$res_line;
} @result
);
}
elsif ($opt->{output_format} eq 'output_as_rsv') {
binmode(STDOUT, ':bytes');
print join(CHR_VALUE_TERMINATOR, @headers) . CHR_VALUE_TERMINATOR;
print CHR_ROW_TERMINATOR;
print join("", map {
my $line = $_;
my $res_line = join("", map {$line->{$_} . CHR_VALUE_TERMINATOR} @headers
);
$res_line . CHR_ROW_TERMINATOR;
} @result
);
binmode(STDOUT, ':encoding(UTF-8)');
}
elsif ($opt->{output_format} eq 'output_as_raw') {
say np($aips_response);
}
return 1;
}
1;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment