diff --git a/perl/exit_strategy.pl b/perl/exit_strategy.pl index d081d7485694685e8125f474cfa47afc028140c7..3aea12afdb73d61548aed50f7a27d505eb5f476a 100644 --- a/perl/exit_strategy.pl +++ b/perl/exit_strategy.pl @@ -51,13 +51,19 @@ use File::Basename qw(basename); use File::Find; use Getopt::Long; use IO::Handle; -use List::Util qw(any); +use List::Util qw(first); use Path::Tiny; use Pod::Usage; use Time::Progress; use XML::LibXML::XPathContext; use XML::LibXML; +sub check_lzaid ($lza_id) { + my $rx_up=qr{[A-Za-z0-9_-]+}; # archive name & internal workflow + my $rx_lw=qr{[a-z0-9_-]+}; # external workflow & external id + return ($lza_id =~ m/^$rx_up:$rx_up:$rx_up:$rx_lw:$rx_lw$/); +}; + STDOUT->autoflush(1); # guarantee, that output will be UTF8 @@ -100,6 +106,7 @@ sub write_tables_creation ($dbh) { CREATE TABLE aip ( id INTEGER, ie_id TEXT NOT NULL, + lza_id TEXT, version INTEGER NOT NULL, PRIMARY KEY(id AUTOINCREMENT) ); @@ -154,6 +161,16 @@ CREATE TABLE deleted ( PRIMARY KEY(id AUTOINCREMENT) ); SQL_CREATE6 + my $sql7=<<"SQL_CREATE7"; +CREATE TABLE transferAIP ( + id INTEGER, + aip_id INTEGER NOT NULL REFERENCES aip (id), + lza_id INTEGER, + transfer_state TEXT NOT NULL, + PRIMARY KEY(id AUTOINCREMENT) + ); +SQL_CREATE7 + my $sth1 = $dbh->prepare($sql1); $sth1->execute() or die "sql problem detected", $dbh->errstr; my $sth2 = $dbh->prepare($sql2); $sth2->execute() or die "sql problem detected", $dbh->errstr; @@ -161,6 +178,7 @@ SQL_CREATE6 my $sth4 = $dbh->prepare($sql4); $sth4->execute() or die "sql problem detected", $dbh->errstr; my $sth5 = $dbh->prepare($sql5); $sth5->execute() or die "sql problem detected", $dbh->errstr; my $sth6 = $dbh->prepare($sql6); $sth6->execute() or die "sql problem detected", $dbh->errstr; + my $sth7 = $dbh->prepare($sql7); $sth7->execute() or die "sql problem detected", $dbh->errstr; return 1; } @@ -173,7 +191,7 @@ sub write_prepare_insert ($dbh) { sub prepare_addsql( $dbh) { my $sql_aip_plan=<<"SQL_AIP_PLAN"; - INSERT INTO aip (ie_id, version) VALUES (\$1, \$2); + INSERT INTO aip (ie_id, lza_id, version) VALUES (\$1, \$2, \$3); SQL_AIP_PLAN my $sql_ie_plan=<<"SQL_IE_PLAN"; INSERT INTO metadatafile (aip_id, location, sourcetype) VALUES ( @@ -248,9 +266,15 @@ sub write_addsql ($dbh, $plans, $refhash) { } else { die "Could not detect PID and Version from IEFile '$iefile'\n"; } - + my $lza_id_kv = List::Util::first { + my ($dckey,$dcvalue) = @{$_}; + ($dckey eq 'dc:identifier') + and (check_lzaid($dcvalue)) + } @{$refhash->{"dcrecords"}}; + my $lza_id = @{$lza_id_kv}[1]; + if (! defined $lza_id) { $lza_id = "";} # start SQL insert - eval {$plans->{aip}->execute($ieid, $ieversion)} or die "sql problem detected with $ieid ($ieversion), plz check if there is a dublette dir, ", $dbh->errstr; + eval {$plans->{aip}->execute($ieid, $lza_id, $ieversion)} or die "sql problem detected with $ieid ($ieversion), plz check if there is a dublette dir, ", $dbh->errstr; # FIXME if multiple locations exists eval{$plans->{ie}->execute( $ieid, $ieversion, $iefile, $sourcetype)} or die "sql problem detected with $ieid ($ieversion, $iefile, $sourcetype), ", $dbh->errstr; diff --git a/perl/t/expected.sql b/perl/t/expected.sql index aa87d58f97763a7dadc515c4ffa3229d5303cca9..85b543fc4047efec1e599a9fbbe593dd22f9b9a5 100644 --- a/perl/t/expected.sql +++ b/perl/t/expected.sql @@ -3,10 +3,11 @@ BEGIN TRANSACTION; CREATE TABLE aip ( id INTEGER, ie_id TEXT NOT NULL, + lza_id TEXT, version INTEGER NOT NULL, PRIMARY KEY(id AUTOINCREMENT) ); -INSERT INTO aip VALUES(1,'IE200928',1); +INSERT INTO aip VALUES(1,'IE200928','SLUB:LZA:Kitodo:goobi:202001030001',1); CREATE TABLE metadatafile ( id INTEGER, aip_id INTEGER NOT NULL REFERENCES aip (id), @@ -63,6 +64,13 @@ CREATE TABLE deleted ( authorized_by TEXT NOT NULL, PRIMARY KEY(id AUTOINCREMENT) ); +CREATE TABLE transferAIP ( + id INTEGER, + aip_id INTEGER NOT NULL REFERENCES aip (id), + lza_id INTEGER, + transfer_state TEXT NOT NULL, + PRIMARY KEY(id AUTOINCREMENT) + ); DELETE FROM sqlite_sequence; INSERT INTO sqlite_sequence VALUES('aip',1); INSERT INTO sqlite_sequence VALUES('metadatafile',1);