Xmldoom/Definition/Property/Object.pm untested

filter: Statementstmt 0.0% 0 / 234 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 405 CC 44 SCAR 47.9
CC
44
Cov
0%
new
272.0
get
210.0
set
72.0
CRAP 120.4
1  
2 package Xmldoom::Definition::Property::Object;
3 use base qw(Xmldoom::Definition::Property);
4  
5 use DBIx::Romani::Query::Variable;
6 use DBIx::Romani::Query::SQL::Column;
7 use Module::Runtime qw(use_module);
8 use Scalar::Util qw(weaken isweak);
9 use Exception::Class::TryCatch;
10 use strict;
11  
12 use Data::Dumper;
13  
14 sub new
15 {
16 my $class = shift;
17 my $args = shift;
18  
19 my $parent;
20 my $prop_name;
21 my $object_name;
22 my $set_name;
23 my $get_name;
24 my $options_prop;
25 my $options_criteria;
26 my $options_dependent;
27 my $inclusive;
28 my $inter_table;
29 my $key_attributes;
30  
31 if ( ref($args) eq 'HASH' )
32 {
33 $parent = $args->{parent};
34 $prop_name = $args->{name};
35 $object_name = $args->{object_name};
36 $set_name = $args->{set_name};
37 $get_name = $args->{get_name};
38 $options_prop = $args->{options_property};
39 $options_criteria = $args->{options_criteria};
40 $options_dependent = $args->{options_dependent};
41 $inclusive = $args->{inclusive};
42 $inter_table = $args->{inter_table};
43 $key_attributes = $args->{key_attributes};
44 }
45 else
46 {
47 $parent = $args;
48 $prop_name = shift;
49 $object_name = shift;
50  
51 $args = {
52 parent => $parent,
53 name => $prop_name,
54 };
55 }
56  
57 # create ourself
58 my $self = $class->SUPER::new( $args );
59  
60 #
61 # Find our link to the object
62 #
63  
64 my $links = $parent->find_links( $object_name );
65 my $link;
66  
67 if ( scalar @$links == 0 )
68 {
69 die $self->{name} . ": There is no link between " . $parent->get_name() . " and " . $object_name . ". Did you forget to setup a foreign-key?";
70 }
71 elsif ( scalar @$links > 1 )
72 {
73 if ( not defined $key_attributes and not defined $inter_table )
74 {
75 die $self->{name} . ": It is ambiguous which connection to the foreign object is intended in this property. You must specify a <key/> section to your <object/> property.";
76 }
77 else
78 {
79 foreach my $possible ( @$links )
80 {
81 if ( defined $key_attributes and $possible->is_start_column_names( $key_attributes ) or
82 defined $inter_table and $possible->get_start()->get_reference_table_name() )
83 {
84 $link = $possible;
85 last;
86 }
87 }
88  
89 if ( not $link )
90 {
91 die "It is ambiguous which connection to the foreign object is intended in this property. The <key/> section or inter_table='...' of this <object/> property is insufficient to disambiguate.";
92 }
93 }
94 }
95 else
96 {
97 if ( defined $key_attributes )
98 {
99 print STDERR "WARNING: Specifying a key attributes for this object property when it is not ambiguous!\n";
100 }
101  
102 $link = $links->[0];
103 }
104
105 if ( defined $inter_table and $link->get_relationship() ne 'many-to-many' )
106 {
107 die "You set inter_table='...' on this property but it isn't a many-to-many relationship";
108 }
109  
110 # we need to know how this object relates the other
111 my $rel_string;
112 if ( defined $link )
113 {
114 $rel_string = $link->get_relationship();
115 }
116 else
117 {
118 # TODO: a hack for inter-table what not
119 $rel_string = "many-to-many";
120 }
121  
122 # get the component parts
123 my @rel_parts;
124 @rel_parts = split /-/, $rel_string;
125 @rel_parts = ( $rel_parts[0], $rel_parts[2] );
126  
127 # conditionally prepare autoload names based on property type
128 my $prop_type;
129 if ( $rel_parts[1] eq 'one' )
130 {
131 $set_name = "set_$prop_name" if not defined $set_name;
132 $get_name = "get_$prop_name" if not defined $get_name;
133 $prop_type = "inherent";
134 }
135 elsif ( $rel_parts[1] eq 'many' )
136 {
137 $set_name = "add_$prop_name" if not defined $set_name;
138 $get_name = "get_${prop_name}s" if not defined $get_name;
139 $prop_type = "external";
140 }
141  
142 # store all of our infos
143 $self->{object_name} = $object_name;
144 $self->{options_prop} = $options_prop;
145 $self->{options_criteria} = $options_criteria;
146 $self->{options_dependent} = $options_dependent;
147 $self->{inclusive} = $inclusive || 0;
148 $self->{inter_table} = $inter_table;
149 $self->{link} = $link;
150 $self->{prop_type} = $prop_type;
151 $self->{set_name} = $set_name;
152 $self->{get_name} = $get_name;
153 $self->{relationship} = \@rel_parts;
154  
155 bless $self, $class;
156 return $self;
157 }
158  
159 sub get_type { return shift->{prop_type}; }
160 sub get_object_name { return shift->{object_name}; }
161 sub get_link { return shift->{link}; }
162  
163 sub get_autoload_get_list
164 {
165 return [ shift->{get_name} ];
166 }
167  
168 sub get_autoload_set_list
169 {
170 return [ shift->{set_name} ];
171 }
172  
173 sub get_object_definition
174 {
175 my $self = shift;
176  
177 return $self->get_parent()->get_database()->get_object( $self->{object_name} );
178 }
179  
180 sub get_object_class
181 {
182 my $self = shift;
183  
184 my $class = $self->get_object_definition()->get_class();
185  
186 if ( not defined $class )
187 {
188 die "The object '$self->{object_name}' isn't attached to a Perl class. Maybe you forgot to 'use' its module?";
189 }
190  
191 use_module($class);
192  
193 return $class;
194 }
195  
196 sub get_data_type
197 {
198 my $self = shift;
199 my $args = shift;
200  
201 my $object;
202 my $include_options;
203  
204 if ( ref($args) eq 'HASH' )
205 {
206 $object = $args->{object};
207 $include_options = $args->{include_options};
208 }
209  
210 my $value = {
211 type => 'object',
212 object_name => $self->{object_name},
213 };
214  
215 # get the selectable options, baby.
216 if ( $self->{inclusive} and defined $self->{options_prop} and $include_options )
217 {
218 $value->{options} = $self->get_options($object);
219 }
220  
221 return $value;
222 }
223  
224 sub get_options
225 {
226 my $self = shift;
227 my $object = shift;
228  
229 my @options;
230
231 if ( defined $self->{options_prop} )
232 {
233 my $criteria;
234 my $parent;
235  
236 # use this object as the parent, if the options are dependent on it.
237 if ( $self->{options_dependent} )
238 {
239 $parent = $object;
240 }
241  
242 # use the options criteria if specified
243 if ( defined $self->{options_criteria} )
244 {
245 $criteria = $self->{options_criteria}->clone( $parent );
246 }
247 else
248 {
249 $criteria = Xmldoom::Criteria->new( $parent );
250 }
251  
252 my $class = $self->get_object_class();
253  
254 my $rs = $class->SearchRS( $criteria );
255 while ( $rs->next() )
256 {
257 my $obj = $rs->get_object();
258  
259 push @options, {
260 value => $obj->_get_key(),
261 description => $obj->_get_property_value( $self->{options_prop} )
262 };
263 }
264 }
265  
266 return \@options;
267 }
268  
269 sub get
270 {
271 my ($self, $object, $args, $object_data) = (shift, shift, shift, shift);
272  
273 my $database = $self->get_parent()->get_database();
274 my $class = $self->get_object_class();
275  
276 if ( $self->get_type() eq 'inherent' )
277 {
278 if ( defined $object_data->{unsaved_object} and $object_data->{unsaved_object}->{new} )
279 {
280 return $object_data->{unsaved_object};
281 }
282 else
283 {
284 # clear the unsaved object, if it actually exists
285 if ( defined $object_data->{unsaved_object} )
286 {
287 $object_data->{unsaved_object} = undef;
288 }
289
290 # simply load the data
291 my $object_key = { };
292 foreach my $conn ( @{$self->{link}->get_column_names()} )
293 {
294 $object_key->{$conn->{foreign_column}} = $object->_get_attr($conn->{local_column});
295 }
296  
297 my $data;
298 try eval
299 {
300 $data = $self->get_object_definition()->load( $object_key );
301 };
302 if ( my $err = catch )
303 {
304 return undef;
305 }
306  
307 # return the appropriate object
308 return $class->new(undef, {
309 data => $data,
310 parent => $object,
311 parent_link => $self->{link}
312 });
313 }
314 }
315 elsif ( $self->get_type() eq 'external' )
316 {
317 my @ret;
318  
319 if ( defined $object_data )
320 {
321 # check the list for undef objects (because they are weak references)
322 # and objects that have been saved.
323 foreach my $unsaved ( @{$object_data->{unsaved_list}} )
324 {
325 if ( defined $unsaved and $unsaved->{new} )
326 {
327 push @ret, $unsaved;
328 }
329 }
330 if ( scalar @{$object_data->{unsaved_list}} != scalar @ret )
331 {
332 # copy into unsaved if there were any changes
333 $object_data->{unsaved_list} = [ @ret ];
334 }
335 }
336  
337 if ( not $object->{new} )
338 {
339 my $criteria = Xmldoom::Criteria->new( $object );
340  
341 # pass any arguments as property equations on the criteria.
342 if ( $self->{relationship}->[1] eq 'many' )
343 {
344 if ( ref($args) eq 'HASH' )
345 {
346 while( my ($key, $val) = each %$args )
347 {
348 my $prop = sprintf "%s/%s", $self->{object_name}, $key;
349 $criteria->add( $prop, $val );
350 }
351 }
352 }
353  
354 # if this is many-to-many, then we manually join the tables because
355 # we want to make sure the selected connection is used.
356 if ( $self->{link}->get_relationship() eq 'many-to-many' )
357 {
358 foreach my $fn ( @{$self->{link}->get_foreign_keys()} )
359 {
360 foreach my $ref ( @{$fn->get_column_names()} )
361 {
362 $criteria->join_attr(
363 sprintf( "%s/%s", $ref->{local_table}, $ref->{local_column} ),
364 sprintf( "%s/%s", $ref->{foreign_table}, $ref->{foreign_column} )
365 );
366 }
367 }
368 }
369  
370 # execute
371 @ret = $class->Search( $criteria );
372 }
373  
374 return wantarray ? @ret : \@ret;
375 }
376 }
377  
378 sub get_value_description
379 {
380 my ($self, $value) = @_;
381  
382 my $prop = $value->_get_property( $self->{options_prop} );
383  
384 return $prop->get();
385 }
386  
387 sub set
388 {
389 my ($self, $object, $args, $object_data) = @_;
390  
391 if ( $self->get_type() eq 'inherent' )
392 {
393 # we are simply setting a value
394 my $value = $args;
395  
396 # link the attributes of the value to ours
397 foreach my $conn ( @{$self->{link}->get_column_names()} )
398 {
399 $object->_link_attr( $conn->{local_column}, $value, $conn->{foreign_column} );
400 }
401  
402 # this object will be saved in the same transaction as us so that no
403 # changes are lost.
404 $object->_add_dependent( $value );
405  
406 # if this value is unsaved, we need to hang onto it
407 if ( $value->{new} )
408 {
409 $object_data->{unsaved_object} = $value;
410 weaken $object_data->{unsaved_object};
411 }
412 }
413 elsif ( $self->get_type() eq 'external' )
414 {
415 # Here we accept an array of hashs (or a single hash), to "add", creating
416 # and returning new objects for each.
417  
418 if ( ref($args) ne 'ARRAY' )
419 {
420 $args = [ $args ];
421 }
422  
423 my $database = $self->get_parent()->get_database();
424 my $class = $self->get_object_class();
425 my @ret;
426  
427 # create new objects
428 foreach my $props ( @$args )
429 {
430 push @ret, $class->new($props, { parent => $object });
431 }
432  
433 # create the unsaved objects list
434 if ( not defined $object_data->{unsaved_list} )
435 {
436 $object_data->{unsaved_list} = [ ];
437 }
438  
439 # add weak references to these new objects in the unsaved objects list
440 foreach my $child ( @ret )
441 {
442 push @{$object_data->{unsaved_list}}, $child;
443 weaken $object_data->{unsaved_list}->[-1];
444 }
445  
446 if ( scalar @$args == 1 )
447 {
448 return $ret[0];
449 }
450  
451 return wantarray ? @ret : \@ret;
452 }
453 }
454  
455 sub get_query_lval
456 {
457 my $self = shift;
458  
459 my @ret;
460 foreach my $conn ( @{$self->{link}->get_start()->get_column_names()} )
461 {
462 push @ret, DBIx::Romani::Query::SQL::Column->new( $conn->{local_table}, $conn->{local_column} );
463 }
464  
465 return \@ret;
466 }
467  
468 sub get_query_rval
469 {
470 my ($self, $value) = @_;
471  
472 my @ret;
473 foreach my $conn ( @{$self->{link}->get_start()->get_column_names()} )
474 {
475 push @ret, DBIx::Romani::Query::SQL::Literal->new( $value->_get_attr($conn->{foreign_column}) );
476 }
477  
478 return \@ret;
479 }
480  
481 sub autoload
482 {
483 my ($self, $object, $func_name) = (shift, shift, shift);
484  
485 if ( $func_name eq $self->{set_name} )
486 {
487 $self->set($object, @_);
488 }
489 elsif ( $func_name eq $self->{get_name} )
490 {
491 return $self->get($object, @_);
492 }
493 else
494 {
495 die "$func_name is not defined by this property";
496 }
497 }
498  
499 1;
500