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 warn "MARCXML OBJECT\n";
56 throw OpenSRF::EX::ERROR
57 ("No record in database with id $recordid");
60 warn "building nodes\n";
61 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc );
62 warn "building tree\n";
63 my $tree = $utils->nodeset2tree( $nodes->nodeset );
64 warn "setting owner doc\n";
65 $tree->owner_doc( $marcxml->id() );
66 warn "returning from retrieve for $recordid\n-------------------------------------\n";
70 __PACKAGE__->register_method(
71 method => "biblio_record_tree_commit",
72 api_name => "open-ils.cat.biblio.record.tree.commit",
73 argc => 3, #(session_id, biblio_tree )
74 note => "Walks the tree and commits any changed nodes " .
75 "adds any new nodes, and deletes any deleted nodes",
78 sub biblio_record_tree_commit {
80 my( $self, $client, $user_session, $tree ) = @_;
81 new Fieldmapper::biblio::record_node ($tree);
83 throw OpenSRF::EX::InvalidArg
84 ("Not enough args to to open-ils.cat.biblio.record.tree.commit")
85 unless ( $user_session and $client and $tree );
87 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
90 my $docid = $tree->owner_doc();
92 # turn the tree into a nodeset
93 my $nodeset = $utils->tree2nodeset($tree);
94 $nodeset = $utils->clean_nodeset( $nodeset );
96 if(!defined($docid)) { # be sure
97 for my $node (@$nodeset) {
98 $docid = $node->owner_doc();
99 last if defined($docid);
103 # turn the nodeset into a doc
104 my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
106 my $biblio = Fieldmapper::biblio::record_marc->new();
107 $biblio->id( $docid );
109 $biblio->marc( $marcxml->toString() );
111 my $session = OpenILS::Application::AppUtils->start_db_session();
113 warn "Sending updated doc $docid to db\n";
114 my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
116 my $status = $req->recv();
117 if(ref($status) and $status->isa("Error")) {
118 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ;
121 OpenILS::Application::AppUtils->commit_db_session( $session );
123 my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
126 throw OpenSRF::EX::PANIC
127 ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
129 my ($ans) = $method->run( $docid );
134 __PACKAGE__->register_method(
135 method => "biblio_mods_slim_retrieve",
136 api_name => "open-ils.cat.biblio.mods.slim.retrieve",
138 note => "Returns the displayable data from the MODS record with a given IDs " .
139 "The first ID provided is considered the 'master' document, which means that " .
140 "it's author, subject, etc. will be used. Subjects are gathered from all docs."
143 sub biblio_mods_slim_retrieve {
145 my( $self, $client, @recordids ) = @_;
147 my $name = "open-ils.storage.biblio.record_marc.retrieve";
148 warn "looking up record_marc retrieve " . time() . "\n";
149 my $method = $self->method_lookup($name);
151 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
154 my $u = $utils->new();
158 =head new way, fix me
160 my $last_xml = undef;
161 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
163 # grab, process, wait, etc...
164 for my $id (@recordids) {
166 my $req = $session->request( $name, $id );
169 $u->start_mods_batch( $last_xml->marc );
172 $u->push_mods_batch( $last_xml->marc );
177 $last_xml = $req->recv;
178 if(UNIVERSAL::isa($last_xml,"OpenSRF::EX")) {
179 throw $last_xml ($last_xml->stringify());;
182 $last_xml = $last_xml->content;
185 if($last_xml) { #grab the last one
186 $u->push_mods_batch( $last_xml->marc );
190 $session->disconnect();
196 for my $id (@recordids) {
198 my ($marcxml) = $method->run($id);
199 warn "retrieved marcxml at " . time() . "\n";
200 if(!$marcxml) { warn "Nothing from storage"; return undef; }
202 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
203 throw $marcxml ($marcxml->stringify());;
207 $u->start_mods_batch( $marcxml->marc );
210 $u->push_mods_batch( $marcxml->marc );
214 warn "returning mods batch " . time . "\n";
215 my $mods = $u->finish_mods_batch();