spool processing
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 14 May 2008 02:37:52 +0000 (02:37 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 14 May 2008 02:37:52 +0000 (02:37 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@9595 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Vandelay.pm
Open-ILS/src/perlmods/OpenILS/WWW/Vandelay.pm

index 3a62054..9ac47de 100644 (file)
@@ -7,10 +7,12 @@ use OpenSRF::EX qw/:try/;
 
 use OpenSRF::AppSession;
 use OpenSRF::Utils::SettingsClient;
+use OpenSRF::Utils::Cache;
 
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 
+use MARC::Batch;
 use MARC::Record;
 use MARC::File::XML;
 
@@ -64,6 +66,7 @@ sub create_bib_queue {
 
        my $new_id = $e->create_vandelay_bib_queue( $queue );
        $e->die_event unless ($new_id);
+       $e->commit;
 
        $queue->id($new_id);
        return $queue;
@@ -98,6 +101,7 @@ sub create_auth_queue {
 
        my $new_id = $e->create_vandelay_authority_queue( $queue );
        $e->die_event unless ($new_id);
+       $e->commit;
 
        $queue->id($new_id);
        return $queue;
@@ -118,19 +122,17 @@ sub add_record_to_bib_queue {
 
        my $e = new_editor(authtoken => $auth, xact => 1);
 
-       $queue = $e->retrieve_vandelay_bib_queue($queue)
+       $queue = $e->retrieve_vandelay_bib_queue($queue);
 
        return $e->die_event unless $e->checkauth;
        return $e->die_event unless
                ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
                 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
 
-       my $rec = new Fieldmapper::vandelay::queued_bib_record();
-       $rec->marc( $marc );
-       $rec->queue( $queue->id );
+       my $new_id = _add_auth_rec($e, $marc, $queue->id);
 
-       my $new_id = $e->create_vandelay_queued_bib_record( $rec );
        $e->die_event unless ($new_id);
+       $e->commit;
 
        $rec->id($new_id);
        return $rec;
@@ -142,6 +144,18 @@ __PACKAGE__->register_method(
        argc            => 3,
 );                      
 
+sub _add_bib_rec {
+       my $e = shift;
+       my $marc = shift;
+       my $queue = shift;
+
+       my $rec = new Fieldmapper::vandelay::queued_bib_record();
+       $rec->marc( $marc );
+       $rec->queue( $queue );
+
+       return $e->create_vandelay_queued_bib_record( $rec );
+}
+
 sub add_record_to_authority_queue {
        my $self = shift;
        my $client = shift;
@@ -151,52 +165,116 @@ sub add_record_to_authority_queue {
 
        my $e = new_editor(authtoken => $auth, xact => 1);
 
-       $queue = $e->retrieve_vandelay_authority_queue($queue)
+       $queue = $e->retrieve_vandelay_authority_queue($queue);
 
        return $e->die_event unless $e->checkauth;
        return $e->die_event unless
                ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
                 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
 
-       my $rec = new Fieldmapper::vandelay::queued_authority_record();
-       $rec->marc( $marc );
-       $rec->queue( $queue->id );
+       my $new_id = _add_auth_rec($e, $marc, $queue->id);
 
-       my $new_id = $e->create_vandelay_queued_authority_record( $rec );
        $e->die_event unless ($new_id);
+       $e->commit;
 
        $rec->id($new_id);
        return $rec;
 }
-__PACKAGE__->register_method(  
+__PACKAGE__->register_method(
        api_name        => "open-ils.vandelay.queued_authority_record.create",
        method          => "add_record_to_authority_queue",
        api_level       => 1,
        argc            => 3,
-);                      
+);
+
+sub _add_auth_rec {
+       my $e = shift;
+       my $marc = shift;
+       my $queue = shift;
+
+       my $rec = new Fieldmapper::vandelay::queued_authority_record();
+       $rec->marc( $marc );
+       $rec->queue( $queue );
 
-sub process_marc {
-    my $r = shift;
-    my $cgi = new CGI;
+       return $e->create_vandelay_queued_authority_record( $rec );
+}
+
+sub process_spool {
+       my $self = shift;
+       my $client = shift;
+       my $auth = shift;
+       my $fingerprint = shift;
+       my $queue = shift;
 
-    my $auth = $cgi->param('ses') || $cgi->cookie('ses');
+       my $e = new_editor(authtoken => $auth, xact => 1);
 
-    return Apache2::Const::FORBIDDEN unless verify_login($auth);
+       if ($self->{record_type} eq 'bib') {
+               return $e->die_event unless $e->checkauth;
+               return $e->die_event unless
+                       ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
+                        $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
+       } else {
+               return $e->die_event unless $e->checkauth;
+               return $e->die_event unless
+                       ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
+                        $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
+       }
 
-    my $fingerprint = $cgi->param('fingerprint')
-    my $type = $cgi->param('type')
-    my $queue = $cgi->param('queue')
+       my $method = 'open-ils.vandelay.queued_'.$self->{record_type}.'_record.create';
+       $method = $self->method_lookup( $method );
 
     my $cache = new OpenSRF::Utils::Cache();
 
     my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
     $data = decode_base64($data);
 
-    print "Content-type: text/plain; charset=utf-8\n\n$data_fingerprint";
-
-    return Apache2::Const::OK;
+       my $fh = new IO::Scalar \$data;
+
+       my $batch = new MARC::Batch ( $type, $fh );
+       $batch->strict_off;
+
+       my $count = 0;
+       while (my $r = $batch->next) {
+               try {
+                       (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
+                       $xml =~ s/^<\?xml.+\?\s*>//go;
+                       $xml =~ s/>\s+</></go;
+                       $xml =~ s/\p{Cc}//go;
+                       $xml = $self->entityize($xml);
+                       $xml =~ s/[\x00-\x1f]//go;
+
+                       if ($self->{record_type} eq 'bib') {
+                               _add_bib_rec( $e, $xml, $queue );
+                       } else {
+                               _add_auth_rec( $e, $xml, $queue );
+                       }
+                       $count++;
+                       
+                       $client->respond( $count );
+               } catch Error with {
+                       my $error = shift;
+                       $log->warn("Encountered a bad record at Vandelay ingest: ".$error);
+               }
+       }
 
+       $e->commit;
+       return undef;
 }
+__PACKAGE__->register_method(  
+       api_name        => "open-ils.vandelay.bib.process_spool",
+       method          => "process_spool",
+       api_level       => 1,
+       argc            => 3,
+       record_type     => 'bib'
+);                      
+__PACKAGE__->register_method(  
+       api_name        => "open-ils.vandelay.auth.process_spool",
+       method          => "process_spool",
+       api_level       => 1,
+       argc            => 3,
+       record_type     => 'auth'
+);                      
+
 
 
 1;
index 44c91a1..3268878 100644 (file)
@@ -16,15 +16,11 @@ use Data::Dumper;
 use Text::CSV;
 
 use OpenSRF::EX qw(:try);
-use OpenSRF::Utils qw/:datetime/;
 use OpenSRF::Utils::Cache;
 use OpenSRF::System;
 use OpenSRF::AppSession;
 use XML::LibXML;
-use XML::LibXSLT;
 
-use Encode;
-use Unicode::Normalize;
 use OpenILS::Utils::Fieldmapper;
 use OpenSRF::Utils::Logger qw/$logger/;