From 1faca965f2d8cfbc456756127f97e575e0a21f6c Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 29 Dec 2005 19:30:58 +0000 Subject: [PATCH] moving out of OpenILS tree into this tree for customizability git-svn-id: svn://svn.open-ils.org/ILS/trunk@2550 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../Search/AddedContent/ContentCafe.pm | 196 ++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 Evergreen/src/perlmods/Evergreen/Application/Search/AddedContent/ContentCafe.pm diff --git a/Evergreen/src/perlmods/Evergreen/Application/Search/AddedContent/ContentCafe.pm b/Evergreen/src/perlmods/Evergreen/Application/Search/AddedContent/ContentCafe.pm new file mode 100644 index 0000000000..b1373d5f0c --- /dev/null +++ b/Evergreen/src/perlmods/Evergreen/Application/Search/AddedContent/ContentCafe.pm @@ -0,0 +1,196 @@ +package Evergreen::Application::Search::AddedContent::ContentCafe; +use base qw/OpenSRF::Application/; +use strict; use warnings; +use OpenILS::Application::AppUtils; +use OpenSRF::Utils::SettingsClient; +my $apputils = "OpenILS::Application::AppUtils"; +use XML::LibXML; +use LWP::UserAgent; +use OpenSRF::EX qw(:try); + +my $host; +my $username; +my $password; +my $enabled = 0; +my $urlbase = "ContentCafe"; +my $types = { + toc => "TOC.asmx", + review => "Review.asmx", + annotation => "Annotation.asmx", + member => "Member.asmx", + }; + +sub initialize { + my $conf = OpenSRF::Utils::SettingsClient->new; + $host = $conf->config_value( + "apps", "open-ils.search","app_settings", "added_content", "host"); + $username = $conf->config_value( + "apps", "open-ils.search","app_settings", "added_content", "username"); + $password = $conf->config_value( + "apps", "open-ils.search","app_settings", "added_content", "password"); + + $enabled = 1 if ($host and $username and $password); +} + + + +# Fetches the added content and returns the data as a string. +# If not data is retrieved (or timeout occurs), undef is returned +sub retrieve_added_content { + my( $type, $isbn, $summary ) = @_; + return undef unless ( $isbn && $isbn ne "" ); + + my $func = "fnDetailByItemKey"; + if($summary) { $func = "fnContentByItemKey"; } + + my $url = "$host/$urlbase/" . $types->{$type} . + "/$func?UserId=$username&Password=$password&ItemKey=$isbn"; + + + warn "Added Content URL: $url\n"; + + my $data = undef; + try { + alarm(15); + $data = LWP::UserAgent->new->get($url)->content; + alarm(0); + } catch Error with { + alarm(0); + }; + alarm(0); + +# warn "received content data:\n$data\n"; + return $data; +} + +__PACKAGE__->register_method( + method => "summary", + api_name => "open-ils.search.added_content.summary.retrieve", + notes => <<" NOTE"); + Returns an object like so: + { + Review : true/false, + Inventory : true/false, + Annotation : true/false, + Jacket : true/false + TOC : true/false + Product : true/false + } + This object indicates the existance of each type of added content for the given ISBN + PARAMS( ISBN ), + NOTE + +sub summary { + my( $self, $client, $isbn ) = @_; + + if(!$enabled) { + return { + Review => "false", + Inventory => "false", + Annotation => "false", + Jacket => "false", + TOC => "false", + Product => "false", + }; + } + + my $data = retrieve_added_content( "member", $isbn, 1 ); + return {} unless $data; + my $doc = XML::LibXML->new->parse_string($data); + my $summary = {}; + return $summary unless $doc; + + for my $node ( $doc->getDocumentElement->childNodes ) { + if( $node->localName ) { + $summary->{$node->localName} = $node->textContent; + } + } + return $summary; +} + + + + + +__PACKAGE__->register_method( + method => "reviews", + api_name => "open-ils.search.added_content.review.retrieve.random", + notes => <<" NOTE"); + Returns a singe random review article object + PARAMS( ISBN ), + NOTE + +__PACKAGE__->register_method( + method => "reviews", + api_name => "open-ils.search.added_content.review.retrieve.all", + notes => <<" NOTE"); + Returns an array review article objects + PARAMS( ISBN ), + NOTE + +sub reviews { + my( $self, $client, $isbn ) = @_; + + my $ret = []; + return $ret unless $enabled; + my $data = retrieve_added_content( "review", $isbn ); + return $ret unless $data; + my $doc = XML::LibXML->new->parse_string($data); + + + if(!$doc) { + if( $self->api_name =~ /random/ ) { return undef; } + return $ret; + } + + my $reviews = $doc->findnodes("//*[local-name()='Review']"); + + for my $rev ( $reviews->get_nodelist() ) { + my $revobj = {}; + for my $node ($rev->childNodes) { + + if( $node->localName ) { + if( $node->localName eq "ReviewText" ) { + $revobj ->{'text'} = $node->textContent; + } + if( $node->localName eq "ReviewLiteral" ) { + $revobj->{'info'} = $node->textContent; + } + + } + } + + if( $self->api_name =~ /random/ ) { return $revobj; } + push( @$ret, $revobj ); + } + + return $ret; +} + + +__PACKAGE__->register_method( + method => "toc", + api_name => "open-ils.search.added_content.toc.retrieve", + notes => <<" NOTE"); + Returns the table of contents for the given ISBN + PARAMS( ISBN ), + NOTE + +sub toc { + my( $self, $client, $isbn ) = @_; + + my $data = retrieve_added_content( "toc", $isbn ); + return undef unless $data; + my $doc = XML::LibXML->new->parse_string($data); + + my @nodes = $doc->findnodes("//*[local-name()='TOCText']")->get_nodelist(); + + if($nodes[0]) { + return $nodes[0]->textContent; + } + + return ""; +} + + +1; -- 2.43.2