RWDE/SearchClient/Indexable.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 142 CC 9 SCAR 20.1
CC
9
Cov
0%
search_index
20.0
delete_from_index
12.0
add_to_index
6.0
CRAP 7.5
1 package RWDE::SearchClient::Indexable;
2  
3 use strict;
4 use warnings;
5  
6 use Error qw(:try);
7  
8 use RWDE::Exceptions;
9 use RWDE::Search;
10  
11 use vars qw($VERSION);
12 $VERSION = sprintf "%d", q$Revision: 509 $ =~ /(\d+)/;
13  
14 =pod
15  
16 =head2 add_to_index
17  
18 Add the content defined in my derived to the remote searchable index
19  
20 =cut
21  
22 sub add_to_index {
23 my ($self, $params) = @_;
24  
25 $self->_check_index();
26  
27 my $index_path;
28 if (defined($$params{index_path})) {
29 $index_path = $$params{index_path};
30 }
31 else {
32 $index_path = $self->get_index_path();
33 }
34  
35 my $index_data;
36  
37 # This try block exists because errors/exceptions are behaving VERY strange here.
38 # Try using an error statement in this method!
39 try {
40 $index_data = $self->get_index_hash();
41 }
42  
43 catch Error with {
44 my $ex = shift;
45  
46 $self->syslog_msg('info', "$ex");
47 $self->syslog_msg('info', "You likely have attempted to add an invalid class term to a class index.");
48 $ex->throw();
49 };
50  
51 RWDE::Search->index_document(
52 {
53 index_fields => $self->get_index_fields(),
54 index_path => $index_path,
55 index_data => $index_data
56 }
57 );
58  
59 return ();
60 }
61  
62  
63 sub optimize_index {
64 my ($self, $params) = @_;
65
66 # dynamically require the object type we are going to try and use
67 my $term = RWDE::AbstractFactory->instantiate({ class => $self });
68  
69 $term->_check_index();
70  
71 my $index_path;
72 if (defined($$params{index_path})) {
73 $index_path = $$params{index_path};
74 }
75 else {
76 $index_path = $term->get_index_path();
77 }
78  
79 RWDE::Search->optimize_index(
80 {
81 index_path => $index_path,
82 index_fields => $term->get_index_fields(),
83 }
84 );
85  
86 return ();
87 }
88  
89  
90 # Delete the defined index_item from the remote searchable index.
91  
92 sub delete_from_index {
93 my ($self, $params) = @_;
94  
95 $self->_check_index();
96  
97 throw RWDE::DataNotFoundException({ info => "Item to delete from search index was not specified." })
98 unless defined $$params{item_id};
99  
100 my $index_path;
101  
102 if (defined($$params{index_path})) {
103 $index_path = $$params{index_path};
104 }
105  
106 else {
107 $index_path = $self->get_index_path();
108 }
109  
110 RWDE::Search->delete_document(
111 {
112 index_path => $index_path,
113 index_fields => $self->get_index_fields(),
114 item_id => $$params{item_id} # "primary key" to delete from the search index
115 }
116 );
117  
118 return ();
119 }
120  
121  
122 # search for content in the index
123 sub search_index {
124 my ($self, $params) = @_;
125  
126 # create the object type we are going to use to
127 # get the static data through polymorphism
128 my $term = RWDE::AbstractFactory->instantiate({ class => $self });
129  
130 $term->_check_index();
131  
132 throw RWDE::DataNotFoundException({ info => "No items matched your query" })
133 unless defined $$params{query} || defined $$params{query_text};
134  
135 my ($docs, $total) = @{RWDE::Search->search_index(
136 {
137 maxreturn => $$params{maxreturn},
138 startidx => $$params{startidx},
139 query => $$params{query},
140 query_text => $$params{query_text},
141 category => $$params{category},
142 index_path => $$params{index_path},
143 index_fields => $term->get_index_fields()
144 }
145 )};
146  
147 unless (ref $docs eq 'ARRAY') {
148 throw RWDE::DataBadException({ info => "Searching is temporarily unavailable, please try again later." });
149 }
150  
151 throw RWDE::DataNotFoundException({ info => "No items matched your query" })
152 unless (scalar @{$docs} > 0);
153  
154 return ($docs, $total);
155 }
156  
157 =pod
158  
159 =head2 get_index_hash
160  
161 Return back the hash defined by the object as targets for indexing
162 specified in an object for indexing as .
163  
164 =cut
165  
166 sub get_index_hash {
167 my ($self, $params) = @_;
168  
169 my $field_values;
170  
171 foreach my $key (keys %{ $self->get_index_fields() }) {
172 $$field_values{$key} = $self->$key;
173 }
174  
175 return $field_values;
176 }
177  
178 sub get_index_fields {
179 my ($self, $number) = @_;
180  
181 $self->check_object();
182  
183 return $self->{_index_fields};
184 }
185  
186 #Determine if a class supports indexing or not. Private method.
187  
188 sub _check_index {
189 my ($self, $params) = @_;
190  
191 my $class = ref $self || $self;
192  
193 if (not defined $self->get_index_fields()) {
194 throw RWDE::DevelException({ info => "Class $class doesn't support indexing" });
195 }
196  
197 return ();
198 }
199  
200 sub get_index_table {
201 my ($self, $number) = @_;
202
203 return $self->{_table};
204 }
205  
206 sub get_index_divide {
207 my ($self, $number) = @_;
208
209 return $self->{_index_divide};
210 }
211  
212  
213 1;