]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
moved biblio-specific searches to Biblio
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search.pm
1 package OpenILS::Application::Search;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use JSON;
5
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::ModsParser;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Cache;
10
11
12 #use OpenILS::Application::Search::StaffClient;
13 use OpenILS::Application::Search::Web;
14 use OpenILS::Application::Search::Biblio;
15
16 use OpenILS::Application::AppUtils;
17
18 use Time::HiRes qw(time);
19 use OpenSRF::EX qw(:try);
20
21 # Houses generic search utilites 
22
23 sub child_init {
24         OpenILS::Application::SearchCache->child_init();
25 }
26
27 sub filter_search {
28         my($self, $string) = @_;
29
30         $string =~ s/\s+the\s+//i;
31         $string =~ s/\s+an\s+//i;
32         $string =~ s/\s+a\s+//i;
33
34         $string =~ s/^the\s+//i;
35         $string =~ s/^an\s+//i;
36         $string =~ s/^a\s+//i;
37
38         $string =~ s/\s+the$//i;
39         $string =~ s/\s+an$//i;
40         $string =~ s/\s+a$//i;
41
42         $string =~ s/^the$//i;
43         $string =~ s/^an$//i;
44         $string =~ s/^a$//i;
45
46         warn "Cleansed string to: $string\n";
47         return $string;
48 }       
49
50
51 __PACKAGE__->register_method(
52         method  => "get_org_tree",
53         api_name        => "open-ils.search.actor.org_tree.retrieve",
54         argc            => 1, 
55         note            => "Returns the entire org tree structure",
56 );
57
58 sub get_org_tree {
59
60         my( $self, $client, $user_session ) = @_;
61
62         if( $user_session ) { # keep for now for backwards compatibility
63
64                 my $user_obj = 
65                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
66                 
67                 my $session = OpenSRF::AppSession->create("open-ils.storage");
68                 my $request = $session->request( 
69                                 "open-ils.storage.actor.org_unit.retrieve", $user_obj->home_ou );
70                 my $response = $request->recv();
71
72                 if(!$response) { 
73                         throw OpenSRF::EX::ERROR (
74                                         "No response from storage for org_unit retrieve");
75                 }
76                 if(UNIVERSAL::isa($response,"Error")) {
77                         throw $response ($response->stringify);
78                 }
79
80                 my $home_ou = $response->content;
81                 $request->finish();
82                 $session->disconnect();
83
84                 return $home_ou;
85         }
86
87         return OpenILS::Application::AppUtils->get_org_tree();
88 }
89
90
91
92 __PACKAGE__->register_method(
93         method  => "get_org_sub_tree",
94         api_name        => "open-ils.search.actor.org_subtree.retrieve",
95         argc            => 1, 
96         note            => "Returns the entire org tree structure",
97 );
98
99 sub get_sub_org_tree {
100
101         my( $self, $client, $user_session ) = @_;
102
103         if(!$user_session) {
104                 throw OpenSRF::EX::InvalidArg 
105                         ("No User session provided to org_subtree.retrieve");
106         }
107
108         if( $user_session ) {
109
110                 my $user_obj = 
111                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
112
113                 
114                 my $session = OpenSRF::AppSession->create("open-ils.storage");
115                 my $request = $session->request( 
116                                 "open-ils.storage.actor.org_unit.retrieve", $user_obj->home_ou );
117                 my $response = $request->recv();
118
119                 if(!$response) { 
120                         throw OpenSRF::EX::ERROR (
121                                         "No response from storage for org_unit retrieve");
122                 }
123                 if(UNIVERSAL::isa($response,"Error")) {
124                         throw $response ($response->stringify);
125                 }
126
127                 my $home_ou = $response->content;
128
129                 # XXX grab descendants and build org tree from them
130 =head comment
131                 my $request = $session->request( 
132                                 "open-ils.storage.actor.org_unit_descendants" );
133                 my $response = $request->recv();
134                 if(!$response) { 
135                         throw OpenSRF::EX::ERROR (
136                                         "No response from storage for org_unit retrieve");
137                 }
138                 if(UNIVERSAL::isa($response,"Error")) {
139                         throw $response ($response->stringify);
140                 }
141
142                 my $descendants = $response->content;
143 =cut
144
145                 $request->finish();
146                 $session->disconnect();
147
148                 return $home_ou;
149         }
150
151         return undef;
152
153 }
154
155
156
157
158
159
160
161
162 package OpenILS::Application::SearchCache;
163 use strict; use warnings;
164
165 my $cache_handle;
166 my $max_timeout;
167
168 sub child_init {
169
170         my $config_client = OpenSRF::Utils::SettingsClient->new();
171         my $memcache_servers = 
172                 $config_client->config_value( 
173                                 "apps","open-ils.search", "app_settings","memcache" );
174
175         if( !$memcache_servers ) {
176                 throw OpenSRF::EX::Config ("
177                                 No Memcache servers specified for open-ils.search!");
178         }
179
180         if(!ref($memcache_servers)) {
181                 $memcache_servers = [$memcache_servers];
182         }
183         $cache_handle = OpenSRF::Utils::Cache->new( "open-ils.search", 0, $memcache_servers );
184         $max_timeout = $config_client->config_value( 
185                         "apps", "open-ils.search", "app_settings", "max_cache_time" );
186
187         if(ref($max_timeout) eq "ARRAY") {
188                 $max_timeout = $max_timeout->[0];
189         }
190
191 }
192
193 sub new {return bless({},shift());}
194
195 sub put_cache {
196         my($self, $key, $data, $timeout) = @_;
197         return undef unless( $key and $data );
198
199         $timeout ||= $max_timeout;
200         $timeout = ($timeout <= $max_timeout) ? $timeout : $max_timeout;
201
202         warn "putting $key into cache for $timeout seconds\n";
203         $cache_handle->put_cache( "_open-ils.search_$key", JSON->perl2JSON($data), $timeout );
204 }
205
206 sub get_cache {
207         my( $self, $key ) = @_;
208         my $json =  $cache_handle->get_cache("_open-ils.search_$key");
209         if($json) {
210                 warn "retrieving from cache $key\n  =>>>  $json";
211         }
212         return JSON->JSON2perl($json);
213 }
214
215
216
217
218 1;