]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
removing deprecated code
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Authority.pm
1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
7
8 use JSON;
9
10 use Time::HiRes qw(time);
11 use OpenSRF::EX qw(:try);
12 use Digest::MD5 qw(md5_hex);
13
14 sub crossref_authority {
15         my $self = shift;
16         my $client = shift;
17         my $class = shift;
18         my $term = shift;
19
20         my $session = OpenSRF::AppSession->create("open-ils.storage");
21
22         my $freq = $session->request(
23                 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
24         my $areq = $session->request(
25                 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
26
27         my $fr = $freq->gather(1);
28         my $al = $areq->gather(1);
29
30         return _auth_flatten( $term, $fr, $al, 1 );
31 }
32
33 sub _auth_flatten {
34         my $term = shift;
35         my $fr = shift;
36         my $al = shift;
37         my $limit = shift;
38
39         my %hash = ();
40         for my $x (@$fr) {
41                 my $string = $$x[0];
42                 for my $i (1..10) {
43                         last unless ($$x[$i]);
44                         if ($string =~ /\W$/o) {
45                                 $string .= ' '.$$x[$i];
46                         } else {
47                                 $string .= ' -- '.$$x[$i];
48                         }
49                 }
50                 next if (lc($string) eq lc($term));
51                 $hash{$string}++;
52                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
53         }
54         my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
55
56 #       $from = [ @$from[0..4] ] if $limit;
57
58         %hash = ();
59         for my $x (@$al) {
60                 my $string = $$x[0];
61                 for my $i (1..10) {
62                         last unless ($$x[$i]);
63                         if ($string =~ /\W$/o) {
64                                 $string .= ' '.$$x[$i];
65                         } else {
66                                 $string .= ' -- '.$$x[$i];
67                         }
68                 }
69                 next if (lc($string) eq lc($term));
70                 $hash{$string}++;
71                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
72         }
73         my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
74
75 #       $also = [ @$also[0..4] ] if $limit;
76
77
78         return { from => $from, also => $also };
79 }
80
81 __PACKAGE__->register_method(
82         method          => "crossref_authority",
83         api_name        => "open-ils.search.authority.crossref",
84         argc            => 2, 
85         note            => "Searches authority data for existing controlled terms and crossrefs",
86 );              
87
88 __PACKAGE__->register_method(
89         method          => "crossref_authority_batch",
90    api_name     => "open-ils.search.authority.crossref.batch",
91    argc         => 1, 
92    note         => <<"  NOTE");
93         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
94
95         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
96
97         Returns an object like so:
98         {
99                 "classname" : {
100                         "term" : { "from" : [ ...], "also" : [...] }
101                         "term2" : { "from" : [ ...], "also" : [...] }
102                 }
103         }
104         NOTE
105
106 sub crossref_authority_batch {
107         my( $self, $client, $reqs ) = @_;
108
109         my $response = {};
110         my $lastr = [];
111         my $session = OpenSRF::AppSession->create("open-ils.storage");
112
113         for my $req (@$reqs) {
114
115                 my $class = $req->[0];
116                 my $term = $req->[1];
117                 next unless $class and $term;
118                 warn "Sending authority request for $class : $term\n";
119                 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
120                 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
121
122                 if( $lastr->[0] ) { #process old data while waiting on new data
123                         my $cls = $lastr->[0];
124                         my $trm = $lastr->[1];
125                         my $fr  = $lastr->[2];
126                         my $al  = $lastr->[3];
127                         warn "Flattening $class : $term\n";
128                         $response->{$cls} = {} unless exists $response->{$cls};
129                         $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
130                 }
131
132                 $lastr->[0] = $class;
133                 $lastr->[1] = $term; 
134                 $lastr->[2] = $freq->gather(1);
135                 $lastr->[3] = $areq->gather(1);
136         }
137
138         if( $lastr->[0] ) { #process old data while waiting on new data
139                 my $cls = $lastr->[0];
140                 my $trm = $lastr->[1];
141                 my $fr  = $lastr->[2];
142                 my $al  = $lastr->[3];
143                 warn "Flattening $cls : $trm\n";
144                 $response->{$cls} = {} unless exists $response->{$cls};
145                 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
146         }
147
148         return $response;
149 }
150
151
152 1;