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 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc );
61 my $tree = $utils->nodeset2tree( $nodes->nodeset );
62 $tree->owner_doc( $marcxml->id() );
68 __PACKAGE__->register_method(
69 method => "biblio_record_tree_commit",
70 api_name => "open-ils.cat.biblio.record.tree.commit",
71 argc => 3, #(session_id, biblio_tree )
72 note => "Walks the tree and commits any changed nodes " .
73 "adds any new nodes, and deletes any deleted nodes",
76 sub biblio_record_tree_commit {
78 my( $self, $client, $user_session, $tree ) = @_;
79 new Fieldmapper::biblio::record_node ($tree);
81 throw OpenSRF::EX::InvalidArg
82 ("Not enough args to to open-ils.cat.biblio.record.tree.commit")
83 unless ( $user_session and $client and $tree );
85 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
88 my $docid = $tree->owner_doc();
90 # turn the tree into a nodeset
91 my $nodeset = $utils->tree2nodeset($tree);
92 $nodeset = $utils->clean_nodeset( $nodeset );
94 if(!defined($docid)) { # be sure
95 for my $node (@$nodeset) {
96 $docid = $node->owner_doc();
97 last if defined($docid);
101 # turn the nodeset into a doc
102 my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
104 my $biblio = Fieldmapper::biblio::record_marc->new();
105 $biblio->id( $docid );
107 $biblio->marc( $marcxml->toString() );
109 my $session = OpenILS::Application::AppUtils->start_db_session();
111 warn "Sending updated doc $docid to db\n";
112 my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
114 my $status = $req->recv();
115 if(ref($status) and $status->isa("Error")) {
116 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ;
119 OpenILS::Application::AppUtils->commit_db_session( $session );
121 my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
124 throw OpenSRF::EX::PANIC
125 ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
127 my ($ans) = $method->run( $docid );
132 __PACKAGE__->register_method(
133 method => "biblio_mods_slim_retrieve",
134 api_name => "open-ils.cat.biblio.mods.slim.retrieve",
136 note => "Returns the displayable data from the MODS record with a given IDs " .
137 "The first ID provided is considered the 'master' document, which means that " .
138 "it's author, subject, etc. will be used. Subjects are gathered from all docs."
141 sub biblio_mods_slim_retrieve {
143 my( $self, $client, @recordids ) = @_;
145 my $name = "open-ils.storage.biblio.record_marc.retrieve";
146 warn "looking up record_marc retrieve " . time() . "\n";
147 my $method = $self->method_lookup($name);
149 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
152 my $u = $utils->new();
156 =head new way, fix me
158 my $last_xml = undef;
159 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
161 # grab, process, wait, etc...
162 for my $id (@recordids) {
164 my $req = $session->request( $name, $id );
167 $u->start_mods_batch( $last_xml->marc );
170 $u->push_mods_batch( $last_xml->marc );
175 $last_xml = $req->recv;
176 if(UNIVERSAL::isa($last_xml,"OpenSRF::EX")) {
177 throw $last_xml ($last_xml->stringify());;
180 $last_xml = $last_xml->content;
183 if($last_xml) { #grab the last one
184 $u->push_mods_batch( $last_xml->marc );
188 $session->disconnect();
194 for my $id (@recordids) {
196 my ($marcxml) = $method->run($id);
197 warn "retrieved marcxml at " . time() . "\n";
198 if(!$marcxml) { warn "Nothing from storage"; return undef; }
200 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
201 throw $marcxml ($marcxml->stringify());;
205 $u->start_mods_batch( $marcxml->marc );
208 $u->push_mods_batch( $marcxml->marc );
212 warn "returning mods batch " . time . "\n";
213 my $mods = $u->finish_mods_batch();
219 __PACKAGE__->register_method(
220 method => "biblio_record_record_metadata",
221 api_name => "open-ils.cat.biblio.record.metadata.retrieve",
222 argc => 1, #(session_id, biblio_tree )
223 note => "Walks the tree and commits any changed nodes " .
224 "adds any new nodes, and deletes any deleted nodes",
227 sub biblio_record_record_metadata {
228 my( $self, $client, @ids ) = @_;
230 if(!@ids){return undef;}
232 my $session = OpenSRF::AppSession->create("open-ils.storage");
233 my $request = $session->request(
234 "open-ils.storage.biblio.record_entry.batch.retrieve", @ids );
238 while( my $response = $request->recv() ) {
241 throw OpenSRF::EX::ERROR ("No Response from Storage");
243 if($response->isa("Error")) {
244 throw $response ($response->stringify);
247 my $record_entry = $response->content;
249 my $creator = $record_entry->creator;
250 my $editor = $record_entry->editor;
252 my $info_session = OpenSRF::AppSession->create("open-ils.storage");
254 # grab the creator's name
255 my $creator_req = $info_session->request(
256 "open-ils.storage.actor.user.retrieve", $creator );
257 my $creator_resp = $creator_req->recv();
258 if(!$creator_resp) { $creator = ""; }
259 if($creator_resp->isa("Error")){
260 throw $creator_resp ($creator_resp);
262 $creator = $creator_resp->content;
264 $creator = $creator->usrid;
265 } else { $creator = ""; }
267 my $editor_req = $info_session->request(
268 "open-ils.storage.actor.user.retrieve", $editor );
269 my $editor_resp = $editor_req->recv();
270 if(!$editor_resp) { $editor = ""; }
271 if($editor_resp->isa("Error")){
272 throw $editor_resp ($editor_resp);
274 $editor = $editor_resp->content;
276 $editor = $editor->usrid;
277 } else { $editor = ""; }
280 $info_session->disconnect();
281 $info_session->kill_me();
283 $record_entry->creator( $creator );
284 $record_entry->editor( $editor );
286 push @$results, $record_entry;
290 $session->disconnect();