ACQ Vandeley : acq ML bits round 1
authorBill Erickson <berick@esilibrary.com>
Thu, 1 Dec 2011 23:28:25 +0000 (18:28 -0500)
committerBill Erickson <berick@esilibrary.com>
Thu, 1 Dec 2011 23:28:25 +0000 (18:28 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm

index 61eb569..1abb1e6 100644 (file)
@@ -2,6 +2,7 @@ package OpenILS::Application::Acq::BatchManager;
 use OpenILS::Application::Acq::Financials;
 use OpenSRF::AppSession;
 use OpenSRF::EX qw/:try/;
+use Digest::MD5 qw(md5_hex);
 use strict; use warnings;
 
 sub new {
@@ -319,22 +320,93 @@ sub delete_lineitem {
 
 # begins and commit transactions as it goes
 sub create_lineitem_list_assets {
-    my($mgr, $li_ids) = @_;
+    my($mgr, $li_ids, $vandelay) = @_;
     return undef if check_import_li_marc_perms($mgr, $li_ids);
 
+    $li_ids = import_lineitems_via_vandelay($mgr, $li_ids, $vandelay);
+    return undef unless $li_ids;
+
     # create the bibs/volumes/copies and ingest the records
     for my $li_id (@$li_ids) {
         $mgr->editor->xact_begin;
         my $data = create_lineitem_assets($mgr, $li_id) or return undef;
         $mgr->editor->xact_commit;
-        # XXX ingest is in-db now
-        #$mgr->push_ingest_queue($data->{li}->eg_bib_id) if $data->{new_bib};
         $mgr->respond;
     }
     $mgr->process_ingest_records;
     return 1;
 }
 
+
+sub import_lineitems_via_vandelay {
+    my ($mgr, $li_ids, $vandelay) = @_;
+
+    my $queue_id = $vandelay->{named_queue};
+    if ($vandelay->{tmp_queue}) {
+
+        my $queue = new Fieldmapper::vandelay::bib_queue;
+        $queue->name(md5_hex($$.rand().time)); # since it's temp, should the DB generate the name?
+        $queue->owner($mgr->editor->requestor->id);
+        $queue->queue_type('acq');
+        $queue->temp(1);
+        $queue = $mgr->editor->create_vandelay_bib_queue($queue) or return undef;
+        $queue_id = $queue->id;
+    } 
+
+    return undef unless $queue_id;
+
+    # load the lineitems into the queue for merge processing
+    my @vqbr_ids;
+    for my $li_id (@$li_ids) {
+
+        my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return undef;
+
+        my $rec = new Fieldmapper::vandelay::queued_bib_record;
+        $rec->marc($li->marc);
+        $rec->queue($queue_id);
+        $rec->bib_source($vandelay->{bib_source});
+        $rec = $mgr->editor->create_vandelay_queued_bib_record->new($rec) or return undef;
+        push(@vqbr_ids, $rec->id);
+
+        # tell the acq record which vandelay record it's linked to
+        $li->queued_record($rec->id);
+        $mgr->editor->update_acq_lineitem($li) or return undef;
+    }
+
+    # attempt to import the queue and collect the results
+    # any lineitems that successfully merged or imported are 
+    # safe for acq asset importing
+
+    # we have to commit the transaction now since vandelay 
+    # importing uses its own transactions.
+    $mgr->editor->commit;
+
+    $vandelay->{report_all} = 1;
+    my $ses = OpenSRF::AppSession->create('open-ils.vandelay');
+    my $req = $ses->request(
+        'open-ils.vandelay.bib_record.list.import',
+        \@vqbr_ids, $vandelay);
+
+    my @imported;
+    while (my $resp = $req->recv) {
+        my $stat = $resp->content;
+        push(@imported, $stat->{imported}) 
+            if $stat and $stat->{imported};
+    }
+
+    # start a new transaction to manage the rest of asset creation
+    $mgr->editor->xact_begin;
+
+    # find the acq lineitems linked to imported vandelay records
+    $li_ids = $mgr->editor->json_query({
+        select => {jub => ['id']},
+        from => 'jub',
+        where => {queued_record => \@imported}
+    });
+
+    return [ map {$_->{id}} @$li_ids ];
+}
+
 # returns event on error, undef on success
 sub check_import_li_marc_perms {
     my($mgr, $li_ids) = @_;
@@ -1204,7 +1276,7 @@ sub upload_records {
     my $create_po       = $args->{create_po};
     my $activate_po     = $args->{activate_po};
     my $ordering_agency = $args->{ordering_agency};
-    my $create_assets   = $args->{create_assets};
+    my $vandelay        = $args->{vandelay};
     my $po;
     my $evt;
 
@@ -1302,8 +1374,8 @@ sub upload_records {
     unlink($filename);
     $cache->delete_cache('vandelay_import_spool_' . $key);
 
-    if ($create_assets) {
-        create_lineitem_list_assets($mgr, \@li_list) or return $e->die_event;
+    if ($vandelay) {
+        create_lineitem_list_assets($mgr, \@li_list, $vandelay) or return $e->die_event;
     }
 
     return $mgr->respond_complete;