From 6f988651fc8c43e0fcfba749c5357e180f415907 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Thu, 30 May 2024 11:49:53 +0200
Subject: [PATCH] - added datemode using ranges

---
 lib/SLUB/LZA/TA/Command/count.pm  | 44 ++++++++++++++++++++++---------
 lib/SLUB/LZA/TA/Command/report.pm | 24 ++++++++++++++++-
 lib/SLUB/LZA/TA/Command/search.pm | 34 +++++++++++-------------
 3 files changed, 70 insertions(+), 32 deletions(-)

diff --git a/lib/SLUB/LZA/TA/Command/count.pm b/lib/SLUB/LZA/TA/Command/count.pm
index a92d24b..f08fff2 100644
--- a/lib/SLUB/LZA/TA/Command/count.pm
+++ b/lib/SLUB/LZA/TA/Command/count.pm
@@ -37,11 +37,13 @@ sub opt_spec {
         ["aip|a=s" => "count AIPs by given AIP id"],
         [ 'datemode' => hidden => {
             one_of => [
-                [ 'creationdate|c=s' => 'search based on creation date in format "YYYY-MM-DD", ranges in format "YYYY-MM-DD...YYYY-MM-DD' ],
+                [ 'creationdate|c=s' => 'count based on creation date in format "YYYY-MM-DD"' ],
+                [ 'creationdate-from=s' => 'count based on creation date ranges, beginning date in format "YYYY-MM-DD", implies "--creationdate-to"'],
                 #[ 'modificationdate|m=s' => 'search based on modificationdate string' ]
             ]
           }
         ],
+        [ 'creationdate-to=s' => 'search based on creation date ranges, beginning date in format "YYYY-MM-DD", implies "--creationdate-from"'],
         ['descriptive|d=s' => 'count based on string search in descriptive metadata, using exact match'],
         ['fuzzy|f=s' => 'count based on string search in descriptive metadata, using phrase prefix match' ],
         ['lzaid|l=s' => 'count AIPs by given LZA id'],
@@ -60,6 +62,7 @@ sub opt_spec {
             one_of => [
                 [ 'only-ldp-saxon', 'only if AIP is LDP Saxon funded' ],
                 [ 'only-ldp', 'only if AIP is LDP funded' ],
+                [ 'only-ldp-without-saxon', 'only if AIP is LDP but not Saxon funded' ],
                 [ 'no-ldp', 'only if AIP is not LDP funded' ],
             ] } ],
     );
