]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
fixing so jason can search
[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         warn "In retrieve " . time() . "\n";
35         my $name = "open-ils.storage.biblio.record_marc.retrieve";
36         my $method = $self->method_lookup($name);
37
38         unless($method) {
39                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
40         }
41
42         my ($marcxml) = $method->run($recordid);
43         warn "After marxml retrieve " . time() . "\n";
44
45
46         if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
47                 throw $marcxml;
48         }
49
50         return undef unless $marcxml;
51
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";
56         return $tree;
57 }
58
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",
65 );
66
67 sub biblio_record_tree_commit {
68
69         my( $self, $user_session, $client, $tree ) = @_;
70         new Fieldmapper::biblio::record_node ($tree);
71
72         $self->OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
73
74         # capture the doc id
75         my $docid = $tree->owner_doc();
76
77         # turn the tree into a nodeset
78         my $nodeset = $utils->tree2nodeset($tree);
79         $nodeset = $utils->clean_nodeset( $nodeset );
80
81         if(!defined($docid)) { # be sure
82                 for my $node (@$nodeset) {
83                         $docid = $node->owner_doc();
84                         last if defined($docid);
85                 }
86         }
87
88         # turn the nodeset into a doc
89         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
90
91         my $biblio =  Fieldmapper::biblio::record_marc->new();
92         $biblio->id( $docid );
93         $biblio->marc( $marcxml->toString() );
94
95         my $session = $utils->start_db_session();
96
97         warn "Sending updated doc $docid to db\n";
98         my $req = $session->request( "open-ils.storage.biblio.record_marc.update", $biblio );
99
100         my $status = $req->recv();
101         if(ref($status) and $status->isa("Error")) { 
102                 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ; 
103         }
104
105         $utils->commit_db_session( $session );
106
107         my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
108
109         if(!$method) {
110                 throw OpenSRF::EX::PANIC 
111                         ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
112         }
113         my ($ans) = $method->run( $docid );
114
115         return $ans;
116 }
117
118 __PACKAGE__->register_method(
119         method  => "biblio_mods_slim_retrieve",
120         api_name        => "open-ils.cat.biblio.mods.slim.retrieve",
121         argc            => 1, 
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."
125 );
126
127 sub biblio_mods_slim_retrieve {
128
129         my( $self, $client, @recordids ) = @_;
130
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);
134         unless($method) {
135                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
136         }
137
138         my $u = $utils->new();
139         my $start = 1;
140
141
142 =head new way, fix me
143
144         my $last_xml    = undef;
145         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
146
147         # grab, process, wait, etc...
148         for my $id (@recordids) {
149                 
150                 my $req = $session->request( $name, $id );
151                 if($last_xml) {
152                         if($start) {
153                                 $u->start_mods_batch( $last_xml->marc );
154                                 $start = 0;
155                         } else {
156                                 $u->push_mods_batch( $last_xml->marc );
157                         }
158                         $last_xml = undef;
159                 }
160                 $req->wait_complete;
161                 $last_xml = $req->recv;
162                 if(UNIVERSAL::isa($last_xml,"OpenSRF::EX")) {
163                         throw $last_xml ($last_xml->stringify());;
164                 }
165                 $req->finish();
166                 $last_xml = $last_xml->content;
167         }
168
169         if($last_xml) { #grab the last one
170                 $u->push_mods_batch( $last_xml->marc );
171         }
172
173         $session->finish();
174         $session->disconnect();
175         $session->kill_me();
176
177 =cut
178
179
180         for my $id (@recordids) {
181
182                 my ($marcxml) = $method->run($id);
183                 warn "retrieved marcxml at " . time() . "\n";
184                 if(!$marcxml) { warn "Nothing from storage"; return undef; }
185
186                 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
187                         throw $marcxml ($marcxml->stringify());;
188                 }
189
190                 if($start) {
191                         $u->start_mods_batch( $marcxml->marc );
192                         $start = 0;
193                 } else {
194                         $u->push_mods_batch( $marcxml->marc );
195                 }
196         }
197
198         warn "returning mods batch " . time . "\n";
199         my $mods = $u->finish_mods_batch();
200         return $mods;
201
202 }
203
204
205 __PACKAGE__->register_method(
206         method  => "marc_test",
207         api_name        => "open-ils.cat.biblio.record.tree.retrieve.test",
208         argc            => 1, 
209         note            => "Returns the tree associated with the nodeset of the given doc id"
210 );
211
212
213 sub marc_test {
214
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 );
221         return $tree;
222
223 }
224
225 1;