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 warn "In retrieve " . time() . "\n";
35 my $name = "open-ils.storage.biblio.record_marc.retrieve";
36 my $method = $self->method_lookup($name);
39 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
42 my ($marcxml) = $method->run($recordid);
43 warn "After marxml retrieve " . time() . "\n";
46 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
50 return undef unless $marcxml;
52 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc );
53 my $tree = $utils->nodeset2tree( $nodes->nodeset );
54 $tree->owner_doc( $marcxml->id() );
55 warn "Returning Tree " . time() . "\n";
59 __PACKAGE__->register_method(
60 method => "biblio_record_tree_commit",
61 api_name => "open-ils.cat.biblio.record.tree.commit",
62 argc => 2, #(session_id, biblio_tree )
63 note => "Walks the tree and commits any changed nodes " .
64 "adds any new nodes, and deletes any deleted nodes",
67 sub biblio_record_tree_commit {
69 my( $self, $user_session, $client, $tree ) = @_;
70 new Fieldmapper::biblio::record_node ($tree);
72 $self->OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
75 my $docid = $tree->owner_doc();
77 # turn the tree into a nodeset
78 my $nodeset = $utils->tree2nodeset($tree);
79 $nodeset = $utils->clean_nodeset( $nodeset );
81 if(!defined($docid)) { # be sure
82 for my $node (@$nodeset) {
83 $docid = $node->owner_doc();
84 last if defined($docid);
88 # turn the nodeset into a doc
89 my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
91 my $biblio = Fieldmapper::biblio::record_marc->new();
92 $biblio->id( $docid );
93 $biblio->marc( $marcxml->toString() );
95 my $session = $utils->start_db_session();
97 warn "Sending updated doc $docid to db\n";
98 my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
100 my $status = $req->recv();
101 if(ref($status) and $status->isa("Error")) {
102 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ;
105 $utils->commit_db_session( $session );
107 my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
110 throw OpenSRF::EX::PANIC
111 ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
113 my ($ans) = $method->run( $docid );
118 __PACKAGE__->register_method(
119 method => "biblio_mods_slim_retrieve",
120 api_name => "open-ils.cat.biblio.mods.slim.retrieve",
122 note => "Returns the displayable data from the MODS record with a given IDs " .
123 "The first ID provided is considered the 'master' document, which means that " .
124 "it's author, subject, etc. will be used. Subjects are gathered from all docs."
127 sub biblio_mods_slim_retrieve {
129 my( $self, $client, @recordids ) = @_;
131 my $name = "open-ils.storage.biblio.record_marc.retrieve";
132 warn "looking up record_marc retrieve " . time() . "\n";
133 my $method = $self->method_lookup($name);
135 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
138 my $u = $utils->new();
142 =head new way, fix me
144 my $last_xml = undef;
145 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
147 # grab, process, wait, etc...
148 for my $id (@recordids) {
150 my $req = $session->request( $name, $id );
153 $u->start_mods_batch( $last_xml->marc );
156 $u->push_mods_batch( $last_xml->marc );
161 $last_xml = $req->recv;
162 if(UNIVERSAL::isa($last_xml,"OpenSRF::EX")) {
163 throw $last_xml ($last_xml->stringify());;
166 $last_xml = $last_xml->content;
169 if($last_xml) { #grab the last one
170 $u->push_mods_batch( $last_xml->marc );
174 $session->disconnect();
180 for my $id (@recordids) {
182 my ($marcxml) = $method->run($id);
183 warn "retrieved marcxml at " . time() . "\n";
184 if(!$marcxml) { warn "Nothing from storage"; return undef; }
186 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
187 throw $marcxml ($marcxml->stringify());;
191 $u->start_mods_batch( $marcxml->marc );
194 $u->push_mods_batch( $marcxml->marc );
198 warn "returning mods batch " . time . "\n";
199 my $mods = $u->finish_mods_batch();
205 __PACKAGE__->register_method(
206 method => "marc_test",
207 api_name => "open-ils.cat.biblio.record.tree.retrieve.test",
209 note => "Returns the tree associated with the nodeset of the given doc id"
215 my( $self, $client ) = @_;
216 my $doc = XML::LibXML->new()->parse_file( "/pines/ilsmods/Application/Cat/test_rec.xml" );
217 my $marcxml = $doc->toString();
218 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml );
219 my $tree = $utils->nodeset2tree( $nodes->nodeset );
220 $tree->owner_doc( 999999 );