@@ -70,22 +73,39 @@ sub validate_args {
     SLUB::LZA::TA::common_global_validate($self, $opt, $args);
     # no args allowed but options!
     $self->usage_error("No args allowed") if @$args;
+    my $from_epoch;
+    my $to_epoch;
     if (exists $opt->{creationdate}) {
         if ($opt->{creationdate} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
-            my $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
-            $opt->{creationdate_epochs}->{from} = $from_epoch;
-            $opt->{creationdate_epochs}->{to} = $to_epoch;
-        } elsif (
-            $opt->{creationdate} =~ m/^(\d{4})-(\d{2})-(\d{2})\.\.\.(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
-            my $to_epoch = Date_to_Time($4, $5, $6, 23, 59, 59);
-            $opt->{creationdate_epochs}->{from} = $from_epoch;
-            $opt->{creationdate_epochs}->{to} = $to_epoch;
+            $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
+            $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
         } else {
-            $self->usage_error('--creationdate expects date in format "YYYY-MM-DD", ranges in format "YYYY-MM-DD...YYYY-MM-DD"');
+            $self->usage_error('--creationdate expects date in format "YYYY-MM-DD"');
         }
     }
+    if (exists $opt->{creationdate_from}) {
+        $self->usage_error('--creationdate-from implies --creationdate-to"') unless exists $opt->{creationdate_to};
+        if ($opt->{creationdate_from} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
+            $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
+        } else {
+
+            $self->usage_error('--creationdate-from expects date in format "YYYY-MM-DD", got "'.$opt->{creationdate_from}.'"');
+        }
+    }
+
+    if (exists $opt->{creationdate_to}) {
+        $self->usage_error('--creationdate-to implies --creationdate-from"') unless exists $opt->{creationdate_from};
+        if ($opt->{creationdate_to} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
+            $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
+        } else {
+            $self->usage_error('--creationdate-to expects date in format "YYYY-MM-DD", got "'.$opt->{creationdate_to}.'"');
+        }
+    }
+    if (defined $from_epoch and defined $to_epoch) {
+        $opt->{creationdate_epochs}->{from} = $from_epoch;
+        $opt->{creationdate_epochs}->{to} = $to_epoch;
+        $self->usage_error('--date-to should have a date newer than --date-from') if ($from_epoch > $to_epoch);
+    }
     if (exists $opt->{pronom_id}) {
         $self->usage_error("--pronom-id expects string which is conform to PUID structure as described in https://www.nationalarchives.gov.uk/aboutapps/pronom/puid.htm")
         unless ($opt->{pronom_id} =~ m/^(x-)?fmt\/[a-z0-9]+$/ );
diff --git a/lib/SLUB/LZA/TA/Command/report.pm b/lib/SLUB/LZA/TA/Command/report.pm
index e9c815c..8e76f2e 100644
--- a/lib/SLUB/LZA/TA/Command/report.pm
+++ b/lib/SLUB/LZA/TA/Command/report.pm
@@ -52,9 +52,11 @@ sub opt_spec {
                 [ 'yearly|y'  => 'report based on last year' ],
                 [ 'ldpyearly' => 'report based on last LDP year 01.11. - 31.10.'],
                 [ 'complete|c'=> 'report based on all AIPs'],
+                [ 'date-from=s' => 'report based on date range, beginning date in format "YYYY-MM-DD", implies "--date-to"'],
             ],
           },
         ],
+        [ 'date-to=s' => 'report based on date range, end date in format "YYYY-MM-DD", implies "--date-from"'],
         [],
         [ 'output-format' => hidden => {
             one_of => [
@@ -100,18 +102,38 @@ sub validate_args {
         ($from_year, $from_month, $from_day) = Add_Delta_YM($cyear, 1, 1, -1, -2);
         ($to_year, $to_month, $to_day) = Add_Delta_YMD($from_year, $from_month, $from_day, 1, 0, -1);
     }
+    if (exists $opt->{date_from}) {
+        $self->usage_error('--date-from implies --date-to"') unless exists $opt->{date_to};
+        if ($opt->{date_from} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
+            ($from_year, $from_month, $from_day) = ($1, $2, $3);
+        } else {
+            $self->usage_error('--date-from expects date in format "YYYY-MM-DD", got "'.$opt->{date_from}.'"');
+        }
+    }
+    if (exists $opt->{date_to}) {
+        $self->usage_error('--date-to implies --date-from"') unless exists $opt->{date_from};
+        if ($opt->{date_to} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
+        ($to_year, $to_month, $to_day) = ($1, $2, $3);
+        } else {
+            $self->usage_error('--date-to expects date in format "YYYY-MM-DD", got "',$opt->{date_to}.'"');
+        }
+    }
+
     if (!exists $opt->{output_format}) {
         $opt->{output_format} = 'output_as_asciidoc';
     }
-    printf STDERR "reporting for period %04u-%02u-%02u … %04u-%02u-%02u\n", $from_year, $from_month, $from_day, $to_year, $to_month, $to_day;
+
     my $from_epoch = Date_to_Time($from_year, $from_month, $from_day, 0, 0, 0);
     my $to_epoch = Date_to_Time($to_year, $to_month, $to_day, 0, 0, 0);
+    $self->usage_error('--date-to should have a date newer than --date-from')if ($from_epoch > $to_epoch);
+    printf STDERR "reporting for period %04u-%02u-%02u … %04u-%02u-%02u\n", $from_year, $from_month, $from_day, $to_year, $to_month, $to_day;
     $opt->{creationdate_epochs}->{from} = $from_epoch;
     $opt->{creationdate_epochs}->{to} = $to_epoch;
     $opt->{creationdate_epochs}->{from_string} = sprintf("%04u-%02u-%02u", $from_year, $from_month, $from_day);
     $opt->{creationdate_epochs}->{to_string} = sprintf("%04u-%02u-%02u", $to_year, $to_month, $to_day);
     return 1;
 }
+
 sub _execute {
     my ($self, $opt, $args) = @_;
     my $aips_query;
diff --git a/lib/SLUB/LZA/TA/Command/search.pm b/lib/SLUB/LZA/TA/Command/search.pm
index 1c34e29..d9ebc91 100644
--- a/lib/SLUB/LZA/TA/Command/search.pm
+++ b/lib/SLUB/LZA/TA/Command/search.pm
@@ -61,8 +61,7 @@ sub opt_spec {
             ]
           }
         ],
-        [ 'creationdate-to=s' => 'search based on creation date ranges, beginning date in format "YYYY-MM-DD", implies "--creationdate-from"',
-            {implies => 'creationdate_from' }],
+        [ 'creationdate-to=s' => 'search based on creation date ranges, beginning date in format "YYYY-MM-DD", implies "--creationdate-from"'],
         [ 'descriptive|d=s', 'search descriptive metadata, using exact match' ],
         [ 'fuzzy|f=s', 'search descriptive metadata, using phrase prefix match' ],
         [ 'lzaid|l=s', 'search a specific AIP by given LZA id' ],
@@ -122,18 +121,12 @@ sub validate_args {
     SLUB::LZA::TA::common_global_validate($self, $opt, $args);
     # no args allowed but options!
     $self->usage_error("No args allowed") if @$args;
+    my $from_epoch;
+    my $to_epoch;
     if (exists $opt->{creationdate}) {
         if ($opt->{creationdate} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
-            my $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
-            $opt->{creationdate_epochs}->{from} = $from_epoch;
-            $opt->{creationdate_epochs}->{to} = $to_epoch;
-        } elsif (
-            $opt->{creationdate} =~ m/^(\d{4})-(\d{2})-(\d{2})\.\.\.(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
-            my $to_epoch = Date_to_Time($4, $5, $6, 23, 59, 59);
-            $opt->{creationdate_epochs}->{from} = $from_epoch;
-            $opt->{creationdate_epochs}->{to} = $to_epoch;
+            $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
+            $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
         } else {
             $self->usage_error('--creationdate expects date in format "YYYY-MM-DD"');
         }
@@ -141,21 +134,24 @@ sub validate_args {
     if (exists $opt->{creationdate_from}) {
         $self->usage_error('--creationdate-from implies --creationdate-to"') unless exists $opt->{creationdate_to};
         if ($opt->{creationdate_from} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
-            $opt->{creationdate_epochs}->{from} = $from_epoch;
+            $from_epoch = Date_to_Time($1, $2, $3, 0, 0, 0);
         } else {
-            $self->usage_error('--creationdate-from expects date in format "YYYY-MM-DD"');
+            $self->usage_error('--creationdate-from expects date in format "YYYY-MM-DD", got "'.$opt->{creationdate_from}.'"');
         }
     }
     if (exists $opt->{creationdate_to}) {
         $self->usage_error('--creationdate-to implies --creationdate-from"') unless exists $opt->{creationdate_from};
-        if ($opt->{creationdate_from} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
-            my $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
-            $opt->{creationdate_epochs}->{to} = $to_epoch;
+        if ($opt->{creationdate_to} =~ m/^(\d{4})-(\d{2})-(\d{2})$/ ) {
+            $to_epoch = Date_to_Time($1, $2, $3, 23, 59, 59);
         } else {
-            $self->usage_error('--creationdate-to expects date in format "YYYY-MM-DD"');
+            $self->usage_error('--creationdate-to expects date in format "YYYY-MM-DD", got "'.$opt->{creationdate_to}.'"');
         }
     }
+    if (defined $from_epoch and defined $to_epoch) {
+        $opt->{creationdate_epochs}->{from} = $from_epoch;
+        $opt->{creationdate_epochs}->{to} = $to_epoch;
+        $self->usage_error('--date-to should have a date newer than --date-from') if ($from_epoch > $to_epoch);
+    }
     if (exists $opt->{pronom_id}) {
         $self->usage_error("--pronom-id expects string which is conform to PUID structure as described in https://www.nationalarchives.gov.uk/aboutapps/pronom/puid.htm")
         unless ($opt->{pronom_id} =~ m/^(x-)?fmt\/[a-z0-9]+$/ );
-- 
GitLab