From 1f04f931db59e2a954508953ed40e0eaf96c52e4 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 5 Apr 2023 12:49:59 +0000
Subject: [PATCH] Bug 33418: Add overlay_framework option to connexion scripts

This patch adds the option to specify a framework to be used when overlaying records from webservices/connexion

To test:
1 - vim /etc/koha/sites/kohadev/connexion.cnf
2 - Set content:
    host:
    port: 8888
    koha:http://localhost:8081
    log:/var/log/koha/kohadev/connexion.log
    match:ISBN
    user:kohauser
    password:kohapass
    overlay_action:replace
    nomatch_action:create_new
    item_action:always_add
    import_mode:direct
    framework:BKS
    overlay_framework:
    debug:1
3 - Save the sample file from this bug into your kohaclone (or copy and paste into a file your koha test site can reach)
4 - On the command line:
    perl misc/bin/connexion_import_daemon.pl -c /etc/koha/sites/kohadev/connexion.cnf
5 - In another terminal:
    cat bug_33418.test  | nc -v localhost 8888
6 - It should report success and a biblionumber
7 - In Koha: Cataloguing->Manage staged MARC record
8 - View the most recent batch with file name (webservice)
9 - Confirm it was imported, no match
10 - Click 'View' under the 'Record' column
11 - Confirm record loads correctly and 'MARC framework' detail is 'Books, Booklets, Workbooks'
12 - On the terminal repeat the command:
    cat bug_33418.test  | nc -v localhost 8888
13 - It should succed
14 - View the new batch, confirm the record matched this time
15 - View the record details, confirm framework is now 'default'
16 - On the first terminal hit Ctrl+C to stop the daemon
16 - Edit connexion.cnf and change:
    import_mode:stage
    framework:ACQ
    overlay_framework:IR
17 - Restart daemon:
     perl misc/bin/connexion_import_daemon.pl -c /etc/koha/sites/kohadev/connexion.cnf
18 - Delete the record created above
19 - On the second terminal repeat the command:
    cat bug_33418.test  | nc -v localhost 8888
20 - Confirm the batch is created, but not imported
21 - In terminal:
     perl misc/cronjobs/import_webservice_batch.pl --framework=ACQ --overlay_framework=BKS
22 - Confirm batch imported, and record in ACQ framework
23 - In terminal:
     cat bug_33418.test  | nc -v localhost 8888
     perl misc/cronjobs/import_webservice_batch.pl --framework=ACQ --overlay_framework=BKS
24 - Confirm batch added, record matched, record imported, and record now in Books framework
25 - Stop the deamon, edit connexion.cnf:
     import_mode:direct
26 - Start the daemon, and on other terminal repeat:
     cat bug_33418.test  | nc -v localhost 8888
27 - Confirm record in Binders framework
28 - Set record framework to Books
29 - Stop daemon, edit cnf and remove 'overlay_framework' setting
30 - Start daemon and cat the file again
31 - Confirm the record remains in Books framework

Signed-off-by: Brendan Lawlor <blawlor@clamsnet.org>
---
 misc/bin/connexion_import_daemon.pl      | 57 ++++++++++-------
 misc/cronjobs/import_webservice_batch.pl | 80 +++++++++++++++++++-----
 svc/import_bib                           | 16 +++--
 3 files changed, 107 insertions(+), 46 deletions(-)

diff --git a/misc/bin/connexion_import_daemon.pl b/misc/bin/connexion_import_daemon.pl
index c7e06c3a7d..40bff5d965 100755
--- a/misc/bin/connexion_import_daemon.pl
+++ b/misc/bin/connexion_import_daemon.pl
@@ -30,7 +30,7 @@ GetOptions(
     'help|?'        => \$help,
 );
 
