]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
basic search classes work (except cn)
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Search.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
9
10
11 # context additions: 
12 #   page_size
13 #   hit_count
14 #   records : list of bre's and copy-count objects
15 sub load_rresults {
16     my $self = shift;
17     my $cgi = $self->cgi;
18     my $ctx = $self->ctx;
19     my $e = $self->editor;
20
21     $ctx->{page} = 'rresult';
22     my $page = $cgi->param('page') || 0;
23     my $item_type = $cgi->param('item_type');
24     my $facet = $cgi->param('facet');
25     my $query = $cgi->param('query');
26     my $search_class = $cgi->param('class');
27     my $limit = $cgi->param('limit') || 10; # TODO user settings
28
29     my $loc = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
30     my $depth = defined $cgi->param('depth') ? 
31         $cgi->param('depth') : $ctx->{find_aou}->($loc)->ou_type->depth;
32
33     my $args = {
34         limit => $limit, offset => $page * $limit,
35         org_unit => $loc, depth => $depth, $item_type ? (item_type => [$item_type]) : ()
36     };
37
38     $query = "$query $facet" if $facet; # TODO
39     # XXX Since open-ils.search is a public service, it is responsible for
40     # being wary of injection/bad input, not us, right?
41     $query = $search_class . ':' . $query if $search_class;
42
43     my $results;
44
45     try {
46
47         my $method = 'open-ils.search.biblio.multiclass.query';
48         $method .= '.staff' if $ctx->{is_staff};
49         $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
50
51     } catch Error with {
52         my $err = shift;
53         $logger->error("multiclass search error: $err");
54         $results = {count => 0, ids => []};
55     };
56
57     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
58
59     $ctx->{records} = [];
60     $ctx->{search_facets} = {};
61     $ctx->{page_size} = $limit;
62     $ctx->{hit_count} = $results->{count};
63
64     return Apache2::Const::OK if @$rec_ids == 0;
65
66     my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
67     my $bre_req = $cstore1->request(
68         'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
69
70     my $search = OpenSRF::AppSession->create('open-ils.search');
71     my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
72
73     my @data;
74     while(my $resp = $bre_req->recv) {
75         my $bre = $resp->content; 
76
77         # XXX farm out to multiple cstore sessions before loop, then collect after
78         my $copy_counts = $e->json_query(
79             {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
80
81         push(@data,
82             {
83                 bre => $bre,
84                 marc_xml => XML::LibXML->new->parse_string($bre->marc),
85                 copy_counts => $copy_counts
86             }
87         );
88     }
89
90     $cstore1->kill_me;
91
92     # shove recs into context in search results order
93     for my $rec_id (@$rec_ids) { 
94         push(
95             @{$ctx->{records}},
96             grep { $_->{bre}->id == $rec_id } @data
97         );
98     }
99
100     my $facets = $facet_req->gather(1);
101
102     $facets->{$_} = {cmf => $ctx->{find_cmf}->($_), data => $facets->{$_}} for keys %$facets;  # quick-n-dirty
103     $ctx->{search_facets} = $facets;
104
105     return Apache2::Const::OK;
106 }
107
108 1;