]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
cleaned up a lot of unnecessary cat code, rearranged some stuff to work with
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat.pm
1 use strict; use warnings;
2 package OpenILS::Application::Cat;
3 use OpenSRF::Application;
4 use OpenILS::Application::Cat::Utils;
5 use base qw/OpenSRF::Application/;
6 use Time::HiRes qw(time);
7 use OpenSRF::EX qw(:try);
8 use JSON;
9 use OpenILS::Utils::Fieldmapper;
10
11 my $utils = "OpenILS::Application::Cat::Utils";
12
13 sub child_init {
14         try {
15                 OpenSRF::Application->method_lookup( "blah" );
16         } catch Error with { 
17                 warn "Child Init Failed: " . shift() . "\n";
18         };
19 }
20
21
22 __PACKAGE__->register_method(
23         method  => "biblio_record_tree_retrieve",
24         api_name        => "open-ils.cat.biblio.record.tree.retrieve",
25         argc            => 1, 
26         note            => "Returns the tree associated with the nodeset of the given doc id"
27 );
28
29 sub biblio_record_tree_retrieve {
30         my( $self, $client, $recordid ) = @_;
31
32         warn "In retrieve " . time() . "\n";
33         my $name = "open-ils.storage.biblio.record_marc.retrieve";
34         my $method = $self->method_lookup($name);
35
36         unless($method) {
37                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
38         }
39
40         my ($marcxml) = $method->run($recordid);
41         warn "After marxml retrieve " . time() . "\n";
42
43
44         if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
45                 throw $marcxml;
46         }
47
48         return undef unless $marcxml;
49 #use Data::Dumper;
50 #       warn $marcxml->marc;
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
60 __PACKAGE__->register_method(
61         method  => "biblio_record_tree_commit",
62         api_name        => "open-ils.cat.biblio.record.tree.commit",
63         argc            => 1, 
64         note            => "Walks the tree and commits any changed nodes " .
65                                         "adds any new nodes, and deletes any deleted nodes",
66 );
67
68
69 sub biblio_record_tree_commit {
70
71         my( $self, $client, $tree ) = @_;
72         new Fieldmapper::biblio::record_node ($tree);
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         use Data::Dumper;
82         warn Dumper $nodeset;
83         
84         if(!defined($docid)) { # be sure
85                 for my $node (@$nodeset) {
86                         $docid = $node->owner_doc();
87                         last if defined($docid);
88                 }
89         }
90
91         # turn the nodeset into a doc
92         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
93
94         my $biblio =  Fieldmapper::biblio::record_marc->new();
95         $biblio->id( $docid );
96         $biblio->marc( $marcxml->toString() );
97
98         use Data::Dumper;
99         warn "Biblio Object\n";
100         warn Dumper $biblio;
101
102         my $session = $utils->start_db_session();
103
104         warn "Sending updated doc $docid to db\n";
105         my $req = $session->request( 
106                 "open-ils.storage.biblio.record_marc.update", $biblio );
107
108         my $status = $req->recv();
109         if(ref($status) and $status->isa("Error")) { 
110                 warn " +++++++ Document Update Failed";
111                 warn $status->stringify() . "\n";
112                 throw $status (" +++++++ Document Update Failed " . $status->stringify() ) ; 
113         }
114         $utils->commit_db_session( $session );
115         warn "Update Successful\n";
116
117
118         # commit the altered nodeset nodes to the db
119         #my $hash = $utils->commit_nodeset( $nodeset );
120         # retrieve the altered tree back from the db and return it
121
122
123         my $method = $self->method_lookup( "open-ils.cat.biblio.record.tree.retrieve" );
124         if(!$method) {
125                 throw OpenSRF::EX::PANIC 
126                         ("Unable to find method open-ils.cat.biblio.record.tree.retrieve");
127         }
128         my ($ans) = $method->run( $docid );
129         warn "=================================================================\n";
130         warn "=================================================================\n";
131         use Data::Dumper;
132         warn Dumper $ans;
133         return $ans;
134 }
135
136 __PACKAGE__->register_method(
137         method  => "biblio_mods_slim_retrieve",
138         api_name        => "open-ils.cat.biblio.mods.slim.retrieve",
139         argc            => 1, 
140         note            => "Returns the displayable data from the MODS record with a given ID",
141 );
142
143 sub biblio_mods_slim_retrieve {
144         my( $self, $client, $recordid ) = @_;
145
146         #my $name = "open-ils.storage.biblio.record_entry.nodeset.retrieve";
147         my $name = "open-ils.storage.biblio.record_marc.retrieve";
148         my $method = $self->method_lookup($name);
149
150         unless($method) {
151                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
152         }
153
154         # grab the marc record
155         my ($marcxml) = $method->run($recordid);
156         if(!$marcxml) { warn "Nothing from storage"; return undef; }
157
158         if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
159                 throw $marcxml ($marcxml->stringify());;
160         }
161
162         my $u = $utils->new();
163         $u->start_mods_batch( $marcxml->marc );
164
165         my $mods = $u->finish_mods_batch();
166         return $u->mods_perl_to_mods_slim( $mods );
167
168 }
169
170
171
172 1;