package GTx::APE::InputController::PCSTUDY_InputController;

use base 'GTx::APE::InputController';

use strict;
use warnings;
use Carp;

use GTx::APE::InputController::Requirement;
use GTx::APE::InputController::EPSA_InputController;

use constant TRUE  => 1;
use constant FALSE => 0;

{
    my %_attributes = ( 
			## ** Add your object attributes here **
			## They will automatically be given a get method.
			## e.g. attribute 'personForename' can be accessed
			## using getPersonForname(). There are no automatic 
			## set routines, they must be declared or the attribute 
			## value must be parsed in the constructor 
			## i.e new InputController(personForename=>'Simon');
		       );

    my %object;
    
    foreach my $attName (keys(%_attributes)) {
	no strict 'refs';
	*{__PACKAGE__ . "::get\u$attName"} = sub { $object{ $_[0] }->{$attName} }
    }


    #################################################################
    ## Initialise the objects private attributes. The argument is
    ## deleted as it is found, initialised and set.
    #################################################################
    sub _init {	
	my ($self,%args) = @_;
	foreach my $attName (keys(%args)) {
	    if(exists($_attributes{$attName})) { 
		$object{ $self }->{$attName} = delete $args{$attName};
	    }
	}   

	## Set the description as this is a specialised InputController.
	$args{description} = "An InputController that requests a study name BEFORE requesting any " .
	    "default requirements.";
	
	return $self->SUPER::_init(%args);
    }
    

    #################################################################
    ## Overridden method from InputController base class.
    #################################################################
    sub createRequirements {
	my $self = shift;

	## Call the super classes createRequirements method to get the
	## basic requirements.
	$self->SUPER::createRequirements();

	## New Requirement requesting the study name.
	$self->unshiftRequirement
	    (new GTx::APE::InputController::Requirement(arrayingEntityIndex=>-1,
							atomicProcessIndex =>-1,
							type               =>'Input',
							default            =>'ExoSeq',
							description        =>'Enter Study name [def:exoseq]: ',
							checkingCode       => sub{
							    my ($self,$data) = @_;
							    if(uc($data) ne 'EXOSEQ') {
								croak "ERROR: 'ExoSeq' is the only " . 
								    "study currently available.\n";
							    }
							}));
	
	## The getData method requires an EPSA_InputController method.
	$object{ $self }->{epsaInputController} =
	    new GTx::APE::InputController::EPSA_InputController
	    (atomicProcessContainer=>$self->getAtomicProcessContainer(),
	     sqlEngineObj          =>$self->getSqlEngineObj());
    }   
    

    #################################################################
    ## Overridden method from InputController base class.
    ## Processes a Requirment object from the requirementObjectList
    ## built by the createRequirements method.
    #################################################################
    sub process {
	my ($self,$requirementObj) = @_;
	if($self->isDuplicateData($requirementObj)) {
	    croak "ERROR: '",$requirementObj->getData(), "' has already been entered.\n";
	}
	## Get data associated with Requirement.
	## Ignore 'Input' type Requirement objects.
	unless($requirementObj->getType() eq 'Input') {
	    $requirementObj = eval  { $self->getData($requirementObj); };
	    if($@)                  { croak $@; }
	}
	## Push this Requirement onto the complete list.
	$self->pushCompletedRequirement($requirementObj);
	## Wait until all of the Requirement objects have been
	## returned.
	print STDERR "Testing: ",$self->getRequirementCount()." == ". 
	    $self->getCompletedRequirementCount()."\n";
	if($self->getRequirementCount() == $self->getCompletedRequirementCount()) {
	    ## Set the sessionId foreach ArrayingEntity via the AtomicProcessContainer.
	    eval  { ${$self->getAtomicProcessContainer()}->setSessionId($self->getSessionId()); };
	    if($@){ croak "ERROR: can't set the sessionId: $@\n";                               }
	    foreach my $req (@{$self->getCompletedRequirementList()}) {
		## Call the generic arrayData method in the base
		## class.
		$self->arrayData($req) unless $req->getType() eq 'Input';
	    }
	}
    }


    #################################################################
    ## Overridden method from base class to fetch data for less well
    ## known entity types. See getData in the base class for a list
    ## of the well known entity types.
    #################################################################
    sub getData {
	my ($self,$requirementObj) = @_;
	my $completedRequirementObj = 
	    eval  { $object{ $self }->{epsaInputController}->getData($requirementObj); };
	if($@){ croak $@; }
	
	return $completedRequirementObj;
    }

    #################################################################
    ## Method to destroy this object.
    #################################################################
    sub DESTROY {
	my $self = shift;
	delete $object{$self};
	$self->SUPER::DESTROY;
    }
}

1;
