f5aa5690d49be41dc932659542a4e1b1ced4c052
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Connection.pm
1 # $Id: Connection.pm,v 1.15 2007-05-01 15:33:30 mike Exp $
2
3 package ZOOM::IRSpy::Connection;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use ZOOM;
10 our @ISA = qw(ZOOM::Connection);
11
12 use ZOOM::IRSpy::Record;
13 use ZOOM::IRSpy::Utils qw(cql_target render_record irspy_identifier2target);
14
15 use ZOOM::IRSpy::Task::Connect;
16 use ZOOM::IRSpy::Task::Search;
17 use ZOOM::IRSpy::Task::Retrieve;
18
19
20 =head1 NAME
21
22 ZOOM::IRSpy::Connection - ZOOM::Connection subclass with IRSpy functionality
23
24 =head1 DESCRIPTION
25
26 This class provides some additional private data and methods that are
27 used by IRSpy but which would be useless in any other application.
28 Keeping the private data in these objects removes the need for ugly
29 mappings in the IRSpy object itself; adding the methods makes the
30 application code cleaner.
31
32 The constructor takes an two additional leading arguments: a reference
33 to the IRSpy object that it is associated with, and the target ID of
34 the connection.
35
36 =cut
37
38 sub create {
39     my $class = shift();
40     my $irspy = shift();
41     my $id = shift();
42
43     my $this = $class->SUPER::create(@_);
44     my $target = irspy_identifier2target($id);
45     $this->option(host => $target);
46     $this->{irspy} = $irspy;
47     $this->{tasks} = [];
48
49     my $query = cql_target($id);
50     my $rs = $irspy->{conn}->search(new ZOOM::Query::CQL($query));
51     my $n = $rs->size();
52     $this->log("irspy", "query '$query' found $n records");
53     my $zeerex;
54     $zeerex = render_record($rs, 0, "zeerex") if $n > 0;
55     $this->{record} = new ZOOM::IRSpy::Record($this, $target, $zeerex);
56
57     return $this;
58 }
59
60
61 sub destroy {
62     my $this = shift();
63     $this->SUPER::destroy(@_);
64 }
65
66
67 sub irspy {
68     my $this = shift();
69     return $this->{irspy};
70 }
71
72
73 sub record {
74     my $this = shift();
75     my($new) = @_;
76
77     my $old = $this->{record};
78     $this->{record} = $new if defined $new;
79     return $old;
80 }
81
82
83 sub tasks {
84     my $this = shift();
85
86     return $this->{tasks};
87 }
88
89
90 sub current_task {
91     my $this = shift();
92     my($new) = @_;
93
94     my $old = $this->{current_task};
95     if (defined $new) {
96         $this->{current_task} = $new;
97         $this->log("irspy_task", "set current task to $new");
98     }
99
100     return $old;
101 }
102
103
104 sub next_task {
105     my $this = shift();
106     my($new) = @_;
107
108     my $old = $this->{next_task};
109     if (defined $new) {
110         $this->{next_task} = $new;
111         $this->log("irspy_task", "set next task to $new");
112     }
113
114     return $old;
115 }
116
117
118 sub log {
119     my $this = shift();
120     my($level, @msg) = @_;
121
122     $this->irspy()->log($level, $this->option("host"), " ", @msg);
123 }
124
125
126 sub irspy_connect {
127     my $this = shift();
128     my($udata, $options, %cb) = @_;
129
130     $this->add_task(new ZOOM::IRSpy::Task::Connect
131                     ($this, $udata, $options, %cb));
132 }
133
134
135 sub irspy_search {
136     my $this = shift();
137     my($qtype, $qstr, $udata, $options, %cb) = @_;
138
139     $this->add_task(new ZOOM::IRSpy::Task::Search
140                     ($qtype, $qstr, $this, $udata, $options, %cb));
141 }
142
143
144 # Wrapper for backwards compatibility
145 sub irspy_search_pqf {
146     my $this = shift();
147     return $this->irspy_search("pqf", @_);
148 }
149
150
151 sub irspy_rs_record {
152     my $this = shift();
153     my($rs, $index0, $udata, $options, %cb) = @_;
154
155     $this->add_task(new ZOOM::IRSpy::Task::Retrieve
156                     ($rs, $index0, $this, $udata, $options, %cb));
157 }
158
159
160 sub add_task {
161     my $this = shift();
162     my($task) = @_;
163
164     my $tasks = $this->{tasks};
165     $tasks->[-1]->{next} = $task if @$tasks > 0;
166     push @$tasks, $task;
167     $this->log("irspy_task", "added task $task");
168 }
169
170
171 sub render {
172     my $this = shift();
173     return ref($this) . "(" . $this->option("host") . ")";
174 }
175
176 use overload '""' => \&render;
177
178
179 =head1 SEE ALSO
180
181 ZOOM::IRSpy
182
183 =head1 AUTHOR
184
185 Mike Taylor, E<lt>mike@indexdata.comE<gt>
186
187 =head1 COPYRIGHT AND LICENSE
188
189 Copyright (C) 2006 by Index Data ApS.
190
191 This library is free software; you can redistribute it and/or modify
192 it under the same terms as Perl itself, either Perl version 5.8.7 or,
193 at your option, any later version of Perl 5 you may have available.
194
195 =cut
196
197 1;