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