| 1 | # | ||
| 2 | # Copyright (c) 2005, Presicient Corp., USA | ||
| 3 | # | ||
| 4 | # Permission is granted to use this software according to the terms of the | ||
| 5 | # Artistic License, as specified in the Perl README file, | ||
| 6 | # with the exception that commercial redistribution, either | ||
| 7 | # electronic or via physical media, as either a standalone package, | ||
| 8 | # or incorporated into a third party product, requires prior | ||
| 9 | # written approval of the author. | ||
| 10 | # | ||
| 11 | # This software is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | # Presicient Corp. reserves the right to provide support for this software | ||
| 16 | # to individual sites under a separate (possibly fee-based) | ||
| 17 | # agreement. | ||
| 18 | # | ||
| 19 | # History: | ||
| 20 | # | ||
| 21 | # 2005-Jan-27 D. Arnold | ||
| 22 | # Coded. | ||
| 23 | # | ||
| 24 | |||
| 25 | package SQL::Amazon::Request::ItemSearch; | ||
| 26 | |||
| 27 | use SQL::Amazon::Request::ItemLookup; | ||
| 28 | use SQL::Amazon::Parser qw(:pred_node_codes); | ||
| 29 | use base qw(SQL::Amazon::Request::ItemLookup); | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | my %is_power_col = ( | ||
| 33 | 'TITLE', [ 'title', 1 ], | ||
| 34 | 'SUBJECT', [ 'subject', undef ], | ||
| 35 | 'AUTHORS', [ 'author', 1 ], | ||
| 36 | 'PUBLISHER', [ 'publisher', 1 ], | ||
| 37 | 'LANGUAGE', [ 'language', undef ], | ||
| 38 | ); | ||
| 39 | my %is_search_col = ( | ||
| 40 | 'TITLE', [ 'Title', '*', 1 ], | ||
| 41 | 'AUTHORS', [ 'Author', 'Books', 1 ], | ||
| 42 | 'PUBLISHER', [ 'Publisher', 'Books', 1 ], | ||
| 43 | 'ARTIST', [ 'Artist', '*', 1 ], | ||
| 44 | 'ACTOR', [ 'Actor', '*', 1 ], | ||
| 45 | 'DIRECTOR', [ 'Director', '*', 1 ], | ||
| 46 | 'MANUFACTURER', [ 'Manufacturer', '*', 1 ], | ||
| 47 | 'MUSICLABEL', [ 'MusicLabel', '*', 1 ], | ||
| 48 | 'COMPOSER', [ 'Composer', '*', 1 ], | ||
| 49 | 'BRAND', [ 'Brand', '*', 1 ], | ||
| 50 | 'CONDUCTOR', [ 'Conductor', '*', 1 ], | ||
| 51 | 'ORCHESTRA', [ 'Orchestra', '*', 1 ], | ||
| 52 | 'CITY', [ 'City', 'Restruants', 1 ], | ||
| 53 | 'CUISINE', [ 'Cuisine', 'Restruants', 1 ], | ||
| 54 | 'NEIGHBORHOOD', [ 'Neighborhood', 'Restruants', 1 ], | ||
| 55 | 'MERCHANTID', [ 'MerchantID', 'Offers', undef ], | ||
| 56 | 'CONDITION', [ 'Condition', 'Offers', undef ], | ||
| 57 | 'DELIVERYMETHOD', [ 'DeliveryMethod', 'Offers', undef ], | ||
| 58 | ); | ||
| 59 | my %can_power_search = qw( | ||
| 60 | BOOKS 1 | ||
| 61 | ); | ||
| 62 | my %is_amzn_function = qw( | ||
| 63 | AMZN_POWER_SEARCH 1 | ||
| 64 | AMZN_IN_ANY 1 | ||
| 65 | AMZN_MATCH_ANY 1 | ||
| 66 | AMZN_MATCH_ALL 1 | ||
| 67 | AMZN_MATCH_TEXT 1 | ||
| 68 | ); | ||
| 69 | |||
| 70 | our $errstr; | ||
| 71 | |||
| 72 | sub errstr { | ||
| 73 | return $errstr | ||
| 74 | if $errstr && $errstr ne ''; | ||
| 75 | |||
| 76 | return shift->{_errstr}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub new { | ||
| 80 | my $class = shift; | ||
| 81 | my $req_attrs = { | ||
| 82 | Power => { | ||
| 83 | Explicit => [], | ||
| 84 | Keywords => [], | ||
| 85 | title => undef, | ||
| 86 | subject => [], | ||
| 87 | author => [], | ||
| 88 | publisher => undef, | ||
| 89 | language => undef | ||
| 90 | }, | ||
| 91 | Keywords => [], | ||
| 92 | }; | ||
| 93 | $errstr = undef; | ||
| 94 | my $predicate = create_search_request($req_attrs, @_); | ||
| 95 | |||
| 96 | return (undef, undef) | ||
| 97 | if $errstr; | ||
| 98 | return ($predicate, undef) | ||
| 99 | unless scalar keys %$req_attrs; | ||
| 100 | |||
| 101 | my $obj = $class->SUPER::new($req_attrs); | ||
| 102 | |||
| 103 | $obj->{url_params}{Operation} = 'ItemSearch'; | ||
| 104 | $obj->{req_attrs} = $req_attrs; | ||
| 105 | return ($predicate, $obj); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub create_search_request { | ||
| 109 | my ($request, $expr, $table, $parser) = @_; | ||
| 110 | $request->{SearchIndex} = $table; | ||
| 111 | my $conjoins = $expr->[0]; | ||
| 112 | my @finalcjs = (); | ||
| 113 | foreach (@$conjoins) { | ||
| 114 | my ($op, $left, $right, $neg) = @$_; | ||
| 115 | $table = uc $table; | ||
| 116 | my ($name, $value); | ||
| 117 | if ($op eq 'USER_DEFINED') { | ||
| 118 | |||
| 119 | $name = (ref $left ne 'HASH') ? | ||
| 120 | $left->name() : $left->{name}; | ||
| 121 | |||
| 122 | push (@finalcjs, $_), | ||
| 123 | next | ||
| 124 | unless $is_amzn_function{uc $name}; | ||
| 125 | |||
| 126 | if ($name eq 'AMZN_IN_ANY') { | ||
| 127 | $op = 'IN'; | ||
| 128 | $value = (ref $left ne 'HASH') ? | ||
| 129 | $left->args()->{value}[0] : | ||
| 130 | $left->{value}{value}[0]; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | push (@finalcjs, $_), | ||
| 135 | next | ||
| 136 | if ($neg && ($op eq 'IN')); | ||
| 137 | push (@finalcjs, $_), | ||
| 138 | next | ||
| 139 | if ((ref $right eq 'HASH') && ($right->{type} eq 'column')); | ||
| 140 | |||
| 141 | $name = ((ref $left eq 'HASH') && ($left->{type} eq 'column')) ? | ||
| 142 | $left->{value} : | ||
| 143 | ($op eq 'IN') ? $value : $name; | ||
| 144 | $name = uc $name; | ||
| 145 | $table = uc $1 | ||
| 146 | if ($name=~s/^([A-Z]\w*)\.(\w+)/$2/); | ||
| 147 | my $aliases = $parser->{struct}{column_aliases}; | ||
| 148 | $name = $aliases->{$name} | ||
| 149 | if $aliases->{$name}; | ||
| 150 | |||
| 151 | push (@finalcjs, $_), | ||
| 152 | next | ||
| 153 | unless ($name=~/^[A-Z]\w*$/i); | ||
| 154 | $name = 'ASIN' if ($name eq 'ISBN'); | ||
| 155 | |||
| 156 | if ($name eq 'ASIN') { | ||
| 157 | push (@finalcjs, $_), | ||
| 158 | next | ||
| 159 | unless ($table eq 'BOOKS'); | ||
| 160 | |||
| 161 | push (@finalcjs, $_), | ||
| 162 | next | ||
| 163 | if ($op eq '<>'); | ||
| 164 | $request->{Power}{ASIN} = $right, | ||
| 165 | next | ||
| 166 | if ($op eq '='); | ||
| 167 | |||
| 168 | push (@finalcjs, $_); | ||
| 169 | } | ||
| 170 | elsif ($can_power_search{$table} && $is_power_col{$name}) { | ||
| 171 | push (@finalcjs, $_), | ||
| 172 | next | ||
| 173 | if ($op eq '<>'); | ||
| 174 | |||
| 175 | $errstr = "Invalid range predicate for $name.", | ||
| 176 | return undef | ||
| 177 | if ($op =~/^[<>]/); | ||
| 178 | |||
| 179 | if (($op eq 'LIKE') || ($op eq 'CLIKE')) { | ||
| 180 | push (@finalcjs, $_), | ||
| 181 | next | ||
| 182 | if $neg; | ||
| 183 | |||
| 184 | unless (defined($request->{Power}{$is_power_col{$name}[0]}) && | ||
| 185 | ref $request->{Power}{$is_power_col{$name}[0]}) { | ||
| 186 | $request->{Power}{$is_power_col{$name}[0]} = $right; | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | push @{$request->{Power}{$is_power_col{$name}[0]}}, $right; | ||
| 190 | } | ||
| 191 | push (@finalcjs, $_) | ||
| 192 | if $is_power_col{$name}[1]; | ||
| 193 | } | ||
| 194 | elsif ($is_search_col{$name}) { | ||
| 195 | $request->{$is_search_col{$name}[0]} = $right; | ||
| 196 | push (@finalcjs, $_) | ||
| 197 | if $is_search_col{$name}[2]; | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | push (@finalcjs, $_); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | elsif ($name=~/^AMZN_MATCH_(ANY|ALL|TEXT)$/) { | ||
| 204 | if ($name eq 'AMZN_MATCH_ANY') { | ||
| 205 | $errstr = "Invalid MATCHES predicate for $table table.", | ||
| 206 | return undef | ||
| 207 | unless ($table eq 'BOOKS'); | ||
| 208 | push @{$request->{Power}{Keywords}}, $left->args(); | ||
| 209 | } | ||
| 210 | elsif ($name eq 'AMZN_MATCH_ALL') { | ||
| 211 | push @{$request->{Keywords}}, $left->args(); | ||
| 212 | } | ||
| 213 | elsif ($name eq 'AMZN_MATCH_TEXT') { | ||
| 214 | $request->{TextStream} = $left->args(); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | elsif ($name=~/^AMZN_POWER_SEARCH$/) { | ||
| 218 | $errstr = "Invalid POWER_SEARCH predicate for $table table.", | ||
| 219 | return undef | ||
| 220 | unless ($table eq 'BOOKS'); | ||
| 221 | push @{$request->{Power}{Explicit}}, $left->args(); | ||
| 222 | } | ||
| 223 | elsif ($name eq 'AUDIENCERATING') { | ||
| 224 | $request->{AudienceRating}{Values} = $right; | ||
| 225 | $request->{AudienceRating}{Operator} = | ||
| 226 | (($op eq 'IN') && $neg) ? 'NOT IN' : $op; | ||
| 227 | push (@finalcjs, $_); | ||
| 228 | } | ||
| 229 | elsif ($name eq 'LISTPRICEAMT') { | ||
| 230 | $errstr = 'Invalid predicate: ListPriceAmt not valid with IN/LIKE/CLIKE.', | ||
| 231 | return undef | ||
| 232 | if (($op eq 'IN') || ($op eq 'LIKE') || ($op eq 'CLIKE')); | ||
| 233 | |||
| 234 | push (@finalcjs, $_), | ||
| 235 | next | ||
| 236 | if (($op eq '=') || ($op eq '<>')); | ||
| 237 | |||
| 238 | $request->{MaximumPrice} = $right | ||
| 239 | if (($op eq '<') || ($op eq '<=')); | ||
| 240 | |||
| 241 | $request->{MinimumPrice} = $right | ||
| 242 | if (($op eq '>') || ($op eq '>=')); | ||
| 243 | } | ||
| 244 | elsif ($is_search_col{$name}) { | ||
| 245 | $errstr = "Unknown column $name for table $table.", | ||
| 246 | return undef | ||
| 247 | unless (($is_search_col{$name}[1] eq '*') || | ||
| 248 | (uc $is_search_col{$name}[1] eq $table)); | ||
| 249 | |||
| 250 | push (@finalcjs, $_), | ||
| 251 | next | ||
| 252 | if (($op eq '<>') || | ||
| 253 | ($op eq 'LIKE') || ($op eq 'CLIKE') || | ||
| 254 | ($op eq 'IN')); | ||
| 255 | |||
| 256 | $errstr = "Invalid range expression for $name.", | ||
| 257 | return undef | ||
| 258 | unless ($op eq '='); | ||
| 259 | |||
| 260 | $request->{$is_search_col{$name}[0]} = $right; | ||
| 261 | push (@finalcjs, $_) | ||
| 262 | if $is_search_col{$name}[2]; | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | push (@finalcjs, $_); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | $expr->[0] = \@finalcjs; | ||
| 269 | return $expr; | ||
| 270 | } | ||
| 271 | sub create_power_search { | ||
| 272 | my ($obj, $stmt) = @_; | ||
| 273 | |||
| 274 | $obj->{_errstr} = undef; | ||
| 275 | my $power = $obj->{req_attrs}->{Power}; | ||
| 276 | my $pwrstr = ''; | ||
| 277 | my $val; | ||
| 278 | my @pwrparms = (); | ||
| 279 | foreach my $expl (@{$power->{Explicit}}) { | ||
| 280 | if (ref $expl eq 'ARRAY') { | ||
| 281 | foreach (@$expl) { | ||
| 282 | $val = $stmt->get_row_value($_, undef, {}); | ||
| 283 | push @pwrparms, $val | ||
| 284 | if defined($val); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | $val = $stmt->get_row_value($expl, undef, {}); | ||
| 289 | push @pwrparms, $val | ||
| 290 | if defined($val); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | $pwrstr = '(' . join(') and (', @pwrparms) . ')' | ||
| 294 | if scalar @pwrparms; | ||
| 295 | @pwrparms = (); | ||
| 296 | foreach my $keys (@{$power->{Keywords}}) { | ||
| 297 | if (ref $keys eq 'ARRAY') { | ||
| 298 | foreach (@$keys) { | ||
| 299 | $val = $stmt->get_row_value($_, undef, {}); | ||
| 300 | push @pwrparms, $val | ||
| 301 | if defined($val); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | $val = $stmt->get_row_value($keys, undef, {}); | ||
| 306 | push @pwrparms, $val | ||
| 307 | if defined($val); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | $pwrstr .= (($pwrstr ne '') ? ' and (keywords: ("' : '(keywords: ("') . | ||
| 311 | join('" or "', @pwrparms) . '"))' | ||
| 312 | if scalar @pwrparms; | ||
| 313 | @pwrparms = (); | ||
| 314 | foreach (keys %$power) { | ||
| 315 | next if (($_ eq 'Explicit') || ($_ eq 'Keywords')); | ||
| 316 | |||
| 317 | if (ref $power->{$_} eq 'HASH') { | ||
| 318 | $val = $stmt->get_row_value($power->{$_}, undef, {}); | ||
| 319 | next unless defined($val); | ||
| 320 | $val=~s/%%/\0/g; | ||
| 321 | next if (substr($val,0, 1) eq '%'); | ||
| 322 | $val=~s/^(.+?)%/$1\*/; | ||
| 323 | $val=~s/\0/%/g; | ||
| 324 | push @pwrparms, (lc $_) . ': "' . $val . '"'; | ||
| 325 | } | ||
| 326 | elsif (ref $power->{$_} eq 'ARRAY') { | ||
| 327 | my @vals = (); | ||
| 328 | foreach my $pwr (@{$power->{$_}}) { | ||
| 329 | my $val = $stmt->get_row_value($pwr, undef, {}); | ||
| 330 | next unless defined($val); | ||
| 331 | $val=~s/%%/\0/g; | ||
| 332 | next if (substr($val,0, 1) eq '%'); | ||
| 333 | $val=~s/^(.+?)%/$1\*/; | ||
| 334 | $val=~s/\0/%/g; | ||
| 335 | push @vals, $val; | ||
| 336 | } | ||
| 337 | push @pwrparms, (lc $_) . ': ("'. | ||
| 338 | join('" and "', @vals) . '")' | ||
| 339 | if scalar @vals; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | $pwrstr .= (($pwrstr ne '') ? ' and (' : '(') . | ||
| 344 | join(') and (', @pwrparms) . ')' | ||
| 345 | if scalar @pwrparms; | ||
| 346 | |||
| 347 | $obj->{url_params}{Power} = ($pwrstr eq '') ? undef : $pwrstr; | ||
| 348 | |||
| 349 | return $obj; | ||
| 350 | } | ||
| 351 | |||
| 352 | sub populate_request { | ||
| 353 | my $obj = shift; | ||
| 354 | my ($subid, $locale, $stmt, $max_pages, $resp_group) = @_; | ||
| 355 | |||
| 356 | my $val; | ||
| 357 | my $req_attrs = $obj->{req_attrs}; | ||
| 358 | my $url_params = $obj->{url_params}; | ||
| 359 | |||
| 360 | $url_params->{ResponseGroup} = $resp_group; | ||
| 361 | $obj->{_max_pages} = $max_pages; | ||
| 362 | return undef | ||
| 363 | if ($req_attrs->{Power} && (! $obj->create_power_search($stmt))); | ||
| 364 | foreach (keys %$req_attrs) { | ||
| 365 | next if ($_ eq 'Power'); | ||
| 366 | |||
| 367 | if ($_ ne 'Keywords') { | ||
| 368 | $url_params->{$_} = | ||
| 369 | $stmt->get_row_value( | ||
| 370 | (ref $req_attrs->{$_} eq 'ARRAY' ? | ||
| 371 | $req_attrs->{$_}[0] : | ||
| 372 | $req_attrs->{$_}), undef, {}), | ||
| 373 | next; | ||
| 374 | } | ||
| 375 | my $keywords = ''; | ||
| 376 | foreach my $key (@{$req_attrs->{$_}}) { | ||
| 377 | $keywords .= $stmt->get_row_value($key, undef, {}), | ||
| 378 | next | ||
| 379 | unless (ref $key eq 'ARRAY'); | ||
| 380 | |||
| 381 | foreach my $keyword (@$key) { | ||
| 382 | my $val = $stmt->get_row_value($keyword, undef, {}); | ||
| 383 | $keywords .= "$val " if defined($val); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | $url_params->{$_} = ($keywords eq '') ? undef : $keywords; | ||
| 387 | } | ||
| 388 | foreach ('MaximumPrice', 'MinimumPrice') { | ||
| 389 | $url_params->{$_} = int($url_params->{$_} * 100) | ||
| 390 | if defined($url_params->{$_}); | ||
| 391 | } | ||
| 392 | return $obj->SUPER::populate_request(@_); | ||
| 393 | } | ||
| 394 | |||
| 395 | 1; | ||
| 396 |