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