From 32b886f4fb3115c290aff6ebd5795cb2c266fa54 Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 14 Mar 2005 18:05:26 +0000 Subject: [PATCH] moved some universal methods to AppUtils, Cat and Cat/Utils now rely on AppUtils. Added session checking to the 'tree commit' method git-svn-id: svn://svn.open-ils.org/ILS/trunk@337 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../perlmods/OpenILS/Application/AppUtils.pm | 66 +++++++++++ .../src/perlmods/OpenILS/Application/Cat.pm | 71 +++++------- .../perlmods/OpenILS/Application/Cat/Utils.pm | 109 ++++++++++-------- 3 files changed, 155 insertions(+), 91 deletions(-) create mode 100644 Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm diff --git a/Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm new file mode 100644 index 0000000000..25beef7253 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm @@ -0,0 +1,66 @@ +package OpenILS::Application::AppUtils; +use strict; use warnings; +use base qw/OpenSRF::Application/; + + +# --------------------------------------------------------------------------- +# Pile of utilty methods used accross applications. +# --------------------------------------------------------------------------- + + +# --------------------------------------------------------------------------- +# on sucess, returns the created session, on failure throws ERROR exception +# --------------------------------------------------------------------------- +sub start_db_session { + my $self = shift; + my $session = OpenSRF::AppSession->connect( "open-ils.storage" ); + my $trans_req = $session->request( "open-ils.storage.transaction.begin" ); + my $trans_resp = $trans_req->recv(); + if(ref($trans_resp) and $trans_resp->isa("Error")) { throw $trans_resp; } + if( ! $trans_resp->content() ) { + throw OpenSRF::ERROR ("Unable to Begin Transaction with database" ); + } + $trans_req->finish(); + return $session; +} + +# --------------------------------------------------------------------------- +# commits and destroys the session +# --------------------------------------------------------------------------- +sub commit_db_session { + my( $self, $session ) = @_; + + my $req = $session->request( "open-ils.storage.transaction.commit" ); + my $resp = $req->recv(); + if(ref($resp) and $resp->isa("Error")) { throw $resp; } + + $session->finish(); + $session->disconnect(); + $session->kill_me(); +} + + + +# --------------------------------------------------------------------------- +# Checks to see if a user is logged in. Returns the user record on success, +# throws an exception on error. +# --------------------------------------------------------------------------- +sub check_user_session { + my( $self, $user_session ) = @_; + my $method = $self->method_lookup("open-ils.auth.session.retrieve"); + if(!$method) { + throw OpenSRF::EX::PANIC ("Can't locate method 'open-ils.auth.session.retrieve'" ); + } + + my ($user) = $method->run( $user_session ); + if(!$user ) { + throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" ); + } + + return $user; + +} + + + +1; diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm index 441e1fed69..065974b074 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm @@ -1,5 +1,6 @@ use strict; use warnings; package OpenILS::Application::Cat; +use OpenILS::Application::AppUtils; use OpenSRF::Application; use OpenILS::Application::Cat::Utils; use base qw/OpenSRF::Application/; @@ -27,6 +28,7 @@ __PACKAGE__->register_method( ); sub biblio_record_tree_retrieve { + my( $self, $client, $recordid ) = @_; warn "In retrieve " . time() . "\n"; @@ -46,8 +48,6 @@ sub biblio_record_tree_retrieve { } return undef unless $marcxml; -#use Data::Dumper; -# warn $marcxml->marc; my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc ); my $tree = $utils->nodeset2tree( $nodes->nodeset ); @@ -56,21 +56,21 @@ sub biblio_record_tree_retrieve { return $tree; } - __PACKAGE__->register_method( method => "biblio_record_tree_commit", api_name => "open-ils.cat.biblio.record.tree.commit", - argc => 1, + argc => 2, #(session_id, biblio_tree ) note => "Walks the tree and commits any changed nodes " . "adds any new nodes, and deletes any deleted nodes", ); - sub biblio_record_tree_commit { - my( $self, $client, $tree ) = @_; + my( $self, $user_session, $client, $tree ) = @_; new Fieldmapper::biblio::record_node ($tree); + $self->OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error + # capture the doc id my $docid = $tree->owner_doc(); @@ -78,9 +78,6 @@ sub biblio_record_tree_commit { my $nodeset = $utils->tree2nodeset($tree); $nodeset = $utils->clean_nodeset( $nodeset ); - use Data::Dumper; - warn Dumper $nodeset; - if(!defined($docid)) { # be sure for my $node (@$nodeset) { $docid = $node->owner_doc(); @@ -95,41 +92,26 @@ sub biblio_record_tree_commit { $biblio->id( $docid ); $biblio->marc( $marcxml->toString() ); - use Data::Dumper; - warn "Biblio Object\n"; - warn Dumper $biblio; - my $session = $utils->start_db_session(); warn "Sending updated doc $docid to db\n"; - my $req = $session->request( - "open-ils.storage.biblio.record_marc.update", $biblio ); + my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio ); my $status = $req->recv(); if(ref($status) and $status->isa("Error")) { - warn " +++++++ Document Update Failed"; - warn $status->stringify() . "\n"; throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ; } - $utils->commit_db_session( $session ); - warn "Update Successful\n"; - - - # commit the altered nodeset nodes to the db - #my $hash = $utils->commit_nodeset( $nodeset ); - # retrieve the altered tree back from the db and return it + $utils->commit_db_session( $session ); my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" ); + if(!$method) { throw OpenSRF::EX::PANIC ("Unable to find method open-ils.cat.biblio.record.tree.retrieve"); } my ($ans) = $method->run( $docid ); - warn "=================================================================\n"; - warn "=================================================================\n"; - use Data::Dumper; - warn Dumper $ans; + return $ans; } @@ -137,13 +119,14 @@ __PACKAGE__->register_method( method => "biblio_mods_slim_retrieve", api_name => "open-ils.cat.biblio.mods.slim.retrieve", argc => 1, - note => "Returns the displayable data from the MODS record with a given ID", + note => "Returns the displayable data from the MODS record with a given IDs " . + "The first ID provided is considered the 'master' document, which means that " . + "it's author, subject, etc. will be used. Subjects are gathered from all docs." ); sub biblio_mods_slim_retrieve { - my( $self, $client, $recordid ) = @_; + my( $self, $client, @recordids ) = @_; - #my $name = "open-ils.storage.biblio.record_entry.nodeset.retrieve"; my $name = "open-ils.storage.biblio.record_marc.retrieve"; my $method = $self->method_lookup($name); @@ -151,19 +134,27 @@ sub biblio_mods_slim_retrieve { throw OpenSRF::EX::PANIC ("Could not lookup method $name"); } - # grab the marc record - my ($marcxml) = $method->run($recordid); - if(!$marcxml) { warn "Nothing from storage"; return undef; } + my $u = $utils->new(); + my $start = 1; - if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) { - throw $marcxml ($marcxml->stringify());; - } + for my $id (@recordids) { + my ($marcxml) = $method->run($id); + if(!$marcxml) { warn "Nothing from storage"; return undef; } - my $u = $utils->new(); - $u->start_mods_batch( $marcxml->marc ); + if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) { + throw $marcxml ($marcxml->stringify());; + } + + if($start) { + $u->start_mods_batch( $marcxml->marc ); + $start = 0; + } else { + $u->push_mods_batch( $marcxml->marc ); + } + } my $mods = $u->finish_mods_batch(); - return $u->mods_perl_to_mods_slim( $mods ); + return $mods; } diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat/Utils.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat/Utils.pm index cda9dc6f79..9e18815934 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Cat/Utils.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat/Utils.pm @@ -1,17 +1,23 @@ package OpenILS::Application::Cat::Utils; use strict; use warnings; +use OpenILS::Application::AppUtils; use OpenILS::Utils::Fieldmapper; use XML::LibXML; use XML::LibXSLT; use OpenSRF::Utils::SettingsParser; use OpenILS::Utils::FlatXML; +use OpenILS::Application::WORM; +my $utils = OpenILS::Application::Cat::Utils->new(); my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $xslt_doc = $parser->parse_file( "/pines/cvs/ILS/Open-ILS/xsl/MARC21slim2MODS.xsl" ); my $mods_sheet = $xslt->parse_stylesheet( $xslt_doc ); +my $isbn_xpath = "//mods:mods/mods:identifier[\@type='isbn']"; +my $resource_xpath = "//mods:mods/mods:typeOfResource"; + sub new { my($class) = @_; @@ -62,8 +68,7 @@ sub tree2nodeset { new Fieldmapper::biblio::record_node ($child); if(!defined($child->parent_node)) { - $child->parent_node($node->intra_doc_id); - $child->ischanged(1); #just to be sure + $child->parent_node($node->intra_doc_id); $child->ischanged(1); #just to be sure } $self->tree2nodeset( $child, $newnodes ); @@ -89,68 +94,47 @@ sub clean_nodeset { return \@newnodes; } -# on sucess, returns the created session, on failure throws ERROR exception -sub start_db_session { - my $self = shift; - my $session = OpenSRF::AppSession->connect( "open-ils.storage" ); - my $trans_req = $session->request( "open-ils.storage.transaction.begin" ); - my $trans_resp = $trans_req->recv(); - if(ref($trans_resp) and $trans_resp->isa("Error")) { throw $trans_resp; } - if( ! $trans_resp->content() ) { - throw OpenSRF::ERROR ("Unable to Begin Transaction with database" ); - } - $trans_req->finish(); - return $session; -} - -# commits and destroys the session -sub commit_db_session { - my( $self, $session ) = @_; - - my $req = $session->request( "open-ils.storage.transaction.commit" ); - my $resp = $req->recv(); - if(ref($resp) and $resp->isa("Error")) { throw $resp; } - - $session->finish(); - $session->disconnect(); - $session->kill_me(); -} - # --------------------------------------------------------------------------- # Grabs the data 'we want' from the MODS doc and returns it in hash form # --------------------------------------------------------------------------- -sub mods_perl_to_mods_slim { +sub mods_values_to_mods_slim { my( $self, $modsperl ) = @_; my $title = ""; my $author = ""; + my $subject = []; - my $tmp = $modsperl->{titleInfo}; - if($tmp) { - if(ref($tmp) eq "ARRAY" ) { - $tmp = $tmp->[0]; - } - $title = $tmp->{title}; + my $tmp = $modsperl->{title}; + + if(!$tmp) { $title = ""; } + else { + ($title = $tmp->{proper}) || + ($title = $tmp->{translated}) || + ($title = $tmp->{abbreviated}) || + ($title = $tmp->{uniform}); } - if( $title and ref($title) eq "ARRAY" ) { - $title = $title->[0]; + + $tmp = $modsperl->{author}; + if(!$tmp) { $author = ""; } + else { + ($author = $tmp->{personal}) || + ($author = $tmp->{other}) || + ($author = $tmp->{corporate}) || + ($author = $tmp->{conference}); } - $tmp = $modsperl->{name}; - if($tmp) { - if(ref($tmp) eq "ARRAY" ) { - $tmp = $tmp->[0]; + $tmp = $modsperl->{subject}; + if(!$tmp) { $subject = []; } + else { + for my $key( keys %{$tmp}) { + push(@$subject, $tmp->{$key}) if $tmp->{$key}; } - $author = $tmp->{namePart}; - } - if($author and ref($author) eq "ARRAY") { - $author = $author->[0]; } - return { "title" => $title, "author" => $author }; + return { title => $title, author => $author, subject => $subject }; } @@ -167,22 +151,47 @@ sub _marcxml_to_perl { # --------------------------------------------------------------------------- # Initializes a MARC -> Unified MODS batch process # --------------------------------------------------------------------------- -sub start_mods_batch { +sub _start_mods_batch { my( $self, $master_doc ) = @_; $self->{master_doc} = $self->_marcxml_to_perl( $master_doc ); } +sub start_mods_batch { + my( $self, $master_doc ) = @_; + my $xmldoc = $parser->parse_string( $master_doc ); + my $mods = $mods_sheet->transform($xmldoc); + $self->{master_doc} = OpenILS::Application::WORM->modsdoc_to_values( $mods ); + $self->{master_doc} = $utils->mods_values_to_mods_slim( $self->{master_doc} ); + $self->{master_doc}->{isbn} = OpenILS::Application::WORM::_get_field_value( $mods, $isbn_xpath ); + $self->{master_doc}->{type_of_resource} = + [ OpenILS::Application::WORM::_get_field_value( $mods, $resource_xpath ) ]; +} + # --------------------------------------------------------------------------- # Takes a MARCXML string an adds it to the growing MODS doc # --------------------------------------------------------------------------- sub push_mods_batch { my( $self, $marcxml ) = @_; - my $xmlperl = $self->_marcxml_to_perl( $marcxml ); + + my $xmldoc = $parser->parse_string($marcxml); + my $mods = $mods_sheet->transform($xmldoc); + my $xmlperl = OpenILS::Application::WORM->modsdoc_to_values( $mods ); + $xmlperl = $utils->mods_values_to_mods_slim( $xmlperl ); + for my $subject( @{$xmlperl->{subject}} ) { push @{$self->{master_doc}->{subject}}, $subject; } + + push( @{$self->{master_doc}->{type_of_resource}}, + OpenILS::Application::WORM::_get_field_value( $mods, $resource_xpath )); + + if(!($self->{master_doc}->{isbn}) ) { + $self->{master_doc}->{isbn} = + OpenILS::Application::WORM::_get_field_value( $mods, $isbn_xpath ); + } } + # --------------------------------------------------------------------------- # Completes a MARC -> Unified MODS batch process and returns the perl hash # --------------------------------------------------------------------------- @@ -194,6 +203,4 @@ sub finish_mods_batch { } - - 1; -- 2.43.2