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