a25d2ea78913288cede26d9dc2ed9a405e15bc21
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Connection.pm
1 # $Id: Connection.pm,v 1.16 2007-05-01 16:30:17 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     #warn "calling $this->irspy_search(", join(", ", @_), ")\n";
140     $this->add_task(new ZOOM::IRSpy::Task::Search
141                     ($qtype, $qstr, $this, $udata, $options, %cb));
142 }
143
144
145 # Wrapper for backwards compatibility
146 sub irspy_search_pqf {
147     my $this = shift();
148     return $this->irspy_search("pqf", @_);
149 }
150
151
152 sub irspy_rs_record {
153     my $this = shift();
154     my($rs, $index0, $udata, $options, %cb) = @_;
155
156     $this->add_task(new ZOOM::IRSpy::Task::Retrieve
157                     ($rs, $index0, $this, $udata, $options, %cb));
158 }
159
160
161 sub add_task {
162     my $this = shift();
163     my($task) = @_;
164
165     my $tasks = $this->{tasks};
166     $tasks->[-1]->{next} = $task if @$tasks > 0;
167     push @$tasks, $task;
168     $this->log("irspy_task", "added task $task");
169 }
170
171
172 sub render {
173     my $this = shift();
174     return ref($this) . "(" . $this->option("host") . ")";
175 }
176
177 use overload '""' => \&render;
178
179
180 =head1 SEE ALSO
181
182 ZOOM::IRSpy
183
184 =head1 AUTHOR
185
186 Mike Taylor, E<lt>mike@indexdata.comE<gt>
187
188 =head1 COPYRIGHT AND LICENSE
189
190 Copyright (C) 2006 by Index Data ApS.
191
192 This library is free software; you can redistribute it and/or modify
193 it under the same terms as Perl itself, either Perl version 5.8.7 or,
194 at your option, any later version of Perl 5 you may have available.
195
196 =cut
197
198 1;