From 84c0715be71d6ab203c9dda61048e7495a38f05e Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Thu, 13 Jun 2024 10:39:27 +0200
Subject: [PATCH] - refactoring, use hash $recipe with coderef instead of
 if-blocks

---
 .../Elasticsearch/PrepareQuery.pm             | 97 ++++++++-----------
 1 file changed, 40 insertions(+), 57 deletions(-)

diff --git a/lib/SLUB/LZA/TA/Archivematica/Elasticsearch/PrepareQuery.pm b/lib/SLUB/LZA/TA/Archivematica/Elasticsearch/PrepareQuery.pm
index 988ee73..f0d5a8c 100644
--- a/lib/SLUB/LZA/TA/Archivematica/Elasticsearch/PrepareQuery.pm
+++ b/lib/SLUB/LZA/TA/Archivematica/Elasticsearch/PrepareQuery.pm
@@ -7,62 +7,63 @@ sub prepare_aip_query ($opt) {
     my @must;
     my @must_not;
     my @should;
-    if (exists $opt->{startrecord}) {
+    my %recipe;
+    $recipe{startrecord} =sub {
         $query->{from} = $opt->{startrecord} - 1; # start from index 0 -> first record
-    }
-    if (exists $opt->{maxrecords}) {
+    };
+    $recipe{maxrecords} =sub {
         $query->{size} = $opt->{maxrecords};
-    }
-    if (exists $opt->{aip}) {
+    };
+    $recipe{aip} =sub {
         push @must, {
             "match_phrase" => {
                 "uuid" => "$opt->{aip}"
             }
         };
-    }
-    if (exists $opt->{lzaid}) {
+    };
+    $recipe{lzaid} =sub {
         push @must, {
             "match_phrase" => {
                 "transferMetadata.bim:bag-info_dict.bim:SLUBArchiv-lzaId" => "$opt->{lzaid}"
             }
         };
-    }
-    if (exists $opt->{only_migrated}) {
+    };
+    $recipe{only_migrated} =sub {
         push @must, {
             "exists" => {
                 field => "transferMetadata.bim:bag-info_dict.bim:SLUBArchiv-migrated-AIP"
             }
         }
-    }
-    if (exists $opt->{only_updated}) {
+    };
+    $recipe{only_updated} =sub {
         push @must, {
             "exists" => {
                 field => "transferMetadata.bim:bag-info_dict.bim:SLUBArchiv-previous-AIP"
             }
         }
-    }
-    if (exists $opt->{only_new}) {
+    };
+    $recipe{only_new} =sub {
         push @must_not, {
             "exists" => {
                 field => "transferMetadata.bim:bag-info_dict.bim:SLUBArchiv-previous-AIP"
             }
         }
-    }
-    if (exists $opt->{only_ldp_project}) {
+    };
+    $recipe{only_ldp_project} =sub {
         push @must, {
             "match_phrase" => {
                 "transferMetadata.bim:bag-info_dict.bim:LDP-project" => $opt->{only_ldp_project}
             }
         }
-    }
-    if (exists $opt->{only_ldp_saxon}) {
+    };
+    $recipe{only_ldp_saxon} =sub {
         push @must, {
             "match_phrase" => {
                 "transferMetadata.bim:bag-info_dict.bim:LDP-funder" => 'LDP Sachsen'
             }
         }
-    }
-    if (exists $opt->{only_ldp_without_saxon}) {
+    };
+    $recipe{only_ldp_without_saxon} =sub {
         push @must_not, {
             "match_phrase" => {
                 "transferMetadata.bim:bag-info_dict.bim:LDP-funder" => 'LDP Sachsen'
@@ -73,22 +74,22 @@ sub prepare_aip_query ($opt) {
                 field => "transferMetadata.bim:bag-info_dict.bim:LDP-funder"
             }
         }
-    }
-    if (exists $opt->{only_ldp}) {
+    };
+    $recipe{only_ldp} =sub {
         push @must, {
             "exists" => {
                 field => "transferMetadata.bim:bag-info_dict.bim:LDP-funder"
             }
         }
-    }
-    if (exists $opt->{no_ldp}) {
+    };
+    $recipe{no_ldp} =sub {
         push @must_not, {
             "exists" => {
                 field => "transferMetadata.bim:bag-info_dict.bim:LDP-funder"
             }
         }
-    }
-    if (exists $opt->{descriptive}) {
+    };
+    $recipe{descriptive} =sub {
         #https://opster.com/guides/elasticsearch/search-apis/elasticsearch-match-multi-match-and-match-phrase-queries/#Multi-match-query
         push @should, {
             "multi_match" => {
@@ -96,8 +97,8 @@ sub prepare_aip_query ($opt) {
                 "fields" => ["transferMetadata.*"] # scan all metadata, exact match
             }
         }
-    }
-    if (exists $opt->{fuzzy}) {
+    };
+    $recipe{fuzzy} =sub {
         push @should, {
             "multi_match" => {
                 "query" => "$opt->{fuzzy}",
@@ -105,8 +106,8 @@ sub prepare_aip_query ($opt) {
                 "type" => "phrase_prefix",
             }
         }
-    }
-    if (exists $opt->{creationdate_epochs}) {
+    };
+    $recipe{creationdate_epochs} =sub {
         push @must, {
             "range" => {
                 "created" => {
@@ -115,40 +116,22 @@ sub prepare_aip_query ($opt) {
                 }
             }
         };
-    }
-    if (exists $opt->{modificationdate}) {
+    };
+    $recipe{modificationdate} =sub {
         #push @queries, "FILE.objectCharacteristics.modificationDate==$opt->{modificationdate}";
-    }
-    if (exists $opt->{workflow}) { # LZA Workflow search
+    };
+    $recipe{workflow} =sub {  # LZA Workflow search
         push @must, {
             "match_phrase" => {
                 "transferMetadata.ObjectType_dict.Metadata_dict.WorkflowName" => $opt->{workflow}
             }
         };
+    };
+    foreach my $key (keys %{ $opt } ) {
+        if (defined $recipe{$key} and ref $recipe{$key} eq 'CODE') {
+            $recipe{$key}->();
+        }
     }
-    if (exists $opt->{'with_format'}) {
-        #push @queries, "FILE.generalFileCharacteristics.formatLibraryId==$opt->{'with_format'}";
-    }
-    #if (exists $opt->{'without_format'}) {
-    #    push @queries, "FILE.generalFileCharacteristics.formatLibraryId!=$opt->{'without_format'}";
-    #}
-    if (exists $opt->{'with_valid_files'}) {
-        #push @queries, "FILE.fileValidation.isValid==true";
-    }
-    if (exists $opt->{'with_invalid_files'}) {
-        #push @queries, "FILE.fileValidation.isValid==false";
-    }
-    if (exists $opt->{'with_passed_viruschecks'}) {
-        #push @queries, "FILE.fileVirusCheck.status==true";
-    }
-    if (exists $opt->{'with_failed_viruschecks'}) {
-        #push @queries, "FILE.fileVirusCheck.status==false";
-    }
-    if (exists $opt->{'with_missed_viruschecks'}) {
-        #push @queries, "FILE.fileVirusCheck.status==\"\"";
-    }
-    #my $query = join(" and ", @queries);
-
     if (scalar @must > 0) {
         $query->{query}->{bool}->{must} = \@must ;
     }
@@ -244,7 +227,7 @@ sub prepare_ldpprojects_query {
                 }
             };
         $ldpprojects_query->{query}->{bool}->{must}=\@must;
-    }
+    };
     return $ldpprojects_query;
 }
 
-- 
GitLab