Z39 Batch ML
authorBill Erickson <berick@esilibrary.com>
Wed, 30 Jan 2013 22:44:30 +0000 (17:44 -0500)
committerBill Erickson <berick@esilibrary.com>
Wed, 30 Jan 2013 22:44:30 +0000 (17:44 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Z3950.pm

index 978b106..09e05ec 100644 (file)
@@ -479,5 +479,158 @@ sub compile_query {
     return $str;
 }
 
+
+__PACKAGE__->register_method(
+    method    => 'bucket_search_queue',
+    api_name  => 'open-ils.search.z3950.bucket_search_queue',
+    stream    => 1,
+    signature => {
+        desc => q/
+            Performs a Z39.50 search for every record in a bucket, using the
+            provided Z39.50 fields.  Add all search results to the specified
+            Vandelay queue.  If no source records or search results are found,
+            no queue is created.
+        /,
+        params => [
+            {desc => q/Authentication token/, type => 'string'},
+            {desc => q/Bucket ID/, type => 'number'},
+            {desc => q/Destination Queue name/, type => 'string'},
+            {desc => q/Z39 Servers.  List of czs.name/, type => 'array'},
+            {desc => q/Z39 Index Maps.  List of czifm.id/, type => 'array'}
+        ],
+        return => {
+            q/Object containing status information about the on-going search
+            and queue operation. 
+            {
+                searches : { 
+                    $zsource  : {
+                        pending : $pending,
+                        sent : $sent,
+                        received : $received
+                    },
+                    ...
+                },
+                queue_count : $queue_count, # on completion; 0 on no-op
+                queue : $queue_obj # on completion and action
+            }
+            This object will be streamed back with each milestone (search
+            result or complete).
+            Event object returned on failure
+            /
+        }
+    }
+);
+
+sub bucket_search_queue {
+    my $self = shift;
+    my $conn = shift;
+    my $auth = shift;
+    my $bucket_id = shift;
+    my $qname = shift;
+    my $z_servers = shift;
+    my $z_indexes = shift;
+
+    my $e = new_editor(authtoken => $auth);
+    return $e->event unless 
+        $e->checkauth and
+        $e->allowed('REMOTE_Z3950_QUERY') and
+        $e->allowed('CREATE_BIB_IMPORT_QUEUE');
+    
+    # find the source bib records
+
+    my $bre_ids = $e->json_query({
+        select => {cbrebi => ['target_biblio_record_entry']},
+        from => 'cbrebi',
+        where => {bucket => $bucket_id},
+        distinct => 1
+    });
+
+    # empty bucket
+    return {queue_count => 0} unless @$bre_ids;
+
+    $bre_ids = [ map {$_->{id}} @$bre_ids ];
+
+    # build the Z39 queries for the source bib records
+
+    $z_indexes = $e->search_config_z3950_index_field_map({id => $z_indexes});
+
+    return OpenILS::Event->new('BAD_PARAMS', 
+        note => q/No z_indexes/) unless @$z_indexes;
+
+
+    # pre-load the metabib_field's we'll need for this batch
+
+    my %mb_fields;
+    my @mb_fields = grep { $_->metabib_field } @$z_indexes;
+    if (@mb_fields) {
+        @mb_fields = map { $_->metabib_field } @mb_fields;
+        my $field_objs = $e->search_config_metabib_field({id => \@mb_fields});
+        %mb_fields = map {$_->id => $_} @$field_objs;
+    }
+
+    # pre-load the referenced z3950_attrs
+
+    my %z3950_attrs;
+    my @z3950_attrs = grep { $_->z3950_attr } @$z_indexes;
+    if (@z3950_attrs) {
+        @z3950_attrs = map { $_->z3950_attr } @z3950_attrs;
+        my $attr_objs = $e->search_config_z3950_attr({id => \@z3950_attrs});
+        %z3950_attrs = map {$_->id => $_} @$attr_objs;
+    }
+
+    # indexes with specific z3950_attr's take precedence
+    my @z_index_attrs = grep { {defined $_->z3959_attr} } @$z_indexes;
+    my @z_index_types = grep { {not defined $_->z3959_attr} } @$z_indexes;
+
+    my %z_searches;
+
+    # for each bib record, extract the indexed value for each of the 
+    # selected indexes.  
+
+    for my $bre_id (@$bre_ids) {
+
+        $z_searches{$bre_id} = []; # TODO
+
+        for my $z_index (@z_index_attrs, @z_index_types) {
+
+            my $bre_val;
+            if ($z_index->record_attr) {
+                my $attrs = $U->get_bre_attrs($bre_id, $e);
+                $bre_val = $attrs->{$z_index->record_attr}->{code}
+                    if $attrs->{$z_index->record_attr};
+
+            } else { # metabib_field
+                my $fid = $z_index->metabib_field;
+
+                # now find the extacted value for this record
+                my $entry_query = sprintf(
+                    'search_%s_field_entry', $mb_fields{$fid}->field_class);
+
+                my $entry = $e->$entry_query({field => $fid, source => $bre_id});
+
+                $bre_val = $entry->value if $entry;
+            }
+
+            push($z_searches{$bre_id}{$z_index->id}, [$z_index, $bre_val]); # TODO
+
+
+
+
+#            if ($bre_val) {
+#                if ($z_index->z3959_attr) { # source-specific
+#                    $z_searches{$z_
+#                } else {
+#                }
+#            }
+
+        }
+    }
+
+    # TODO
+    return \%z_searches;
+}
+
+
+
 1;
 # vim:et:ts=4:sw=4: