SIAM/Object.pm

filter: Statementstmt 67.5% 104 / 154 Branchbran 59.3% 19 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 20 / 30 Podpod 100% 23 / 23 totaltotal 69.4% 166 / 239 CC 24 SCAR 15.4
CC
24
Cov
66%
get_objects_by_attribute
20.0
new
8.8
_walk_recursive
5.0
CRAP 4.7
« SIAM/Driver/Simple.pm ↑ Summary SIAM/User.pm »
1 package SIAM::Object;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Log::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use SIAM::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # default log manager
10 3 our $logmgr = Log::Handler->new(
11 'screen' =>
12 {'log_to' => 'STDERR',
13 'maxlevel' => 'warning',
14 'minlevel' => 'emergency'});
15  
16  
17 =head1 NAME
18  
19  
20 SIAM::Object - the base class for all SIAM object (including the root).
21  
22  
23 =cut
24  
25  
26 =head1 SYNOPSIS
27  
28  
29  
30 =head1 INSTANCE METHODS
31  
32 =head2 new
33  
34 $new_object = new SIAM::Object($driver, $id)
35  
36 Instantiates a new object. The method expects a driver object and an Object ID.
37  
38 =cut
39  
40 sub new
41 115 ▶{
Subroutine new: called CC=8 SCAR=21.8
Pod documented
42 115 my $class = shift;
43 115 my $driver = shift;
44 115 my $id = shift;
45  
46 115 my $self = {};
47 115 bless $self, $class;
48  
49 115 $self->{'_attr'} = {'siam.object.id' => $id,
50 'siam.object.class' => $class};
51 115 $self->{'_driver'} = $driver;
52  
53 # retrieve attributes from the driver unless I am root
54 115 ▶ if( not $self->is_root )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_root) 114 1 115
55 {
56 114 ▶ if( not $driver->fetch_attributes($self->{'_attr'}) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver->fetch_attributes($self->{"_attr"})) 0 114 114
57 {
58 0 SIAM::Object->error('Failed fetching attributes for ' . $id);
59 0 return undef;
60 }
61  
62 # set siam.object.complete to true if undefined
63 114 ▶ if( not defined($self->{'_attr'}{'siam.object.complete'}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_attr"}{"siam.object.complete"}) 114 0 114
64 {
65 114 $self->{'_attr'}{'siam.object.complete'} = 1;
66 }
67  
68 # set siam.object.has_reports to false if undefined
69 114 ▶ if( not defined($self->{'_attr'}{'siam.object.has_reports'}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_attr"}{"siam.object.has_reports"}) 110 4 114
70 {
71 110 $self->{'_attr'}{'siam.object.has_reports'} = 0;
72 }
73
74 # check if mandatory attributes are defined by the driver
75 114 ▶ if( $self->can('_mandatory_attributes') )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_mandatory_attributes")) 114 0 114
76 {
77 114 foreach my $attr (@{ $self->_mandatory_attributes() })
78 {
79 254 ▶ if( not defined($self->{'_attr'}{$attr}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_attr"}{$attr}) 0 254 254
80 {
81 0 SIAM::Object->error
82 ('Driver did not fetch a mandatory attribute "' .
83 $attr . '" for object ID "' . $id . '"');
84 0 return undef;
85 }
86 }
87 }
88 }
89  
90 115 return $self;
91 }
92  
93  
94 =head2 instantiate_object
95  
96 Expects the object class and ID. Returns an object retrieved from the driver.
97  
98 =cut
99  
100 sub instantiate_object
101 6 ▶{
Subroutine instantiate_object: called CC=2 SCAR=7.5
Pod documented
102 6 my $self = shift;
103 6 my $obj_class = shift;
104 6 my $obj_id = shift;
105  
106 6 my $obj = eval 'new ' . $obj_class . '($self->_driver, $obj_id)';
107 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
108 {
109 0 $self->error('Cannot instantiate object of class "' . $obj_class .
110 '" and ID "' . $obj_id . '": ' . $@);
111 0 return undef;
112 }
113
114 6 return $obj;
115 }
116  
117  
118 =head2 get_contained_objects
119  
120 my $list = $object->get_contained_objects($classname, $options);
121  
122 Fetches the list of contained objects of a given class. Returns arrayref of
123 C<SIAM::Object> instances. This is the preferred method of instantiating new
124 objects instead of manually calling the C<new> method.
125  
126 It is assumed that the class name is already known to Perl, and the
127 corresponding module was loaded with C<use> or C<require>.
128  
129 Without the options, the method retrieves all available objects. Options may
130 define a filter criteria as follows:
131  
132 my $list =
133 $siam->get_contained_objects('SIAM::Contract', {
134 'match_attribute' => [ 'siam.object.access_scope_id',
135 ['SCOPEID01', 'SCOPEID02'] ]
136 });
137  
138 Currently only one filter condition is supported.
139  
140 =cut
141  
142 sub get_contained_objects
143 55 ▶{
Subroutine get_contained_objects: called CC=4 SCAR=14.0
Pod documented
144 55 my $self = shift;
145 55 my $classname = shift;
146 55 my $options = shift;
147  
148 55 my $driver = $self->_driver;
149 55 my $ids =
150 $driver->fetch_contained_object_ids($self->id, $classname, $options);
151
152 55 my $ret = [];
153 55 foreach my $id (@{$ids})
154 {
155 100 my $obj = eval($classname . '->new($driver, $id)');
156  
157 100 ▶ if( $@ )
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 100 100
elsif (defined $obj) { } 100 0 100
158 {
159 0 SIAM::Object->error($@);
160 }
161 elsif( defined($obj) )
162 {
163 100 push(@{$ret}, $obj);
164 }
165 }
166  
167 55 return $ret;
168 }
169  
170  
171 =head2 get_objects_by_attribute
172  
173 my $list = $siam->get_objects_by_attribute(
174 'SIAM::Device', 'siam.device.inventory_id', $id);
175  
176 The method takes 3 arguments: class name, attribute name, and attribute
177 value. It returns an arrayref of objects matching the attribute. Empty
178 arrayref is returned if no objects match the criteria.
179  
180 =cut
181  
182 sub get_objects_by_attribute
183 0 ▶{
Subroutine get_objects_by_attribute: not called CC=4 SCAR=30.0
Pod documented
184 0 my $self = shift;
185 0 my $classname = shift;
186 0 my $attr = shift;
187 0 my $value = shift;
188  
189 0 my $driver = $self->_driver;
190 0 my $ids =
191 $driver->fetch_object_ids_by_attribute($classname, $attr, $value);
192
193 0 my $ret = [];
194 0 foreach my $id (@{$ids})
195 {
196 0 my $obj = eval($classname . '->new($driver, $id)');
197
198 0 ▶ if( $@ )
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (defined $obj) { } 0 0 0
199 {
200 0 SIAM::Object->error($@);
201 }
202 elsif( defined($obj) )
203 {
204 0 push(@{$ret}, $obj);
205 }
206 }
207  
208 0 return $ret;
209 }
210
211  
212  
213 =head2 deep_walk_contained_objects
214  
215 my $list = $object->deep_walk_contained_objects($classname);
216  
217 The method walks down the tree of contained objects and retrieves a list
218 of all found objects. It returns an array reference with all found objects;
219  
220 =cut
221  
222  
223 sub deep_walk_contained_objects
224 1 ▶{
Subroutine deep_walk_contained_objects: called CC=1 SCAR=0.0
Pod documented
225 1 my $self = shift;
226 1 my $classname = shift;
227  
228 # id => objref
229 # the hash is needed to avoid object duplications
230 1 my $results = {};
231 1 $self->_walk_recursive($classname, $results);
232  
233 1 return [values %{$results}];
234 }
235  
236  
237 sub _walk_recursive
238 10 ▶{
Subroutine _walk_recursive: called CC=5 SCAR=16.1
239 10 my $self = shift;
240 10 my $classname = shift;
241 10 my $results = shift;
242  
243 10 my $driver = $self->_driver;
244 10 my $contained_classes = $driver->fetch_contained_classes($self->id());
245  
246 10 foreach my $obect_class (@{$contained_classes})
247 {
248 7 my $list = $self->get_contained_objects($obect_class);
249  
250 7 ▶ if( $obect_class eq $classname )
Branch: 1 branch
BranchTrueFalseTotal
if ($obect_class eq $classname) 2 5 7
251 {
252 2 foreach my $item (@{$list})
253 {
254 3 $results->{$item->id()} = $item;
255 }
256 }
257
258 7 foreach my $item (@{$list})
259 {
260 9 $item->_walk_recursive($classname, $results);
261 }
262 }
263 10 return;
264 }
265  
266  
267  
268 =head2 id
269  
270 Returns a value of C<siam.object.id> attribute
271  
272 =cut
273  
274 360 ▶sub id { shift->attr('siam.object.id') }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
275  
276  
277 =head2 objclass
278  
279 Returns the value of C<siam.object.class> attribute.
280  
281 =cut
282
283 59 ▶sub objclass { shift->attr('siam.object.class') }
Subroutine objclass: called CC=1 SCAR=0.0
Pod documented
284  
285  
286 =head2 attr
287  
288 $val = $contract->attr('siam.contract.inventory_id');
289  
290 Returns a value of an attribute.
291  
292 =cut
293  
294 sub attr
295 457 ▶{
Subroutine attr: called CC=1 SCAR=0.0
Pod documented
296 457 my $self = shift;
297 457 my $key = shift;
298 457 return $self->{'_attr'}{$key};
299 }
300  
301  
302 =head2 attributes
303  
304 Returns a hashref with copies of all object attributes.
305  
306 =cut
307  
308 sub attributes
309 50 ▶{
Subroutine attributes: called CC=2 SCAR=6.9
Pod documented
310 50 my $self = shift;
311  
312 50 my $ret = {};
313 50 ▶ while( my($key, $val) = each %{$self->{'_attr'}} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_attr"};}) 335 50 385
314 {
315 335 $ret->{$key} = $val;
316 }
317 50 return $ret;
318 }
319  
320  
321 =head2 is_complete
322  
323 Returns the value of C<siam.object.complete>.
324  
325 =cut
326  
327 sub is_complete
328 0 ▶{
Subroutine is_complete: not called CC=1 SCAR=6.9
Pod documented
329 0 my $self = shift;
330 0 my $key = shift;
331 0 return $self->{'_attr'}{'siam.object.complete'};
332 }
333  
334
335  
336 =head2 computable
337  
338 $val = $contract->computable('siam.contract.content_md5hash');
339  
340 Returns a value of a computable.
341  
342 =cut
343  
344 sub computable
345 3 ▶{
Subroutine computable: called CC=1 SCAR=0.0
Pod documented
346 3 my $self = shift;
347 3 my $key = shift;
348 3 return $self->_driver->fetch_computable($self->id, $key);
349 }
350  
351  
352 =head2 set_condition
353  
354 $dataelement->set_condition('torrus.import_successful', 1);
355  
356 The SIAM client application may use this method to send a (key, value)
357 pair to the driver and tell it about some state update. The condition
358 names and accepted values are defined by the driver and are
359 driver-specific. This is a one-way communication, and there is no way to
360 read the condition value.
361  
362 =cut
363  
364 sub set_condition
365 0 ▶{
Subroutine set_condition: not called CC=1 SCAR=6.9
Pod documented
366 0 my $self = shift;
367 0 my $key = shift;
368 0 my $value = shift;
369 0 $self->_driver->set_condition($self->id, $key, $value);
370 }
371  
372 =head2 is_root
373  
374 Returns true if the object is a root.
375  
376 =cut
377  
378 215 ▶sub is_root { (shift->id) eq 'SIAM.ROOT' }
Subroutine is_root: called CC=1 SCAR=0.0
Pod documented
379  
380  
381 =head2 is_predefined
382  
383 Returns true if the object is a predefined object (the one with the ID
384 starting with I<SIAM.>)
385  
386 =cut
387  
388 0 ▶sub is_predefined { substr(shift->id, 0, 5) eq 'SIAM.' }
Subroutine is_predefined: not called CC=1 SCAR=6.9
Pod documented
389  
390  
391 =head2 contained_in
392  
393 Returns the object that contains this object. Returns undef if container
394 is the root object.
395  
396 =cut
397  
398 sub contained_in
399 2 ▶{
Subroutine contained_in: called CC=2 SCAR=6.9
Pod documented
400 2 my $self = shift;
401  
402 2 my $attr = $self->_driver->fetch_container($self->id);
403 2 ▶ if( $attr->{'siam.object.id'} eq 'SIAM.ROOT' )
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"siam.object.id"} eq "SIAM.ROOT") 1 1 2
404 {
405 1 return undef;
406 }
407
408 return $self->instantiate_object($attr->{'siam.object.class'},
409 1 $attr->{'siam.object.id'});
410 }
411
412  
413  
414 =head2 get_reports
415  
416 Returns arrayref with contained SIAM::Report objects
417  
418 =cut
419  
420 sub get_reports
421 1 ▶{
Subroutine get_reports: called CC=2 SCAR=7.6
Pod documented
422 1 my $self = shift;
423  
424 1 ▶ if( $self->attr('siam.object.has_reports') )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->attr('siam.object.has_reports')) { } 1 0 1
425 {
426 1 return $self->get_contained_objects('SIAM::Report');
427 }
428 else
429 {
430 0 return [];
431 }
432 }
433  
434  
435  
436 =head1 CLASS METHODS
437  
438 =head2 validate_driver
439  
440 Takes a driver object as an argument and verifies if it implements all
441 required methods. returns true if all required methods are present. It
442 issues error messages in case of missing methods.
443  
444 =cut
445  
446 sub validate_driver
447 1 ▶{
Subroutine validate_driver: called CC=3 SCAR=11.8
Pod documented
448 1 my $class = shift;
449 1 my $driver = shift;
450  
451 1 my $ok = 1;
452 1 foreach my $m ('fetch_attributes', 'fetch_contained_object_ids',
453 'fetch_contained_classes', 'fetch_container',
454 'fetch_object_ids_by_attribute', 'set_condition',
455 'manifest_attributes', 'connect', 'disconnect')
456 {
457 9 ▶ if( not $driver->can($m) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver->can($m)) 0 9 9
458 {
459 0 SIAM::Object->error
460 ('The driver of class ' . ref($driver) . ' does not ' .
461 'implement a required method: ' . $m);
462 0 $ok = 0;
463 }
464 }
465  
466 1 return $ok;
467 }
468  
469  
470  
471  
472  
473 =head2 set_log_manager
474  
475 Sets a log manager for SIAM objects. Note that it does not set the log
476 manager for the driver. The default log manager is a C<Log::Handler>
477 object with STDERR output of warnings and errors. The method expects one
478 argument, an object which implements the following methods:
479  
480 =over 4
481  
482 =item * debug
483  
484 =item * info
485  
486 =item * warn
487  
488 =item * error
489  
490 =back
491  
492 Classes that suit as log managers: C<Log::Handler>, C<Log::Log4perl>, ...
493  
494 =cut
495  
496 sub set_log_manager
497 0 ▶{
Subroutine set_log_manager: not called CC=1 SCAR=6.9
Pod documented
498 0 my $class = shift;
499 0 $logmgr = shift;
500 }
501  
502  
503 =head2 get_log_manager
504  
505 Returns the current logger object.
506  
507 =cut
508  
509 sub get_log_manager
510 1 ▶{
Subroutine get_log_manager: called CC=1 SCAR=0.0
Pod documented
511 1 return $logmgr;
512 }
513  
514  
515 =head2 debug, info, warn, error
516  
517 These methods dispatch a message to the log manager. If the log manager
518 is undefined, all except C<debug()> print the message to STDERR with a
519 preceeding timestamp.
520  
521 =cut
522  
523 sub debug
524 0 ▶{
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
525 0 my $class = shift;
526 0 my $msg = shift;
527 0 $logmgr->debug($msg);
528 }
529  
530 sub info
531 0 ▶{
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
532 0 my $class = shift;
533 0 my $msg = shift;
534 0 $logmgr->info($msg);
535 }
536  
537 sub warn
538 0 ▶{
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
539 0 my $class = shift;
540 0 my $msg = shift;
541 0 $logmgr->warn($msg);
542 }
543  
544 sub error
545 0 ▶{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
546 0 my $class = shift;
547 0 my $msg = shift;
548 0 $logmgr->error($msg);
549 }
550  
551  
552  
553 =head1 PRIVATE METHODS
554  
555 =head2 _driver
556  
557 Returns the driver object
558  
559 =cut
560  
561 140 ▶sub _driver { shift->{'_driver'} }
Subroutine _driver: called CC=1 SCAR=0.0
562  
563  
564 =head2 _print_stderr
565  
566 Prints a message to STDERR with a preceeding timestamp
567  
568 =cut
569  
570 sub _print_stderr
571 0 ▶{
Subroutine _print_stderr: not called CC=1 SCAR=6.9
572 0 my $class = shift;
573 0 my $msg = shift;
574  
575 0 print STDERR '[' . scalar(localtime(time())) . '] ' . $msg . "\n";
576 }
577  
578  
579  
580 3 1;
581  
582 # Local Variables:
583 # mode: cperl
584 # indent-tabs-mode: nil
585 # cperl-indent-level: 4
586 # cperl-continued-statement-offset: 4
587 # cperl-continued-brace-offset: -4
588 # cperl-brace-offset: 0
589 # cperl-label-offset: -2
590 # End:
« SIAM/Driver/Simple.pm ↑ Summary SIAM/User.pm »