improper added content info is now ignored / not displayed
[Evergreen.git] / Evergreen / src / perlmods / Evergreen / Application / Search / AddedContent / ContentCafe.pm
1 package Evergreen::Application::Search::AddedContent::ContentCafe;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use OpenILS::Application::AppUtils;
5 use OpenSRF::Utils::SettingsClient;
6 my $apputils = "OpenILS::Application::AppUtils";
7 use XML::LibXML;
8 use LWP::UserAgent;
9 use OpenSRF::EX qw(:try);
10
11 my $host;
12 my $username;
13 my $password;
14 my $enabled = 0;
15 my $urlbase = "ContentCafe";
16 my $types = {
17         toc                     => "TOC.asmx",
18         review          => "Review.asmx",
19         annotation      => "Annotation.asmx",
20         member          => "Member.asmx",
21         };
22
23 sub initialize {
24         my $conf = OpenSRF::Utils::SettingsClient->new;
25         $host = $conf->config_value(                                    
26                 "apps", "open-ils.search","app_settings", "added_content", "host");
27         $username = $conf->config_value(                                        
28                 "apps", "open-ils.search","app_settings", "added_content", "username");
29         $password = $conf->config_value(                                        
30                 "apps", "open-ils.search","app_settings", "added_content", "password");
31
32         $enabled = 1 if ($host and $username and $password);
33 }
34
35
36
37 # Fetches the added content and returns the data as a string.
38 # If not data is retrieved (or timeout occurs), undef is returned
39 sub retrieve_added_content {
40         my( $type, $isbn, $summary ) = @_;
41         return undef unless ( $isbn && $isbn ne "" );
42
43         my $func = "fnDetailByItemKey";
44         if($summary) { $func = "fnContentByItemKey"; }
45
46         my $url = "$host/$urlbase/" . $types->{$type} . 
47                 "/$func?UserId=$username&Password=$password&ItemKey=$isbn";
48
49
50         warn "Added Content URL: $url\n";
51
52         my $data = undef;
53         try {
54                 alarm(15);
55                 $data = LWP::UserAgent->new->get($url)->content;
56                 alarm(0);
57         } catch Error with {
58                 alarm(0);
59         };
60         alarm(0);
61
62 #       warn "received content data:\n$data\n";
63         return $data;
64 }
65
66 __PACKAGE__->register_method(
67         method  => "summary",
68         api_name        => "open-ils.search.added_content.summary.retrieve",
69         notes           => <<"  NOTE");
70                 Returns an object like so:
71                         {
72                                 Review : true/false,
73                                 Inventory : true/false,
74                                 Annotation : true/false,
75                                 Jacket : true/false
76                                 TOC : true/false
77                                 Product : true/false
78                         }
79                 This object indicates the existance of each type of added content for the given ISBN
80                 PARAMS( ISBN ),
81         NOTE
82
83 sub summary {
84         my( $self, $client, $isbn ) = @_;
85
86         if(!$enabled) {
87                 return { 
88                         Review          => "false",
89                         Inventory       => "false",
90                         Annotation      => "false",
91                         Jacket          => "false",
92                         TOC                     => "false",
93                         Product         => "false",
94                 };
95         }
96
97         my $data = retrieve_added_content( "member", $isbn, 1 );
98         return {} unless $data;
99         my $doc;
100         try {
101                 $doc = XML::LibXML->new->parse_string($data);
102         } catch Error with { $doc = undef };
103
104         my $summary = {};
105         return $summary unless $doc;
106
107         for my $node ( $doc->getDocumentElement->childNodes ) {
108                 if( $node->localName ) {
109                         $summary->{$node->localName} = $node->textContent;      
110                 }
111         }
112         return $summary;
113 }
114
115
116
117
118
119 __PACKAGE__->register_method(
120         method  => "reviews",
121         api_name        => "open-ils.search.added_content.review.retrieve.random",
122         notes           => <<"  NOTE");
123                 Returns a singe random review article object
124                 PARAMS( ISBN ),
125         NOTE
126
127 __PACKAGE__->register_method(
128         method  => "reviews",
129         api_name        => "open-ils.search.added_content.review.retrieve.all",
130         notes           => <<"  NOTE");
131                 Returns an array review article objects
132                 PARAMS( ISBN ),
133         NOTE
134
135 sub reviews {
136         my( $self, $client, $isbn ) = @_;
137
138         my $ret = [];
139         return $ret unless $enabled;
140         my $data = retrieve_added_content( "review", $isbn );
141         return $ret unless $data;
142
143         my $doc;
144         try {
145                 $doc = XML::LibXML->new->parse_string($data);
146         } catch Error with { $doc = undef };
147
148
149         if(!$doc) {
150                 if( $self->api_name =~ /random/ ) { return undef; }
151                 return $ret;
152         }
153
154         my $reviews = $doc->findnodes("//*[local-name()='Review']");
155
156         for my $rev ( $reviews->get_nodelist() ) {
157                 my $revobj = {};
158                 for my $node ($rev->childNodes) {
159
160                         if( $node->localName ) {
161                                 if( $node->localName eq "ReviewText" ) {
162                                         $revobj ->{'text'} = $node->textContent;
163                                 }
164                                 if( $node->localName eq "ReviewLiteral" ) {
165                                         $revobj->{'info'} = $node->textContent;
166                                 }
167
168                         }
169                 }
170
171                 if( $self->api_name =~ /random/ ) { return $revobj; }
172                 push( @$ret, $revobj );
173         }
174
175         return $ret;
176 }
177
178
179 __PACKAGE__->register_method(
180         method  => "toc",
181         api_name        => "open-ils.search.added_content.toc.retrieve",
182         notes           => <<"  NOTE");
183                 Returns the table of contents for the given ISBN
184                 PARAMS( ISBN ),
185         NOTE
186
187 sub toc {
188         my( $self, $client, $isbn ) = @_;
189
190         my $data = retrieve_added_content( "toc", $isbn );
191         return undef unless $data;
192
193         my $doc;
194         try {
195                 $doc = XML::LibXML->new->parse_string($data);
196         } catch Error with { $doc = undef };
197
198
199         my @nodes =  $doc->findnodes("//*[local-name()='TOCText']")->get_nodelist();
200                 
201         if($nodes[0]) {
202                 return $nodes[0]->textContent;
203         }
204
205         return "";
206 }
207
208
209 1;