Try access-point 44 if other searches fail.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.19 2007-01-29 17:24:51 mike Exp $
2
3 # See the "Main" test package for documentation
4
5 package ZOOM::IRSpy::Test::Record::Fetch;
6
7 use 5.008;
8 use strict;
9 use warnings;
10
11 use ZOOM::IRSpy::Test;
12 our @ISA = qw(ZOOM::IRSpy::Test);
13
14
15 # These queries 
16 my @queries = (
17                "\@attr 1=4 mineral",
18                "\@attr 1=4 computer",
19                "\@attr 1=44 mineral", # Smithsonian doesn't support AP 4!
20                ### We can add more queries here
21                );
22
23
24 sub start {
25     my $class = shift();
26     my($conn) = @_;
27
28     # Here I want to get a use attribute from the session, which we've
29     # managed to search for in the Search/Bib1 or Search/Dan1 tests.
30     # But how?  So far we search for title: 1=4
31     $conn->irspy_search_pqf($queries[0], { queryindex => 0 }, {},
32                             ZOOM::Event::RECV_SEARCH, \&completed_search,
33                             exception => \&search_error);
34 }
35
36
37 sub completed_search {
38     my($conn, $task, $udata, $event) = @_;
39
40     my $n = $task->{rs}->size();
41     $conn->log("irspy_test", "Fetch test search found $n records");
42     if ($n == 0) {
43         my $n = $udata->{queryindex}+1;
44         my $q = $queries[$n];
45         if (defined $q) {
46             $conn->log("irspy_test", "Trying another search ...");
47             $conn->irspy_search_pqf($queries[$n], { queryindex => $n }, {},
48                                     ZOOM::Event::RECV_SEARCH, \&completed_search,
49                                     exception => \&search_error);
50             return ZOOM::IRSpy::Status::TASK_DONE;
51         } else {
52             return ZOOM::IRSpy::Status::TEST_SKIPPED;
53         }
54     }
55
56     my @syntax = (
57                    'canmarc',
58                    'danmarc',
59                    'grs-1',
60                    'ibermarc',
61                    'intermarc',
62                    'jpmarc',
63                    'librismarc',
64                    'mab',
65                    'normarc',
66                    'opac',
67                    'picamarc',
68                    'rusmarc',
69                    'summary',
70                    'sutrs',
71                    'swemarc',
72                    'ukmarc',
73                    'unimarc',
74                    'usmarc',
75                    'xml'
76                 );
77     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
78     foreach my $i (0 ..$#syntax) {
79         my $syntax = $syntax[$i];
80         $conn->irspy_rs_record($task->{rs}, 0,
81                                { syntax => $syntax,
82                                  last => ($i == $#syntax) },
83                                { start => 0, count => 1,
84                                  preferredRecordSyntax => $syntax },
85                                 ZOOM::Event::RECV_RECORD, \&record,
86                                 exception => \&fetch_error);
87     }
88
89     return ZOOM::IRSpy::Status::TASK_DONE;
90 }
91
92
93 sub record {
94     my($conn, $task, $udata, $event) = @_;
95     my $syn = $udata->{'syntax'};
96     my $rs = $task->{rs};
97
98     my $record = _fetch_record($rs, 0, $syn);
99     my $ok = 0;
100     if ($record->error()) {
101         $conn->log("irspy_test", "retrieval of $syn record failed: ",
102                    $record->exception());
103     } else {
104         $ok = 1;
105         my $text = $record->render();
106         $conn->log("irspy_test", "Successfully retrieved a $syn record");
107         if (0) {
108             print STDERR "Hits: ", $rs->size(), "\n";
109             print STDERR "Syntax: ", $syn, "\n";
110             print STDERR $text;
111         }
112     }
113
114     $conn->record()->store_result('record_fetch',
115                                   'syntax'   => $syn,
116                                   'ok'       => $ok);
117
118     return ($udata->{last} ?
119             ZOOM::IRSpy::Status::TEST_GOOD :
120             ZOOM::IRSpy::Status::TASK_DONE);
121 }
122
123
124 sub _fetch_record {
125     my($rs, $index0, $syntax) = @_;
126
127     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
128     my $record = $rs->record(0);
129     $oldSyntax = "" if !defined $oldSyntax;
130     $rs->option(preferredRecordSyntax => $oldSyntax);
131
132     return $record;
133 }
134
135
136 sub search_error {
137     my($conn, $task, $test_args, $exception) = @_;
138
139     $conn->log("irspy_test", "Initial search failed: ", $exception);
140     return ZOOM::IRSpy::Status::TEST_SKIPPED;
141 }
142
143
144 sub fetch_error {
145     my($conn, $task, $test_args, $exception) = @_;
146     my $syn = $test_args->{'syntax'};
147
148     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
149     $conn->record()->store_result('record_fetch',
150                                   'syntax'       => $syn,
151                                   'ok'        => 0);
152     return ZOOM::IRSpy::Status::TASK_DONE;
153 }
154
155
156 1;