]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
added much, moving fast ;)
[working/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         warn "Getting ORG Tree\n";
89         my $org_tree = OpenILS::Application::AppUtils->get_org_tree();
90         warn "Returning Org Tree\n";
91
92         return $org_tree;
93 }
94
95
96
97 __PACKAGE__->register_method(
98         method  => "get_org_sub_tree",
99         api_name        => "open-ils.search.actor.org_subtree.retrieve",
100         argc            => 1, 
101         note            => "Returns the entire org tree structure",
102 );
103
104 sub get_sub_org_tree {
105
106         my( $self, $client, $user_session ) = @_;
107
108         if(!$user_session) {
109                 throw OpenSRF::EX::InvalidArg 
110                         ("No User session provided to org_subtree.retrieve");
111         }
112
113         if( $user_session ) {
114
115                 my $user_obj = 
116                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
117
118                 
119                 my $session = OpenSRF::AppSession->create("open-ils.storage");
120                 my $request = $session->request( 
121                                 "open-ils.storage.direct.actor.org_unit.retrieve", $user_obj->home_ou );
122                 my $response = $request->recv();
123
124                 if(!$response) { 
125                         throw OpenSRF::EX::ERROR (
126                                         "No response from storage for org_unit retrieve");
127                 }
128                 if(UNIVERSAL::isa($response,"Error")) {
129                         throw $response ($response->stringify);
130                 }
131
132                 my $home_ou = $response->content;
133
134                 # XXX grab descendants and build org tree from them
135 =head comment
136                 my $request = $session->request( 
137                                 "open-ils.storage.actor.org_unit_descendants" );
138                 my $response = $request->recv();
139                 if(!$response) { 
140                         throw OpenSRF::EX::ERROR (
141                                         "No response from storage for org_unit retrieve");
142                 }
143                 if(UNIVERSAL::isa($response,"Error")) {
144                         throw $response ($response->stringify);
145                 }
146
147                 my $descendants = $response->content;
148 =cut
149
150                 $request->finish();
151                 $session->disconnect();
152
153                 return $home_ou;
154         }
155
156         return undef;
157
158 }
159
160
161
162
163
164
165
166
167 package OpenILS::Application::SearchCache;
168 use strict; use warnings;
169
170 my $cache_handle;
171 my $max_timeout;
172
173 sub child_init {
174
175         my $config_client = OpenSRF::Utils::SettingsClient->new();
176         my $memcache_servers = 
177                 $config_client->config_value( 
178                                 "apps","open-ils.search", "app_settings","memcache" );
179
180         if( !$memcache_servers ) {
181                 throw OpenSRF::EX::Config ("
182                                 No Memcache servers specified for open-ils.search!");
183         }
184
185         if(!ref($memcache_servers)) {
186                 $memcache_servers = [$memcache_servers];
187         }
188         $cache_handle = OpenSRF::Utils::Cache->new( "open-ils.search", 0, $memcache_servers );
189         $max_timeout = $config_client->config_value( 
190                         "apps", "open-ils.search", "app_settings", "max_cache_time" );
191
192         if(ref($max_timeout) eq "ARRAY") {
193                 $max_timeout = $max_timeout->[0];
194         }
195
196 }
197
198 sub new {return bless({},shift());}
199
200 sub put_cache {
201         my($self, $key, $data, $timeout) = @_;
202         return undef unless( $key and $data );
203
204         $timeout ||= $max_timeout;
205         $timeout = ($timeout <= $max_timeout) ? $timeout : $max_timeout;
206
207         warn "putting $key into cache for $timeout seconds\n";
208         $cache_handle->put_cache( "_open-ils.search_$key", JSON->perl2JSON($data), $timeout );
209 }
210
211 sub get_cache {
212         my( $self, $key ) = @_;
213         my $json =  $cache_handle->get_cache("_open-ils.search_$key");
214         if($json) {
215                 warn "retrieving from cache $key\n  =>>>  $json";
216         }
217         return JSON->JSON2perl($json);
218 }
219
220
221
222
223 1;