-if($help || !$config){
+if ( $help || !$config ) {
     print <<EOF
 $0 --config=my.conf
 Parameters :
@@ -55,11 +55,13 @@ Config file format:
   password - koha user password, authentication
   match          - marc_matchers.code: ISBN or ISSN
   overlay_action - import_batches.overlay_action: replace, create_new or ignore
+  overlay_framework - if overlaying records - move to a new framework, if blank will use default
+                      if not included it will use the framework of the existing record
   nomatch_action - import_batches.nomatch_action: create_new or ignore
   item_action    - import_batches.item_action:    always_add,
                       add_only_for_matches, add_only_for_new or ignore
   import_mode    - stage or direct
-  framework      - to be used if import_mode is direct
+  framework      - to be used if import_mode is direct, if blank, will use default
   connexion_user      - User sent from connexion client
   connexion_password  - Password sent from connexion client
 
@@ -69,7 +71,7 @@ Config file format:
   All process related parameters (all but ip and port) have default values as
   per Koha import process.
 EOF
-;
+        ;
     exit;
 }
 
@@ -353,29 +355,38 @@ sub handle_request {
     }
 
     my $base_url = $self->{koha};
-    my $resp = $ua->post( $base_url.IMPORT_SVC_URI,
-                              {'nomatch_action' => $self->{params}->{nomatch_action},
-                               'overlay_action' => $self->{params}->{overlay_action},
-                               'match'          => $self->{params}->{match},
-                               'import_mode'    => $self->{params}->{import_mode},
-                               'framework'      => $self->{params}->{framework},
-                               'item_action'    => $self->{params}->{item_action},
-                               'xml'            => $data});
+    my $resp = $ua->post(
+        $base_url . IMPORT_SVC_URI,
+        {
+            'nomatch_action'    => $self->{params}->{nomatch_action},
+            'overlay_action'    => $self->{params}->{overlay_action},
+            'match'             => $self->{params}->{match},
+            'import_mode'       => $self->{params}->{import_mode},
+            'framework'         => $self->{params}->{framework},
+            'overlay_framework' => $self->{params}->{overlay_framework},
+            'item_action'       => $self->{params}->{item_action},
+            'xml'               => $data
+        }
+    );
 
     my $status = $resp->code;
-    if ($status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN) {
-        my $user = $self->{user};
+    if ( $status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN ) {
+        my $user     = $self->{user};
         my $password = $self->{password};
-        $resp = $ua->post( $base_url.AUTH_URI, { userid => $user, password => $password } );
-        $resp = $ua->post( $base_url.IMPORT_SVC_URI,
-                              {'nomatch_action' => $self->{params}->{nomatch_action},
-                               'overlay_action' => $self->{params}->{overlay_action},
-                               'match'          => $self->{params}->{match},
-                               'import_mode'    => $self->{params}->{import_mode},
-                               'framework'      => $self->{params}->{framework},
-                               'item_action'    => $self->{params}->{item_action},
-                               'xml'            => $data})
-          if $resp->is_success;
+        $resp = $ua->post( $base_url . AUTH_URI, { userid => $user, password => $password } );
+        $resp = $ua->post(
+            $base_url . IMPORT_SVC_URI,
+            {
+                'nomatch_action'    => $self->{params}->{nomatch_action},
+                'overlay_action'    => $self->{params}->{overlay_action},
+                'match'             => $self->{params}->{match},
+                'import_mode'       => $self->{params}->{import_mode},
+                'framework'         => $self->{params}->{framework},
+                'overlay_framework' => $self->{params}->{overlay_framework},
+                'item_action'       => $self->{params}->{item_action},
+                'xml'               => $data
+            }
+        ) if $resp->is_success;
     }
     unless ($resp->is_success) {
         $self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string);
diff --git a/misc/cronjobs/import_webservice_batch.pl b/misc/cronjobs/import_webservice_batch.pl
index d5dcdc78f7..e65bcc136d 100755
--- a/misc/cronjobs/import_webservice_batch.pl
+++ b/misc/cronjobs/import_webservice_batch.pl
@@ -22,31 +22,77 @@ use warnings;
 use utf8;
 
 use Getopt::Long qw( GetOptions );
+use Pod::Usage   qw( pod2usage );
+
 use Koha::Script -cron;
 use C4::ImportBatch qw( BatchCommitRecords GetStagedWebserviceBatches );
 
-my ($help, $framework);
+=head1 NAME
+
+import_webservice_batch.pl - Find batches staged by webservice and import them.
+
+=head1 SYNOPSIS
+
+import_webservice_batch.pl [--framework=<frameworkcode> --overlay_framework=<frameworkcode>]
+
+Options:
+
+   --help                   brief help message
+   --framework              specify frameworkcode for new records
+   --overlay_framework      specify frameworkcode when overlaying records
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<--help>
+
+Print a brief help message and exits.
+
+=item B<--framework>
+
+Specify frameworkcode for new records. Uses default if not specified.
+
+=item B<--overlay_framework>
+
+Specify frameworkcode when overlaying records.  Current framework is preserved if not specified.
+
+=back
+
+=head1 DESCRIPTION
+
+This script is designed to import batches staged by webservices (e.g. connexion).
+
+=head1 USAGE EXAMPLES
+
+C<import_webservice_batch.pl> - Imports the batches using default framework
+
+C<import_webservice_batch.pl> -f=<frameworkcode> Imports the batches adding new records into specified framework, not adjusting framework of matched records
+
+C<import_webservice_batch.pl> -f=<frameworkcode> -o=<frameworkcode> Imports the batches adding new records into specified framework, overlaying matched records to specified framework
+
+=cut
+
+my ( $help, $man, $framework, $overlay_framework );
 
 GetOptions(
-    'help|?'         => \$help,
-    'framework=s'    => \$framework,
+    'help|?'                => \$help,
+    'man'                   => \$man,
+    'f|framework=s'         => \$framework,
+    'o|overlay_framework=s' => \$overlay_framework,
 );
 
-if($help){
-    print <<EOF
-$0 --framework=myframework
-Parameters :
---help|? This message
---framework default ""
-EOF
-;
-    exit;
-}
+pod2usage(1) if $help;
+
+pod2usage( -verbose => 2 ) if $man;
 
 my $batch_ids = GetStagedWebserviceBatches() or exit;
 
 $framework ||= '';
-BatchCommitRecords({
-    batch_id  => $_,
-    framework => $framework
-}) foreach @$batch_ids;
+BatchCommitRecords(
+    {
+        batch_id          => $_,
+        framework         => $framework,
+        overlay_framework => $overlay_framework,
+    }
+) foreach @$batch_ids;
diff --git a/svc/import_bib b/svc/import_bib
index d5727e6589..27de935d50 100755
--- a/svc/import_bib
+++ b/svc/import_bib
@@ -58,8 +58,9 @@ sub import_bib {
 
     my $result = {};
 
-    my $import_mode = delete $params->{import_mode} || '';
-    my $framework   = delete $params->{framework}   || '';
+    my $import_mode       = delete $params->{import_mode} || '';
+    my $framework         = delete $params->{framework}   || '';
+    my $overlay_framework = $params->{overlay_framework};
 
     if (my $matcher_code = delete $params->{match}) {
         $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
@@ -92,10 +93,13 @@ sub import_bib {
     my $number_of_matches =  BatchFindDuplicates($batch_id, $matcher);
 
     # XXX we are ignoring the result of this;
-    BatchCommitRecords({
-        batch_id  => $batch_id,
-        framework => $framework
-    }) if lc($import_mode) eq 'direct';
+    BatchCommitRecords(
+        {
+            batch_id          => $batch_id,
+            framework         => $framework,
+            overlay_framework => $overlay_framework
+        }
+    ) if lc($import_mode) eq 'direct';
 
     my $dbh = C4::Context->dbh();
     my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
-- 
2.30.2