]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
fixed an exception
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat.pm
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);
9 use JSON;
10 use OpenILS::Utils::Fieldmapper;
11
12 my $utils = "OpenILS::Application::Cat::Utils";
13
14 sub _child_init {
15         try {
16                 OpenSRF::Application->method_lookup( "blah" );
17         } catch Error with { 
18                 warn "Child Init Failed: " . shift() . "\n";
19         };
20 }
21
22
23 __PACKAGE__->register_method(
24         method  => "biblio_record_tree_retrieve",
25         api_name        => "open-ils.cat.biblio.record.tree.retrieve",
26         argc            => 1, 
27         note            => "Returns the tree associated with the nodeset of the given doc id"
28 );
29
30 sub biblio_record_tree_retrieve {
31
32         my( $self, $client, $recordid ) = @_;
33
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";
39
40         if(!$response) { 
41                 throw OpenSRF::EX::ERROR ("No record in database with id $recordid");
42         }
43
44         if( $response->isa("OpenSRF::EX")) {
45                 throw $response ($response->stringify);
46         }
47
48         warn "grabbing content in retrieve\n";
49         my $marcxml = $response->content;
50
51         use Data::Dumper;
52         warn "MARCXML OBJECT\n";
53         warn Dumper $marcxml;
54
55         if(!$marcxml) {
56                 throw OpenSRF::EX::ERROR 
57                         ("No record in database with id $recordid");
58         }
59
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";
67         return $tree;
68 }
69
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",
76 );
77
78 sub biblio_record_tree_commit {
79
80         my( $self, $client, $user_session,  $tree ) = @_;
81         new Fieldmapper::biblio::record_node ($tree);
82
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 );
86
87         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
88
89         # capture the doc id
90         my $docid = $tree->owner_doc();
91
92         # turn the tree into a nodeset
93         my $nodeset = $utils->tree2nodeset($tree);
94         $nodeset = $utils->clean_nodeset( $nodeset );
95
96         if(!defined($docid)) { # be sure
97                 for my $node (@$nodeset) {
98                         $docid = $node->owner_doc();
99                         last if defined($docid);
100                 }
101         }
102
103         # turn the nodeset into a doc
104         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
105
106         my $biblio =  Fieldmapper::biblio::record_marc->new();
107         $biblio->id( $docid );
108
109         $biblio->marc( $marcxml->toString() );
110
111         my $session = OpenILS::Application::AppUtils->start_db_session();
112
113         warn "Sending updated doc $docid to db\n";
114         my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
115
116         my $status = $req->recv();
117         if(ref($status) and $status->isa("Error")) { 
118                 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ; 
119         }
120
121         OpenILS::Application::AppUtils->commit_db_session( $session );
122
123         my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
124
125         if(!$method) {
126                 throw OpenSRF::EX::PANIC 
127                         ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
128         }
129         my ($ans) = $method->run( $docid );
130
131         return $ans;
132 }
133
134 __PACKAGE__->register_method(
135         method  => "biblio_mods_slim_retrieve",
136         api_name        => "open-ils.cat.biblio.mods.slim.retrieve",
137         argc            => 1, 
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."
141 );
142
143 sub biblio_mods_slim_retrieve {
144
145         my( $self, $client, @recordids ) = @_;
146
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);
150         unless($method) {
151                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
152         }
153
154         my $u = $utils->new();
155         my $start = 1;
156
157
158 =head new way, fix me
159
160         my $last_xml    = undef;
161         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
162
163         # grab, process, wait, etc...
164         for my $id (@recordids) {
165                 
166                 my $req = $session->request( $name, $id );
167                 if($last_xml) {
168                         if($start) {
169                                 $u->start_mods_batch( $last_xml->marc );
170                                 $start = 0;
171                         } else {
172                                 $u->push_mods_batch( $last_xml->marc );
173                         }
174                         $last_xml = undef;
175                 }
176                 $req->wait_complete;
177                 $last_xml = $req->recv;
178                 if(UNIVERSAL::isa($last_xml,"OpenSRF::EX")) {
179                         throw $last_xml ($last_xml->stringify());;
180                 }
181                 $req->finish();
182                 $last_xml = $last_xml->content;
183         }
184
185         if($last_xml) { #grab the last one
186                 $u->push_mods_batch( $last_xml->marc );
187         }
188
189         $session->finish();
190         $session->disconnect();
191         $session->kill_me();
192
193 =cut
194
195
196         for my $id (@recordids) {
197
198                 my ($marcxml) = $method->run($id);
199                 warn "retrieved marcxml at " . time() . "\n";
200                 if(!$marcxml) { warn "Nothing from storage"; return undef; }
201
202                 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
203                         throw $marcxml ($marcxml->stringify());;
204                 }
205
206                 if($start) {
207                         $u->start_mods_batch( $marcxml->marc );
208                         $start = 0;
209                 } else {
210                         $u->push_mods_batch( $marcxml->marc );
211                 }
212         }
213
214         warn "returning mods batch " . time . "\n";
215         my $mods = $u->finish_mods_batch();
216         return $mods;
217
218 }
219
220 1;