MOSES-MOBY

 view release on metacpan or  search on metacpan

lib/MOSES/MOBY/Package.pm  view on Meta::CPAN

#-----------------------------------------------------------------
# MOSES::MOBY::Package
# Author: Edward Kawas <edward.kawas@gmail.com>,
#         Martin Senger <martin.senger@gmail.com>
# For copyright and disclaimer see below.
#
# $Id: Package.pm,v 1.4 2008/04/29 19:45:01 kawas Exp $
#-----------------------------------------------------------------

#-----------------------------------------------------------------
#
# MOSES::MOBY::Package
#
#-----------------------------------------------------------------
package MOSES::MOBY::Package;
use base qw( MOSES::MOBY::Base );
use XML::LibXML;
use MOSES::MOBY::Tags;
use MOSES::MOBY::ServiceException;
use strict;

# add versioning to this module
use vars qw /$VERSION/;
$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;

#-----------------------------------------------------------------
# A list of allowed attribute names. See MOSES::MOBY::Base for details.
#-----------------------------------------------------------------
{
    my %_allowed =
	(
	 authority     => undef,
	 jobs          => {type => 'MOSES::MOBY::Job', is_array => 1},
	 exceptions    => {type => 'MOSES::MOBY::ServiceException', is_array => 1},
	 serviceNotes  => undef,
         cdata         => {type => MOSES::MOBY::Base->BOOLEAN},
	 );

    sub _accessible {
	my ($self, $attr) = @_;
	exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
    }
    sub _attr_prop {
	my ($self, $attr_name, $prop_name) = @_;
	my $attr = $_allowed {$attr_name};
	return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
	return $self->SUPER::_attr_prop ($attr_name, $prop_name);
    }
}

#-----------------------------------------------------------------
# init
#-----------------------------------------------------------------
sub init {
    my ($self) = shift;
    $self->SUPER::init();
    $self->cdata ('no');
}

#-----------------------------------------------------------------
# job_by_id
#-----------------------------------------------------------------

=head2 job_by_id

In this package, find and return a C<MOSES::MOBY::Job> object with the given
ID. Or throw an exception if such job does not exist. (TBD)

=cut

sub job_by_id {
    my ($self, $jobId) = @_;
    foreach my $job (@{ $self->jobs }) {
	return $job if $job->jid eq $jobId;
    }
    $self->throw ("Job '$jobId' not found.");
}


#-----------------------------------------------------------------
# size
#-----------------------------------------------------------------
sub size {
    my $self = shift;
    return 0 unless $self->jobs;
    return 0+@{ $self->jobs };
}

#-----------------------------------------------------------------
# toXML
#-----------------------------------------------------------------
sub toXML {
    my $self = shift;
    $self->increaseXMLCounter;
    my $root = $self->createXMLElement (MOBY);
    my $elemContent = $self->createXMLElement (MOBYCONTENT);
    $self->setXMLAttribute ($elemContent, AUTHORITY, $self->authority);

    if ($self->serviceNotes or $self->{exceptions}) {
	my $sNotes = $self->createXMLElement (SERVICENOTES);
	if ($self->serviceNotes) {
	    my $notes =	$self->createXMLElement (NOTES);
	    if ($self->cdata) {
		$notes->appendChild (XML::LibXML::CDATASection->new ($self->serviceNotes));
	    } else {
		$notes->appendText ($self->serviceNotes);
	    }
	    $sNotes->appendChild ($notes);
	}
	if ($self->exceptions) {
	    foreach my $exception (@{ $self->exceptions }) {
		$sNotes->appendChild ($exception->toXML);
	    }
	}
	$elemContent->appendChild ($sNotes);
    }
    
    if ($self->jobs) {
	foreach my $job (@{ $self->jobs }) {
	    $elemContent->appendChild ($job->toXML);
	}
    }

    $root->appendChild ($elemContent);
    return $self->closeXML ($root);
}


#-----------------------------------------------------------------
#
# MOSES::MOBY::DataElement
#
#-----------------------------------------------------------------
package MOSES::MOBY::DataElement;
use base qw( MOSES::MOBY::Base );
use XML::LibXML;
use MOSES::MOBY::Tags;
use strict;

#-----------------------------------------------------------------
# A list of allowed attribute names. See MOSES::MOBY::Base for details.
#-----------------------------------------------------------------
{
    my %_allowed =
	(
	 name => undef,
	 );

    sub _accessible {
	my ($self, $attr) = @_;
	exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
    }
    sub _attr_prop {
	my ($self, $attr_name, $prop_name) = @_;
	my $attr = $_allowed {$attr_name};
	return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
	return $self->SUPER::_attr_prop ($attr_name, $prop_name);
    }
}

sub init {
    my ($self) = shift;
    $self->SUPER::init();



( run in 1.653 second using v1.01-cache-2.11-cpan-d8267643d1d )