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);
10 use OpenILS::Utils::Fieldmapper;
12 my $utils = "OpenILS::Application::Cat::Utils";
16 OpenSRF::Application->method_lookup( "blah" );
18 warn "Child Init Failed: " . shift() . "\n";
23 __PACKAGE__->register_method(
24 method => "biblio_record_tree_retrieve",
25 api_name => "open-ils.cat.biblio.record.tree.retrieve",
27 note => "Returns the tree associated with the nodeset of the given doc id"
30 sub biblio_record_tree_retrieve {
32 my( $self, $client, $recordid ) = @_;
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";
41 throw OpenSRF::EX::ERROR ("No record in database with id $recordid");
44 if( $response->isa("OpenSRF::EX")) {
45 throw $response ($response->stringify);
48 warn "grabbing content in retrieve\n";
49 my $marcxml = $response->content;
52 throw OpenSRF::EX::ERROR
53 ("No record in database with id $recordid");
57 $session->disconnect();
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 );
65 $tree->owner_doc( $marcxml->id() );
67 warn "returning tree\n";
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",
80 sub biblio_record_tree_commit {
82 my( $self, $client, $user_session, $tree ) = @_;
83 new Fieldmapper::biblio::record_node ($tree);
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 );
92 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
95 my $docid = $tree->owner_doc();
97 # turn the tree into a nodeset
98 my $nodeset = $utils->tree2nodeset($tree);
99 $nodeset = $utils->clean_nodeset( $nodeset );
101 if(!defined($docid)) { # be sure
102 for my $node (@$nodeset) {
103 $docid = $node->owner_doc();
104 last if defined($docid);
108 # turn the nodeset into a doc
109 my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
111 my $biblio = Fieldmapper::biblio::record_marc->new();
112 $biblio->id( $docid );
113 $biblio->marc( $marcxml->toString() );
115 warn "Starting db session\n";
116 my $session = OpenILS::Application::AppUtils->start_db_session();
118 my $x = _update_record_metadata( $session, { user => $user_obj, docid => $docid } );
119 OpenILS::Application::AppUtils->rollback_db_session($session) unless $x;
121 warn "Sending updated doc $docid to db\n";
122 my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
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");
133 OpenILS::Application::AppUtils->commit_db_session( $session );
135 my $method = $self->method_lookup(
136 "open-ils.cat.biblio.record.tree.retrieve" );
138 my ($ans) = $method->run( $docid );
140 warn "Finished updating, returning tree to client";
141 $client->respond_complete($ans);
146 # Send the doc to the wormer for wormizing
147 warn "Starting worm session\n";
148 my $wses = OpenSRF::AppSession->create("open-ils.worm");
154 my $wreq = $wses->request(
155 "open-ils.worm.wormize.marc", $docid, $marcxml->toString );
156 warn "Calling worm receive\n";
157 $wresp = $wreq->recv();
159 if( $wresp && $wresp->can("content") and $wresp->content ) {
165 warn "Looping in worm call\n";
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);
180 OpenILS::Application::AppUtils->rollback_db_session($session);
182 throw OpenSRF::EX::ERROR ("Wormizing Failed for $docid" );
188 warn "Done wormizing\n";
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",
202 sub biblio_record_record_metadata {
203 my( $self, $client, @ids ) = @_;
205 if(!@ids){return undef;}
207 my $session = OpenSRF::AppSession->create("open-ils.storage");
208 my $request = $session->request(
209 "open-ils.storage.biblio.record_entry.batch.retrieve", @ids );
213 while( my $response = $request->recv() ) {
216 throw OpenSRF::EX::ERROR ("No Response from Storage");
218 if($response->isa("Error")) {
219 throw $response ($response->stringify);
222 my $record_entry = $response->content;
224 my $creator = $record_entry->creator;
225 my $editor = $record_entry->editor;
227 ($creator, $editor) = _get_userid_by_id($creator, $editor) || ("","");
229 $record_entry->creator( $creator );
230 $record_entry->editor( $editor );
232 push @$results, $record_entry;
236 $session->disconnect();
244 sub _get_userid_by_id {
249 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
250 my $request = $session->request(
251 "open-ils.storage.actor.user.batch.retrieve", @ids );
253 my $response = $request->recv();
254 if(!$response) { return undef; }
256 if($response->isa("Error")){
257 throw $response ($response);
260 for my $u (@{$response->content}) {
262 push @users, $u->usrid;
266 $session->disconnect;
272 # open-ils.storage.actor.user.search.usrid
274 sub _get_id_by_userid {
279 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
280 my $request = $session->request(
281 "open-ils.storage.actor.user.search.usrid", @users );
283 my $response = $request->recv();
284 if(!$response) { return undef; }
286 if($response->isa("Error")){
287 throw $response ($response);
290 for my $u (@{$response->content}) {
296 $session->disconnect;
303 # commits metadata objects to the db
304 sub _update_record_metadata {
306 my ($session, @docs ) = @_;
308 for my $doc (@docs) {
310 my $user_obj = $doc->{user};
311 my $docid = $doc->{docid};
313 warn "Updating metata for doc $docid\n";
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();
323 throw OpenSRF::EX::ERROR ("No meta info returned for biblio $docid");
325 if($meta->isa("Error")) {
326 throw $meta ($meta->stringify);
329 $meta = $meta->content;
330 my ($id) = _get_id_by_userid($user_obj->usrid);
331 warn "got $id from _get_id_by_userid\n";
334 $user_request->finish;
335 $user_session->disconnect;
336 $user_session->kill_me;
337 # -------------------------------------
339 warn "Grabbed the record, updating and moving on\n";
341 my $request = $session->request(
342 "open-ils.storage.biblio.record_entry.update", $meta );
344 my $response = $request->recv();
346 throw OpenSRF::EX::ERROR
347 ("Error commit record metadata for " . $meta->id);
350 if($response->isa("Error")){
351 throw $response ($response->stringify);
357 warn "committing metarecord update\n";
365 __PACKAGE__->register_method(
366 method => "retrieve_copies",
367 api_name => "open-ils.cat.asset.copy.retrieve",
368 argc => 2, #(user_session, record_id)
370 Returns the copies for a given bib record and for the users home library
374 sub retrieve_copies {
376 my( $self, $client, $user_session, $docid ) = @_;
383 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
385 warn "user session is ok\n";
386 warn "Home Lib: " . $user_obj->home_ou . "\n";
388 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
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 );
395 my $org_unit = $request->recv();
397 throw OpenSRF::EX::ERROR
398 ("No response from storage for org unit search");
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";
404 # ------------------------------------------------------
407 # ------------------------------------------------------
408 # grab all the volumes for the given record and location
409 my $search_hash = { record => $docid, owning_lib => $location };
411 warn "Using search hash: " . Dumper( $search_hash );
413 warn "searching call_numbers for $docid and $location\n";
415 $request = $session->request(
416 "open-ils.storage.asset.call_number.search", $search_hash );
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;
429 # ------------------------------------------------------
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 );
438 my $copies = $copy_req->recv();
441 throw OpenSRF::EX::ERROR ("no response from storage for copy search");
444 if($copies->isa("Error")) {
445 throw $copies ($copies->stringify);
448 warn "received data from copy search:\n";
449 warn Dumper $copies->content;
452 $session->disconnect();
455 return $copies->content;