]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
adding more search methods
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat.pm
1 use strict; use warnings;
2 package OpenILS::Application::Cat;
3 use OpenILS::Application::AppUtils;
4 use OpenSRF::Application;
5 use OpenILS::Application::Cat::Utils;
6 use base qw/OpenSRF::Application/;
7 use Time::HiRes qw(time);
8 use OpenSRF::EX qw(:try);
9 use JSON;
10 use OpenILS::Utils::Fieldmapper;
11
12 my $utils = "OpenILS::Application::Cat::Utils";
13
14 sub _child_init {
15         try {
16                 OpenSRF::Application->method_lookup( "blah" );
17         } catch Error with { 
18                 warn "Child Init Failed: " . shift() . "\n";
19         };
20 }
21
22
23 __PACKAGE__->register_method(
24         method  => "biblio_record_tree_retrieve",
25         api_name        => "open-ils.cat.biblio.record.tree.retrieve",
26         argc            => 1, 
27         note            => "Returns the tree associated with the nodeset of the given doc id"
28 );
29
30 sub biblio_record_tree_retrieve {
31
32         my( $self, $client, $recordid ) = @_;
33
34         my $name = "open-ils.storage.biblio.record_marc.retrieve";
35         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
36         my $request = $session->request( $name, $recordid );
37         my $response = $request->recv();
38         warn "got response from storage in retrieve for $recordid\n";
39
40         if(!$response) { 
41                 throw OpenSRF::EX::ERROR ("No record in database with id $recordid");
42         }
43
44         if( $response->isa("OpenSRF::EX")) {
45                 throw $response ($response->stringify);
46         }
47
48         warn "grabbing content in retrieve\n";
49         my $marcxml = $response->content;
50
51         if(!$marcxml) {
52                 throw OpenSRF::EX::ERROR 
53                         ("No record in database with id $recordid");
54         }
55
56         $request->finish();
57         $session->disconnect();
58         $session->kill_me();
59
60         warn "turning into nodeset\n";
61         my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc ); 
62         warn "turning nodeset into tree\n";
63         my $tree = $utils->nodeset2tree( $nodes->nodeset );
64
65         $tree->owner_doc( $marcxml->id() );
66
67         warn "returning tree\n";
68
69         return $tree;
70 }
71
72 __PACKAGE__->register_method(
73         method  => "biblio_record_tree_commit",
74         api_name        => "open-ils.cat.biblio.record.tree.commit",
75         argc            => 3, #(session_id, biblio_tree ) 
76         note            => "Walks the tree and commits any changed nodes " .
77                                         "adds any new nodes, and deletes any deleted nodes",
78 );
79
80 sub biblio_record_tree_commit {
81
82         my( $self, $client, $user_session,  $tree ) = @_;
83         new Fieldmapper::biblio::record_node ($tree);
84
85         use Data::Dumper;
86
87         throw OpenSRF::EX::InvalidArg 
88                 ("Not enough args to to open-ils.cat.biblio.record.tree.commit")
89                 unless ( $user_session and $client and $tree );
90
91         my $user_obj = 
92                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
93
94         # capture the doc id
95         my $docid = $tree->owner_doc();
96
97         # turn the tree into a nodeset
98         my $nodeset = $utils->tree2nodeset($tree);
99         $nodeset = $utils->clean_nodeset( $nodeset );
100
101         if(!defined($docid)) { # be sure
102                 for my $node (@$nodeset) {
103                         $docid = $node->owner_doc();
104                         last if defined($docid);
105                 }
106         }
107
108         # turn the nodeset into a doc
109         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
110
111         my $biblio =  Fieldmapper::biblio::record_marc->new();
112         $biblio->id( $docid );
113         $biblio->marc( $marcxml->toString() );
114
115         warn "Starting db session\n";
116         my $session = OpenILS::Application::AppUtils->start_db_session();
117
118         my $x = _update_record_metadata( $session, { user => $user_obj, docid => $docid } );
119         OpenILS::Application::AppUtils->rollback_db_session($session) unless $x;
120
121         warn "Sending updated doc $docid to db\n";
122         my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
123
124         my $status = $req->recv();
125         if( !$status || $status->isa("Error") || ! $status->content) {
126                 OpenILS::Application::AppUtils->rollback_db_session($session);
127                 if($status->isa("Error")) { throw $status ($status); }
128                 throw OpenSRF::EX::ERROR ("Error updating biblio record");
129         }
130         $req->finish();
131
132
133         OpenILS::Application::AppUtils->commit_db_session( $session );
134
135         my $method = $self->method_lookup( 
136                         "open-ils.cat.biblio.record.tree.retrieve" );
137
138         my ($ans) = $method->run( $docid );
139
140         warn "Finished updating, returning tree to client";
141         $client->respond_complete($ans);
142
143
144
145
146         # Send the doc to the wormer for wormizing
147         warn "Starting worm session\n";
148         my $wses = OpenSRF::AppSession->create("open-ils.worm");
149
150         my $success = 0;
151         my $wresp;
152         for(0..1) {
153
154                 my $wreq = $wses->request( 
155                                 "open-ils.worm.wormize.marc", $docid, $marcxml->toString );
156                 warn "Calling worm receive\n";
157                 $wresp = $wreq->recv();
158
159                 if( $wresp && $wresp->can("content") and $wresp->content ) {
160                         $success = 1;
161                         $wreq->finish();
162                         last;
163                 }
164
165                 warn "Looping in worm call\n";
166                 $wreq->finish();
167         }
168
169         if( !$success ) {
170
171                 warn "wormizing failed, rolling back\n";
172                 if($wresp and $wresp->isa("Error") ) {
173                         OpenILS::Application::AppUtils->rollback_db_session($session);
174                         throw $wresp ($wresp->stringify);
175                 }
176
177                 $wses->disconnect;
178                 $wses->kill_me;
179
180                 OpenILS::Application::AppUtils->rollback_db_session($session);
181
182                 throw OpenSRF::EX::ERROR ("Wormizing Failed for $docid" );
183         }
184
185         $wses->disconnect;
186         $wses->kill_me;
187
188         warn "Done wormizing\n";
189
190 }
191
192
193
194 __PACKAGE__->register_method(
195         method  => "biblio_record_record_metadata",
196         api_name        => "open-ils.cat.biblio.record.metadata.retrieve",
197         argc            => 1, #(session_id, biblio_tree ) 
198         note            => "Walks the tree and commits any changed nodes " .
199                                         "adds any new nodes, and deletes any deleted nodes",
200 );
201
202 sub biblio_record_record_metadata {
203         my( $self, $client, @ids ) = @_;
204
205         if(!@ids){return undef;}
206
207         my $session = OpenSRF::AppSession->create("open-ils.storage");
208         my $request = $session->request( 
209                         "open-ils.storage.biblio.record_entry.batch.retrieve", @ids );
210
211         my $results = [];
212
213         while( my $response = $request->recv() ) {
214
215                 if(!$response) {
216                         throw OpenSRF::EX::ERROR ("No Response from Storage");
217                 }
218                 if($response->isa("Error")) {
219                         throw $response ($response->stringify);
220                 }
221
222                 my $record_entry = $response->content;
223
224                 my $creator = $record_entry->creator;
225                 my $editor      = $record_entry->editor;
226
227                 ($creator, $editor) = _get_userid_by_id($creator, $editor) || ("","");
228
229                 $record_entry->creator( $creator );
230                 $record_entry->editor( $editor );
231
232                 push @$results, $record_entry;
233
234         }
235
236         $session->disconnect();
237         $session->kill_me();
238
239         return $results;
240
241 }
242
243 # gets the username
244 sub _get_userid_by_id {
245
246         my @ids = @_;
247         my @users;
248
249         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
250         my $request = $session->request( 
251                 "open-ils.storage.actor.user.batch.retrieve", @ids );
252
253         my $response = $request->recv();
254         if(!$response) { return undef; }
255
256         if($response->isa("Error")){
257                 throw $response ($response);
258         }
259
260         for my $u (@{$response->content}) {
261                 next unless ref($u);
262                 push @users, $u->usrid;
263         }
264
265         $request->finish;
266         $session->disconnect;
267         $session->kill_me();
268
269         return @users;
270 }
271
272 # open-ils.storage.actor.user.search.usrid
273
274 sub _get_id_by_userid {
275
276         my @users = @_;
277         my @ids;
278
279         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
280         my $request = $session->request( 
281                 "open-ils.storage.actor.user.search.usrid", @users );
282
283         my $response = $request->recv();
284         if(!$response) { return undef; }
285
286         if($response->isa("Error")){
287                 throw $response ($response);
288         }
289
290         for my $u (@{$response->content}) {
291                 next unless ref($u);
292                 push @ids, $u->id();
293         }
294
295         $request->finish;
296         $session->disconnect;
297         $session->kill_me();
298
299         return @ids;
300 }
301
302
303 # commits metadata objects to the db
304 sub _update_record_metadata {
305
306         my ($session, @docs ) = @_;
307
308         for my $doc (@docs) {
309
310                 my $user_obj = $doc->{user};
311                 my $docid = $doc->{docid};
312
313                 warn "Updating metata for doc $docid\n";
314
315                 # ----------------------------------------
316                 # grab the meta information  and update it
317                 my $user_session = OpenSRF::AppSession->create("open-ils.storage");
318                 my $user_request = $user_session->request( 
319                         "open-ils.storage.biblio.record_entry.retrieve", $docid );
320                 my $meta = $user_request->recv();
321
322                 if(!$meta) {
323                         throw OpenSRF::EX::ERROR ("No meta info returned for biblio $docid");
324                 }
325                 if($meta->isa("Error")) {
326                         throw $meta ($meta->stringify);
327                 }
328
329                 $meta = $meta->content;
330                 my ($id) = _get_id_by_userid($user_obj->usrid);
331                 warn "got $id from _get_id_by_userid\n";
332                 $meta->editor($id);
333
334                 $user_request->finish;
335                 $user_session->disconnect;
336                 $user_session->kill_me;
337                 # -------------------------------------
338                 
339                 warn "Grabbed the record, updating and moving on\n";
340
341                 my $request = $session->request( 
342                         "open-ils.storage.biblio.record_entry.update", $meta );
343
344                 my $response = $request->recv();
345                 if(!$response) { 
346                         throw OpenSRF::EX::ERROR 
347                                 ("Error commit record metadata for " . $meta->id);
348                 }
349
350                 if($response->isa("Error")){ 
351                         throw $response ($response->stringify); 
352                 }
353
354                 $request->finish;
355         }
356
357         warn "committing metarecord update\n";
358
359         return 1;
360 }
361
362
363
364
365 __PACKAGE__->register_method(
366         method  => "retrieve_copies",
367         api_name        => "open-ils.cat.asset.copy.retrieve",
368         argc            => 2,  #(user_session, record_id)
369         note            => <<TEXT
370         Returns the copies for a given bib record and for the users home library
371 TEXT
372 );
373
374 sub retrieve_copies {
375
376         my( $self, $client, $user_session, $docid ) = @_;
377
378         $docid = "$docid";
379
380         use Data::Dumper;
381
382         my $user_obj = 
383                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
384
385         warn "user session is ok\n";
386         warn "Home Lib: " . $user_obj->home_ou . "\n";
387
388         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
389         
390         # ------------------------------------------------------
391         # grab the short name of the library location
392         my $request = $session->request( 
393                         "open-ils.storage.actor.org_unit.retrieve", $user_obj->home_ou );
394
395         my $org_unit = $request->recv();
396         if(!$org_unit) {
397                 throw OpenSRF::EX::ERROR 
398                         ("No response from storage for org unit search");
399         }
400         if($org_unit->isa("Error")) { throw $org_unit ($org_unit->stringify);}
401         my $location = $org_unit->content->shortname;
402         warn "found location $location\n";
403         $request->finish();
404         # ------------------------------------------------------
405
406
407         # ------------------------------------------------------
408         # grab all the volumes for the given record and location
409         my $search_hash = { record => $docid, owning_lib => $location };
410
411         warn "Using search hash: " . Dumper( $search_hash );
412
413         warn "searching call_numbers for $docid and $location\n";
414
415         $request = $session->request( 
416                         "open-ils.storage.asset.call_number.search", $search_hash );
417
418         my $volume;
419         my @volume_ids;
420
421         while( $volume = $request->recv() ) {
422                 if($volume->isa("Error")) { 
423                         throw $volume ($volume->stringify);}
424                 $volume = $volume->content;
425                 warn "pusing onto volume ids: " . $volume->id . "\n";
426                 push @volume_ids, $volume->id;
427         }
428         $request->finish();
429         # ------------------------------------------------------
430
431
432         # ------------------------------------------------------
433         # grab all of the copies for the given call_numbers
434         warn "grabbing copies for @volume_ids\n";
435         my $copy_req = $session->request(
436                         "open-ils.storage.asset.copy.search.call_number", @volume_ids );
437
438         my $copies = $copy_req->recv(); 
439
440         if(!$copies) {
441                 throw OpenSRF::EX::ERROR ("no response from storage for copy search");
442         }
443
444         if($copies->isa("Error")) {
445                 throw $copies ($copies->stringify);
446         }
447
448         warn "received data from copy search:\n";
449         warn Dumper $copies->content;
450
451         $session->finish();
452         $session->disconnect();
453         $session->kill_me();
454
455         return $copies->content;
456         
457 }
458
459
460
461
462 1;