]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
moving forward, added wormizing
[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         if(!$marcxml) {
52                 throw OpenSRF::EX::ERROR 
53                         ("No record in database with id $recordid");
54         }
55
56         $request->finish();
57         $session->disconnect();
58         $session->kill_me();
59
60         warn "turning into nodeset\n";
61         my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc ); 
62         warn "turning nodeset into tree\n";
63         my $tree = $utils->nodeset2tree( $nodes->nodeset );
64
65         $tree->owner_doc( $marcxml->id() );
66
67         warn "returning tree\n";
68
69         return $tree;
70 }
71
72 __PACKAGE__->register_method(
73         method  => "biblio_record_tree_commit",
74         api_name        => "open-ils.cat.biblio.record.tree.commit",
75         argc            => 3, #(session_id, biblio_tree ) 
76         note            => "Walks the tree and commits any changed nodes " .
77                                         "adds any new nodes, and deletes any deleted nodes",
78 );
79
80 sub biblio_record_tree_commit {
81
82         my( $self, $client, $user_session,  $tree ) = @_;
83         new Fieldmapper::biblio::record_node ($tree);
84
85         throw OpenSRF::EX::InvalidArg 
86                 ("Not enough args to to open-ils.cat.biblio.record.tree.commit")
87                 unless ( $user_session and $client and $tree );
88
89         my $user_obj = 
90                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
91
92         $client->respond("keepalive");
93
94         # capture the doc id
95         my $docid = $tree->owner_doc();
96
97         # turn the tree into a nodeset
98         my $nodeset = $utils->tree2nodeset($tree);
99         $nodeset = $utils->clean_nodeset( $nodeset );
100
101         if(!defined($docid)) { # be sure
102                 for my $node (@$nodeset) {
103                         $docid = $node->owner_doc();
104                         last if defined($docid);
105                 }
106         }
107
108         # turn the nodeset into a doc
109         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
110
111         my $biblio =  Fieldmapper::biblio::record_marc->new();
112         $biblio->id( $docid );
113         $biblio->marc( $marcxml->toString() );
114
115         warn "Starting db session\n";
116         my $session = OpenILS::Application::AppUtils->start_db_session();
117
118         my $x = _update_record_metadata( $session, { user => $user_obj, docid => $docid } );
119         OpenILS::Application::AppUtils->rollback_db_session($session) unless $x;
120         $client->respond("keepalive");
121
122
123         warn "Sending updated doc $docid to db\n";
124         my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
125
126         my $status = $req->recv();
127         if( !$status || $status->isa("Error") || ! $status->content) {
128                 OpenILS::Application::AppUtils->rollback_db_session($session);
129                 if($status->isa("Error")) { throw $status ($status); }
130                 throw OpenSRF::EX::ERROR ("Error updating biblio record");
131         }
132         $req->finish();
133         
134         # Send the doc to the wormer for wormizing
135         warn "Starting worm session\n";
136         my $wses = OpenSRF::AppSession->create("open-ils.worm");
137
138         my $success = 0;
139         my $wresp;
140         for(0..1) {
141
142                 my $wreq = $wses->request( 
143                                 "open-ils.worm.wormize.marc", $docid, $marcxml->toString );
144                 $wresp = $wreq->recv();
145
146                 if( $wresp && $wresp->can("content") and $wresp->content ) {
147                         $success = 1;
148                         $wreq->finish();
149                         last;
150                 }
151
152                 warn "Looping in worm call\n";
153                 $wreq->finish();
154         }
155
156         if( !$success ) {
157
158
159                 if($wresp and $wresp->isa("Error") ) {
160                         $wses->disconnect;
161                         $wses->kill_me;
162                         throw $wresp ($wresp->stringify);
163                 }
164
165                 $wses->disconnect;
166                 $wses->kill_me;
167
168                 OpenILS::Application::AppUtils->rollback_db_session($session);
169
170                 return undef;
171         }
172
173
174         $client->respond("keepalive");
175
176         $wses->disconnect;
177         $wses->kill_me;
178
179         warn "committing db session\n";
180         OpenILS::Application::AppUtils->commit_db_session( $session );
181
182         my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
183
184         if(!$method) {
185                 throw OpenSRF::EX::PANIC 
186                         ("Unable to find method open-ils.cat.biblio.record.tree.retrieve"); }
187
188         my ($ans) = $method->run( $docid );
189
190         warn "Returning from commit\n";
191
192         return $ans;
193 }
194
195
196
197 __PACKAGE__->register_method(
198         method  => "biblio_record_record_metadata",
199         api_name        => "open-ils.cat.biblio.record.metadata.retrieve",
200         argc            => 1, #(session_id, biblio_tree ) 
201         note            => "Walks the tree and commits any changed nodes " .
202                                         "adds any new nodes, and deletes any deleted nodes",
203 );
204
205 sub biblio_record_record_metadata {
206         my( $self, $client, @ids ) = @_;
207
208         if(!@ids){return undef;}
209
210         my $session = OpenSRF::AppSession->create("open-ils.storage");
211         my $request = $session->request( 
212                         "open-ils.storage.biblio.record_entry.batch.retrieve", @ids );
213
214         my $results = [];
215
216         while( my $response = $request->recv() ) {
217
218                 if(!$response) {
219                         throw OpenSRF::EX::ERROR ("No Response from Storage");
220                 }
221                 if($response->isa("Error")) {
222                         throw $response ($response->stringify);
223                 }
224
225                 my $record_entry = $response->content;
226
227                 my $creator = $record_entry->creator;
228                 my $editor      = $record_entry->editor;
229
230                 ($creator, $editor) = _get_userid_by_id($creator, $editor) || ("","");
231
232                 $record_entry->creator( $creator );
233                 $record_entry->editor( $editor );
234
235                 push @$results, $record_entry;
236
237         }
238
239         $session->disconnect();
240         $session->kill_me();
241
242         return $results;
243
244 }
245
246 # gets the username
247 sub _get_userid_by_id {
248
249         my @ids = @_;
250         my @users;
251
252         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
253         my $request = $session->request( 
254                 "open-ils.storage.actor.user.batch.retrieve", @ids );
255
256         my $response = $request->recv();
257         if(!$response) { return undef; }
258
259         if($response->isa("Error")){
260                 throw $response ($response);
261         }
262
263         for my $u (@{$response->content}) {
264                 next unless ref($u);
265                 push @users, $u->usrid;
266         }
267
268         $request->finish;
269         $session->disconnect;
270         $session->kill_me();
271
272         return @users;
273 }
274
275 # open-ils.storage.actor.user.search.usrid
276
277 sub _get_id_by_userid {
278
279         my @users = @_;
280         my @ids;
281
282         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
283         my $request = $session->request( 
284                 "open-ils.storage.actor.user.search.usrid", @users );
285
286         my $response = $request->recv();
287         if(!$response) { return undef; }
288
289         if($response->isa("Error")){
290                 throw $response ($response);
291         }
292
293         for my $u (@{$response->content}) {
294                 next unless ref($u);
295                 push @ids, $u->id();
296         }
297
298         $request->finish;
299         $session->disconnect;
300         $session->kill_me();
301
302         return @ids;
303 }
304
305
306 # commits metadata objects to the db
307 sub _update_record_metadata {
308
309         my ($session, @docs ) = @_;
310
311         for my $doc (@docs) {
312
313                 my $user_obj = $doc->{user};
314                 my $docid = $doc->{docid};
315
316                 warn "Updating metata for doc $docid\n";
317
318                 # ----------------------------------------
319                 # grab the meta information  and update it
320                 my $user_session = OpenSRF::AppSession->create("open-ils.storage");
321                 my $user_request = $user_session->request( 
322                         "open-ils.storage.biblio.record_entry.retrieve", $docid );
323                 my $meta = $user_request->recv();
324
325                 if(!$meta) {
326                         throw OpenSRF::EX::ERROR ("No meta info returned for biblio $docid");
327                 }
328                 if($meta->isa("Error")) {
329                         throw $meta ($meta->stringify);
330                 }
331
332                 $meta = $meta->content;
333                 my ($id) = _get_id_by_userid($user_obj->usrid);
334                 warn "got $id from _get_id_by_userid\n";
335                 $meta->editor($id);
336
337                 $user_request->finish;
338                 $user_session->disconnect;
339                 $user_session->kill_me;
340                 # -------------------------------------
341                 
342                 warn "Grabbed the record, updating and moving on\n";
343
344                 my $request = $session->request( 
345                         "open-ils.storage.biblio.record_entry.update", $meta );
346
347                 my $response = $request->recv();
348                 if(!$response) { 
349                         throw OpenSRF::EX::ERROR 
350                                 ("Error commit record metadata for " . $meta->id);
351                 }
352
353                 if($response->isa("Error")){ 
354                         throw $response ($response->stringify); 
355                 }
356
357                 $request->finish;
358         }
359
360         warn "committing metarecord update\n";
361         return 1;
362 }
363
364
365
366
367
368 1;