]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
moved some universal methods to AppUtils, Cat and Cat/Utils now rely
[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         my( $self, $client, @recordids ) = @_;
129
130         my $name = "open-ils.storage.biblio.record_marc.retrieve";
131         my $method = $self->method_lookup($name);
132
133         unless($method) {
134                 throw OpenSRF::EX::PANIC ("Could not lookup method $name");
135         }
136
137         my $u = $utils->new();
138         my $start = 1;
139
140         for my $id (@recordids) {
141                 my ($marcxml) = $method->run($id);
142                 if(!$marcxml) { warn "Nothing from storage"; return undef; }
143
144                 if(UNIVERSAL::isa($marcxml,"OpenSRF::EX")) {
145                         throw $marcxml ($marcxml->stringify());;
146                 }
147
148                 if($start) {
149                         $u->start_mods_batch( $marcxml->marc );
150                         $start = 0;
151                 } else {
152                         $u->push_mods_batch( $marcxml->marc );
153                 }
154         }
155
156         my $mods = $u->finish_mods_batch();
157         return $mods;
158
159 }
160
161
162
163 1;