lib/MOSES/MOBY/Package.pm untested

filter: Statementstmt 0.0% 0 / 272 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 57 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 516 CC 49 SCAR 28.9
CC
49
Cov
0%
addData
90.0
toXML
42.0
getData
30.0
CRAP 18.0
1 #-----------------------------------------------------------------
2 # MOSES::MOBY::Package
3 # Author: Edward Kawas <edward.kawas@gmail.com>,
4 # Martin Senger <martin.senger@gmail.com>
5 # For copyright and disclaimer see below.
6 #
7 # $Id: Package.pm,v 1.4 2008/04/29 19:45:01 kawas Exp $
8 #-----------------------------------------------------------------
9  
10 #-----------------------------------------------------------------
11 #
12 # MOSES::MOBY::Package
13 #
14 #-----------------------------------------------------------------
15 package MOSES::MOBY::Package;
16 use base qw( MOSES::MOBY::Base );
17 use XML::LibXML;
18 use MOSES::MOBY::Tags;
19 use MOSES::MOBY::ServiceException;
20 use strict;
21  
22 # add versioning to this module
23 use vars qw /$VERSION/;
24 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
25  
26 #-----------------------------------------------------------------
27 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
28 #-----------------------------------------------------------------
29 {
30 my %_allowed =
31 (
32 authority => undef,
33 jobs => {type => 'MOSES::MOBY::Job', is_array => 1},
34 exceptions => {type => 'MOSES::MOBY::ServiceException', is_array => 1},
35 serviceNotes => undef,
36 cdata => {type => MOSES::MOBY::Base->BOOLEAN},
37 );
38  
39 sub _accessible {
40 my ($self, $attr) = @_;
41 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
42 }
43 sub _attr_prop {
44 my ($self, $attr_name, $prop_name) = @_;
45 my $attr = $_allowed {$attr_name};
46 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
47 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
48 }
49 }
50  
51 #-----------------------------------------------------------------
52 # init
53 #-----------------------------------------------------------------
54 sub init {
55 my ($self) = shift;
56 $self->SUPER::init();
57 $self->cdata ('no');
58 }
59  
60 #-----------------------------------------------------------------
61 # job_by_id
62 #-----------------------------------------------------------------
63  
64 =head2 job_by_id
65  
66 In this package, find and return a C<MOSES::MOBY::Job> object with the given
67 ID. Or throw an exception if such job does not exist. (TBD)
68  
69 =cut
70  
71 sub job_by_id {
72 my ($self, $jobId) = @_;
73 foreach my $job (@{ $self->jobs }) {
74 return $job if $job->jid eq $jobId;
75 }
76 $self->throw ("Job '$jobId' not found.");
77 }
78  
79  
80 #-----------------------------------------------------------------
81 # size
82 #-----------------------------------------------------------------
83 sub size {
84 my $self = shift;
85 return 0 unless $self->jobs;
86 return 0+@{ $self->jobs };
87 }
88  
89 #-----------------------------------------------------------------
90 # toXML
91 #-----------------------------------------------------------------
92 sub toXML {
93 my $self = shift;
94 $self->increaseXMLCounter;
95 my $root = $self->createXMLElement (MOBY);
96 my $elemContent = $self->createXMLElement (MOBYCONTENT);
97 $self->setXMLAttribute ($elemContent, AUTHORITY, $self->authority);
98  
99 if ($self->serviceNotes or $self->{exceptions}) {
100 my $sNotes = $self->createXMLElement (SERVICENOTES);
101 if ($self->serviceNotes) {
102 my $notes = $self->createXMLElement (NOTES);
103 if ($self->cdata) {
104 $notes->appendChild (XML::LibXML::CDATASection->new ($self->serviceNotes));
105 } else {
106 $notes->appendText ($self->serviceNotes);
107 }
108 $sNotes->appendChild ($notes);
109 }
110 if ($self->exceptions) {
111 foreach my $exception (@{ $self->exceptions }) {
112 $sNotes->appendChild ($exception->toXML);
113 }
114 }
115 $elemContent->appendChild ($sNotes);
116 }
117
118 if ($self->jobs) {
119 foreach my $job (@{ $self->jobs }) {
120 $elemContent->appendChild ($job->toXML);
121 }
122 }
123  
124 $root->appendChild ($elemContent);
125 return $self->closeXML ($root);
126 }
127  
128  
129 #-----------------------------------------------------------------
130 #
131 # MOSES::MOBY::DataElement
132 #
133 #-----------------------------------------------------------------
134 package MOSES::MOBY::DataElement;
135 use base qw( MOSES::MOBY::Base );
136 use XML::LibXML;
137 use MOSES::MOBY::Tags;
138 use strict;
139  
140 #-----------------------------------------------------------------
141 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
142 #-----------------------------------------------------------------
143 {
144 my %_allowed =
145 (
146 name => undef,
147 );
148  
149 sub _accessible {
150 my ($self, $attr) = @_;
151 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
152 }
153 sub _attr_prop {
154 my ($self, $attr_name, $prop_name) = @_;
155 my $attr = $_allowed {$attr_name};
156 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
157 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
158 }
159 }
160  
161 sub init {
162 my ($self) = shift;
163 $self->SUPER::init();
164 $self->name ('');
165 }
166  
167 #-----------------------------------------------------------------
168 # toXML
169 # return a LibXML::Element called _dummy_ (it will be later
170 # changed either to Simple or to Collection)
171 #-----------------------------------------------------------------
172 sub toXML {
173 my $self = shift;
174 $self->increaseXMLCounter;
175  
176 my $root = XML::LibXML::Element->new ('_dummy_');
177 $root->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
178 $root->setAttributeNS (MOBY_XML_NS, ARTICLENAME, $self->name)
179 if $self->name;
180
181 return $self->closeXML ($root);
182 }
183  
184 #-----------------------------------------------------------------
185 #
186 # MOSES::MOBY::Simple
187 #
188 #-----------------------------------------------------------------
189 package MOSES::MOBY::Simple;
190 use base qw( MOSES::MOBY::DataElement );
191 use XML::LibXML;
192 use MOSES::MOBY::Tags;
193 use strict;
194  
195 #-----------------------------------------------------------------
196 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
197 #-----------------------------------------------------------------
198 {
199 my %_allowed =
200 (
201 data => {type => 'MOSES::MOBY::Data::Object'},
202 );
203  
204 sub _accessible {
205 my ($self, $attr) = @_;
206 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
207 }
208 sub _attr_prop {
209 my ($self, $attr_name, $prop_name) = @_;
210 my $attr = $_allowed {$attr_name};
211 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
212 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
213 }
214 }
215  
216 #-----------------------------------------------------------------
217 # toXML
218 #-----------------------------------------------------------------
219 sub toXML {
220 my $self = shift;
221 $self->increaseXMLCounter;
222 my $root = $self->SUPER::toXML;
223 $root->setNodeName (MOBY_XML_NS_PREFIX . ':' . SIMPLE);
224 $root->appendChild ($self->data->toXML)
225 if $self->data;
226 return $self->closeXML ($root);
227 }
228  
229  
230 #-----------------------------------------------------------------
231 #
232 # MOSES::MOBY::Collection
233 #
234 #-----------------------------------------------------------------
235 package MOSES::MOBY::Collection;
236 use base qw( MOSES::MOBY::DataElement );
237 use XML::LibXML;
238 use MOSES::MOBY::Tags;
239 use strict;
240  
241 #-----------------------------------------------------------------
242 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
243 #-----------------------------------------------------------------
244 {
245 my %_allowed =
246 (
247 data => {type => 'MOSES::MOBY::Simple', is_array => 1},
248 );
249  
250 sub _accessible {
251 my ($self, $attr) = @_;
252 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
253 }
254 sub _attr_prop {
255 my ($self, $attr_name, $prop_name) = @_;
256 my $attr = $_allowed {$attr_name};
257 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
258 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
259 }
260 }
261  
262 #-----------------------------------------------------------------
263 # toXML
264 #-----------------------------------------------------------------
265 sub toXML {
266 my $self = shift;
267 $self->increaseXMLCounter;
268 my $root = $self->SUPER::toXML;
269 $root->setNodeName (MOBY_XML_NS_PREFIX . ':' . COLLECTION);
270 if ($self->data) {
271 foreach my $simple (@{ $self->data }) {
272 $root->appendChild ($simple->toXML);
273 }
274 }
275 return $self->closeXML ($root);
276 }
277  
278  
279 #-----------------------------------------------------------------
280 #
281 # MOSES::MOBY::Parameter
282 #
283 #-----------------------------------------------------------------
284 package MOSES::MOBY::Parameter;
285 use base qw( MOSES::MOBY::DataElement );
286 use XML::LibXML;
287 use MOSES::MOBY::Tags;
288 use strict;
289  
290 #-----------------------------------------------------------------
291 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
292 #-----------------------------------------------------------------
293 {
294 my %_allowed =
295 (
296 value => undef,
297 );
298  
299 sub _accessible {
300 my ($self, $attr) = @_;
301 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
302 }
303 sub _attr_prop {
304 my ($self, $attr_name, $prop_name) = @_;
305 my $attr = $_allowed {$attr_name};
306 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
307 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
308 }
309 }
310  
311 #-----------------------------------------------------------------
312 # toXML
313 #-----------------------------------------------------------------
314 sub toXML {
315 my $self = shift;
316 $self->increaseXMLCounter;
317  
318 my $root = XML::LibXML::Element->new (PARAMETER);
319 $root->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
320 $root->setAttributeNS (MOBY_XML_NS, ARTICLENAME, $self->name)
321 if $self->name;
322 if ($self->value) {
323 my $val = XML::LibXML::Element->new (VALUE);
324 $val->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
325 $val->appendText ($self->value);
326 $root->appendChild ($val);
327 }
328 return $self->closeXML ($root);
329 }
330  
331 #-----------------------------------------------------------------
332 #
333 # MOSES::MOBY::Job
334 #
335 #-----------------------------------------------------------------
336 package MOSES::MOBY::Job;
337 use base qw( MOSES::MOBY::Base );
338 use XML::LibXML;
339 use MOSES::MOBY::Tags;
340 use strict;
341  
342 #-----------------------------------------------------------------
343 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
344 #-----------------------------------------------------------------
345 {
346 my %_allowed =
347 (
348 jid => undef,
349 dataElements => {type => 'MOSES::MOBY::DataElement', is_array => 1},
350  
351 # used internally (which context this job belongs to)
352 _context => {type => 'MOSES::MOBY::Package'},
353 );
354  
355 sub _accessible {
356 my ($self, $attr) = @_;
357 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
358 }
359 sub _attr_prop {
360 my ($self, $attr_name, $prop_name) = @_;
361 my $attr = $_allowed {$attr_name};
362 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
363 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
364 }
365 }
366  
367 #-----------------------------------------------------------------
368 # record things into this job's context
369 #-----------------------------------------------------------------
370 sub record_info {
371 shift->_record (MOSES::MOBY::ServiceException->info (@_));
372 }
373  
374 sub record_warning {
375 shift->_record (MOSES::MOBY::ServiceException->warning (@_));
376 }
377  
378 sub record_error {
379 shift->_record (MOSES::MOBY::ServiceException->error (@_));
380 }
381  
382 sub _record {
383 my ($self, $exception) = @_;
384 return unless defined $self->_context;
385 $exception->jobId ($self->jid);
386 $self->_context->add_exceptions ($exception);
387 }
388  
389 #-----------------------------------------------------------------
390 # toXML
391 #-----------------------------------------------------------------
392 sub toXML {
393 my $self = shift;
394 $self->increaseXMLCounter;
395 my $root = XML::LibXML::Element->new (MOBYDATA);
396 $root->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
397 $root->setAttributeNS (MOBY_XML_NS, QUERYID, $self->jid)
398 if $self->jid;
399 if ($self->dataElements) {
400 foreach my $element (@{ $self->dataElements }) {
401 $root->appendChild ($element->toXML);
402 }
403 }
404 return $self->closeXML ($root);
405 }
406  
407 #-----------------------------------------------------------------
408 # getData
409 # return a data element by its name (an element can be a Simple or
410 # a Collection); or the first data element if no $element_name was
411 # given; or throw an exception if there is no such element
412 #-----------------------------------------------------------------
413 sub getData {
414 my ($self, $element_name) = @_;
415 $self->throw ('Job does not have any data, at all.')
416 unless $self->dataElements;
417 return ${ $self->dataElements }[0] unless ($element_name);
418 foreach my $elem (@{ $self->dataElements }) {
419 next unless $elem->name;
420 return $elem if $elem->name eq $element_name;
421 }
422 $self->throw ("Job does not have data element '$element_name'.");
423 }
424  
425 #-----------------------------------------------------------------
426 # setData
427 #
428 # Set $value as a new data element in this job. If an element of
429 # the same name already exists it is replaced.
430 #
431 # The name is either from $element_name, or, if $element_name is
432 # not given, from the element itself ($value->name). The $value
433 # should always have a name (playing the role of an 'article
434 # name'). If none has a name, the $value is added at the end - but
435 # that should not happen (BioMoby API requires data to be
436 # named). Note, however, that adding at the end can be done only
437 # with a value that is already a MOSES::MOBY::DataElement (see below).
438 #
439 # Note that except of the ability to replace an existing element,
440 # it is equivalent to the add_dataElements ($new_element).
441 #
442 # The $value must be of type MOSES::MOBY::DataElement (actually, of one of
443 # its sub-classes: Simple, Collection, or Parameter), or a Moby
444 # data object (in which case, it is wrapped in a Simple), or a
445 # primitive value (in which case it is first wrapped in a Moby
446 # primitive type - its type is chosen according the given
447 # $element_type - and then into a Simple).
448 #
449 #-----------------------------------------------------------------
450 sub setData {
451 my ($self, $value, $element_name, $element_type) = @_;
452  
453 my $value_ref = ref ($value);
454  
455 # value may already be a container - in which case just store it
456 if ($value_ref and $value->isa ('MOSES::MOBY::DataElement')) {
457 $self->_place_value ($value, $element_name);
458 return;
459 }
460  
461 # from now we definitely need an element name
462 # (because $value needs to be wrapped in something named)
463 $self->throw ("An element name must be given when setting data to a job.")
464 unless $element_name;
465  
466 # now $value is a moby data type or a primitive value
467 $element_type ||= MOSES::MOBY::Base->STRING;
468 $self->_place_value ($value_ref ?
469 new MOSES::MOBY::Simple ( data => $value,
470 name => $element_name ) :
471 new MOSES::MOBY::Simple ( data => $self->check_type ($element_name, $element_type, $value),
472 name => $element_name ),
473 $element_name);
474 }
475  
476 sub _place_value {
477 my ($self, $value, $element_name) = @_;
478  
479 # make sure that we have a space where to put value
480 $self->dataElements ([])
481 unless $self->dataElements;
482  
483 my $name = $element_name || $value->name;
484 if ($name) {
485 foreach my $elem (@{ $self->dataElements }) {
486 if ($name eq $elem->name) {
487 $elem = $value;
488 return;
489 }
490 }
491 }
492 push (@{ $self->dataElements }, $value);
493 }
494  
495 # $value is an ARRAY or a single value; if $value is not already a
496 # Simple, it will be wrapped in Simple (or Simples). If $value is not
497 # even a reference (which means that it represents just a value of a
498 # primitive type) we use $element_type to create first a primitive
499 # type, and then wrap it into a Simple.
500  
501 # $element_name is here mandatory (no magic with a potential name in
502 # $value as in 'setData').
503  
504 # This is used for adding data to a collection. Therefore, if there is
505 # no collection named $element_name found, a new collection is
506 # created.
507  
508 sub addData {
509 my ($self, $element_name, $element_type, @values) = @_;
510 return unless @values;
511 my $value_ref = ref ($values[0]);
512  
513 # just in case somebody sends here a ready collection
514 return $self->setData ($values[0], $element_name)
515 if $value_ref eq 'MOSES::MOBY::Collection';
516  
517 # from now we definitely need an element name
518 $self->throw ("An element name must be given when adding things to a job.")
519 unless $element_name;
520  
521 # make sure that values are Simples
522 $element_type ||= MOSES::MOBY::Base->STRING;
523 # my @values = $value_ref eq 'ARRAY' ? @$value : $value;
524 foreach my $val (@values) {
525 next if ref ($val) eq 'MOSES::MOBY::Simple';
526 $val = (ref ($val) ?
527 new MOSES::MOBY::Simple ( data => $val ) :
528 new MOSES::MOBY::Simple ( data => $self->check_type ($element_name, $element_type, $val) ) );
529 }
530  
531 # find a place where values should be added
532 $self->dataElements ([])
533 unless $self->dataElements;
534 foreach my $elem (@{ $self->dataElements }) {
535 if ($element_name eq $elem->name) {
536 $self->throw ("In a job, a collection was expected but found this:\n" .
537 $elem->toString)
538 unless ref($elem) eq 'MOSES::MOBY::Collection';
539 push (@{ $elem->{data} }, @values);
540 return;
541 }
542 }
543 $self->setData (new MOSES::MOBY::Collection ( name => $element_name,
544 data => [@values] ),
545 $element_name);
546 }
547  
548 #-----------------------------------------------------------------
549 # getParameter
550 # return a value of a secondary parameter given by its name;
551 # throw an exception if there is no such parameter
552 #-----------------------------------------------------------------
553 sub getParameter {
554 my ($self, $element_name) = @_;
555 $self->throw ('Job does not have any parameters, at all.')
556 unless $self->dataElements;
557 foreach my $elem (@{ $self->dataElements }) {
558 next unless $elem->name;
559 return $elem->value
560 if $elem->name eq $element_name and ref ($elem) eq 'MOSES::MOBY::Parameter';
561 }
562 $self->throw ("Job does not have parameter '$element_name'.");
563 }
564  
565  
566  
567 1;
568 __END__