moving out of OpenILS tree into this tree for customizability
[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 = XML::LibXML->new->parse_string($data);
100         my $summary = {};
101         return $summary unless $doc;
102
103         for my $node ( $doc->getDocumentElement->childNodes ) {
104                 if( $node->localName ) {
105                         $summary->{$node->localName} = $node->textContent;      
106                 }
107         }
108         return $summary;
109 }
110
111
112
113
114
115 __PACKAGE__->register_method(
116         method  => "reviews",
117         api_name        => "open-ils.search.added_content.review.retrieve.random",
118         notes           => <<"  NOTE");
119                 Returns a singe random review article object
120                 PARAMS( ISBN ),
121         NOTE
122
123 __PACKAGE__->register_method(
124         method  => "reviews",
125         api_name        => "open-ils.search.added_content.review.retrieve.all",
126         notes           => <<"  NOTE");
127                 Returns an array review article objects
128                 PARAMS( ISBN ),
129         NOTE
130
131 sub reviews {
132         my( $self, $client, $isbn ) = @_;
133
134         my $ret = [];
135         return $ret unless $enabled;
136         my $data = retrieve_added_content( "review", $isbn );
137         return $ret unless $data;
138         my $doc = XML::LibXML->new->parse_string($data);
139
140
141         if(!$doc) {
142                 if( $self->api_name =~ /random/ ) { return undef; }
143                 return $ret;
144         }
145
146         my $reviews = $doc->findnodes("//*[local-name()='Review']");
147
148         for my $rev ( $reviews->get_nodelist() ) {
149                 my $revobj = {};
150                 for my $node ($rev->childNodes) {
151
152                         if( $node->localName ) {
153                                 if( $node->localName eq "ReviewText" ) {
154                                         $revobj ->{'text'} = $node->textContent;
155                                 }
156                                 if( $node->localName eq "ReviewLiteral" ) {
157                                         $revobj->{'info'} = $node->textContent;
158                                 }
159
160                         }
161                 }
162
163                 if( $self->api_name =~ /random/ ) { return $revobj; }
164                 push( @$ret, $revobj );
165         }
166
167         return $ret;
168 }
169
170
171 __PACKAGE__->register_method(
172         method  => "toc",
173         api_name        => "open-ils.search.added_content.toc.retrieve",
174         notes           => <<"  NOTE");
175                 Returns the table of contents for the given ISBN
176                 PARAMS( ISBN ),
177         NOTE
178
179 sub toc {
180         my( $self, $client, $isbn ) = @_;
181
182         my $data = retrieve_added_content( "toc", $isbn );
183         return undef unless $data;
184         my $doc = XML::LibXML->new->parse_string($data);
185
186         my @nodes =  $doc->findnodes("//*[local-name()='TOCText']")->get_nodelist();
187                 
188         if($nodes[0]) {
189                 return $nodes[0]->textContent;
190         }
191
192         return "";
193 }
194
195
196 1;