OpenInteract2/ResultsIterator.pm

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 8 / 8 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« OpenInteract2/FullTextIterator.pm ↑ Summary
1 package OpenInteract2::ResultsIterator;
2  
3 # $Id: ResultsIterator.pm,v 1.7 2005/03/18 04:09:48 lachoy Exp $
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw( SPOPS::Iterator );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use Log::Log4perl qw( get_logger );
8 use OpenInteract2::Constants qw( :log );
9 use SPOPS::Iterator qw( ITER_IS_DONE );
10  
11 $OpenInteract2::ResultsIterator::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
12  
13 my ( $log );
14  
15 sub initialize {
16 my ( $self, $p ) = @_;
17  
18 $log ||= get_logger( LOG_APP );
19  
20 $self->{_SEARCH_RESULTS} = $p->{results};
21 $self->{_SEARCH_EXTRA_NAME} = $p->{extra_name};
22 $self->{_SEARCH_COUNT} = 1;
23 $self->{_SEARCH_RAW_COUNT} = 0;
24 $self->{_SEARCH_OFFSET} = $p->{min};
25 $self->{_SEARCH_MAX} = $p->{max};
26 }
27  
28  
29 sub fetch_object {
30 my ( $self ) = @_;
31  
32 # Get the info for retrieving the object
33  
34 my $current_count = $self->{_SEARCH_RAW_COUNT};
35 my $this_result = $self->{_SEARCH_RESULTS}->[ $current_count ];
36 my $object_class = $this_result->[0];
37 my $object_id = $this_result->[1];
38  
39 $log->is_debug &&
40 $log->debug( "Item '$self->{_SEARCH_COUNT}' trying to fetch ",
41 "[$object_class: $object_id]" );
42  
43 unless ( $object_class and $object_id ) {
44 $log->is_info &&
45 $log->info( "No class or ID found in iterator, all done." );
46 return ITER_IS_DONE;
47 }
48  
49 my $object = eval {
50 $object_class->fetch( $object_id,
51 { skip_security => $self->{_SKIP_SECURITY} } )
52 };
53  
54 if ( $@ ) {
55 if ( ref $@ and $@->isa( 'SPOPS::Exception::Security' ) ) {
56 $log->is_info &&
57 $log->info( "Caught security exception; skip to next" );
58 $self->{_SEARCH_RAW_COUNT}++;
59 return $self->fetch_object;
60 }
61 $log->is_debug && $log->debug( "Caught non-security exception: $@" );
62 die $@;
63 }
64  
65 unless ( $object ) {
66 $log->is_debug &&
67 $log->debug( "Iterator is depleted (no object fetched), ",
68 "notify parent" );
69 return ITER_IS_DONE;
70 }
71  
72 $log->is_debug && $log->debug( "Fetched object ok, checking min/max" );
73  
74 # Using min/max and haven't reached it yet
75  
76 if ( $self->{_SEARCH_OFFSET} and
77 ( $self->{_SEARCH_COUNT} < $self->{_SEARCH_OFFSET} ) ) {
78 $self->{_SEARCH_COUNT}++;
79 $self->{_SEARCH_RAW_COUNT}++;
80 $log->is_debug &&
81 $log->debug( "Haven't reached min threshold" );
82 return $self->fetch_object;
83 }
84  
85 if ( $self->{_SEARCH_MAX} and
86 ( $self->{_SEARCH_COUNT} > $self->{_SEARCH_MAX} ) ) {
87 $log->is_debug && $log->debug( "Over max threshold, we're all done" );
88 return ITER_IS_DONE;
89 }
90  
91 # Ok, we've gone through all the necessary contortions -- we can
92 # actually return the object. Finish up.
93  
94 $log->is_debug &&
95 $log->debug( "Min/max passed ok; assign extra data then finish" );
96  
97 my %extra_data = ();
98 if ( $self->{_SEARCH_EXTRA_NAME} ) {
99 my $max_result = scalar( @{ $this_result } ) - 1;
100 my @extra_data = @{ $this_result }[ 2..$max_result ];
101 foreach my $name ( @{ $self->{_SEARCH_EXTRA_NAME} } ) {
102 my $extra_value = shift @extra_data;
103 $log->is_debug && $log->debug( "Adding extra data '$name' => ",
104 "'$extra_value'" );
105 $object->{ "tmp_$name" } = $extra_value;
106 $extra_data{ $name } = $extra_value;
107 }
108 }
109  
110 $self->{_SEARCH_RAW_COUNT}++;
111 $self->{_SEARCH_COUNT}++;
112  
113 return wantarray ? ( $object, $self->{_SEARCH_COUNT}, \%extra_data ) : $object;
114 }
115  
116 1;
117  
118 __END__
« OpenInteract2/FullTextIterator.pm ↑ Summary