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