]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/AddedContent.pm
did some robustification
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / AddedContent.pm
1 package OpenILS::Application::Search::AddedContent;
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
12 my $host;
13 my $username;
14 my $password;
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
33
34
35 # Fetches the added content and returns the data as a string.
36 # If not data is retrieved (or timeout occurs), undef is returned
37 sub retrieve_added_content {
38         my( $type, $isbn, $summary ) = @_;
39         return undef unless ( $isbn && $isbn ne "" );
40
41         my $func = "fnDetailByItemKey";
42         if($summary) { $func = "fnContentByItemKey"; }
43
44         my $url = "$host/$urlbase/" . $types->{$type} . 
45                 "/$func?UserId=$username&Password=$password&ItemKey=$isbn";
46
47
48         warn "Added Content URL: $url\n";
49
50         my $data = undef;
51         try {
52                 alarm(15);
53                 $data = LWP::UserAgent->new->get($url)->content;
54                 alarm(0);
55         } catch Error with {
56                 alarm(0);
57         };
58         alarm(0);
59
60         warn "received content data:\n$data\n";
61         return $data;
62 }
63
64 __PACKAGE__->register_method(
65         method  => "summary",
66         api_name        => "open-ils.search.added_content.summary.retrieve",
67         notes           => <<"  NOTE");
68                 Returns an object like so:
69                         {
70                                 Review : true/false,
71                                 Inventory : true/false,
72                                 Annotation : true/false,
73                                 Jacket : true/false
74                                 TOC : true/false
75                                 Product : true/false
76                         }
77                 This object indicates the existance of each type of added content for the given ISBN
78                 PARAMS( ISBN ),
79         NOTE
80
81 sub summary {
82         my( $self, $client, $isbn ) = @_;
83         my $data = retrieve_added_content( "member", $isbn, 1 );
84         return {} unless $data;
85         my $doc = XML::LibXML->new->parse_string($data);
86         my $summary = {};
87         return $summary unless $doc;
88
89         for my $node ( $doc->getDocumentElement->childNodes ) {
90                 if( $node->localName ) {
91                         $summary->{$node->localName} = $node->textContent;      
92                 }
93         }
94         return $summary;
95 }
96
97
98
99
100
101 __PACKAGE__->register_method(
102         method  => "reviews",
103         api_name        => "open-ils.search.added_content.review.retrieve.random",
104         notes           => <<"  NOTE");
105                 Returns a singe random review article object
106                 PARAMS( ISBN ),
107         NOTE
108
109 __PACKAGE__->register_method(
110         method  => "reviews",
111         api_name        => "open-ils.search.added_content.review.retrieve.all",
112         notes           => <<"  NOTE");
113                 Returns an array review article objects
114                 PARAMS( ISBN ),
115         NOTE
116
117 sub reviews {
118         my( $self, $client, $isbn ) = @_;
119
120         my $data = retrieve_added_content( "review", $isbn );
121         return undef unless $data;
122         my $doc = XML::LibXML->new->parse_string($data);
123         my $ret = [];
124
125         if(!$doc) {
126                 if( $self->api_name =~ /random/ ) { return undef; }
127                 return $ret;
128         }
129
130         my $reviews = $doc->findnodes("//*[local-name()='Review']");
131
132         for my $rev ( $reviews->get_nodelist() ) {
133                 my $revobj = {};
134                 for my $node ($rev->childNodes) {
135
136                         if( $node->localName ) {
137                                 if( $node->localName eq "ReviewText" ) {
138                                         $revobj ->{'text'} = $node->textContent;
139                                 }
140                                 if( $node->localName eq "ReviewLiteral" ) {
141                                         $revobj->{'info'} = $node->textContent;
142                                 }
143
144                         }
145                 }
146
147                 if( $self->api_name =~ /random/ ) { return $revobj; }
148                 push( @$ret, $revobj );
149         }
150
151         return $ret;
152 }
153
154
155 __PACKAGE__->register_method(
156         method  => "toc",
157         api_name        => "open-ils.search.added_content.toc.retrieve",
158         notes           => <<"  NOTE");
159                 Returns the table of contents for the given ISBN
160                 PARAMS( ISBN ),
161         NOTE
162
163 sub toc {
164         my( $self, $client, $isbn ) = @_;
165
166         my $data = retrieve_added_content( "toc", $isbn );
167         return undef unless $data;
168         my $doc = XML::LibXML->new->parse_string($data);
169
170         my @nodes =  $doc->findnodes("//*[local-name()='TOCText']")->get_nodelist();
171                 
172         if($nodes[0]) {
173                 return $nodes[0]->textContent;
174         }
175
176         return "";
177 }
178
179
180 1;