ACQ support for two-step vandelay imports
authorBill Erickson <berick@esilibrary.com>
Wed, 2 May 2012 13:55:06 +0000 (09:55 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 30 Aug 2012 15:48:03 +0000 (11:48 -0400)
If a lineitm is loaded into a queue via ACQ+Vandelay and a later attempt
is made to load the record into the catalog via ACQ+Vandelay, the
attempt will fail, because ACQ+Vandelay treats all
not-yet-fully-imported lineitems as new to vandelay.  This change
inspects lineitems at vandelay processing time to determine if they are
already linked to a VL record or if a new one is needed.  It also
removes the requirement that a queue be provided when all records in
process are already linked to a VL record (which already lives in a
queue).

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>

Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm

index 8bbb550..4865a30 100644 (file)
@@ -321,10 +321,14 @@ sub create_lineitem_list_assets {
 
 sub test_vandelay_import_args {
     my $vandelay = shift;
+    my $q_needed = shift;
 
-    # we need a queue
-    return 0 unless $vandelay and 
-        ($vandelay->{queue_name} or $vandelay->{existing_queue});
+    # we need valid args and (sometimes) a queue
+    return 0 unless $vandelay and (
+        !$q_needed or
+        $vandelay->{queue_name} or 
+        $vandelay->{existing_queue}
+    );
 
     # match-based merge/overlay import
     return 2 if $vandelay->{merge_profile} and (
@@ -392,18 +396,44 @@ sub import_li_bibs_via_vandelay {
         return {li_ids => $li_ids};
     }
 
+    # see if we have any records that are not yet linked to VL records (i.e. 
+    # not in a queue).  This will tell us if lack of a queue name is an error.
+    my $non_queued = $e->search_acq_lineitem(
+        {id => $needs_importing, queued_record => undef},
+        {idlist => 1}
+    );
+
     # add the already-imported records to the response list
     push(@{$res->{li_ids}}, grep { $_ != @$needs_importing } @$li_ids);
 
     $logger->info("acq-vl: processing recs via Vandelay with args: ".Dumper($vandelay));
 
-    my $vl_stat = test_vandelay_import_args($vandelay);
+    my $vl_stat = test_vandelay_import_args($vandelay, scalar(@$non_queued));
     if ($vl_stat == 0) {
         $logger->error("acq-vl: invalid vandelay arguments for acq import (queue needed)");
         return $res;
     }
 
-    my $queue = find_or_create_vandelay_queue($e, $vandelay) or return $res;
+    my $queue;
+    if (@$non_queued) {
+        # when any non-queued lineitems exist, their vandelay counterparts 
+        # require a place to live.
+        $queue = find_or_create_vandelay_queue($e, $vandelay) or return $res;
+
+    } else {
+        # if all lineitems are already queued, the queue reported to the user
+        # is purely for information / convenience.  pick a random queue.
+        $queue = $e->retrieve_acq_lineitem([
+            $needs_importing->[0], {   
+                flesh => 2, 
+                flesh_fields => {
+                    jub => ['queued_record'], 
+                    vqbr => ['queue']
+                }
+            }
+        ])->queued_record->queue;
+    }
+
     $mgr->{args}->{queue} = $queue;
 
     # load the lineitems into the queue for merge processing
@@ -413,18 +443,28 @@ sub import_li_bibs_via_vandelay {
 
         my $li = $e->retrieve_acq_lineitem($li_id) or return $res;
 
-        my $vqbr = Fieldmapper::vandelay::queued_bib_record->new;
-        $vqbr->marc($li->marc);
-        $vqbr->queue($queue->id);
-        $vqbr->bib_source($vandelay->{bib_source} || undef); # avoid ''
-        $vqbr = $e->create_vandelay_queued_bib_record($vqbr) or return $res;
-        push(@vqbr_ids, $vqbr->id);
+        if ($li->queued_record) {
+            $logger->info("acq-vl: $li_id already linked to a vandelay record");
+            push(@vqbr_ids, $li->queued_record);
+
+        } else {
+            $logger->info("acq-vl: creating new vandelay record for lineitem $li_id");
+
+            # create a new VL queued record and link it up
+            my $vqbr = Fieldmapper::vandelay::queued_bib_record->new;
+            $vqbr->marc($li->marc);
+            $vqbr->queue($queue->id);
+            $vqbr->bib_source($vandelay->{bib_source} || undef); # avoid ''
+            $vqbr = $e->create_vandelay_queued_bib_record($vqbr) or return $res;
+            push(@vqbr_ids, $vqbr->id);
+
+            # tell the acq record which vandelay record it's linked to
+            $li->queued_record($vqbr->id);
+            $e->update_acq_lineitem($li) or return $res;
+        }
+
         $mgr->add_vqbr;
         $mgr->respond;
-
-        # tell the acq record which vandelay record it's linked to
-        $li->queued_record($vqbr->id);
-        $e->update_acq_lineitem($li) or return $res;
         push(@lis, $li);
     }