Record presentation.
[egate.git] / www / search.egw
1 <html>
2 {
3 # $Id: search.egw,v 1.5 1995/10/31 16:56:24 adam Exp $
4
5 proc search-response {} {
6     global sessionWait
7
8     set status [z39.1 responseStatus]
9     if {[lindex $status 0] == "NSD"} {
10         z39.1 nextResultSetPosition 0
11         set code [lindex $status 1]
12         set msg [lindex $status 2]
13         set addinfo [lindex $status 3]
14         htmlr {<strong>Error} $code {: } $msg {: } $addinfo { </strong><br>}
15         set sessionWait -2
16     } else {
17         set sessionWait 1
18     }
19 }
20
21 proc ok-response {} {
22     global sessionWait
23     set sessionWait 1
24 }
25
26 proc fail-response {} {
27     global sessionWait
28     set sessionWait -1
29 }
30
31 proc display-brief {zset no} {
32     global env
33     global sessionId
34
35     set type [$zset type $no]
36     if {$type == "SD"} {
37         set err [lindex [$zset diag $no] 1]
38         set add [lindex [$zset diag $no] 2]
39         if {$add != {}} {
40             set add " :${add}"
41         }
42         htmlr "${no} Error ${err}${add} <br>"
43         return
44     }
45     if {$type != "DB"} {
46         return
47     }
48     html "${no} "
49     set rtype [$zset recordType $no]
50     if {$rtype == "SUTRS"} {
51         html [join [$zset getSutrs $no]]
52         htmlr {<br>}
53         return
54     } 
55     if {![catch {
56         set title [lindex [$zset getMarc $no field 245 * a] 0]
57         set year [lindex [$zset getMarc $no field 260 * c] 0]
58     } ] } {
59         html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME) /
60         html $sessionId {/showfull.egw/} $no {"> } $title {</a>}
61         html " <i> ${year} </i>"
62     }
63     htmlr {<br>}
64 }
65
66 proc display-full {zset no} {
67     set type [$zset type $no]
68     if {$type == "SD"} {
69         set err [lindex [$zset diag $no] 1]
70         set add [lindex [$zset diag $no] 2]
71         if {$add != {}} {
72             set add " :${add}"
73         }
74         htmlr "<hr> ${no} <br>"
75         htmlr "Error ${err}${add} <br>"
76         return
77     }
78     if {$type != "DB"} {
79         return
80     }
81     htmlr "<hr> ${no} <br>"
82     set rtype [$zset recordType $no]
83     if {$rtype == "SUTRS"} {
84         htmlr [join [$zset getSutrs $no]]
85         return
86     } 
87     if {[catch {set r [$zset getMarc $no line * * *]}]} {
88         htmlr "Unknown record type: $rtype"
89         return
90     }
91     foreach line $r {
92         set tag [lindex $line 0]
93         set indicator [lindex $line 1]
94         set fields [lindex $line 2]
95         set l [string length $indicator]
96         html "$tag "
97         if {$l > 0} {
98             for {set i 0} {$i < $l} {incr i} {
99                 if {[string index $tag $i] == " "} {
100                     html "_"
101                 } else {
102                     html [string index $tag $i]
103                 }
104             }
105         }
106         foreach field $fields {
107             set id [lindex $field 0]
108             set data [lindex $field 1]
109             if {$id != ""} {
110                 html " <b>\$$id</b> "
111             }
112             html $data
113         }
114         htmlr {<br>}
115     }
116 }
117
118 proc display-rec {from to} {
119     while {$from <= $to} { 
120         display-brief z39.1 $from
121         incr from
122     }
123 }
124
125 proc build-query {} {
126     global targets
127     global t
128
129     set op {}
130     set q {}
131     for {set i 1} {$i < 4} {incr i} {
132         set term [wform entry$i]
133         if {$term != ""} {
134             set field [wform menu$i]
135             foreach x [lindex $targets($t) 2] {
136                 if {[lindex $x 0] == $field} {
137                     set attr [lindex $x 1]
138                 }
139             }
140             switch $op {
141             And
142                 { set q "@and $q ${attr} ${term}" }
143             Or
144                 { set q "@or $q ${attr} ${term}" }
145             {And not}
146                 { set q "@not $q ${attr} ${term}" }
147             {}
148                 { set q "${attr} ${term}" }
149             }
150             set op [wform logic$i]
151         }
152     }
153     return $q
154 }
155
156     global sessionWait
157     global host
158
159     set newHost $sessionParms
160     set databases [lindex $targets($newHost) 1]
161
162     htmlr {<head><title> WWW/Z39.50 Gateway Search } $newHost { </title>}
163     htmlr {</head><body>}
164     wflush
165
166     if {[catch {z39 callback ok-response}]} {
167         ir z39
168     }
169     if {$newHost != $host} {
170         set host $newHost
171         z39 disconnect
172         z39 callback ok-response
173         z39 failback fail-response
174
175         htmlr {Connecting to target } $host { <br>}
176         set sessionWait 0
177         if {[catch {z39 connect $host}]} {
178             htmlr "Cannot connect to target ${host} <br>"
179             htmlr "</body></html>"
180             wabort
181         } elseif {$sessionWait == 0} {
182             zwait sessionWait
183             if {$sessionWait != 1} {
184                 htmlr "Cannot connect to target ${host} <br>"
185                 htmlr "</body></html>"
186                 wabort
187             }
188         }
189         set sessionWait 0
190         z39 init
191         zwait sessionWait
192         if {$sessionWait != "1"} {
193             htmlr "Cannot initialize with target ${host} <br>"
194             htmlr "</body></html>"
195             wabort
196         }
197     }
198     ir-set z39.1 z39
199     set b [wform base]
200     if {$b == ""} {
201         z39.1 databaseNames [lindex $targets($host) 1]
202     } else {
203         z39.1 databaseNames [list $b]
204         htmlr {selected: } $b { <br>}
205     }
206     z39.1 preferredRecordSyntax USMARC
207
208     set query [build-query]
209
210     htmlr {<hr>query: --} $query {-- <br>}
211     htmlr {sessionId: } $sessionId {<br>}
212     htmlr {sessionParms: } $sessionParms {<br>}
213     htmlr {form: } [wform] { <br>}
214     htmlr {databases: } $databases { <br>}
215     htmlr {selected: } [wform base] { <br><hr>}
216
217     z39 callback search-response
218     set sessionWait 0
219     z39.1 search $query
220
221     zwait sessionWait
222     if {$sessionWait == 1} {
223         set r [z39.1 resultCount]
224         htmlr {<strong> } $r { hits</strong><br><br>}
225     } else {
226         htmlr {</body></html>}
227         wabort
228     }
229     set setOffset [z39.1 numberOfRecordsReturned]
230     display-rec 1 $setOffset
231     wflush
232     incr setOffset
233     set setMax [z39.1 resultCount]
234     if {$setMax > 30} {
235         set setMax 30
236     }
237     set toGet [expr 1 + $setMax - $setOffset]
238     while {$toGet > 0} {
239         set sessionWait 0
240         z39.1 present $setOffset $toGet
241         zwait sessionWait
242         if {$sessionWait != "1"} {
243             break
244         }
245         set got [z39.1 numberOfRecordsReturned]
246         display-rec $setOffset [expr $got + $setOffset - 1]
247         set setOffset [expr $got + $setOffset]
248         set toGet [expr 1 + $setMax - $setOffset]
249         wflush
250     }
251 }
252 </body>
253 </html>
254